| 1 | import * as fs from "node:fs"; |
| 2 | import * as path from "node:path"; |
| 3 | |
| 4 | import mergeJson from "core/util/merge"; |
| 5 | import { convertTheme } from "monaco-vscode-textmate-theme-converter/lib/cjs"; |
| 6 | import * as vscode from "vscode"; |
| 7 | |
| 8 | /** |
| 9 | * Strip comments from theme |
| 10 | */ |
| 11 | |
| 12 | function 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 | |
| 37 | function 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 | |
| 48 | export function getThemeString(): string { |
| 49 | const workbenchConfig = vscode.workspace.getConfiguration(); |
| 50 | const themeString = |
| 51 | workbenchConfig.get<string>("workbench.colorTheme") ?? |
| 52 | "Default Dark Modern"; |
| 53 | return themeString; |
| 54 | } |
| 55 | |
| 56 | export function getTheme() { |
| 57 | let currentTheme: string | undefined = undefined; |
| 58 | // Get color theme from settings |
| 59 | // use user settings if available |
| 60 | // otherwise use default |
| 61 | let colorTheme: string | undefined = undefined; |
| 62 | |
| 63 | // Get color theme from settings |
| 64 | const workbenchConfig = vscode.workspace.getConfiguration(); |
| 65 | const autoDetectColorScheme = workbenchConfig.get<boolean>( |
| 66 | "window.autoDetectColorScheme", |
| 67 | ); |
| 68 | const autoDetectHighContrast = workbenchConfig.get<boolean>( |
| 69 | "window.autoDetectHighContrast", |
| 70 | ); |
| 71 | const activeColorTheme = vscode.window.activeColorTheme.kind; |
| 72 | |
| 73 | // prettier-ignore |
| 74 | switch (true) { |
| 75 | case autoDetectColorScheme && vscode.ColorThemeKind.Dark === activeColorTheme: |
| 76 | colorTheme = workbenchConfig.get<string>( |
| 77 | "workbench.preferredDarkColorTheme", |
| 78 | ); |
| 79 | break; |
| 80 | case autoDetectColorScheme && vscode.ColorThemeKind.Light === activeColorTheme: |
| 81 | colorTheme = workbenchConfig.get<string>( |
| 82 | "workbench.preferredLightColorTheme", |
| 83 | ); |
| 84 | break; |
| 85 | case autoDetectHighContrast && vscode.ColorThemeKind.HighContrast === activeColorTheme: |
| 86 | colorTheme = workbenchConfig.get<string>( |
| 87 | "workbench.preferredHighContrastColorTheme", |
| 88 | ); |
| 89 | break; |
| 90 | case autoDetectHighContrast && vscode.ColorThemeKind.HighContrastLight === activeColorTheme: |
| 91 | colorTheme = workbenchConfig.get<string>( |
| 92 | "workbench.preferredHighContrastLightColorTheme", |
| 93 | ); |
| 94 | break; |
| 95 | default: |
| 96 | colorTheme = |
| 97 | workbenchConfig.get<string>("workbench.colorTheme") ?? |
| 98 | "Default Dark Modern"; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | let parsed; |
| 103 | try { |
| 104 | // Pass color theme to webview for syntax highlighting |
| 105 | for (let i = vscode.extensions.all.length - 1; i >= 0; i--) { |
| 106 | const extension = vscode.extensions.all[i]; |
| 107 | if (extension.packageJSON?.contributes?.themes?.length > 0) { |
| 108 | if (currentTheme) { |
| 109 | break; |
| 110 | } |
| 111 | for (const theme of extension.packageJSON.contributes.themes) { |
| 112 | if (theme.id === colorTheme || theme.label === colorTheme) { |
| 113 | const themePath = path.join(extension.extensionPath, theme.path); |
| 114 | currentTheme = fs.readFileSync(themePath).toString(); |
| 115 | |
| 116 | parsed = parseThemeString(currentTheme); |
| 117 | |
| 118 | // Handle nested includes |
| 119 | let currentParsedTheme = parsed; |
| 120 | let currentThemePath = themePath; |
| 121 | let mergedTheme = currentParsedTheme; |
| 122 | |
| 123 | while (currentParsedTheme.include) { |
| 124 | const themeDir = path.dirname(currentThemePath); |
| 125 | const includeThemePath = path.join( |
| 126 | themeDir, |
| 127 | currentParsedTheme.include, |
| 128 | ); |
| 129 | |
| 130 | if (fs.existsSync(includeThemePath)) { |
| 131 | const includeThemeString = fs |
| 132 | .readFileSync(includeThemePath) |
| 133 | .toString(); |
| 134 | |
| 135 | const includeTheme = parseThemeString(includeThemeString); |
| 136 | // Merge with base theme taking precedence, then overlay current customizations |
| 137 | mergedTheme = mergeJson( |
| 138 | mergeJson({}, includeTheme), // Start with base |
| 139 | mergedTheme, // Overlay with customizations |
| 140 | ); |
| 141 | |
| 142 | // Update for next iteration - only update path and parsed theme for include checking |
| 143 | currentThemePath = includeThemePath; |
| 144 | currentParsedTheme = includeTheme; |
| 145 | } else { |
| 146 | console.log( |
| 147 | `include theme not found for ${currentTheme} looked for ${currentParsedTheme.include} in ${themeDir}`, |
| 148 | includeThemePath, |
| 149 | ); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | parsed = mergedTheme; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!currentTheme) { |
| 162 | console.warn(`did not find any theme files for theme ${colorTheme}`); |
| 163 | return undefined; |
| 164 | } |
| 165 | |
| 166 | let convertedTheme: { base: string; include?: string } & Record< |
| 167 | string, |
| 168 | any |
| 169 | > = convertTheme(parsed); |
| 170 | |
| 171 | convertedTheme.base = ( |
| 172 | ["vs", "hc-black"].includes(convertedTheme.base) |
| 173 | ? convertedTheme.base |
| 174 | : activeColorTheme === vscode.ColorThemeKind.Light || |
| 175 | activeColorTheme === vscode.ColorThemeKind.HighContrastLight |
| 176 | ? "vs" |
| 177 | : "vs-dark" |
| 178 | ) as any; |
| 179 | |
| 180 | return convertedTheme; |
| 181 | } catch (e) { |
| 182 | console.log("Error loading color theme: ", e); |
| 183 | } |
| 184 | return undefined; |
| 185 | } |