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) {
|
2018-10-23 22:14:56 -07:00
|
|
|
|
2015-11-05 17:33:07 +01:00
|
|
|
console.log('decorator sample is activated');
|
2016-12-30 11:17:41 +01:00
|
|
|
|
2019-03-01 16:15:58 +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
|
2016-12-30 12:39:27 +01:00
|
|
|
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
|
2016-12-30 12:39:27 +01:00
|
|
|
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
|
|
|
|
2016-12-30 12:39:27 +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;
|
|
|
|
|
}
|
2016-12-30 12:39:27 +01:00
|
|
|
const regEx = /\d+/g;
|
|
|
|
|
const text = activeEditor.document.getText();
|
|
|
|
|
const smallNumbers: vscode.DecorationOptions[] = [];
|
|
|
|
|
const largeNumbers: vscode.DecorationOptions[] = [];
|
|
|
|
|
let match;
|
2020-05-29 13:12:13 -07:00
|
|
|
while ((match = regEx.exec(text))) {
|
2016-12-30 12:39:27 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2019-03-01 16:15:58 +01:00
|
|
|
|
2022-04-13 15:49:42 -07:00
|
|
|
function triggerUpdateDecorations(throttle = false) {
|
2019-03-01 16:15:58 +01:00
|
|
|
if (timeout) {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
timeout = undefined;
|
|
|
|
|
}
|
2021-11-30 13:48:48 -03:00
|
|
|
if (throttle) {
|
|
|
|
|
timeout = setTimeout(updateDecorations, 500);
|
|
|
|
|
} else {
|
|
|
|
|
updateDecorations();
|
|
|
|
|
}
|
2019-03-01 16:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2021-11-30 13:48:48 -03:00
|
|
|
triggerUpdateDecorations(true);
|
2019-03-01 16:15:58 +01:00
|
|
|
}
|
|
|
|
|
}, null, context.subscriptions);
|
|
|
|
|
|
2015-11-05 17:33:07 +01:00
|
|
|
}
|
2018-10-23 22:14:56 -07:00
|
|
|
|