-
Notifications
You must be signed in to change notification settings - Fork 0
/
extsample.ts
100 lines (90 loc) · 2.85 KB
/
extsample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as vscode from "vscode";
// this method is called when vs code is activated
export function activate(context) {
console.log("decorator sample is activated");
let timeout= undefined;
// create a decorator type that we use to decorate small numbers
const smallNumberDecorationType =
vscode.window.createTextEditorDecorationType({
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",
},
});
// create a decorator type that we use to decorate large numbers
const largeNumberDecorationType =
vscode.window.createTextEditorDecorationType({
cursor: "crosshair",
// use a themable color. See package.json for the declaration and default values.
backgroundColor: { id: "myextension.largeNumberBackground" },
});
let activeEditor = vscode.window.activeTextEditor;
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] + "**",
};
if (match[0].length < 3) {
smallNumbers.push(decoration);
} else {
largeNumbers.push(decoration);
}
}
activeEditor.setDecorations(smallNumberDecorationType, smallNumbers);
activeEditor.setDecorations(largeNumberDecorationType, largeNumbers);
}
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
);
}