diff options
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 101 |
1 files changed, 76 insertions, 25 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index d21d8a06a..ee39ca64c 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 | import * as scopesMapper from './scopes_mapper'; | ||
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 | |||
29 | function createDecorationFromTextmate(themeStyle: scopes.TextMateRuleSettings): vscode.TextEditorDecorationType { | ||
30 | const options: vscode.DecorationRenderOptions = {}; | ||
31 | options.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; | ||
32 | if (themeStyle.foreground) { | ||
33 | options.color = themeStyle.foreground; | ||
34 | } | ||
35 | if (themeStyle.background) { | ||
36 | options.backgroundColor = themeStyle.background; | ||
37 | } | ||
38 | if (themeStyle.fontStyle) { | ||
39 | const parts: string[] = themeStyle.fontStyle.split(' '); | ||
40 | parts.forEach((part) => { | ||
41 | switch (part) { | ||
42 | case 'italic': | ||
43 | options.fontStyle = 'italic'; | ||
44 | break; | ||
45 | case 'bold': | ||
46 | options.fontWeight = 'bold'; | ||
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,51 @@ 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 | |
36 | const decor = vscode.window.createTextEditorDecorationType({ | 69 | const rule = scopesMapper.toRule(tag, scopes.find); |
37 | color, | 70 | |
38 | textDecoration | 71 | if (rule) { |
39 | }); | 72 | const decor = createDecorationFromTextmate(rule); |
40 | return [tag, decor]; | 73 | return [tag, decor]; |
74 | } | ||
75 | else { | ||
76 | const fallBackTag = 'ralsp.' + tag; | ||
77 | // console.log(' '); | ||
78 | // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); | ||
79 | // console.log(scopesMapper.find(tag)); | ||
80 | // console.log('Falling back to values defined in: ' + fallBackTag); | ||
81 | // console.log(' '); | ||
82 | const color = new vscode.ThemeColor(fallBackTag); | ||
83 | const decor = vscode.window.createTextEditorDecorationType({ | ||
84 | color, | ||
85 | textDecoration | ||
86 | }); | ||
87 | return [tag, decor]; | ||
88 | } | ||
41 | }; | 89 | }; |
42 | 90 | ||
43 | const decorations: Iterable< | 91 | const decorations: Iterable< |
44 | [string, vscode.TextEditorDecorationType] | 92 | [string, vscode.TextEditorDecorationType] |
45 | > = [ | 93 | > = [ |
46 | decoration('comment'), | 94 | decoration('comment'), |
47 | decoration('string'), | 95 | decoration('string'), |
48 | decoration('keyword'), | 96 | decoration('keyword'), |
49 | decoration('keyword.control'), | 97 | decoration('keyword.control'), |
50 | decoration('keyword.unsafe'), | 98 | decoration('keyword.unsafe'), |
51 | decoration('function'), | 99 | decoration('function'), |
52 | decoration('parameter'), | 100 | decoration('parameter'), |
53 | decoration('constant'), | 101 | decoration('constant'), |
54 | decoration('type'), | 102 | decoration('type'), |
55 | decoration('builtin'), | 103 | decoration('builtin'), |
56 | decoration('text'), | 104 | decoration('text'), |
57 | decoration('attribute'), | 105 | decoration('attribute'), |
58 | decoration('literal'), | 106 | decoration('literal'), |
59 | decoration('macro'), | 107 | decoration('macro'), |
60 | decoration('variable'), | 108 | decoration('variable'), |
61 | decoration('variable.mut', 'underline'), | 109 | decoration('variable.mut', 'underline'), |
62 | decoration('field'), | 110 | decoration('field'), |
63 | decoration('module') | 111 | decoration('module') |
64 | ]; | 112 | ]; |
65 | 113 | ||
66 | return new Map<string, vscode.TextEditorDecorationType>(decorations); | 114 | return new Map<string, vscode.TextEditorDecorationType>(decorations); |
67 | } | 115 | } |
@@ -89,6 +137,8 @@ export class Highlighter { | |||
89 | // | 137 | // |
90 | // Note: decoration objects need to be kept around so we can dispose them | 138 | // Note: decoration objects need to be kept around so we can dispose them |
91 | // if the user disables syntax highlighting | 139 | // if the user disables syntax highlighting |
140 | |||
141 | |||
92 | if (this.decorations == null) { | 142 | if (this.decorations == null) { |
93 | this.decorations = Highlighter.initDecorations(); | 143 | this.decorations = Highlighter.initDecorations(); |
94 | } | 144 | } |
@@ -133,6 +183,7 @@ export class Highlighter { | |||
133 | tag | 183 | tag |
134 | ) as vscode.TextEditorDecorationType; | 184 | ) as vscode.TextEditorDecorationType; |
135 | const ranges = byTag.get(tag)!; | 185 | const ranges = byTag.get(tag)!; |
186 | |||
136 | editor.setDecorations(dec, ranges); | 187 | editor.setDecorations(dec, ranges); |
137 | } | 188 | } |
138 | 189 | ||