-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
145 lines (112 loc) · 4.16 KB
/
extension.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const vscode = require('vscode');
var window = vscode.window;
var workspace = vscode.workspace;
function escapeRegExp(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
function getAssembledData(keywords, isCaseSensitive) {
var result = {}
keywords.forEach((v) => {
let text = v.text;
if (!text) return;//NOTE: in case of the text is empty
if (!isCaseSensitive) {
text = text.toUpperCase();
}
result[text] = v;
})
return result;
}
function createStatusBarItem() {
var statusBarItem = window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.text = "$(checklist): 0";
statusBarItem.tooltip = 'List current annotations';
// statusBarItem.command = 'currenttodos.showOutputChannel';
return statusBarItem;
}
function updateDecorations() {
var activeEditor = window.activeTextEditor;
if (!activeEditor || !activeEditor.document) {
window.statusBarItem.text = "$(checklist): 0";
window.statusBarItem.show();
return;
}
var text = activeEditor.document.getText();
var matches = {}, match;
while (match = window.pattern.exec(text)) {
var startPos = activeEditor.document.positionAt(match.index);
var endPos = activeEditor.document.positionAt(match.index + match[0].length);
var decoration = {
range: new vscode.Range(startPos, endPos)
};
var matchedValue = match[0];
if (matches[matchedValue]) {
matches[matchedValue].push(decoration);
} else {
matches[matchedValue] = [decoration];
}
}
var sb_msg = "";
Object.keys(window.decorationTypes).forEach((v) => {
var rangeOption = window.settings.get('enable') && matches[v] ? matches[v] : [];
var decorationType = window.decorationTypes[v];
activeEditor.setDecorations(decorationType, rangeOption);
if (matches[v]) {
sb_msg = sb_msg + " " + v + matches[v].length;
}
})
if (sb_msg) {
window.statusBarItem.text = "$(checklist)" + sb_msg;
}
else {
window.statusBarItem.text = "$(checklist): 0";
}
window.statusBarItem.show();
}
function init(settings) {
let isCaseSensitive = settings.get('isCaseSensitive');
if (!window.statusBarItem) {
window.statusBarItem = createStatusBarItem();
}
// if (!window.outputChannel) {
// window.outputChannel = window.createOutputChannel('TodoHighlight');
// }
window.decorationTypes = {};
let assembledData = getAssembledData(settings.get('keywords'), isCaseSensitive);
Object.keys(assembledData).forEach((v) => {
var mergedStyle = Object.assign({}, {
overviewRulerLane: vscode.OverviewRulerLane.Right
}, assembledData[v]);
if (!mergedStyle.overviewRulerColor) {
// use backgroundColor as the default overviewRulerColor if not specified by the user setting
mergedStyle.overviewRulerColor = mergedStyle.backgroundColor;
}
window.decorationTypes[v] = window.createTextEditorDecorationType(mergedStyle);
});
let pattern = Object.keys(assembledData).map((v) => {
return escapeRegExp(v);
}).join('|');
if (isCaseSensitive) {
window.pattern = new RegExp(pattern, 'g');
} else {
window.pattern = new RegExp(pattern, 'gi');
}
}
function activate(context) {
console.log('Extension "currenttodos" activated!');
window.settings = workspace.getConfiguration('currenttodos');
init(window.settings);
let disposable = vscode.commands.registerCommand('currenttodos.refresh', updateDecorations);
context.subscriptions.push(disposable);
workspace.onDidChangeConfiguration(function () {
window.settings = workspace.getConfiguration('currenttodos');
if (!window.settings.get('enable')) return;
init(window.settings);
updateDecorations();
}, null, context.subscriptions);
window.onDidChangeActiveTextEditor(updateDecorations);
workspace.onDidSaveTextDocument(updateDecorations);
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;