aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package-lock.json11
-rw-r--r--editors/code/package.json65
-rw-r--r--editors/code/src/config.ts14
-rw-r--r--editors/code/src/highlighting.ts64
-rw-r--r--editors/code/src/scopes.ts146
-rw-r--r--editors/code/src/scopes_mapper.ts79
6 files changed, 364 insertions, 15 deletions
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index e6bcbfa60..a41497a23 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -598,9 +598,9 @@
598 } 598 }
599 }, 599 },
600 "https-proxy-agent": { 600 "https-proxy-agent": {
601 "version": "2.2.2", 601 "version": "2.2.3",
602 "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-2.2.2.tgz", 602 "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-2.2.3.tgz",
603 "integrity": "sha512-c8Ndjc9Bkpfx/vCJueCPy0jlP4ccCCSNDp8xwCZzPjKJUm+B+u9WX2x98Qx4n1PiMNTWo3D7KK5ifNV/yJyRzg==", 603 "integrity": "sha512-Ytgnz23gm2DVftnzqRRz2dOXZbGd2uiajSw/95bPp6v53zPRspQjLm/AfBgqbJ2qfeRXWIOMVLpp86+/5yX39Q==",
604 "dev": true, 604 "dev": true,
605 "requires": { 605 "requires": {
606 "agent-base": "^4.3.0", 606 "agent-base": "^4.3.0",
@@ -720,6 +720,11 @@
720 "esprima": "^4.0.0" 720 "esprima": "^4.0.0"
721 } 721 }
722 }, 722 },
723 "jsonc-parser": {
724 "version": "2.2.0",
725 "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-2.2.0.tgz",
726 "integrity": "sha512-4fLQxW1j/5fWj6p78vAlAafoCKtuBm6ghv+Ij5W2DrDx0qE+ZdEl2c6Ko1mgJNF5ftX1iEWQQ4Ap7+3GlhjkOA=="
727 },
723 "lcid": { 728 "lcid": {
724 "version": "2.0.0", 729 "version": "2.0.0",
725 "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", 730 "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz",
diff --git a/editors/code/package.json b/editors/code/package.json
index 94887674b..e21dfa174 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -32,7 +32,8 @@
32 }, 32 },
33 "dependencies": { 33 "dependencies": {
34 "seedrandom": "^3.0.1", 34 "seedrandom": "^3.0.1",
35 "vscode-languageclient": "^5.3.0-next.4" 35 "vscode-languageclient": "^5.3.0-next.4",
36 "jsonc-parser": "^2.1.0"
36 }, 37 },
37 "devDependencies": { 38 "devDependencies": {
38 "@types/glob": "^7.1.1", 39 "@types/glob": "^7.1.1",
@@ -172,6 +173,68 @@
172 "default": false, 173 "default": false,
173 "description": "Highlight Rust code (overrides built-in syntax highlighting)" 174 "description": "Highlight Rust code (overrides built-in syntax highlighting)"
174 }, 175 },
176 "rust-analyzer.scopeMappings": {
177 "type": "object",
178 "definitions": {},
179 "properties": {
180 "comment": {
181 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
182 },
183 "string": {
184 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
185 },
186 "keyword": {
187 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
188 },
189 "keyword.control": {
190 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
191 },
192 "keyword.unsafe": {
193 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
194 },
195 "function": {
196 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
197 },
198 "parameter": {
199 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
200 },
201 "constant": {
202 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
203 },
204 "type": {
205 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
206 },
207 "builtin": {
208 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
209 },
210 "text": {
211 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
212 },
213 "attribute": {
214 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
215 },
216 "literal": {
217 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
218 },
219 "macro": {
220 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
221 },
222 "variable": {
223 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
224 },
225 "variable.mut": {
226 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
227 },
228 "field": {
229 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
230 },
231 "module": {
232 "$ref": "vscode://schemas/textmate-colors#/items/properties/scope"
233 }
234 },
235 "additionalProperties": false,
236 "description": "Mapping Rust Analyzer scopes to TextMateRule scopes list."
237 },
175 "rust-analyzer.rainbowHighlightingOn": { 238 "rust-analyzer.rainbowHighlightingOn": {
176 "type": "boolean", 239 "type": "boolean",
177 "default": false, 240 "default": false,
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 95c3f42e5..4cedbea46 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -1,5 +1,6 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2 2import * as scopes from './scopes';
3import * as scopesMapper from './scopes_mapper';
3import { Server } from './server'; 4import { Server } from './server';
4 5
5const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; 6const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
@@ -45,8 +46,15 @@ export class Config {
45 46
46 public userConfigChanged() { 47 public userConfigChanged() {
47 const config = vscode.workspace.getConfiguration('rust-analyzer'); 48 const config = vscode.workspace.getConfiguration('rust-analyzer');
49
50 Server.highlighter.removeHighlights();
51
48 if (config.has('highlightingOn')) { 52 if (config.has('highlightingOn')) {
49 this.highlightingOn = config.get('highlightingOn') as boolean; 53 this.highlightingOn = config.get('highlightingOn') as boolean;
54 if (this.highlightingOn) {
55 scopes.load();
56 scopesMapper.load();
57 }
50 } 58 }
51 59
52 if (config.has('rainbowHighlightingOn')) { 60 if (config.has('rainbowHighlightingOn')) {
@@ -55,10 +63,6 @@ export class Config {
55 ) as boolean; 63 ) as boolean;
56 } 64 }
57 65
58 if (!this.highlightingOn && Server) {
59 Server.highlighter.removeHighlights();
60 }
61
62 if (config.has('enableEnhancedTyping')) { 66 if (config.has('enableEnhancedTyping')) {
63 this.enableEnhancedTyping = config.get( 67 this.enableEnhancedTyping = config.get(
64 'enableEnhancedTyping' 68 'enableEnhancedTyping'
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 @@
1import seedrandom = require('seedrandom'); 1import seedrandom = require('seedrandom');
2import * as vscode from 'vscode'; 2import * as vscode from 'vscode';
3import * as lc from 'vscode-languageclient'; 3import * as lc from 'vscode-languageclient';
4import * as scopes from './scopes';
5import * as scopesMapper from './scopes_mapper';
4 6
5import { Server } from './server'; 7import { 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
28function 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
26export class Highlighter { 63export 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
diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts
new file mode 100644
index 000000000..8f288d761
--- /dev/null
+++ b/editors/code/src/scopes.ts
@@ -0,0 +1,146 @@
1import * as fs from 'fs';
2import * as jsonc from 'jsonc-parser';
3import * as path from 'path';
4import * as vscode from 'vscode';
5
6export interface TextMateRule {
7 scope: string | string[];
8 settings: TextMateRuleSettings;
9}
10
11export interface TextMateRuleSettings {
12 foreground: string | undefined;
13 background: string | undefined;
14 fontStyle: string | undefined;
15}
16
17// Current theme colors
18const rules = new Map<string, TextMateRuleSettings>();
19
20export function find(scope: string): TextMateRuleSettings | undefined {
21 return rules.get(scope);
22}
23
24// Load all textmate scopes in the currently active theme
25export function load() {
26 // Remove any previous theme
27 rules.clear();
28 // Find out current color theme
29 const themeName = vscode.workspace
30 .getConfiguration('workbench')
31 .get('colorTheme');
32
33 if (typeof themeName !== 'string') {
34 // console.warn('workbench.colorTheme is', themeName)
35 return;
36 }
37 // Try to load colors from that theme
38 try {
39 loadThemeNamed(themeName);
40 } catch (e) {
41 // console.warn('failed to load theme', themeName, e)
42 }
43}
44
45function filterThemeExtensions(extension: vscode.Extension<any>): boolean {
46 return (
47 extension.extensionKind === vscode.ExtensionKind.UI &&
48 extension.packageJSON.contributes &&
49 extension.packageJSON.contributes.themes
50 );
51}
52
53// Find current theme on disk
54function loadThemeNamed(themeName: string) {
55 const themePaths = vscode.extensions.all
56 .filter(filterThemeExtensions)
57 .reduce((list, extension) => {
58 return extension.packageJSON.contributes.themes
59 .filter(
60 (element: any) =>
61 (element.id || element.label) === themeName
62 )
63 .map((element: any) =>
64 path.join(extension.extensionPath, element.path)
65 )
66 .concat(list);
67 }, Array<string>());
68
69 themePaths.forEach(loadThemeFile);
70
71 const tokenColorCustomizations: [any] = [
72 vscode.workspace
73 .getConfiguration('editor')
74 .get('tokenColorCustomizations')
75 ];
76
77 tokenColorCustomizations
78 .filter(custom => custom && custom.textMateRules)
79 .map(custom => custom.textMateRules)
80 .forEach(loadColors);
81}
82
83function loadThemeFile(themePath: string) {
84 const themeContent = [themePath]
85 .filter(isFile)
86 .map(readFileText)
87 .map(parseJSON)
88 .filter(theme => theme);
89
90 themeContent
91 .filter(theme => theme.tokenColors)
92 .map(theme => theme.tokenColors)
93 .forEach(loadColors);
94
95 themeContent
96 .filter(theme => theme.include)
97 .map(theme => path.join(path.dirname(themePath), theme.include))
98 .forEach(loadThemeFile);
99}
100
101function mergeRuleSettings(
102 defaultSetting: TextMateRuleSettings | undefined,
103 override: TextMateRuleSettings
104): TextMateRuleSettings {
105 if (defaultSetting === undefined) {
106 return override;
107 }
108 const mergedRule = defaultSetting;
109
110 mergedRule.background = override.background || defaultSetting.background;
111 mergedRule.foreground = override.foreground || defaultSetting.foreground;
112 mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground;
113
114 return mergedRule;
115}
116
117function updateRules(
118 scope: string,
119 updatedSettings: TextMateRuleSettings
120): void {
121 [rules.get(scope)]
122 .map(settings => mergeRuleSettings(settings, updatedSettings))
123 .forEach(settings => rules.set(scope, settings));
124}
125
126function loadColors(textMateRules: TextMateRule[]): void {
127 textMateRules.forEach(rule => {
128 if (typeof rule.scope === 'string') {
129 updateRules(rule.scope, rule.settings);
130 } else if (rule.scope instanceof Array) {
131 rule.scope.forEach(scope => updateRules(scope, rule.settings));
132 }
133 });
134}
135
136function isFile(filePath: string): boolean {
137 return [filePath].map(fs.statSync).every(stat => stat.isFile());
138}
139
140function readFileText(filePath: string): string {
141 return fs.readFileSync(filePath, 'utf8');
142}
143
144function parseJSON(content: string): any {
145 return jsonc.parse(content);
146}
diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts
new file mode 100644
index 000000000..85c791ff5
--- /dev/null
+++ b/editors/code/src/scopes_mapper.ts
@@ -0,0 +1,79 @@
1import * as vscode from 'vscode';
2import { TextMateRuleSettings } from './scopes';
3
4let mappings = new Map<string, string[]>();
5
6const defaultMapping = new Map<string, string[]>([
7 [
8 'comment',
9 [
10 'comment',
11 'comment.block',
12 'comment.line',
13 'comment.block.documentation'
14 ]
15 ],
16 ['string', ['string']],
17 ['keyword', ['keyword']],
18 ['keyword.control', ['keyword.control', 'keyword', 'keyword.other']],
19 [
20 'keyword.unsafe',
21 ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword']
22 ],
23 ['function', ['entity.name.function']],
24 ['parameter', ['variable.parameter']],
25 ['constant', ['constant', 'variable']],
26 ['type', ['entity.name.type']],
27 ['builtin', ['variable.language', 'support.type', 'support.type']],
28 ['text', ['string', 'string.quoted', 'string.regexp']],
29 ['attribute', ['keyword']],
30 ['literal', ['string', 'string.quoted', 'string.regexp']],
31 ['macro', ['support.other']],
32 ['variable', ['variable']],
33 ['variable.mut', ['variable', 'storage.modifier']],
34 [
35 'field',
36 [
37 'variable.object.property',
38 'meta.field.declaration',
39 'meta.definition.property',
40 'variable.other'
41 ]
42 ],
43 ['module', ['entity.name.section', 'entity.other']]
44]);
45
46// Temporary exported for debugging for now.
47export function find(scope: string): string[] {
48 return mappings.get(scope) || [];
49}
50
51export function toRule(
52 scope: string,
53 intoRule: (scope: string) => TextMateRuleSettings | undefined
54): TextMateRuleSettings | undefined {
55 return find(scope)
56 .map(intoRule)
57 .filter(rule => rule !== undefined)[0];
58}
59
60function isString(value: any): value is string {
61 return typeof value === 'string';
62}
63
64function isArrayOfString(value: any): value is string[] {
65 return Array.isArray(value) && value.every(item => isString(item));
66}
67
68export function load() {
69 const rawConfig: { [key: string]: any } =
70 vscode.workspace
71 .getConfiguration('rust-analyzer')
72 .get('scopeMappings') || {};
73
74 mappings = Object.entries(rawConfig)
75 .filter(([_, value]) => isString(value) || isArrayOfString(value))
76 .reduce((list, [key, value]: [string, string | string[]]) => {
77 return list.set(key, isString(value) ? [value] : value);
78 }, defaultMapping);
79}