aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-06 08:57:00 +0100
committerBenjamin Coenen <[email protected]>2020-05-06 08:57:00 +0100
commitc4d128e454448191c4b21d8e151c673e4c42376e (patch)
tree17cf5bbf429642c52708cd0d3c1d8885b63543f0 /editors/code
parent0bf02f5ccac99c91f10ef46bb06ff2ea316c382c (diff)
parent30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4311
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package-lock.json5
-rw-r--r--editors/code/package.json14
-rw-r--r--editors/code/src/color_theme.ts129
3 files changed, 13 insertions, 135 deletions
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index 55ba5351d..71c627a2a 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -1066,11 +1066,6 @@
1066 "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", 1066 "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=",
1067 "dev": true 1067 "dev": true
1068 }, 1068 },
1069 "jsonc-parser": {
1070 "version": "2.2.1",
1071 "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-2.2.1.tgz",
1072 "integrity": "sha512-o6/yDBYccGvTz1+QFevz6l6OBZ2+fMVu2JZ9CIhzsYRX4mjaK5IyX9eldUdCmga16zlgQxyrj5pt9kzuj2C02w=="
1073 },
1074 "leven": { 1069 "leven": {
1075 "version": "3.1.0", 1070 "version": "3.1.0",
1076 "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz", 1071 "resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz",
diff --git a/editors/code/package.json b/editors/code/package.json
index 442b9de98..eeb3d3513 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -33,7 +33,6 @@
33 "fix": " tsfmt -r && eslint -c .eslintrc.js --ext ts ./src --fix" 33 "fix": " tsfmt -r && eslint -c .eslintrc.js --ext ts ./src --fix"
34 }, 34 },
35 "dependencies": { 35 "dependencies": {
36 "jsonc-parser": "^2.2.1",
37 "node-fetch": "^2.6.0", 36 "node-fetch": "^2.6.0",
38 "vscode-languageclient": "7.0.0-next.1" 37 "vscode-languageclient": "7.0.0-next.1"
39 }, 38 },
@@ -233,6 +232,14 @@
233 "default": false, 232 "default": false,
234 "markdownDescription": "Run `cargo check` on startup to get the correct value for package OUT_DIRs" 233 "markdownDescription": "Run `cargo check` on startup to get the correct value for package OUT_DIRs"
235 }, 234 },
235 "rust-analyzer.cargo.target": {
236 "type": [
237 "null",
238 "string"
239 ],
240 "default": null,
241 "description": "Specify the compilation target"
242 },
236 "rust-analyzer.rustfmt.extraArgs": { 243 "rust-analyzer.rustfmt.extraArgs": {
237 "type": "array", 244 "type": "array",
238 "items": { 245 "items": {
@@ -288,6 +295,11 @@
288 "default": true, 295 "default": true,
289 "markdownDescription": "Check all targets and tests (will be passed as `--all-targets`)" 296 "markdownDescription": "Check all targets and tests (will be passed as `--all-targets`)"
290 }, 297 },
298 "rust-analyzer.checkOnSave.allFeatures": {
299 "type": "boolean",
300 "default": true,
301 "markdownDescription": "Check with all features (will be passed as `--all-features`)"
302 },
291 "rust-analyzer.inlayHints.typeHints": { 303 "rust-analyzer.inlayHints.typeHints": {
292 "type": "boolean", 304 "type": "boolean",
293 "default": true, 305 "default": true,
diff --git a/editors/code/src/color_theme.ts b/editors/code/src/color_theme.ts
deleted file mode 100644
index 5b9327b28..000000000
--- a/editors/code/src/color_theme.ts
+++ /dev/null
@@ -1,129 +0,0 @@
1import * as fs from 'fs';
2import * as jsonc from 'jsonc-parser';
3import * as path from 'path';
4import * as vscode from 'vscode';
5
6export interface TextMateRuleSettings {
7 foreground?: string;
8 background?: string;
9 fontStyle?: string;
10}
11
12export class ColorTheme {
13 private rules: Map<string, TextMateRuleSettings> = new Map();
14
15 static load(): ColorTheme {
16 // Find out current color theme
17 const themeName = vscode.workspace
18 .getConfiguration('workbench')
19 .get('colorTheme');
20
21 if (typeof themeName !== 'string') {
22 // console.warn('workbench.colorTheme is', themeName)
23 return new ColorTheme();
24 }
25 return loadThemeNamed(themeName);
26 }
27
28 static fromRules(rules: TextMateRule[]): ColorTheme {
29 const res = new ColorTheme();
30 for (const rule of rules) {
31 const scopes = typeof rule.scope === 'undefined'
32 ? []
33 : typeof rule.scope === 'string'
34 ? [rule.scope]
35 : rule.scope;
36
37 for (const scope of scopes) {
38 res.rules.set(scope, rule.settings);
39 }
40 }
41 return res;
42 }
43
44 lookup(scopes: string[]): TextMateRuleSettings {
45 let res: TextMateRuleSettings = {};
46 for (const scope of scopes) {
47 this.rules.forEach((value, key) => {
48 if (scope.startsWith(key)) {
49 res = mergeRuleSettings(res, value);
50 }
51 });
52 }
53 return res;
54 }
55
56 mergeFrom(other: ColorTheme) {
57 other.rules.forEach((value, key) => {
58 const merged = mergeRuleSettings(this.rules.get(key), value);
59 this.rules.set(key, merged);
60 });
61 }
62}
63
64function loadThemeNamed(themeName: string): ColorTheme {
65 function isTheme(extension: vscode.Extension<unknown>): boolean {
66 return (
67 extension.extensionKind === vscode.ExtensionKind.UI &&
68 extension.packageJSON.contributes &&
69 extension.packageJSON.contributes.themes
70 );
71 }
72
73 const themePaths: string[] = vscode.extensions.all
74 .filter(isTheme)
75 .flatMap(
76 ext => ext.packageJSON.contributes.themes
77 .filter((it: any) => (it.id || it.label) === themeName)
78 .map((it: any) => path.join(ext.extensionPath, it.path))
79 );
80
81 const res = new ColorTheme();
82 for (const themePath of themePaths) {
83 res.mergeFrom(loadThemeFile(themePath));
84 }
85
86 const globalCustomizations: any = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations');
87 res.mergeFrom(ColorTheme.fromRules(globalCustomizations?.textMateRules ?? []));
88
89 const themeCustomizations: any = vscode.workspace.getConfiguration('editor.tokenColorCustomizations').get(`[${themeName}]`);
90 res.mergeFrom(ColorTheme.fromRules(themeCustomizations?.textMateRules ?? []));
91
92
93 return res;
94}
95
96function loadThemeFile(themePath: string): ColorTheme {
97 let text;
98 try {
99 text = fs.readFileSync(themePath, 'utf8');
100 } catch {
101 return new ColorTheme();
102 }
103 const obj = jsonc.parse(text);
104 const tokenColors: TextMateRule[] = obj?.tokenColors ?? [];
105 const res = ColorTheme.fromRules(tokenColors);
106
107 for (const include of obj?.include ?? []) {
108 const includePath = path.join(path.dirname(themePath), include);
109 res.mergeFrom(loadThemeFile(includePath));
110 }
111
112 return res;
113}
114
115interface TextMateRule {
116 scope: string | string[];
117 settings: TextMateRuleSettings;
118}
119
120function mergeRuleSettings(
121 defaultSetting: TextMateRuleSettings | undefined,
122 override: TextMateRuleSettings,
123): TextMateRuleSettings {
124 return {
125 foreground: override.foreground ?? defaultSetting?.foreground,
126 background: override.background ?? defaultSetting?.background,
127 fontStyle: override.fontStyle ?? defaultSetting?.fontStyle,
128 };
129}