Skip to content

Commit

Permalink
Merge pull request #150 from TheSench/feature/language-overridable-co…
Browse files Browse the repository at this point in the history
…nfiguration

Make `gremlins.characters` language-overridable
  • Loading branch information
nhoizey authored Nov 5, 2020
2 parents 9fb83e2 + 60f7a6c commit 03fbd20
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ 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:

```
```jsonc
"gremlins.characters": {
"000c" : {
"zeroWidth": true,
"description": "FORM FEED (FF)",
"backgroundColor": "rgba(255,127,80,.5)",
"overviewRulerColor": "rgba(255,127,80,1)",
}
}
Expand All @@ -44,6 +43,31 @@ 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:

```jsonc
"[markdown]": {
"gremlins.characters": {
// Add the form feed character for markdown files
"000c" : {
"zeroWidth": true,
"description": "FORM FEED (FF)",
"level": "error",
},
// 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.
Expand All @@ -52,7 +76,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
Expand Down
77 changes: 41 additions & 36 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ let decorationTypes = {}

let processedDocuments = {}

let configuration = null
const icons = {
light : null,
dark: null
}

let diagnosticCollection = null

Expand All @@ -47,13 +50,25 @@ function disposeDecorationTypes() {
decorationTypes = {}
}

function loadConfiguration(context) {
const gremlinsConfiguration = vscode.workspace.getConfiguration(GREMLINS)
/**
*
* @param {vscode.ExtensionContext} context
*/
function loadIcons(context) {
icons.light = context.asAbsolutePath('images/gremlins-light.svg')
icons.dark = context.asAbsolutePath('images/gremlins-dark.svg')
}

const gremlins = gremlinsFromConfig(gremlinsConfiguration, context)
/**
*
* @param {vscode.TextDocument} document
*/
function loadConfiguration(document) {
const gremlinsConfiguration = vscode.workspace.getConfiguration(GREMLINS, document)

const gremlins = gremlinsFromConfig(gremlinsConfiguration)

const showDiagnostics = vscode.workspace.getConfiguration(GREMLINS)
.showInProblemPane
const showDiagnostics = gremlinsConfiguration.showInProblemPane
const diagnosticCollection = configureDiagnosticsCollection(showDiagnostics)

let regexpWithAllChars = new RegExp(
Expand All @@ -63,23 +78,14 @@ function loadConfiguration(context) {
'g',
)

const dispose = () => {
if (diagnosticCollection) {
diagnosticCollection.clear()
diagnosticCollection.dispose()
}
disposeDecorationTypes()
}

return {
gremlins,
regexpWithAllChars,
diagnosticCollection,
dispose,
}
}

function gremlinsFromConfig(gremlinsConfiguration, context) {
function gremlinsFromConfig(gremlinsConfiguration) {
const gremlinsLevels = {
[GREMLINS_LEVELS.INFO]: gremlinsConfiguration.color_info,
[GREMLINS_LEVELS.WARNING]: gremlinsConfiguration.color_warning,
Expand All @@ -90,11 +96,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,
}

Expand Down Expand Up @@ -171,16 +177,15 @@ function charFromHex(hexCodePoint) {
*/
function checkForGremlins(
activeTextEditor,
gremlins,
regexpWithAllChars,
diagnosticCollection,
) {
if (!activeTextEditor) {
return
}

const doc = activeTextEditor.document

let { gremlins, regexpWithAllChars, diagnosticCollection } = loadConfiguration(doc)

const decorationOption = {}
for (const char in gremlins) {
decorationOption[char] = []
Expand Down Expand Up @@ -262,16 +267,12 @@ function drawDecorations(activeTextEditor, decorations) {
}
}

/**
*
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
configuration = loadConfiguration(context)

const doCheckForGremlins = (editor) =>
checkForGremlins(
editor,
configuration.gremlins,
configuration.regexpWithAllChars,
configuration.diagnosticCollection,
)
loadIcons(context)

eventListeners.push(
vscode.workspace.onDidChangeConfiguration(
Expand All @@ -280,9 +281,8 @@ function activate(context) {
disposeDecorationTypes()
processedDocuments = {}

configuration = loadConfiguration(context)
vscode.window.visibleTextEditors.forEach((editor) =>
doCheckForGremlins(editor),
checkForGremlins(editor),
)
}
},
Expand All @@ -297,7 +297,7 @@ function activate(context) {
if (editor) {
const processedDocument = processedDocuments[editor.document.uri]
if (!processedDocument) {
doCheckForGremlins(editor)
checkForGremlins(editor)
} else {
drawDecorations(editor, processedDocument.decorations)
}
Expand All @@ -310,7 +310,7 @@ function activate(context) {

eventListeners.push(
vscode.workspace.onDidChangeTextDocument(
(_event) => doCheckForGremlins(vscode.window.activeTextEditor),
(_event) => checkForGremlins(vscode.window.activeTextEditor),
null,
context.subscriptions,
),
Expand All @@ -327,13 +327,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
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"gremlins.characters": {
"type": "object",
"description": "List of characters the extension should track",
"scope": "language-overridable",
"default": {
"2013": {
"description": "en dash",
Expand Down Expand Up @@ -171,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",
Expand Down

0 comments on commit 03fbd20

Please sign in to comment.