diff --git a/src/extension.js b/src/extension.js index 8ae0439..60f2105 100644 --- a/src/extension.js +++ b/src/extension.js @@ -69,20 +69,36 @@ function activate(context) { workspace.onDidChangeConfiguration(function () { settings = workspace.getConfiguration('todohighlight'); - - //NOTE: if disabled, do not re-initialize the data or we will not be able to clear the style immediatly via 'toggle highlight' command - if (!settings.get('isEnable')) return; - - init(settings); + + // Clear all existing decorations first + Object.keys(decorationTypes).forEach((v) => { + decorationTypes[v].dispose(); + }); + decorationTypes = {}; + + // Only reinitialize if enabled + if (settings.get('isEnable')) { + init(settings); + } + triggerUpdateDecorations(); }, null, context.subscriptions); function updateDecorations() { - if (!activeEditor || !activeEditor.document) { return; } + // Clear all existing decorations first + Object.keys(decorationTypes).forEach((v) => { + activeEditor.setDecorations(decorationTypes[v], []); + }); + + // Only proceed with new decorations if enabled + if (!settings.get('isEnable')) { + return; + } + var text = activeEditor.document.getText(); var mathes = {}, match; while (match = pattern.exec(text)) { @@ -108,14 +124,14 @@ function activate(context) { } } + // Apply new decorations Object.keys(decorationTypes).forEach((v) => { if (!isCaseSensitive) { v = v.toUpperCase(); } - var rangeOption = settings.get('isEnable') && mathes[v] ? mathes[v] : []; - var decorationType = decorationTypes[v]; - activeEditor.setDecorations(decorationType, rangeOption); - }) + var rangeOption = mathes[v] ? mathes[v] : []; + activeEditor.setDecorations(decorationTypes[v], rangeOption); + }); } function init(settings) {