]> git.lizzy.rs Git - rust.git/commitdiff
Dynamically apply highlightingOn config
authorAdolfo Ochagavía <aochagavia92@gmail.com>
Sat, 6 Oct 2018 20:53:12 +0000 (22:53 +0200)
committerAdolfo Ochagavía <aochagavia92@gmail.com>
Sat, 6 Oct 2018 20:53:12 +0000 (22:53 +0200)
Fixes #84

editors/code/package.json
editors/code/src/extension.ts

index 305d6eaf12880cf83a43bb5a4e29de402d98d607..9f05fe91a023f817fddc919cde6258ef5fa8f424 100644 (file)
         ],
         "configuration": {
             "type": "object",
-            "title": "Rust Analyzer configuration",
+            "title": "Rust Analyzer",
             "properties": {
                 "ra-lsp.highlightingOn": {
                     "type": "boolean",
index dc1792e9478737a769ff83d1693f2305e1f59410..fde6a480d85677dfceb5acf49aa63178a7da6eb9 100644 (file)
@@ -11,10 +11,22 @@ let uris = {
 let highlightingOn = true;
 
 export function activate(context: vscode.ExtensionContext) {
-    let config = vscode.workspace.getConfiguration('ra-lsp');
-    if (config.has('highlightingOn')) {
-        highlightingOn = config.get('highlightingOn') as boolean;
-    }
+    let applyHighlightingOn = () => {
+        let config = vscode.workspace.getConfiguration('ra-lsp');
+        if (config.has('highlightingOn')) {
+            highlightingOn = config.get('highlightingOn') as boolean;
+        };
+
+        if (!highlightingOn) {
+            removeHighlights();
+        }
+    };
+
+    // Apply the highlightingOn config now and whenever the config changes
+    applyHighlightingOn();
+    vscode.workspace.onDidChangeConfiguration(_ => {
+        applyHighlightingOn();
+    });
 
     let textDocumentContentProvider = new TextDocumentContentProvider()
     let dispose = (disposable: vscode.Disposable) => {
@@ -130,7 +142,7 @@ export function activate(context: vscode.ExtensionContext) {
         })
     }, null, context.subscriptions)
     vscode.window.onDidChangeActiveTextEditor(async (editor) => {
-        if (!editor || editor.document.languageId != 'rust') return
+        if (!highlightingOn || !editor || editor.document.languageId != 'rust') return
         let params: lc.TextDocumentIdentifier = {
             uri: editor.document.uri.toString()
         }
@@ -179,7 +191,7 @@ function startServer() {
                 let editor = vscode.window.visibleTextEditors.find(
                     (editor) => editor.document.uri.toString() == params.uri
                 )
-                if (editor == null) return;
+                if (!highlightingOn || !editor) return;
                 setHighlights(
                     editor,
                     params.decorations,
@@ -213,10 +225,11 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
     }
 }
 
+let decorations: { [index: string]: vscode.TextEditorDecorationType } = {};
 
-const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() => {
+function initDecorations() {
     const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj })
-    return {
+    decorations = {
         background: decor("#3F3F3F"),
         error: vscode.window.createTextEditorDecorationType({
             borderColor: "red",
@@ -232,14 +245,26 @@ const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() =>
         attribute: decor("#BFEBBF"),
         literal: decor("#DFAF8F"),
     }
-})()
+}
+
+function removeHighlights() {
+    for (let tag in decorations) {
+        decorations[tag].dispose();
+    }
+
+    decorations = {};
+}
 
 function setHighlights(
     editor: vscode.TextEditor,
     highlights: Array<Decoration>
 ) {
-    if (!highlightingOn) {
-        return;
+    // Initialize decorations if necessary
+    //
+    // Note: decoration objects need to be kept around so we can dispose them
+    // if the user disables syntax highlighting
+    if (Object.keys(decorations).length === 0) {
+        initDecorations();
     }
 
     let byTag: Map<string, vscode.Range[]> = new Map()