diff options
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/color_theme.ts | 129 |
1 files changed, 0 insertions, 129 deletions
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 @@ | |||
1 | import * as fs from 'fs'; | ||
2 | import * as jsonc from 'jsonc-parser'; | ||
3 | import * as path from 'path'; | ||
4 | import * as vscode from 'vscode'; | ||
5 | |||
6 | export interface TextMateRuleSettings { | ||
7 | foreground?: string; | ||
8 | background?: string; | ||
9 | fontStyle?: string; | ||
10 | } | ||
11 | |||
12 | export 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 | |||
64 | function 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 | |||
96 | function 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 | |||
115 | interface TextMateRule { | ||
116 | scope: string | string[]; | ||
117 | settings: TextMateRuleSettings; | ||
118 | } | ||
119 | |||
120 | function 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 | } | ||