Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
VSCode decoration API
'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
You can’t perform that action at this time.