diff options
author | Seivan Heidari <[email protected]> | 2019-10-24 16:25:23 +0100 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-10-24 16:25:23 +0100 |
commit | 3e8616cf6df5ba6e5dceba8b834dc63c20bac4d4 (patch) | |
tree | bce2c26d159b4c48eceb3ddce5a3c51cbbda904f /editors/code/src/highlighting.ts | |
parent | 95cf5c86fae3adf3bb38521905bf357450125709 (diff) |
Proof of concept theming and 'tokenColorCustomizations' support.
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 94 |
1 files changed, 69 insertions, 25 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index d21d8a06a..4b961170b 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -1,6 +1,8 @@ | |||
1 | import seedrandom = require('seedrandom'); | 1 | import seedrandom = require('seedrandom'); |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import * as lc from 'vscode-languageclient'; | 3 | import * as lc from 'vscode-languageclient'; |
4 | import * as scopes from './scopes' | ||
5 | |||
4 | 6 | ||
5 | import { Server } from './server'; | 7 | import { Server } from './server'; |
6 | 8 | ||
@@ -23,6 +25,37 @@ function fancify(seed: string, shade: 'light' | 'dark') { | |||
23 | return `hsl(${h},${s}%,${l}%)`; | 25 | return `hsl(${h},${s}%,${l}%)`; |
24 | } | 26 | } |
25 | 27 | ||
28 | function createDecorationFromTextmate(themeStyle: scopes.TextMateRuleSettings): vscode.TextEditorDecorationType { | ||
29 | const options: vscode.DecorationRenderOptions = {} | ||
30 | options.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen | ||
31 | if (themeStyle.foreground) { | ||
32 | options.color = themeStyle.foreground | ||
33 | } | ||
34 | if (themeStyle.background) { | ||
35 | options.backgroundColor = themeStyle.background | ||
36 | } | ||
37 | if (themeStyle.fontStyle) { | ||
38 | const parts: string[] = themeStyle.fontStyle.split(' ') | ||
39 | parts.forEach((part) => { | ||
40 | switch (part) { | ||
41 | case 'italic': | ||
42 | options.fontStyle = 'italic' | ||
43 | break | ||
44 | case 'bold': | ||
45 | options.fontWeight = 'bold' | ||
46 | |||
47 | break | ||
48 | case 'underline': | ||
49 | options.textDecoration = 'underline' | ||
50 | break | ||
51 | default: | ||
52 | break | ||
53 | } | ||
54 | }) | ||
55 | } | ||
56 | return vscode.window.createTextEditorDecorationType(options) | ||
57 | } | ||
58 | |||
26 | export class Highlighter { | 59 | export class Highlighter { |
27 | private static initDecorations(): Map< | 60 | private static initDecorations(): Map< |
28 | string, | 61 | string, |
@@ -32,36 +65,44 @@ export class Highlighter { | |||
32 | tag: string, | 65 | tag: string, |
33 | textDecoration?: string | 66 | textDecoration?: string |
34 | ): [string, vscode.TextEditorDecorationType] => { | 67 | ): [string, vscode.TextEditorDecorationType] => { |
35 | const color = new vscode.ThemeColor('ralsp.' + tag); | 68 | const scope = scopes.find(tag) |
36 | const decor = vscode.window.createTextEditorDecorationType({ | 69 | |
37 | color, | 70 | if (scope) { |
38 | textDecoration | 71 | const decor = createDecorationFromTextmate(scope); |
39 | }); | 72 | return [tag, decor]; |
40 | return [tag, decor]; | 73 | } |
74 | else { | ||
75 | const color = new vscode.ThemeColor('ralsp.' + tag); | ||
76 | const decor = vscode.window.createTextEditorDecorationType({ | ||
77 | color, | ||
78 | textDecoration | ||
79 | }); | ||
80 | return [tag, decor]; | ||
81 | } | ||
41 | }; | 82 | }; |
42 | 83 | ||
43 | const decorations: Iterable< | 84 | const decorations: Iterable< |
44 | [string, vscode.TextEditorDecorationType] | 85 | [string, vscode.TextEditorDecorationType] |
45 | > = [ | 86 | > = [ |
46 | decoration('comment'), | 87 | decoration('comment'), |
47 | decoration('string'), | 88 | decoration('string'), |
48 | decoration('keyword'), | 89 | decoration('keyword'), |
49 | decoration('keyword.control'), | 90 | decoration('keyword.control'), |
50 | decoration('keyword.unsafe'), | 91 | decoration('keyword.unsafe'), |
51 | decoration('function'), | 92 | decoration('function'), |
52 | decoration('parameter'), | 93 | decoration('parameter'), |
53 | decoration('constant'), | 94 | decoration('constant'), |
54 | decoration('type'), | 95 | decoration('type'), |
55 | decoration('builtin'), | 96 | decoration('builtin'), |
56 | decoration('text'), | 97 | decoration('text'), |
57 | decoration('attribute'), | 98 | decoration('attribute'), |
58 | decoration('literal'), | 99 | decoration('literal'), |
59 | decoration('macro'), | 100 | decoration('macro'), |
60 | decoration('variable'), | 101 | decoration('variable'), |
61 | decoration('variable.mut', 'underline'), | 102 | decoration('variable.mut', 'underline'), |
62 | decoration('field'), | 103 | decoration('field'), |
63 | decoration('module') | 104 | decoration('module') |
64 | ]; | 105 | ]; |
65 | 106 | ||
66 | return new Map<string, vscode.TextEditorDecorationType>(decorations); | 107 | return new Map<string, vscode.TextEditorDecorationType>(decorations); |
67 | } | 108 | } |
@@ -89,6 +130,8 @@ export class Highlighter { | |||
89 | // | 130 | // |
90 | // Note: decoration objects need to be kept around so we can dispose them | 131 | // Note: decoration objects need to be kept around so we can dispose them |
91 | // if the user disables syntax highlighting | 132 | // if the user disables syntax highlighting |
133 | |||
134 | |||
92 | if (this.decorations == null) { | 135 | if (this.decorations == null) { |
93 | this.decorations = Highlighter.initDecorations(); | 136 | this.decorations = Highlighter.initDecorations(); |
94 | } | 137 | } |
@@ -133,6 +176,7 @@ export class Highlighter { | |||
133 | tag | 176 | tag |
134 | ) as vscode.TextEditorDecorationType; | 177 | ) as vscode.TextEditorDecorationType; |
135 | const ranges = byTag.get(tag)!; | 178 | const ranges = byTag.get(tag)!; |
179 | |||
136 | editor.setDecorations(dec, ranges); | 180 | editor.setDecorations(dec, ranges); |
137 | } | 181 | } |
138 | 182 | ||