-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
parser.ts
359 lines (288 loc) · 13.2 KB
/
parser.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import * as vscode from 'vscode';
import { Configuration } from './configuration';
export class Parser {
private tags: CommentTag[] = [];
private expression: string = "";
private delimiter: string = "";
private blockCommentStart: string = "";
private blockCommentEnd: string = "";
private highlightSingleLineComments = true;
private highlightMultilineComments = false;
private highlightJSDoc = false;
// * this will allow plaintext files to show comment highlighting if switched on
private isPlainText = false;
// * this is used to prevent the first line of the file (specifically python) from coloring like other comments
private ignoreFirstLine = false;
// * this is used to trigger the events when a supported language code is found
public supportedLanguage = true;
// Read from the package.json
private contributions: Contributions = vscode.workspace.getConfiguration('better-comments') as any;
// The configuration necessary to find supported languages on startup
private configuration: Configuration;
/**
* Creates a new instance of the Parser class
* @param configuration
*/
public constructor(config: Configuration) {
this.configuration = config;
this.setTags();
}
/**
* Sets the regex to be used by the matcher based on the config specified in the package.json
* @param languageCode The short code of the current language
* https://code.visualstudio.com/docs/languages/identifiers
*/
public async SetRegex(languageCode: string) {
await this.setDelimiter(languageCode);
// if the language isn't supported, we don't need to go any further
if (!this.supportedLanguage) {
return;
}
let characters: Array<string> = [];
for (let commentTag of this.tags) {
characters.push(commentTag.escapedTag);
}
if (this.isPlainText && this.contributions.highlightPlainText) {
// start by tying the regex to the first character in a line
this.expression = "(^)+([ \\t]*[ \\t]*)";
} else {
// start by finding the delimiter (//, --, #, ') with optional spaces or tabs
this.expression = "(" + this.delimiter + ")+( |\t)*";
}
// Apply all configurable comment start tags
this.expression += "(";
this.expression += characters.join("|");
this.expression += ")+(.*)";
}
/**
* Finds all single line comments delimited by a given delimiter and matching tags specified in package.json
* @param activeEditor The active text editor containing the code document
*/
public FindSingleLineComments(activeEditor: vscode.TextEditor): void {
// If highlight single line comments is off, single line comments are not supported for this language
if (!this.highlightSingleLineComments) return;
let text = activeEditor.document.getText();
// if it's plain text, we have to do mutliline regex to catch the start of the line with ^
let regexFlags = (this.isPlainText) ? "igm" : "ig";
let regEx = new RegExp(this.expression, regexFlags);
let match: any;
while (match = regEx.exec(text)) {
let startPos = activeEditor.document.positionAt(match.index);
let endPos = activeEditor.document.positionAt(match.index + match[0].length);
let range = { range: new vscode.Range(startPos, endPos) };
// Required to ignore the first line of .py files (#61)
if (this.ignoreFirstLine && startPos.line === 0 && startPos.character === 0) {
continue;
}
// Find which custom delimiter was used in order to add it to the collection
let matchTag = this.tags.find(item => item.tag.toLowerCase() === match[3].toLowerCase());
if (matchTag) {
matchTag.ranges.push(range);
}
}
}
/**
* Finds block comments as indicated by start and end delimiter
* @param activeEditor The active text editor containing the code document
*/
public FindBlockComments(activeEditor: vscode.TextEditor): void {
// If highlight multiline is off in package.json or doesn't apply to his language, return
if (!this.highlightMultilineComments) return;
let text = activeEditor.document.getText();
// Build up regex matcher for custom delimiter tags
let characters: Array<string> = [];
for (let commentTag of this.tags) {
characters.push(commentTag.escapedTag);
}
// Combine custom delimiters and the rest of the comment block matcher
let commentMatchString = "(^)+([ \\t]*[ \\t]*)(";
commentMatchString += characters.join("|");
commentMatchString += ")([ ]*|[:])+([^*/][^\\r\\n]*)";
// Use start and end delimiters to find block comments
let regexString = "(^|[ \\t])(";
regexString += this.blockCommentStart;
regexString += "[\\s])+([\\s\\S]*?)(";
regexString += this.blockCommentEnd;
regexString += ")";
let regEx = new RegExp(regexString, "gm");
let commentRegEx = new RegExp(commentMatchString, "igm");
// Find the multiline comment block
let match: any;
while (match = regEx.exec(text)) {
let commentBlock = match[0];
// Find the line
let line;
while (line = commentRegEx.exec(commentBlock)) {
let startPos = activeEditor.document.positionAt(match.index + line.index + line[2].length);
let endPos = activeEditor.document.positionAt(match.index + line.index + line[0].length);
let range: vscode.DecorationOptions = { range: new vscode.Range(startPos, endPos) };
// Find which custom delimiter was used in order to add it to the collection
let matchString = line[3] as string;
let matchTag = this.tags.find(item => item.tag.toLowerCase() === matchString.toLowerCase());
if (matchTag) {
matchTag.ranges.push(range);
}
}
}
}
/**
* Finds all multiline comments starting with "*"
* @param activeEditor The active text editor containing the code document
*/
public FindJSDocComments(activeEditor: vscode.TextEditor): void {
// If highlight multiline is off in package.json or doesn't apply to his language, return
if (!this.highlightMultilineComments && !this.highlightJSDoc) return;
let text = activeEditor.document.getText();
// Build up regex matcher for custom delimiter tags
let characters: Array<string> = [];
for (let commentTag of this.tags) {
characters.push(commentTag.escapedTag);
}
// Combine custom delimiters and the rest of the comment block matcher
let commentMatchString = "(^)+([ \\t]*\\*[ \\t]*)("; // Highlight after leading *
let regEx = /(^|[ \t])(\/\*\*)+([\s\S]*?)(\*\/)/gm; // Find rows of comments matching pattern /** */
commentMatchString += characters.join("|");
commentMatchString += ")([ ]*|[:])+([^*/][^\\r\\n]*)";
let commentRegEx = new RegExp(commentMatchString, "igm");
// Find the multiline comment block
let match: any;
while (match = regEx.exec(text)) {
let commentBlock = match[0];
// Find the line
let line;
while (line = commentRegEx.exec(commentBlock)) {
let startPos = activeEditor.document.positionAt(match.index + line.index + line[2].length);
let endPos = activeEditor.document.positionAt(match.index + line.index + line[0].length);
let range: vscode.DecorationOptions = { range: new vscode.Range(startPos, endPos) };
// Find which custom delimiter was used in order to add it to the collection
let matchString = line[3] as string;
let matchTag = this.tags.find(item => item.tag.toLowerCase() === matchString.toLowerCase());
if (matchTag) {
matchTag.ranges.push(range);
}
}
}
}
/**
* Apply decorations after finding all relevant comments
* @param activeEditor The active text editor containing the code document
*/
public ApplyDecorations(activeEditor: vscode.TextEditor): void {
for (let tag of this.tags) {
activeEditor.setDecorations(tag.decoration, tag.ranges);
// clear the ranges for the next pass
tag.ranges.length = 0;
}
}
//#region Private Methods
/**
* Sets the comment delimiter [//, #, --, '] of a given language
* @param languageCode The short code of the current language
* https://code.visualstudio.com/docs/languages/identifiers
*/
private async setDelimiter(languageCode: string): Promise<void> {
this.supportedLanguage = false;
this.ignoreFirstLine = false;
this.isPlainText = false;
const config = await this.configuration.GetCommentConfiguration(languageCode);
if (config) {
let blockCommentStart = config.blockComment ? config.blockComment[0] : null;
let blockCommentEnd = config.blockComment ? config.blockComment[1] : null;
this.setCommentFormat(config.lineComment || blockCommentStart, blockCommentStart, blockCommentEnd);
this.supportedLanguage = true;
}
switch (languageCode) {
case "apex":
case "javascript":
case "javascriptreact":
case "typescript":
case "typescriptreact":
this.highlightJSDoc = true;
break;
case "elixir":
case "python":
case "tcl":
this.ignoreFirstLine = true;
break;
case "plaintext":
this.isPlainText = true;
// If highlight plaintext is enabled, this is a supported language
this.supportedLanguage = this.contributions.highlightPlainText;
break;
}
}
/**
* Sets the highlighting tags up for use by the parser
*/
private setTags(): void {
let items = this.contributions.tags;
for (let item of items) {
let options: vscode.DecorationRenderOptions = { color: item.color, backgroundColor: item.backgroundColor };
// ? the textDecoration is initialised to empty so we can concat a preceeding space on it
options.textDecoration = "";
if (item.strikethrough) {
options.textDecoration += "line-through";
}
if (item.underline) {
options.textDecoration += " underline";
}
if (item.bold) {
options.fontWeight = "bold";
}
if (item.italic) {
options.fontStyle = "italic";
}
let escapedSequence = item.tag.replace(/([()[{*+.$^\\|?])/g, '\\$1');
this.tags.push({
tag: item.tag,
escapedTag: escapedSequence.replace(/\//gi, "\\/"), // ! hardcoded to escape slashes
ranges: [],
decoration: vscode.window.createTextEditorDecorationType(options)
});
}
}
/**
* Escapes a given string for use in a regular expression
* @param input The input string to be escaped
* @returns {string} The escaped string
*/
private escapeRegExp(input: string): string {
return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
/**
* Set up the comment format for single and multiline highlighting
* @param singleLine The single line comment delimiter. If NULL, single line is not supported
* @param start The start delimiter for block comments
* @param end The end delimiter for block comments
*/
private setCommentFormat(
singleLine: string | string[] | null,
start: string | null = null,
end: string | null = null): void {
this.delimiter = "";
this.blockCommentStart = "";
this.blockCommentEnd = "";
// If no single line comment delimiter is passed, single line comments are not supported
if (singleLine) {
if (typeof singleLine === 'string') {
this.delimiter = this.escapeRegExp(singleLine).replace(/\//ig, "\\/");
}
else if (singleLine.length > 0) {
// * if multiple delimiters are passed, the language has more than one single line comment format
var delimiters = singleLine
.map(s => this.escapeRegExp(s))
.join("|");
this.delimiter = delimiters;
}
}
else {
this.highlightSingleLineComments = false;
}
if (start && end) {
this.blockCommentStart = this.escapeRegExp(start);
this.blockCommentEnd = this.escapeRegExp(end);
this.highlightMultilineComments = this.contributions.multilineComments;
}
}
//#endregion
}