diff options
author | Adolfo OchagavĂa <[email protected]> | 2018-10-06 21:53:12 +0100 |
---|---|---|
committer | Adolfo OchagavĂa <[email protected]> | 2018-10-06 21:53:12 +0100 |
commit | 9ccc56860107ed46f3abdc0f7c0d53766a3eded2 (patch) | |
tree | 2c3b2df2c21395b9c8afba134f9917a885c57fd2 /editors | |
parent | 81bf190f7aca4cadec5394c397bd7c084b53b9f5 (diff) |
Dynamically apply highlightingOn config
Fixes #84
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/package.json | 2 | ||||
-rw-r--r-- | editors/code/src/extension.ts | 47 |
2 files changed, 37 insertions, 12 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index 305d6eaf1..9f05fe91a 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -111,7 +111,7 @@ | |||
111 | ], | 111 | ], |
112 | "configuration": { | 112 | "configuration": { |
113 | "type": "object", | 113 | "type": "object", |
114 | "title": "Rust Analyzer configuration", | 114 | "title": "Rust Analyzer", |
115 | "properties": { | 115 | "properties": { |
116 | "ra-lsp.highlightingOn": { | 116 | "ra-lsp.highlightingOn": { |
117 | "type": "boolean", | 117 | "type": "boolean", |
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index dc1792e94..fde6a480d 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts | |||
@@ -11,10 +11,22 @@ let uris = { | |||
11 | let highlightingOn = true; | 11 | let highlightingOn = true; |
12 | 12 | ||
13 | export function activate(context: vscode.ExtensionContext) { | 13 | export function activate(context: vscode.ExtensionContext) { |
14 | let config = vscode.workspace.getConfiguration('ra-lsp'); | 14 | let applyHighlightingOn = () => { |
15 | if (config.has('highlightingOn')) { | 15 | let config = vscode.workspace.getConfiguration('ra-lsp'); |
16 | highlightingOn = config.get('highlightingOn') as boolean; | 16 | if (config.has('highlightingOn')) { |
17 | } | 17 | highlightingOn = config.get('highlightingOn') as boolean; |
18 | }; | ||
19 | |||
20 | if (!highlightingOn) { | ||
21 | removeHighlights(); | ||
22 | } | ||
23 | }; | ||
24 | |||
25 | // Apply the highlightingOn config now and whenever the config changes | ||
26 | applyHighlightingOn(); | ||
27 | vscode.workspace.onDidChangeConfiguration(_ => { | ||
28 | applyHighlightingOn(); | ||
29 | }); | ||
18 | 30 | ||
19 | let textDocumentContentProvider = new TextDocumentContentProvider() | 31 | let textDocumentContentProvider = new TextDocumentContentProvider() |
20 | let dispose = (disposable: vscode.Disposable) => { | 32 | let dispose = (disposable: vscode.Disposable) => { |
@@ -130,7 +142,7 @@ export function activate(context: vscode.ExtensionContext) { | |||
130 | }) | 142 | }) |
131 | }, null, context.subscriptions) | 143 | }, null, context.subscriptions) |
132 | vscode.window.onDidChangeActiveTextEditor(async (editor) => { | 144 | vscode.window.onDidChangeActiveTextEditor(async (editor) => { |
133 | if (!editor || editor.document.languageId != 'rust') return | 145 | if (!highlightingOn || !editor || editor.document.languageId != 'rust') return |
134 | let params: lc.TextDocumentIdentifier = { | 146 | let params: lc.TextDocumentIdentifier = { |
135 | uri: editor.document.uri.toString() | 147 | uri: editor.document.uri.toString() |
136 | } | 148 | } |
@@ -179,7 +191,7 @@ function startServer() { | |||
179 | let editor = vscode.window.visibleTextEditors.find( | 191 | let editor = vscode.window.visibleTextEditors.find( |
180 | (editor) => editor.document.uri.toString() == params.uri | 192 | (editor) => editor.document.uri.toString() == params.uri |
181 | ) | 193 | ) |
182 | if (editor == null) return; | 194 | if (!highlightingOn || !editor) return; |
183 | setHighlights( | 195 | setHighlights( |
184 | editor, | 196 | editor, |
185 | params.decorations, | 197 | params.decorations, |
@@ -213,10 +225,11 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider | |||
213 | } | 225 | } |
214 | } | 226 | } |
215 | 227 | ||
228 | let decorations: { [index: string]: vscode.TextEditorDecorationType } = {}; | ||
216 | 229 | ||
217 | const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() => { | 230 | function initDecorations() { |
218 | const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj }) | 231 | const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj }) |
219 | return { | 232 | decorations = { |
220 | background: decor("#3F3F3F"), | 233 | background: decor("#3F3F3F"), |
221 | error: vscode.window.createTextEditorDecorationType({ | 234 | error: vscode.window.createTextEditorDecorationType({ |
222 | borderColor: "red", | 235 | borderColor: "red", |
@@ -232,14 +245,26 @@ const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() => | |||
232 | attribute: decor("#BFEBBF"), | 245 | attribute: decor("#BFEBBF"), |
233 | literal: decor("#DFAF8F"), | 246 | literal: decor("#DFAF8F"), |
234 | } | 247 | } |
235 | })() | 248 | } |
249 | |||
250 | function removeHighlights() { | ||
251 | for (let tag in decorations) { | ||
252 | decorations[tag].dispose(); | ||
253 | } | ||
254 | |||
255 | decorations = {}; | ||
256 | } | ||
236 | 257 | ||
237 | function setHighlights( | 258 | function setHighlights( |
238 | editor: vscode.TextEditor, | 259 | editor: vscode.TextEditor, |
239 | highlights: Array<Decoration> | 260 | highlights: Array<Decoration> |
240 | ) { | 261 | ) { |
241 | if (!highlightingOn) { | 262 | // Initialize decorations if necessary |
242 | return; | 263 | // |
264 | // Note: decoration objects need to be kept around so we can dispose them | ||
265 | // if the user disables syntax highlighting | ||
266 | if (Object.keys(decorations).length === 0) { | ||
267 | initDecorations(); | ||
243 | } | 268 | } |
244 | 269 | ||
245 | let byTag: Map<string, vscode.Range[]> = new Map() | 270 | let byTag: Map<string, vscode.Range[]> = new Map() |