Skip to content

Prototype of getting active vscode theme in the extension #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions apps/vscode-editor/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export function editorThemeFromVSCode(fontFamily?: string, fontSizePx?: number)
const theme = defaultTheme();

// get vscode theme colors
const colors: Record<string,string> = {};
const colors: Record<string, string> = {};
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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"];
Expand All @@ -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;
}
}
57 changes: 57 additions & 0 deletions apps/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,70 @@ 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<any, any> {
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,
host: ExtensionHost,
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);

Expand Down
Loading