diff options
author | Adolfo OchagavĂa <[email protected]> | 2018-10-07 21:44:25 +0100 |
---|---|---|
committer | Adolfo OchagavĂa <[email protected]> | 2018-10-07 22:03:38 +0100 |
commit | 69de7e2fd71c3a808f0ac856d7b105eeb210f169 (patch) | |
tree | 62a163c43cb710cff18de6c7e8e47a81038ad1bb /editors/code/src/highlighting.ts | |
parent | e4fdfd15012c983e4555996aa466b57d787e4385 (diff) |
Refactor vscode extension
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts new file mode 100644 index 000000000..169ddb0df --- /dev/null +++ b/editors/code/src/highlighting.ts | |||
@@ -0,0 +1,78 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient' | ||
3 | |||
4 | import { Server } from './server'; | ||
5 | |||
6 | export interface Decoration { | ||
7 | range: lc.Range, | ||
8 | tag: string, | ||
9 | } | ||
10 | |||
11 | export class Highlighter { | ||
12 | private decorations: { [index: string]: vscode.TextEditorDecorationType }; | ||
13 | constructor() { | ||
14 | this.decorations = {}; | ||
15 | } | ||
16 | |||
17 | removeHighlights() { | ||
18 | for (let tag in this.decorations) { | ||
19 | this.decorations[tag].dispose(); | ||
20 | } | ||
21 | |||
22 | this.decorations = {}; | ||
23 | } | ||
24 | |||
25 | setHighlights( | ||
26 | editor: vscode.TextEditor, | ||
27 | highlights: Array<Decoration> | ||
28 | ) { | ||
29 | // Initialize decorations if necessary | ||
30 | // | ||
31 | // Note: decoration objects need to be kept around so we can dispose them | ||
32 | // if the user disables syntax highlighting | ||
33 | if (Object.keys(this.decorations).length === 0) { | ||
34 | this.initDecorations(); | ||
35 | } | ||
36 | |||
37 | let byTag: Map<string, vscode.Range[]> = new Map() | ||
38 | for (let tag in this.decorations) { | ||
39 | byTag.set(tag, []) | ||
40 | } | ||
41 | |||
42 | for (let d of highlights) { | ||
43 | if (!byTag.get(d.tag)) { | ||
44 | console.log(`unknown tag ${d.tag}`) | ||
45 | continue | ||
46 | } | ||
47 | byTag.get(d.tag)!.push( | ||
48 | Server.client.protocol2CodeConverter.asRange(d.range) | ||
49 | ) | ||
50 | } | ||
51 | |||
52 | for (let tag of byTag.keys()) { | ||
53 | let dec: vscode.TextEditorDecorationType = this.decorations[tag] | ||
54 | let ranges = byTag.get(tag)! | ||
55 | editor.setDecorations(dec, ranges) | ||
56 | } | ||
57 | } | ||
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 | } | ||