Created
September 28, 2018 08:37
-
-
Save gabro/8f5811e6dd55f15138e3c0165169c1a6 to your computer and use it in GitHub Desktop.
VSCode decoration API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// The module 'vscode' contains the VS Code extensibility API | |
// Import the module and reference it with the alias vscode in your code below | |
import { window, commands, ExtensionContext, Range, Position, TextEditor, workspace, DecorationOptions } from 'vscode'; | |
const decorationType = window.createTextEditorDecorationType({ }) | |
function addDecorations(e: TextEditor) { | |
try { | |
const params = { | |
42: 'someIntParam', | |
foo: 'someStringParam' | |
}; | |
const decorations: DecorationOptions[] = [] | |
for (let lineNumber = 0; lineNumber < e.document.lineCount; lineNumber++) { | |
const lineText = e.document.lineAt(lineNumber).text; | |
const decs = Object.keys(params).reduce((acc: DecorationOptions[], param) => { | |
const index = lineText.indexOf(param) | |
if (index !== -1) { | |
return acc.concat({ | |
renderOptions: { before: { contentText: params[param] + ' = ', color: 'grey' }, }, | |
range: new Range(new Position(lineNumber, index), new Position(lineNumber, index + param.length)) | |
}) | |
} | |
return acc | |
}, []) | |
decs.forEach(d => decorations.push(d)) | |
} | |
e.setDecorations(decorationType, decorations) | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
export function activate(context: ExtensionContext) { | |
workspace.onDidChangeTextDocument(e => addDecorations(window.activeTextEditor)) | |
window.onDidChangeActiveTextEditor(e => { | |
e && addDecorations(e) | |
}) | |
if (window.activeTextEditor) { | |
addDecorations(window.activeTextEditor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment