Skip to content

Add configuration to disable ALL gremlins (useful for language-overrides) #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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).
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). To completely disable gremlins for a specific language, you can use the `gremlins.disabled` property.

> 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.

Expand All @@ -65,6 +65,10 @@ As an example, the following snippet adds the "U+000C" (form feed) character and
"level": "none"
}
}
},
// Do not show any gremlins in plaintext files
"[plaintext]": {
"gremlins.disabled": true
}
```
## Specifying a Range of Invalid Characters
Expand Down
47 changes: 43 additions & 4 deletions __snapshots__/extension.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`configuration level setting level to none prevents decoration from being displayed 1`] = `
exports[`configuration gremlins.disabled when set to \`true\` prevents decoration from being displayed 1`] = `
Array [
Array [
Object {
"dispose": [MockFunction],
},
Array [],
],
]
`;

exports[`configuration gremlins.disabled when set to \`true\` prevents decoration from being displayed 2`] = `Array []`;

exports[`configuration gremlins.disabled when set to \`true\` prevents diagnostic from being displayed 1`] = `
Array [
Array [
"document1",
Array [],
],
]
`;

exports[`configuration gremlins.disabled when set to \`true\` prevents diagnostic from being displayed 2`] = `
Array [
Array [
"document1",
Array [],
],
Array [
"document2",
Array [],
],
Array [
"document3",
Array [],
],
]
`;

exports[`configuration level when set to none prevents decoration from being displayed 1`] = `
Array [
Array [
Object {
Expand All @@ -25,9 +64,9 @@ Array [
]
`;

exports[`configuration level setting level to none prevents decoration from being displayed 2`] = `Array []`;
exports[`configuration level when set to none prevents decoration from being displayed 2`] = `Array []`;

exports[`configuration level setting level to none prevents decoration from being displayed 3`] = `
exports[`configuration level when set to none prevents diagnostic from being displayed 1`] = `
Array [
Array [
"document1",
Expand All @@ -36,7 +75,7 @@ Array [
]
`;

exports[`configuration level setting level to none prevents decoration from being displayed 4`] = `
exports[`configuration level when set to none prevents diagnostic from being displayed 2`] = `
Array [
Array [
"document1",
Expand Down
13 changes: 13 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ function loadConfiguration(document) {
}

function gremlinsFromConfig(gremlinsConfiguration) {
if (gremlinsConfiguration.disabled) {
return {}
}

const gremlinsLevels = {
[GREMLINS_LEVELS.INFO]: gremlinsConfiguration.color_info,
[GREMLINS_LEVELS.WARNING]: gremlinsConfiguration.color_warning,
Expand Down Expand Up @@ -188,6 +192,15 @@ function checkForGremlins(activeTextEditor) {
let { gremlins, regexpWithAllChars, diagnosticCollection } =
loadConfiguration(doc)

if (Object.keys(gremlins).length === 0) {
// If there are now no configured gremlins, clear any diagnostics from
// previous runs and short-curcuit.
if (diagnosticCollection) {
diagnosticCollection.set(activeTextEditor.document.uri, [])
}
return
}

const decorationOption = {}
for (const char in gremlins) {
decorationOption[char] = []
Expand Down
40 changes: 37 additions & 3 deletions extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe('deactivate', () => {

describe('configuration', () => {
describe('level', () => {
it('setting level to none prevents decoration from being displayed', () => {
it('when set to none prevents decoration from being displayed', () => {
// Default is to display decoration
mockDocument.text = 'zero width space \u200b'
activate(context)
Expand All @@ -463,8 +463,8 @@ describe('configuration', () => {
// Decoration is no longer displayed
expect(mockSetDecorations.mock.calls).toMatchSnapshot()
})
it('setting level to none prevents decoration from being displayed', () => {

it('when set to none prevents diagnostic from being displayed', () => {
// Default is to create diagnostic
mockDocument.text = 'zero width space \u200b'
activate(context)
Expand All @@ -480,4 +480,38 @@ describe('configuration', () => {
expect(mockSetDiagnostics.mock.calls).toMatchSnapshot()
})
})

describe('gremlins.disabled', () => {
it('when set to `true` prevents decoration from being displayed', () => {
// Default is to display decoration
mockDocument.text = 'zero width space \u200b'
activate(context)
expect(mockSetDecorations.mock.calls).toMatchSnapshot()

// When overriding level to 'none'
mockSetDecorations.mockClear()
mockConfiguration.disabled = true
const configChangeHandler = mockVscode.workspace.onDidChangeConfiguration.mock.calls[0][0]
configChangeHandler({ affectsConfiguration: () => true})

// Decoration is no longer displayed
expect(mockSetDecorations.mock.calls).toMatchSnapshot()
})

it('when set to `true` prevents diagnostic from being displayed', () => {
// Default is to create diagnostic
mockDocument.text = 'zero width space \u200b'
activate(context)
expect(mockSetDiagnostics.mock.calls).toMatchSnapshot()

// When overriding level to 'none'
mockSetDecorations.mockClear()
mockConfiguration.disabled = true
const configChangeHandler = mockVscode.workspace.onDidChangeConfiguration.mock.calls[0][0]
configChangeHandler({ affectsConfiguration: () => true})

// Diagnostic is no longer created
expect(mockSetDiagnostics.mock.calls).toMatchSnapshot()
})
})
})
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@
"type": "boolean",
"default": false,
"description": "Show gremlins in the problem pane"
},
"gremlins.disabled": {
"type": "boolean",
"default": false,
"description": "Disable processing of all gremlins characters (used for disabling for files of a specific languages)",
"scope": "language-overridable"
}
}
}
Expand Down