Elffuss BraidHub

kikocis/continue public

1f089284a191 · 5.5 KiBRawPermalink
1import * as fs from "node:fs";
2import * as path from "node:path";
3
4import mergeJson from "core/util/merge";
5import { convertTheme } from "monaco-vscode-textmate-theme-converter/lib/cjs";
6import * as vscode from "vscode";
7
8/**
9 * Strip comments from theme
10 */
11
12function stripInLineComment(line: string): string {
13 let inString = false;
14 let pCh = '';
15
16 for (let i = 0; i < line.length-1; i++) {
17 const ch = line[i];
18 const nCh = line[i + 1];
19
20 // If we're not in a string and we see '//' this is a comment.
21 if (!inString && ch === '/' && nCh === '/') {
22 // Stop processing this line from here.
23 return line.substring(0, i);
24 }
25
26 // Toggle inString state if we see a double quote not escaped by a backslash.
27 if (ch === '"' && pCh !== '\\') {
28 inString = !inString;
29 }
30
31 pCh = ch;
32 }
33
34 return line;
35}
36
37function parseThemeString(themeString: string | undefined): any {
38 themeString = themeString
39 ?.split("\n")
40 .filter((line) => {
41 return !line.trim().startsWith("//");
42 })
43 .map(stripInLineComment)
44 .join("\n");
45 return JSON.parse(themeString ?? "{}");
46}
47
48export function getTheme() {
49 let currentTheme: string | undefined = undefined;
50 // Get color theme from settings
51 // use user settings if available
52 // otherwise use default
53 let colorTheme: string | undefined = undefined;
54
55 // Get color theme from settings
56 const workbenchConfig = vscode.workspace.getConfiguration();
57 const autoDetectColorScheme = workbenchConfig.get<boolean>(
58 "window.autoDetectColorScheme",
59 );
60 const autoDetectHighContrast = workbenchConfig.get<boolean>(
61 "window.autoDetectHighContrast",
62 );
63 const activeColorTheme = vscode.window.activeColorTheme.kind;
64
65 // prettier-ignore
66 switch (true) {
67 case autoDetectColorScheme && vscode.ColorThemeKind.Dark === activeColorTheme:
68 colorTheme = workbenchConfig.get<string>(
69 "workbench.preferredDarkColorTheme",
70 );
71 break;
72 case autoDetectColorScheme && vscode.ColorThemeKind.Light === activeColorTheme:
73 colorTheme = workbenchConfig.get<string>(
74 "workbench.preferredLightColorTheme",
75 );
76 break;
77 case autoDetectHighContrast && vscode.ColorThemeKind.HighContrast === activeColorTheme:
78 colorTheme = workbenchConfig.get<string>(
79 "workbench.preferredHighContrastColorTheme",
80 );
81 break;
82 case autoDetectHighContrast && vscode.ColorThemeKind.HighContrastLight === activeColorTheme:
83 colorTheme = workbenchConfig.get<string>(
84 "workbench.preferredHighContrastLightColorTheme",
85 );
86 break;
87 default:
88 colorTheme =
89 workbenchConfig.get<string>("workbench.colorTheme") ??
90 "Default Dark Modern";
91 break;
92 }
93
94 let parsed;
95 try {
96 // Pass color theme to webview for syntax highlighting
97 for (let i = vscode.extensions.all.length - 1; i >= 0; i--) {
98 const extension = vscode.extensions.all[i];
99 if (extension.packageJSON?.contributes?.themes?.length > 0) {
100 if (currentTheme) {
101 break;
102 }
103 for (const theme of extension.packageJSON.contributes.themes) {
104 if (theme.id === colorTheme || theme.label === colorTheme) {
105 const themePath = path.join(extension.extensionPath, theme.path);
106 currentTheme = fs.readFileSync(themePath).toString();
107
108 parsed = parseThemeString(currentTheme);
109
110 // Handle nested includes
111 let currentParsedTheme = parsed;
112 let currentThemePath = themePath;
113 let mergedTheme = currentParsedTheme;
114
115 while (currentParsedTheme.include) {
116 const themeDir = path.dirname(currentThemePath);
117 const includeThemePath = path.join(
118 themeDir,
119 currentParsedTheme.include,
120 );
121
122 if (fs.existsSync(includeThemePath)) {
123 const includeThemeString = fs
124 .readFileSync(includeThemePath)
125 .toString();
126
127 const includeTheme = parseThemeString(includeThemeString);
128 // Merge with base theme taking precedence, then overlay current customizations
129 mergedTheme = mergeJson(
130 mergeJson({}, includeTheme), // Start with base
131 mergedTheme, // Overlay with customizations
132 );
133
134 // Update for next iteration - only update path and parsed theme for include checking
135 currentThemePath = includeThemePath;
136 currentParsedTheme = includeTheme;
137 } else {
138 console.log(
139 `include theme not found for ${currentTheme} looked for ${currentParsedTheme.include} in ${themeDir}`,
140 includeThemePath,
141 );
142 break;
143 }
144 }
145
146 parsed = mergedTheme;
147 break;
148 }
149 }
150 }
151 }
152
153 if (!currentTheme) {
154 console.warn(`did not find any theme files for theme ${colorTheme}`);
155 return undefined;
156 }
157
158 let convertedTheme: { base: string; include?: string } & Record<
159 string,
160 any
161 > = convertTheme(parsed);
162
163 convertedTheme.base = (
164 ["vs", "hc-black"].includes(convertedTheme.base)
165 ? convertedTheme.base
166 : activeColorTheme === vscode.ColorThemeKind.Light ||
167 activeColorTheme === vscode.ColorThemeKind.HighContrastLight
168 ? "vs"
169 : "vs-dark"
170 ) as any;
171
172 return convertedTheme;
173 } catch (e) {
174 console.log("Error loading color theme: ", e);
175 }
176 return undefined;
177}