diff options
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 71f8e5baa..e2ac4d629 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -9,17 +9,42 @@ export interface Decoration { | |||
9 | } | 9 | } |
10 | 10 | ||
11 | export class Highlighter { | 11 | export class Highlighter { |
12 | private decorations: { [index: string]: vscode.TextEditorDecorationType }; | 12 | private static initDecorations(): Map<string, vscode.TextEditorDecorationType> { |
13 | constructor() { | 13 | const decor = (color: string) => vscode.window.createTextEditorDecorationType({ color }); |
14 | this.decorations = {}; | 14 | |
15 | const decorations: Iterable<[string, vscode.TextEditorDecorationType]> = [ | ||
16 | ['background', decor('#3F3F3F')], | ||
17 | ['error', vscode.window.createTextEditorDecorationType({ | ||
18 | borderColor: 'red', | ||
19 | borderStyle: 'none none dashed none', | ||
20 | })], | ||
21 | ['comment', decor('#7F9F7F')], | ||
22 | ['string', decor('#CC9393')], | ||
23 | ['keyword', decor('#F0DFAF')], | ||
24 | ['function', decor('#93E0E3')], | ||
25 | ['parameter', decor('#94BFF3')], | ||
26 | ['builtin', decor('#DD6718')], | ||
27 | ['text', decor('#DCDCCC')], | ||
28 | ['attribute', decor('#BFEBBF')], | ||
29 | ['literal', decor('#DFAF8F')], | ||
30 | ]; | ||
31 | |||
32 | return new Map<string, vscode.TextEditorDecorationType>(decorations); | ||
15 | } | 33 | } |
16 | 34 | ||
35 | private decorations: (Map<string, vscode.TextEditorDecorationType> | null) = null; | ||
36 | |||
17 | public removeHighlights() { | 37 | public removeHighlights() { |
18 | for (const tag in this.decorations) { | 38 | if (this.decorations == null) { |
19 | this.decorations[tag].dispose(); | 39 | return; |
20 | } | 40 | } |
21 | 41 | ||
22 | this.decorations = {}; | 42 | // Decorations are removed when the object is disposed |
43 | for (const decoration of this.decorations.values()) { | ||
44 | decoration.dispose(); | ||
45 | } | ||
46 | |||
47 | this.decorations = null; | ||
23 | } | 48 | } |
24 | 49 | ||
25 | public setHighlights( | 50 | public setHighlights( |
@@ -30,18 +55,17 @@ export class Highlighter { | |||
30 | // | 55 | // |
31 | // Note: decoration objects need to be kept around so we can dispose them | 56 | // Note: decoration objects need to be kept around so we can dispose them |
32 | // if the user disables syntax highlighting | 57 | // if the user disables syntax highlighting |
33 | if (Object.keys(this.decorations).length === 0) { | 58 | if (this.decorations == null) { |
34 | this.initDecorations(); | 59 | this.decorations = Highlighter.initDecorations(); |
35 | } | 60 | } |
36 | 61 | ||
37 | const byTag: Map<string, vscode.Range[]> = new Map(); | 62 | const byTag: Map<string, vscode.Range[]> = new Map(); |
38 | for (const tag in this.decorations) { | 63 | for (const tag of this.decorations.keys()) { |
39 | byTag.set(tag, []); | 64 | byTag.set(tag, []); |
40 | } | 65 | } |
41 | 66 | ||
42 | for (const d of highlights) { | 67 | for (const d of highlights) { |
43 | if (!byTag.get(d.tag)) { | 68 | if (!byTag.get(d.tag)) { |
44 | console.log(`unknown tag ${d.tag}`); | ||
45 | continue; | 69 | continue; |
46 | } | 70 | } |
47 | byTag.get(d.tag)!.push( | 71 | byTag.get(d.tag)!.push( |
@@ -50,29 +74,9 @@ export class Highlighter { | |||
50 | } | 74 | } |
51 | 75 | ||
52 | for (const tag of byTag.keys()) { | 76 | for (const tag of byTag.keys()) { |
53 | const dec: vscode.TextEditorDecorationType = this.decorations[tag]; | 77 | const dec = this.decorations.get(tag) as vscode.TextEditorDecorationType; |
54 | const ranges = byTag.get(tag)!; | 78 | const ranges = byTag.get(tag)!; |
55 | editor.setDecorations(dec, ranges); | 79 | editor.setDecorations(dec, ranges); |
56 | } | 80 | } |
57 | } | 81 | } |
58 | |||
59 | private initDecorations() { | ||
60 | const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj }); | ||
61 | this.decorations = { | ||
62 | background: decor('#3F3F3F'), | ||
63 | error: vscode.window.createTextEditorDecorationType({ | ||
64 | borderColor: 'red', | ||
65 | borderStyle: 'none none dashed none', | ||
66 | }), | ||
67 | comment: decor('#7F9F7F'), | ||
68 | string: decor('#CC9393'), | ||
69 | keyword: decor('#F0DFAF'), | ||
70 | function: decor('#93E0E3'), | ||
71 | parameter: decor('#94BFF3'), | ||
72 | builtin: decor('#DD6718'), | ||
73 | text: decor('#DCDCCC'), | ||
74 | attribute: decor('#BFEBBF'), | ||
75 | literal: decor('#DFAF8F'), | ||
76 | }; | ||
77 | } | ||
78 | } | 82 | } |