From 70801f8d75a17216d013f47d7346e7b849e97b73 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Thu, 15 Oct 2020 18:46:45 -0400 Subject: [PATCH 1/9] Mark `gremlins.characters` as `language-overridable` --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 1ca3d51..965de39 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "gremlins.characters": { "type": "object", "description": "List of characters the extension should track", + "scope": "language-overridable", "default": { "2013": { "description": "en dash", From de9431665321faad9ad0a5314328e1c198869c73 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 06:39:18 -0400 Subject: [PATCH 2/9] Remove dependency on context when loading config --- extension.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/extension.js b/extension.js index 17bb22c..6cd3890 100644 --- a/extension.js +++ b/extension.js @@ -25,6 +25,11 @@ let processedDocuments = {} let configuration = null +const icons = { + light : null, + dark: null +} + let diagnosticCollection = null function configureDiagnosticsCollection(showDiagnostics) { @@ -47,10 +52,19 @@ function disposeDecorationTypes() { decorationTypes = {} } -function loadConfiguration(context) { +/** + * + * @param {vscode.ExtensionContext} context + */ +function loadIcons(context) { + icons.light = context.asAbsolutePath('images/gremlins-light.svg') + icons.dark = context.asAbsolutePath('images/gremlins-dark.svg') +} + +function loadConfiguration() { const gremlinsConfiguration = vscode.workspace.getConfiguration(GREMLINS) - const gremlins = gremlinsFromConfig(gremlinsConfiguration, context) + const gremlins = gremlinsFromConfig(gremlinsConfiguration) const showDiagnostics = vscode.workspace.getConfiguration(GREMLINS) .showInProblemPane @@ -79,7 +93,7 @@ function loadConfiguration(context) { } } -function gremlinsFromConfig(gremlinsConfiguration, context) { +function gremlinsFromConfig(gremlinsConfiguration) { const gremlinsLevels = { [GREMLINS_LEVELS.INFO]: gremlinsConfiguration.color_info, [GREMLINS_LEVELS.WARNING]: gremlinsConfiguration.color_warning, @@ -90,11 +104,11 @@ function gremlinsFromConfig(gremlinsConfiguration, context) { const hexCodePointsRangeRegex = /^([0-9a-f]+)(?:-([0-9a-f]+))?$/i const lightIcon = { - gutterIconPath: context.asAbsolutePath('images/gremlins-light.svg'), + gutterIconPath: icons.light, gutterIconSize: gutterIconSize, } const darkIcon = { - gutterIconPath: context.asAbsolutePath('images/gremlins-dark.svg'), + gutterIconPath: icons.dark, gutterIconSize: gutterIconSize, } @@ -262,8 +276,14 @@ function drawDecorations(activeTextEditor, decorations) { } } +/** + * + * @param {vscode.ExtensionContext} context + */ function activate(context) { - configuration = loadConfiguration(context) + loadIcons(context) + + configuration = loadConfiguration() const doCheckForGremlins = (editor) => checkForGremlins( From 87e9cd2914a5d9c8c72a733abeffa456e15c0171 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 06:54:33 -0400 Subject: [PATCH 3/9] Load configuration every time we process a doc --- extension.js | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/extension.js b/extension.js index 6cd3890..2bf8759 100644 --- a/extension.js +++ b/extension.js @@ -23,8 +23,6 @@ let decorationTypes = {} let processedDocuments = {} -let configuration = null - const icons = { light : null, dark: null @@ -66,8 +64,7 @@ function loadConfiguration() { const gremlins = gremlinsFromConfig(gremlinsConfiguration) - const showDiagnostics = vscode.workspace.getConfiguration(GREMLINS) - .showInProblemPane + const showDiagnostics = gremlinsConfiguration.showInProblemPane const diagnosticCollection = configureDiagnosticsCollection(showDiagnostics) let regexpWithAllChars = new RegExp( @@ -77,19 +74,10 @@ function loadConfiguration() { 'g', ) - const dispose = () => { - if (diagnosticCollection) { - diagnosticCollection.clear() - diagnosticCollection.dispose() - } - disposeDecorationTypes() - } - return { gremlins, regexpWithAllChars, diagnosticCollection, - dispose, } } @@ -185,9 +173,6 @@ function charFromHex(hexCodePoint) { */ function checkForGremlins( activeTextEditor, - gremlins, - regexpWithAllChars, - diagnosticCollection, ) { if (!activeTextEditor) { return @@ -195,6 +180,8 @@ function checkForGremlins( const doc = activeTextEditor.document + let { gremlins, regexpWithAllChars, diagnosticCollection } = loadConfiguration() + const decorationOption = {} for (const char in gremlins) { decorationOption[char] = [] @@ -283,16 +270,6 @@ function drawDecorations(activeTextEditor, decorations) { function activate(context) { loadIcons(context) - configuration = loadConfiguration() - - const doCheckForGremlins = (editor) => - checkForGremlins( - editor, - configuration.gremlins, - configuration.regexpWithAllChars, - configuration.diagnosticCollection, - ) - eventListeners.push( vscode.workspace.onDidChangeConfiguration( (event) => { @@ -300,9 +277,8 @@ function activate(context) { disposeDecorationTypes() processedDocuments = {} - configuration = loadConfiguration(context) vscode.window.visibleTextEditors.forEach((editor) => - doCheckForGremlins(editor), + checkForGremlins(editor), ) } }, @@ -317,7 +293,7 @@ function activate(context) { if (editor) { const processedDocument = processedDocuments[editor.document.uri] if (!processedDocument) { - doCheckForGremlins(editor) + checkForGremlins(editor) } else { drawDecorations(editor, processedDocument.decorations) } @@ -330,7 +306,7 @@ function activate(context) { eventListeners.push( vscode.workspace.onDidChangeTextDocument( - (_event) => doCheckForGremlins(vscode.window.activeTextEditor), + (_event) => checkForGremlins(vscode.window.activeTextEditor), null, context.subscriptions, ), @@ -347,13 +323,18 @@ function activate(context) { ), ) - doCheckForGremlins(vscode.window.activeTextEditor) + checkForGremlins(vscode.window.activeTextEditor) } exports.activate = activate // this method is called when your extension is deactivated function deactivate() { - configuration.dispose() + if (diagnosticCollection) { + diagnosticCollection.clear() + diagnosticCollection.dispose() + } + + disposeDecorationTypes() eventListeners.forEach((listener) => listener.dispose()) eventListeners.length = 0 From a32ec48123ff3f469ef1cde0e5cdec12936b5b91 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 06:54:55 -0400 Subject: [PATCH 4/9] Get doc-specific configuration --- extension.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/extension.js b/extension.js index 2bf8759..642a615 100644 --- a/extension.js +++ b/extension.js @@ -59,8 +59,12 @@ function loadIcons(context) { icons.dark = context.asAbsolutePath('images/gremlins-dark.svg') } -function loadConfiguration() { - const gremlinsConfiguration = vscode.workspace.getConfiguration(GREMLINS) +/** + * + * @param {vscode.TextDocument} document + */ +function loadConfiguration(document) { + const gremlinsConfiguration = vscode.workspace.getConfiguration(GREMLINS, document) const gremlins = gremlinsFromConfig(gremlinsConfiguration) @@ -180,7 +184,7 @@ function checkForGremlins( const doc = activeTextEditor.document - let { gremlins, regexpWithAllChars, diagnosticCollection } = loadConfiguration() + let { gremlins, regexpWithAllChars, diagnosticCollection } = loadConfiguration(doc) const decorationOption = {} for (const char in gremlins) { @@ -333,7 +337,7 @@ function deactivate() { diagnosticCollection.clear() diagnosticCollection.dispose() } - + disposeDecorationTypes() eventListeners.forEach((listener) => listener.dispose()) From df904587694d033047fe7daaeabebe24d6cabc44 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 07:12:46 -0400 Subject: [PATCH 5/9] Add language-specific instructions to the README --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b11ca5..9093334 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You can also use the [“Unicode code point of current character” extension](h ## Adding new gremlins characters -You can configure the list of additionnal characters and how they are shown under user settings key `gremlins.characters`. +You can configure the list of additional characters and how they are shown under user settings key `gremlins.characters`. As an example, the following snippet adds the "U+000C" FORM FEED character: @@ -44,6 +44,32 @@ Please help enhance the extension by suggesting new default characters, through You can find all characters in [Unicode Table](https://unicode-table.com/en/). +## Language-specific gremlins characters + +You can override the characters for a specific language by configuring them in the `gremlins.characters` property of the language-specific settings key (e.g. `[markdown]` for Markdown files). + +> More information about language specific settings can be found in the [Language specific editor settings](https://code.visualstudio.com/docs/getstarted/settings#_language-specific-editor-settings) VSCode documentation page. + +As an example, the following snippet adds the "U+000C" (form feed) character and disables the "U+00A0" (non-breaking space) character for markdown files: + +``` +"[markdown]": { + "gremlins.characters": { + // Add the form feed character for markdown files + "000c" : { + "zeroWidth": true, + "description": "FORM FEED (FF)", + "backgroundColor": "rgba(255,127,80,.5)", + "overviewRulerColor": "rgba(255,127,80,1)", + }, + // Ignore the non-breaking space character for markdown files + "00a0": { + "level": "none" + } + } +} +``` + ## Hiding the gremlin icon in the gutter for a character You can chose to hide the gremlin icon in the gutter for some characters. From 89855333e2b4d934dade427f4bcad012f588c9db Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 07:13:03 -0400 Subject: [PATCH 6/9] Add language to code snippets for highlighting --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9093334..3e968f2 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ You can configure the list of additional characters and how they are shown under As an example, the following snippet adds the "U+000C" FORM FEED character: -``` +```jsonc "gremlins.characters": { "000c" : { "zeroWidth": true, @@ -52,7 +52,7 @@ You can override the characters for a specific language by configuring them in t As an example, the following snippet adds the "U+000C" (form feed) character and disables the "U+00A0" (non-breaking space) character for markdown files: -``` +```jsonc "[markdown]": { "gremlins.characters": { // Add the form feed character for markdown files @@ -78,7 +78,7 @@ Still under user settings key `gremlins.characters`, you can add the `hideGutter For example, this removes the gremlin icon in the gutter for non breakable spaces: -``` +```jsonc "gremlins.characters": { "00a0" : { "hideGutterIcon": true From da9914adad7455bcd79d52c76db458a76dd69294 Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Fri, 16 Oct 2020 07:25:34 -0400 Subject: [PATCH 7/9] Add note about default severity --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 965de39..1be8bdf 100644 --- a/package.json +++ b/package.json @@ -172,7 +172,8 @@ }, "level": { "type": "string", - "description": "Severity level associated with this character", + "description": "Severity level associated with this character (default is error)", + "default": "error", "enum": [ "none", "info", From 0452ecd0dece058ce2705c0c871a042eeaef3cba Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Tue, 3 Nov 2020 06:59:56 -0500 Subject: [PATCH 8/9] Remove backgroundColor from README examples --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index ef52e81..cfe20e9 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ As an example, the following snippet adds the "U+000C" FORM FEED character: "000c" : { "zeroWidth": true, "description": "FORM FEED (FF)", - "backgroundColor": "rgba(255,127,80,.5)", "overviewRulerColor": "rgba(255,127,80,1)", } } @@ -59,7 +58,6 @@ As an example, the following snippet adds the "U+000C" (form feed) character and "000c" : { "zeroWidth": true, "description": "FORM FEED (FF)", - "backgroundColor": "rgba(255,127,80,.5)", "overviewRulerColor": "rgba(255,127,80,1)", }, // Ignore the non-breaking space character for markdown files From 543a0a85675e45f39e9152032938e052c100f54c Mon Sep 17 00:00:00 2001 From: Jon Senchyna Date: Tue, 3 Nov 2020 07:49:36 -0500 Subject: [PATCH 9/9] Change example to use level --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cfe20e9..a522b75 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ As an example, the following snippet adds the "U+000C" (form feed) character and "000c" : { "zeroWidth": true, "description": "FORM FEED (FF)", - "overviewRulerColor": "rgba(255,127,80,1)", + "level": "error", }, // Ignore the non-breaking space character for markdown files "00a0": {