Dynamically apply highlightingOn config

Fixes #84
This commit is contained in:
Adolfo Ochagavía 2018-10-06 22:53:12 +02:00
parent 81bf190f7a
commit 9ccc568601
2 changed files with 37 additions and 12 deletions

View file

@ -111,7 +111,7 @@
], ],
"configuration": { "configuration": {
"type": "object", "type": "object",
"title": "Rust Analyzer configuration", "title": "Rust Analyzer",
"properties": { "properties": {
"ra-lsp.highlightingOn": { "ra-lsp.highlightingOn": {
"type": "boolean", "type": "boolean",

View file

@ -11,10 +11,22 @@ let uris = {
let highlightingOn = true; let highlightingOn = true;
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
let config = vscode.workspace.getConfiguration('ra-lsp'); let applyHighlightingOn = () => {
if (config.has('highlightingOn')) { let config = vscode.workspace.getConfiguration('ra-lsp');
highlightingOn = config.get('highlightingOn') as boolean; 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 textDocumentContentProvider = new TextDocumentContentProvider()
let dispose = (disposable: vscode.Disposable) => { let dispose = (disposable: vscode.Disposable) => {
@ -130,7 +142,7 @@ export function activate(context: vscode.ExtensionContext) {
}) })
}, null, context.subscriptions) }, null, context.subscriptions)
vscode.window.onDidChangeActiveTextEditor(async (editor) => { vscode.window.onDidChangeActiveTextEditor(async (editor) => {
if (!editor || editor.document.languageId != 'rust') return if (!highlightingOn || !editor || editor.document.languageId != 'rust') return
let params: lc.TextDocumentIdentifier = { let params: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString() uri: editor.document.uri.toString()
} }
@ -179,7 +191,7 @@ function startServer() {
let editor = vscode.window.visibleTextEditors.find( let editor = vscode.window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() == params.uri (editor) => editor.document.uri.toString() == params.uri
) )
if (editor == null) return; if (!highlightingOn || !editor) return;
setHighlights( setHighlights(
editor, editor,
params.decorations, 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 }) const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj })
return { decorations = {
background: decor("#3F3F3F"), background: decor("#3F3F3F"),
error: vscode.window.createTextEditorDecorationType({ error: vscode.window.createTextEditorDecorationType({
borderColor: "red", borderColor: "red",
@ -232,14 +245,26 @@ const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() =>
attribute: decor("#BFEBBF"), attribute: decor("#BFEBBF"),
literal: decor("#DFAF8F"), literal: decor("#DFAF8F"),
} }
})() }
function removeHighlights() {
for (let tag in decorations) {
decorations[tag].dispose();
}
decorations = {};
}
function setHighlights( function setHighlights(
editor: vscode.TextEditor, editor: vscode.TextEditor,
highlights: Array<Decoration> highlights: Array<Decoration>
) { ) {
if (!highlightingOn) { // Initialize decorations if necessary
return; //
// 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() let byTag: Map<string, vscode.Range[]> = new Map()