diff --git a/apps/vscode-editor/src/theme.ts b/apps/vscode-editor/src/theme.ts index dcce0d90..64374e69 100644 --- a/apps/vscode-editor/src/theme.ts +++ b/apps/vscode-editor/src/theme.ts @@ -31,15 +31,15 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) const theme = defaultTheme(); // get vscode theme colors - const colors: Record = {}; + const colors: Record = {}; Object.values(document.getElementsByTagName('html')[0].style) .forEach((rv) => { colors[rv] = document .getElementsByTagName('html')[0] - .style.getPropertyValue(rv) + .style.getPropertyValue(rv); } - ); - + ); + const bodyCls = document.body.classList; const hcLight = bodyCls.contains('vscode-high-contrast-light'); const hcDark = bodyCls.contains('vscode-high-contrast') && !hcLight; @@ -48,10 +48,10 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) theme.solarizedMode = isSolarizedThemeActive(); theme.cursorColor = colors["--vscode-editorCursor-foreground"]; theme.selectionColor = colors["--vscode-editor-selectionBackground"]; - theme.selectionForegroundColor = colors["--vscode-editor-selectionForeground"] + theme.selectionForegroundColor = colors["--vscode-editor-selectionForeground"]; theme.nodeSelectionColor = colors["--vscode-notebook-focusedCellBorder"]; theme.backgroundColor = colors["--vscode-editor-background"]; - theme.metadataBackgroundColor = theme.backgroundColor; + theme.metadataBackgroundColor = theme.backgroundColor; theme.chunkBackgroundColor = colors["--vscode-notebook-cellEditorBackground"]; theme.spanBackgroundColor = theme.chunkBackgroundColor; theme.divBackgroundColor = theme.chunkBackgroundColor; @@ -62,12 +62,12 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) theme.linkTextColor = colors["--vscode-textLink-foreground"]; theme.placeholderTextColor = colors["--vscode-editorGhostText-foreground"]; theme.invisibleTextColor = colors["--vscode-editorWhitespace-foreground"]; - theme.markupTextColor = theme.darkMode - ? colors["--vscode-charts-orange"] + theme.markupTextColor = theme.darkMode + ? colors["--vscode-charts-orange"] : colors["--vscode-editorInfo-foreground"]; theme.findTextBackgroundColor = colors["--vscode-editor-foldBackground"]; theme.findTextBorderColor = "transparent"; - theme.borderBackgroundColor = theme.darkMode + theme.borderBackgroundColor = theme.darkMode ? colors["--vscode-titleBar-activeBackground"] : colors["--vscode-titleBar-inactiveBackground"]; theme.gutterBackgroundColor = theme.borderBackgroundColor; @@ -78,9 +78,9 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) theme.surfaceWidgetTextColor = theme.gutterTextColor; theme.focusOutlineColor = colors["--vscode-focusBorder"]; theme.paneBorderColor = theme.darkMode ? colors["--vscode-commandCenter-border"] : colors["--vscode-panel-border"]; - theme.blockBorderColor = theme.darkMode - ? theme.paneBorderColor - : colors["--vscode-notebook-cellBorderColor"]; + theme.blockBorderColor = theme.darkMode + ? theme.paneBorderColor + : colors["--vscode-notebook-cellBorderColor"]; theme.hrBackgroundColor = theme.highContrast ? colors["--vscode-list-deemphasizedForeground"] : theme.blockBorderColor; theme.fixedWidthFont = colors["--vscode-editor-font-family"]; theme.proportionalFont = fontFamily || defaultPrefs().fontFamily; @@ -91,7 +91,7 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) const match = editorFontSize.match(/(\d+)px/); fontSizePx = match ? parseInt(match[1]) : 12; } - const fontSizePt = Math.round(fontSizePx / 1.333) ; + const fontSizePt = Math.round(fontSizePx / 1.333); theme.fixedWidthFontSizePt = fontSizePt; theme.proportionalFontSizePt = fontSizePt + 1; theme.suggestWidgetBackgroundColor = colors["--vscode-editorSuggestWidget-background"]; @@ -117,4 +117,4 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number) theme.debugStartForegroundColor = colors["--vscode-debugIcon-startForeground"]; theme.debugStepForgroundColor = colors["--vscode-debugIcon-stepOverForeground"]; return theme; -} \ No newline at end of file +} diff --git a/apps/vscode/src/extension.ts b/apps/vscode/src/extension.ts index b85e7e2b..a94fbdc0 100644 --- a/apps/vscode/src/extension.ts +++ b/apps/vscode/src/extension.ts @@ -25,6 +25,58 @@ import { textFormattingCommands } from "./providers/text-format"; import { activateCodeFormatting } from "./providers/format"; import { activateContextKeySetter } from "./providers/context-keys"; import { ExtensionHost } from "./host"; +import path, { dirname } from "node:path"; +import { readFileSync } from "node:fs"; + +export function getActiveThemeName(): string | undefined { + return vscode.workspace.getConfiguration("workbench").get("colorTheme"); +} +// reference: https://github.com/microsoft/vscode/issues/32813#issuecomment-524174937 +// reference: https://github.com/textX/textX-LS/blob/master/client/src/utils.ts +export function getTokenColorsForTheme(themeName: string | undefined): Map { + const tokenColors = new Map(); + if (!themeName) return tokenColors; + let currentThemePath; + for (const extension of vscode.extensions.all) { + const themes = extension.packageJSON.contributes && extension.packageJSON.contributes.themes; + const currentTheme = themes && themes.find((theme: any) => theme.id === themeName); + if (currentTheme) { + currentThemePath = path.join(extension.extensionPath, currentTheme.path); + break; + } + } + const themePaths = []; + if (currentThemePath) { themePaths.push(currentThemePath); } + while (themePaths.length > 0) { + const themePath = themePaths.pop(); + if (themePath === undefined) return tokenColors; + const theme = loadJSON(themePath); + if (theme) { + if (theme.include) { + themePaths.push(path.join(dirname(themePath), theme.include)); + } + if (theme.tokenColors) { + theme.tokenColors.forEach((rule: any) => { + if (typeof rule.scope === "string" && !tokenColors.has(rule.scope)) { + tokenColors.set(rule.scope, rule.settings); + } else if (rule.scope instanceof Array) { + rule.scope.forEach((scope: any) => { + if (!tokenColors.has(rule.scope)) { + tokenColors.set(scope, rule.settings); + } + }); + } + }); + } + } + } + return tokenColors; +} +export function loadJSON(path: string): any { + try { + return JSON.parse(readFileSync(path).toString()); + } catch { } +} export function activateCommon( context: vscode.ExtensionContext, @@ -32,6 +84,11 @@ export function activateCommon( engine: MarkdownEngine, commands?: Command[] ) { + console.log( + 'HELLO getCurrentTheme', + getActiveThemeName(), + [...getTokenColorsForTheme(getActiveThemeName()).entries()].map(([k, v]) => [k, Object.entries(v)]) + ); // option enter handler activateOptionEnterProvider(context, engine);