diff options
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 64 |
1 files changed, 58 insertions, 6 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index d21d8a06a..0a38c9ef6 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,41 @@ 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( | ||
29 | themeStyle: scopes.TextMateRuleSettings | ||
30 | ): vscode.TextEditorDecorationType { | ||
31 | const decorationOptions: vscode.DecorationRenderOptions = {}; | ||
32 | decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; | ||
33 | |||
34 | if (themeStyle.foreground) { | ||
35 | decorationOptions.color = themeStyle.foreground; | ||
36 | } | ||
37 | |||
38 | if (themeStyle.background) { | ||
39 | decorationOptions.backgroundColor = themeStyle.background; | ||
40 | } | ||
41 | |||
42 | if (themeStyle.fontStyle) { | ||
43 | const parts: string[] = themeStyle.fontStyle.split(' '); | ||
44 | parts.forEach(part => { | ||
45 | switch (part) { | ||
46 | case 'italic': | ||
47 | decorationOptions.fontStyle = 'italic'; | ||
48 | break; | ||
49 | case 'bold': | ||
50 | decorationOptions.fontWeight = 'bold'; | ||
51 | break; | ||
52 | case 'underline': | ||
53 | decorationOptions.textDecoration = 'underline'; | ||
54 | break; | ||
55 | default: | ||
56 | break; | ||
57 | } | ||
58 | }); | ||
59 | } | ||
60 | return vscode.window.createTextEditorDecorationType(decorationOptions); | ||
61 | } | ||
62 | |||
26 | export class Highlighter { | 63 | export class Highlighter { |
27 | private static initDecorations(): Map< | 64 | private static initDecorations(): Map< |
28 | string, | 65 | string, |
@@ -32,12 +69,25 @@ export class Highlighter { | |||
32 | tag: string, | 69 | tag: string, |
33 | textDecoration?: string | 70 | textDecoration?: string |
34 | ): [string, vscode.TextEditorDecorationType] => { | 71 | ): [string, vscode.TextEditorDecorationType] => { |
35 | const color = new vscode.ThemeColor('ralsp.' + tag); | 72 | const rule = scopesMapper.toRule(tag, scopes.find); |
36 | const decor = vscode.window.createTextEditorDecorationType({ | 73 | |
37 | color, | 74 | if (rule) { |
38 | textDecoration | 75 | const decor = createDecorationFromTextmate(rule); |
39 | }); | 76 | return [tag, decor]; |
40 | return [tag, decor]; | 77 | } else { |
78 | const fallBackTag = 'ralsp.' + tag; | ||
79 | // console.log(' '); | ||
80 | // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); | ||
81 | // console.log(scopesMapper.find(tag)); | ||
82 | // console.log('Falling back to values defined in: ' + fallBackTag); | ||
83 | // console.log(' '); | ||
84 | const color = new vscode.ThemeColor(fallBackTag); | ||
85 | const decor = vscode.window.createTextEditorDecorationType({ | ||
86 | color, | ||
87 | textDecoration | ||
88 | }); | ||
89 | return [tag, decor]; | ||
90 | } | ||
41 | }; | 91 | }; |
42 | 92 | ||
43 | const decorations: Iterable< | 93 | const decorations: Iterable< |
@@ -89,6 +139,7 @@ export class Highlighter { | |||
89 | // | 139 | // |
90 | // Note: decoration objects need to be kept around so we can dispose them | 140 | // Note: decoration objects need to be kept around so we can dispose them |
91 | // if the user disables syntax highlighting | 141 | // if the user disables syntax highlighting |
142 | |||
92 | if (this.decorations == null) { | 143 | if (this.decorations == null) { |
93 | this.decorations = Highlighter.initDecorations(); | 144 | this.decorations = Highlighter.initDecorations(); |
94 | } | 145 | } |
@@ -133,6 +184,7 @@ export class Highlighter { | |||
133 | tag | 184 | tag |
134 | ) as vscode.TextEditorDecorationType; | 185 | ) as vscode.TextEditorDecorationType; |
135 | const ranges = byTag.get(tag)!; | 186 | const ranges = byTag.get(tag)!; |
187 | |||
136 | editor.setDecorations(dec, ranges); | 188 | editor.setDecorations(dec, ranges); |
137 | } | 189 | } |
138 | 190 | ||