This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
documentDecoration.ts
504 lines (420 loc) · 20 KB
/
documentDecoration.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import * as vscode from "vscode";
import Bracket from "./bracket";
import BracketClose from "./bracketClose";
import { IStackElement } from "./IExtensionGrammar";
import LanguageConfig from "./languageConfig";
import LineState from "./lineState";
import Settings from "./settings";
import TextLine from "./textLine";
import { ignoreBracketsInToken, LineTokens } from "./vscodeFiles";
import { TextDocumentContentChangeEvent } from "vscode";
export default class DocumentDecoration {
public readonly settings: Settings;
// This program caches lines, and will only analyze linenumbers including or above a modified line
private lines: TextLine[] = [];
private readonly document: vscode.TextDocument;
private readonly languageConfig: LanguageConfig;
private scopeDecorations: vscode.TextEditorDecorationType[] = [];
private scopeSelectionHistory: vscode.Selection[][] = [];
constructor(
document: vscode.TextDocument,
config: LanguageConfig,
settings: Settings,
) {
this.settings = settings;
this.document = document;
this.languageConfig = config;
}
public dispose() {
this.disposeScopeDecorations();
}
public onDidChangeTextDocument(contentChanges: ReadonlyArray<TextDocumentContentChangeEvent>) {
if (contentChanges.length > 1 || !contentChanges[0].range.isSingleLine || contentChanges[0].text.length > 1) {
let minLineIndexToUpdate = 0;
for (const contentChange of contentChanges) {
minLineIndexToUpdate = Math.min(minLineIndexToUpdate, contentChange.range.start.line);
}
if (minLineIndexToUpdate === 0) {
this.lines = [];
}
else {
this.lines.splice(minLineIndexToUpdate);
}
this.tokenizeDocument();
return;
}
const change = contentChanges[0];
const lineNumber = change.range.start.line;
// Parse overlapped lines with goal to see if we can avoid document reparse
// By just moving existing brackets if the amount of brackets on a line didn't change
const newLine = this.tokenizeLine(lineNumber);
const currentLine = this.lines[lineNumber];
// Current line has new brackets which need to be colored
if (
!currentLine.getRuleStack().equals(newLine.getRuleStack()) ||
currentLine.getBracketHash() !== newLine.getBracketHash()
) {
this.lines[lineNumber] = newLine;
this.lines.splice(lineNumber + 1);
this.tokenizeDocument();
return;
}
const charOffset = change.text.length - change.rangeLength;
currentLine.offset(change.range.start.character, charOffset);
}
public expandBracketSelection(editor: vscode.TextEditor) {
const newSelections: vscode.Selection[] = [];
editor.selections.forEach((selection) => {
if (this.scopeSelectionHistory.length === 0) {
this.scopeSelectionHistory.push(editor.selections);
}
const nextPos = this.document.validatePosition(selection.active.translate(0, 1));
const endBracket = this.searchScopeForwards(nextPos);
if (!endBracket) {
return;
}
const start = endBracket.openBracket.token.range.start.translate(0, 1);
const end = endBracket.token.range.end.translate(0, -1);
newSelections.push(new vscode.Selection(start, end));
});
if (newSelections.length > 0) {
this.scopeSelectionHistory.push(newSelections);
editor.selections = newSelections;
}
}
public undoBracketSelection(editor: vscode.TextEditor) {
this.scopeSelectionHistory.pop();
if (this.scopeSelectionHistory.length === 0) {
return;
}
const scopes = this.scopeSelectionHistory[this.scopeSelectionHistory.length - 1];
editor.selections = scopes;
}
// Lines are stored in an array, if line is requested outside of array bounds
// add emptys lines until array is correctly sized
public getLine(index: number, state: IStackElement): TextLine {
if (index < this.lines.length) {
return this.lines[index];
}
else {
if (this.lines.length === 0) {
this.lines.push(
new TextLine(state, new LineState(this.settings, this.languageConfig), 0),
);
}
if (index < this.lines.length) {
return this.lines[index];
}
if (index === this.lines.length) {
const previousLine = this.lines[this.lines.length - 1];
const newLine =
new TextLine(state, previousLine.cloneState(), index);
this.lines.push(newLine);
return newLine;
}
throw new Error("Cannot look more than one line ahead");
}
}
public tokenizeDocument() {
// console.log("Tokenizing " + this.document.fileName);
// One document may be shared by multiple editors (side by side view)
const editors: vscode.TextEditor[] =
vscode.window.visibleTextEditors.filter((e) => this.document === e.document);
if (editors.length === 0) {
console.warn("No editors associated with document: " + this.document.fileName);
return;
}
// console.time("tokenizeDocument");
const lineIndex = this.lines.length;
const lineCount = this.document.lineCount;
if (lineIndex < lineCount) {
// console.log("Reparse from line: " + (lineIndex + 1));
for (let i = lineIndex; i < lineCount; i++) {
const newLine = this.tokenizeLine(i);
this.lines.push(newLine);
}
}
// console.log("Coloring document");
this.colorDecorations(editors);
// console.timeEnd("tokenizeDocument");
}
public updateScopeDecorations(event: vscode.TextEditorSelectionChangeEvent) {
// console.time("updateScopeDecorations");
this.disposeScopeDecorations();
// For performance reasons we only do one selection for now.
// Simply wrap in foreach selection for multicursor, maybe put it behind an option?
const selection = event.textEditor.selection;
const closeBracket = this.searchScopeForwards(selection.active);
if (!closeBracket) {
return;
}
const openBracket = closeBracket.openBracket;
const beginRange = openBracket.token.range;
const endRange = closeBracket.token.range;
const startLineIndex = openBracket.token.range.start.line;
const endLineIndex = closeBracket.token.range.start.line;
if (this.settings.highlightActiveScope) {
const decoration =
this.settings.createScopeBracketDecorations(closeBracket.color);
event.textEditor.setDecorations(decoration, [beginRange, endRange]);
this.scopeDecorations.push(decoration);
}
if (this.settings.showBracketsInGutter) {
if (startLineIndex === endLineIndex) {
const decoration = this.settings.createGutterBracketDecorations
(closeBracket.color, openBracket.token.character + closeBracket.token.character);
event.textEditor.setDecorations(decoration, [beginRange, endRange]);
this.scopeDecorations.push(decoration);
}
else {
const decorationOpen =
this.settings.createGutterBracketDecorations(openBracket.color, openBracket.token.character);
event.textEditor.setDecorations(decorationOpen, [beginRange]);
this.scopeDecorations.push(decorationOpen);
const decorationClose =
this.settings.createGutterBracketDecorations(closeBracket.color, closeBracket.token.character);
event.textEditor.setDecorations(decorationClose, [endRange]);
this.scopeDecorations.push(decorationClose);
}
}
if (this.settings.showBracketsInRuler) {
const decoration =
this.settings.createRulerBracketDecorations(closeBracket.color);
event.textEditor.setDecorations(decoration, [beginRange, endRange]);
this.scopeDecorations.push(decoration);
}
const lastWhiteSpaceCharacterIndex =
this.document.lineAt(endRange.start).firstNonWhitespaceCharacterIndex;
const lastBracketStartIndex = endRange.start.character;
const lastBracketIsFirstCharacterOnLine = lastWhiteSpaceCharacterIndex === lastBracketStartIndex;
let leftBorderColumn = Infinity;
const tabSize = event.textEditor.options.tabSize as number;
const position =
this.settings.scopeLineRelativePosition ?
Math.min(endRange.start.character, beginRange.start.character) : 0;
let leftBorderIndex = position;
const start = beginRange.start.line + 1;
const end = endRange.start.line;
// Start -1 because prefer draw line at current indent level
for (let lineIndex = start - 1; lineIndex <= end; lineIndex++) {
const line = this.document.lineAt(lineIndex);
if (!line.isEmptyOrWhitespace) {
const firstCharIndex = line.firstNonWhitespaceCharacterIndex;
leftBorderIndex = Math.min(leftBorderIndex, firstCharIndex);
leftBorderColumn = Math.min(leftBorderColumn,
this.calculateColumnFromCharIndex(line.text, firstCharIndex, tabSize));
}
}
if (this.settings.showVerticalScopeLine) {
const verticalLineRanges: Array<{ range: vscode.Range, valid: boolean }> = [];
const endOffset = lastBracketIsFirstCharacterOnLine ? end - 1 : end;
for (let lineIndex = start; lineIndex <= endOffset; lineIndex++) {
const line = this.document.lineAt(lineIndex);
const linePosition = new vscode.Position(lineIndex,
this.calculateCharIndexFromColumn(line.text, leftBorderColumn, tabSize));
const range = new vscode.Range(linePosition, linePosition);
const valid = line.text.length >= leftBorderIndex;
verticalLineRanges.push({ range, valid });
}
const safeFallbackPosition = new vscode.Position(start - 1, leftBorderIndex);
this.setVerticalLineDecoration(closeBracket, event, safeFallbackPosition, verticalLineRanges);
}
if (this.settings.showHorizontalScopeLine) {
const underlineLineRanges: vscode.Range[] = [];
const overlineLineRanges: vscode.Range[] = [];
if (startLineIndex === endLineIndex) {
underlineLineRanges.push(new vscode.Range(beginRange.start, endRange.end));
}
else {
const startTextLine = this.document.lineAt(startLineIndex);
const endTextLine = this.document.lineAt(endLineIndex);
const leftStartPos = new vscode.Position(beginRange.start.line,
this.calculateCharIndexFromColumn(startTextLine.text, leftBorderColumn, tabSize));
const leftEndPos = new vscode.Position(endRange.start.line,
this.calculateCharIndexFromColumn(endTextLine.text, leftBorderColumn, tabSize));
underlineLineRanges.push(new vscode.Range(leftStartPos, beginRange.end));
if (lastBracketIsFirstCharacterOnLine) {
overlineLineRanges.push(new vscode.Range(leftEndPos, endRange.end));
}
else {
underlineLineRanges.push(new vscode.Range(leftEndPos, endRange.end));
}
}
if (underlineLineRanges) {
this.setUnderLineDecoration(closeBracket, event, underlineLineRanges);
}
if (overlineLineRanges) {
this.setOverLineDecoration(closeBracket, event, overlineLineRanges);
}
}
// console.timeEnd("updateScopeDecorations");
}
private tokenizeLine(index: number) {
const newText = this.document.lineAt(index).text;
const previousLineRuleStack = index > 0 ?
this.lines[index - 1].getRuleStack() :
undefined;
const previousLineState = index > 0 ?
this.lines[index - 1].cloneState() :
new LineState(this.settings, this.languageConfig);
const tokenized = this.languageConfig.grammar.tokenizeLine2(newText, previousLineRuleStack);
const tokens = tokenized.tokens;
const lineTokens = new LineTokens(tokens, newText);
const matches = new Array<{ content: string, index: number }>();
const count = lineTokens.getCount();
for (let i = 0; i < count; i++) {
const tokenType = lineTokens.getStandardTokenType(i);
if (!ignoreBracketsInToken(tokenType)) {
const searchStartOffset = tokens[i * 2];
const searchEndOffset = i < count ? tokens[(i + 1) * 2] : newText.length;
const currentTokenText = newText.substring(searchStartOffset, searchEndOffset);
let result: RegExpExecArray | null;
// tslint:disable-next-line:no-conditional-assignment
while ((result = this.languageConfig.regex.exec(currentTokenText)) !== null) {
matches.push({ content: result[0], index: result.index + searchStartOffset });
}
}
}
const newLine = new TextLine(tokenized.ruleStack, previousLineState, index);
for (const match of matches) {
const lookup = this.languageConfig.bracketToId.get(match.content);
if (lookup) {
newLine.AddToken(match.content, match.index, lookup.key, lookup.open);
}
}
return newLine;
}
private setOverLineDecoration(
bracket: Bracket,
event: vscode.TextEditorSelectionChangeEvent,
overlineLineRanges: vscode.Range[]) {
const lineDecoration = this.settings.createScopeLineDecorations(bracket.color, true, false, false, false);
event.textEditor.setDecorations(lineDecoration, overlineLineRanges);
this.scopeDecorations.push(lineDecoration);
}
private setUnderLineDecoration(
bracket: Bracket,
event: vscode.TextEditorSelectionChangeEvent,
underlineLineRanges: vscode.Range[]) {
const lineDecoration = this.settings.createScopeLineDecorations(bracket.color, false, false, true, false);
event.textEditor.setDecorations(lineDecoration, underlineLineRanges);
this.scopeDecorations.push(lineDecoration);
}
private setVerticalLineDecoration(
bracket: Bracket,
event: vscode.TextEditorSelectionChangeEvent,
fallBackPosition: vscode.Position,
verticalLineRanges: Array<{ range: vscode.Range, valid: boolean }>,
) {
const offsets:
Array<{ range: vscode.Range, downOffset: number }> = [];
const normalDecoration = this.settings.createScopeLineDecorations(bracket.color, false, false, false, true);
if (verticalLineRanges.length === 0) {
return;
}
const normalRanges = verticalLineRanges.filter((e) => e.valid).map((e) => e.range);
// Get first valid range, if non fall-back to opening position
let aboveValidRange = new vscode.Range(fallBackPosition, fallBackPosition);
for (const lineRange of verticalLineRanges) {
if (lineRange.valid) {
aboveValidRange = lineRange.range;
break;
}
}
/* Keep updating last valid range to keep offset distance minimum
to prevent missing decorations when scrolling */
for (const lineRange of verticalLineRanges) {
if (lineRange.valid) {
aboveValidRange = lineRange.range;
}
else {
const offset = lineRange.range.start.line - aboveValidRange.start.line;
offsets.push({ range: aboveValidRange, downOffset: offset });
}
}
event.textEditor.setDecorations(normalDecoration, normalRanges);
this.scopeDecorations.push(normalDecoration);
offsets.forEach((offset) => {
const decoration = this.settings.createScopeLineDecorations(
bracket.color, false, false, false, true, offset.downOffset,
);
event.textEditor.setDecorations(decoration, [offset.range]);
this.scopeDecorations.push(decoration);
});
}
private disposeScopeDecorations() {
for (const decoration of this.scopeDecorations) {
decoration.dispose();
}
this.scopeDecorations = [];
}
private searchScopeForwards(position: vscode.Position): BracketClose | undefined {
for (let i = position.line; i < this.lines.length; i++) {
const endBracket = this.lines[i].getClosingBracket(position);
if (endBracket) {
return endBracket;
}
}
}
private colorDecorations(editors: vscode.TextEditor[]) {
// console.time("colorDecorations");
const colorMap = new Map<string, vscode.Range[]>();
// Reduce all the colors/ranges of the lines into a singular map
for (const line of this.lines) {
{
const brackets = line.getAllBrackets();
for (const bracket of brackets) {
const existingRanges = colorMap.get(bracket.color);
if (existingRanges !== undefined) {
existingRanges.push(bracket.token.range);
}
else {
colorMap.set(bracket.color, [bracket.token.range]);
}
}
}
}
for (const [color, decoration] of this.settings.bracketDecorations) {
if (color === "") {
continue;
}
const ranges = colorMap.get(color);
for (const editor of editors) {
if (ranges !== undefined) {
editor.setDecorations(decoration, ranges);
}
else {
// We must set non-used colors to an empty array
// or previous decorations will not be invalidated
editor.setDecorations(decoration, []);
}
}
}
// console.timeEnd("colorDecorations");
}
private calculateColumnFromCharIndex(lineText: string, charIndex: number, tabSize: number): number {
let spacing = 0;
for (let index = 0; index < charIndex; index++) {
if (lineText.charAt(index) === "\t") {
spacing += tabSize - spacing % tabSize;
}
else {
spacing++;
}
}
return spacing;
}
private calculateCharIndexFromColumn(lineText: string, column: number, tabSize: number): number {
let spacing = 0;
for (let index = 0; index <= column; index++) {
if (spacing >= column) {
return index;
}
if (lineText.charAt(index) === "\t") {
spacing += tabSize - spacing % tabSize;
}
else { spacing++; }
}
return spacing;
}
}