Skip to content

Instantly share code, notes, and snippets.

@gabro
Created September 28, 2018 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabro/8f5811e6dd55f15138e3c0165169c1a6 to your computer and use it in GitHub Desktop.
Save gabro/8f5811e6dd55f15138e3c0165169c1a6 to your computer and use it in GitHub Desktop.
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