Files
vscode-extension-samples/decorator-sample/src/extension.ts

89 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-12-30 11:17:41 +01:00
import * as vscode from 'vscode';
2015-11-05 17:33:07 +01:00
// this method is called when vs code is activated
export function activate(context: vscode.ExtensionContext) {
2015-11-05 17:33:07 +01:00
console.log('decorator sample is activated');
2016-12-30 11:17:41 +01:00
let timeout: NodeJS.Timer | undefined = undefined;
2015-11-05 17:33:07 +01:00
// create a decorator type that we use to decorate small numbers
const smallNumberDecorationType = vscode.window.createTextEditorDecorationType({
2015-11-05 17:33:07 +01:00
borderWidth: '1px',
borderStyle: 'solid',
overviewRulerColor: 'blue',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: {
// this color will be used in light color themes
borderColor: 'darkblue'
},
dark: {
// this color will be used in dark color themes
borderColor: 'lightblue'
}
});
2016-12-30 11:17:41 +01:00
2015-11-05 17:33:07 +01:00
// create a decorator type that we use to decorate large numbers
const largeNumberDecorationType = vscode.window.createTextEditorDecorationType({
2015-11-05 17:33:07 +01:00
cursor: 'crosshair',
2018-10-29 08:34:37 +01:00
// use a themable color. See package.json for the declaration and default values.
2018-10-28 22:29:18 +01:00
backgroundColor: { id: 'myextension.largeNumberBackground' }
2016-12-30 11:17:41 +01:00
});
2015-11-05 17:33:07 +01:00
let activeEditor = vscode.window.activeTextEditor;
2016-12-30 11:17:41 +01:00
2015-11-05 17:33:07 +01:00
function updateDecorations() {
if (!activeEditor) {
return;
}
const regEx = /\d+/g;
const text = activeEditor.document.getText();
const smallNumbers: vscode.DecorationOptions[] = [];
const largeNumbers: vscode.DecorationOptions[] = [];
let match;
while ((match = regEx.exec(text))) {
const startPos = activeEditor.document.positionAt(match.index);
const endPos = activeEditor.document.positionAt(match.index + match[0].length);
const decoration = { range: new vscode.Range(startPos, endPos), hoverMessage: 'Number **' + match[0] + '**' };
2015-11-05 17:33:07 +01:00
if (match[0].length < 3) {
smallNumbers.push(decoration);
} else {
largeNumbers.push(decoration);
}
}
activeEditor.setDecorations(smallNumberDecorationType, smallNumbers);
activeEditor.setDecorations(largeNumberDecorationType, largeNumbers);
}
2022-04-13 15:49:42 -07:00
function triggerUpdateDecorations(throttle = false) {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
if (throttle) {
timeout = setTimeout(updateDecorations, 500);
} else {
updateDecorations();
}
}
if (activeEditor) {
triggerUpdateDecorations();
}
vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document) {
triggerUpdateDecorations(true);
}
}, null, context.subscriptions);
2015-11-05 17:33:07 +01:00
}