-
Notifications
You must be signed in to change notification settings - Fork 15
/
extension.ts
233 lines (203 loc) · 6.85 KB
/
extension.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
import * as vscode from "vscode";
import { languages } from "./languages";
const deepDecorations = [
vscode.window.createTextEditorDecorationType({
color: { id: "rainbowend.deep1" }
}),
vscode.window.createTextEditorDecorationType({
color: { id: "rainbowend.deep2" }
}),
vscode.window.createTextEditorDecorationType({
color: { id: "rainbowend.deep3" }
})
];
let timeout: NodeJS.Timer | null = null;
let regExs: { [index: string]: RegExp } = {};
export function activate(context: vscode.ExtensionContext) {
Object.keys(languages).forEach(language => {
regExs[language] = buildRegex(language);
});
let activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
triggerUpdateDecorations(activeEditor);
}
vscode.window.onDidChangeActiveTextEditor(
editor => {
activeEditor = editor;
if (activeEditor) {
triggerUpdateDecorations(activeEditor);
}
},
null,
context.subscriptions
);
vscode.workspace.onDidChangeTextDocument(
event => {
if (activeEditor && event.document === activeEditor.document) {
triggerUpdateDecorations(activeEditor);
}
},
null,
context.subscriptions
);
}
function triggerUpdateDecorations(activeEditor: vscode.TextEditor) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(updateDecorations, 250);
}
function buildRegex(language: string) {
const languageConfiguration = languages[language];
let tokens: Array<string> = languageConfiguration["openTokens"];
tokens = tokens.concat(languageConfiguration["inlineOpenTokens"]);
tokens = tokens.concat(languageConfiguration["closeTokens"]);
tokens = tokens.concat(languageConfiguration["neutralTokens"]);
return RegExp("(\\b)(" + tokens.join("|") + ")(\\b)", "gm");
}
function ignoreInDelimiters(
token_pairs:
| Array<{
open: string;
close: string;
}>
| undefined,
text: string
) {
/* This function replaces text inside each token pair with spaces,
so as to ignore the text between delimiters */
if (token_pairs) {
token_pairs.forEach(({ open: open_delim, close: close_delim }) => {
/* Only allow nesting if delimiters are different */
if (open_delim == close_delim) {
let regexp = RegExp(
`${open_delim}[^${close_delim}]*${close_delim}`,
"gm"
);
text = text.replace(regexp, match => {
return " ".repeat(match.length);
});
} else {
let openRegexp = RegExp(`${open_delim}`, "gm");
let closeRegexp = RegExp(`${close_delim}`, "gm");
let indices = [];
let match = openRegexp.exec(text);
if (match == null) {
return;
}
while (match != null) {
indices.push({ index: match.index, type: "open" });
match = openRegexp.exec(text);
}
match = closeRegexp.exec(text);
if (match == null) {
return;
}
while (match != null) {
indices.push({ index: match.index, type: "close" });
match = closeRegexp.exec(text);
}
/* Sort by index */
indices = indices.sort(({ index: a }, { index: b }) => a - b);
let ignore_env_counter = 0;
let first_index = indices[0].index;
let index: number;
let type: string;
/* This isn't so inefficient in that it is
O(indices.length), instead of O(text.length).
Also, the list is already ordered, which is really helpful */
for ({ index, type } of indices) {
/* skip current token if trying to close when there is no open block
cannot just break because '\n' can be both a closing token and a
normal line end
*/
if (type == "close" && ignore_env_counter == 0) {
continue;
}
/* if counter is zero, should begin an ignore block */
if (ignore_env_counter == 0) {
first_index = index;
}
if (type == "open") {
/* if it is an open token, always increment env counter */
ignore_env_counter++;
} else {
ignore_env_counter--;
/* if counter has reached zero after a closing token,
end ignore block */
let last_index = index;
/* Set ignore block slice as whitespace and keep the rest */
text =
text.slice(0, first_index) +
" ".repeat(last_index - first_index + 1) +
text.slice(last_index + 1);
}
}
if (ignore_env_counter != 0) {
/* Didn't close last block */
text =
text.slice(0, first_index) +
" ".repeat(text.length - first_index + 1);
}
}
});
}
return text;
}
function updateDecorations() {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
const languageConfiguration = languages[activeEditor.document.languageId];
let text = activeEditor.document.getText();
const options: vscode.DecorationOptions[][] = [];
deepDecorations.forEach(d => {
options.push([]);
});
let match;
let deep = 0;
// if we are not case sensitive, then ensure the case of text matches then keyworkd matches
if (!languageConfiguration.caseSensitive) {
text = text.toLowerCase();
}
// substitute all ignore intervals with spaces
// this ensures commented code or
// keywords inside strings are ignored properly
// also, prepend a whitespace to allow matching the first character in document
// if needed
text =
" " + ignoreInDelimiters(languageConfiguration.ignoreInDelimiters, text);
while ((match = regExs[activeEditor.document.languageId].exec(text))) {
const startIndex = match.index + match[1].length - 1; // Decrement to compensate for added character
const startPos = activeEditor.document.positionAt(startIndex);
const endPos = activeEditor.document.positionAt(
startIndex + match[2].length
);
const decoration: vscode.DecorationOptions = {
range: new vscode.Range(startPos, endPos)
};
if (languageConfiguration.closeTokens.indexOf(match[2]) > -1) {
if (deep > 0) {
deep -= 1;
}
options[deep % deepDecorations.length].push(decoration);
} else if (languageConfiguration.neutralTokens.indexOf(match[2]) > -1) {
if (deep > 0) {
options[(deep - 1) % deepDecorations.length].push(decoration);
}
} else if (languageConfiguration.openTokens.indexOf(match[2]) > -1) {
options[deep % deepDecorations.length].push(decoration);
deep += 1;
} else {
if (match[1].length === 0 || match[1].match("^[\\s\n]+$")) {
options[deep % deepDecorations.length].push(decoration);
deep += 1;
}
}
}
deepDecorations.forEach((deepDecoration, i) => {
activeEditor.setDecorations(deepDecoration, options[i]);
});
}