diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index 24f9e31702..8a6cd2fad7 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -82,7 +82,7 @@ const markdown = { ".tok-string, .tok-string2": { color: "var(--syn-string)" }, ".tok-number, .tok-bool, .tok-atom": { color: "var(--syn-number)" }, ".tok-typeName, .tok-className, .tok-namespace": { color: "var(--syn-type)" }, - ".tok-comment": { color: "var(--syn-comment)", fontStyle: "italic" }, + ".tok-comment": { color: "var(--syn-comment)", fontStyle: "var(--syn-comment-italic, var(--syn-comment-style))" }, ".tok-meta, .tok-macroName": { color: "var(--syn-annotation)" }, ".tok-propertyName": { color: "var(--syn-property)" }, ".tok-variableName.tok-definition": { color: "var(--syn-function)" }, diff --git a/src/features/settings/__tests__/colorScheme.test.tsx b/src/features/settings/__tests__/colorScheme.test.tsx index a21cdf3d39..35c398e0f3 100644 --- a/src/features/settings/__tests__/colorScheme.test.tsx +++ b/src/features/settings/__tests__/colorScheme.test.tsx @@ -6,6 +6,7 @@ import { executeAction } from "@/platform/actions"; import { CLEAR_COLOR, _setSchemeSource, resolvedAttribute } from "@/platform/colorScheme"; import { setLocale } from "@/platform/i18n"; import { useProject } from "@/platform/project"; +import { usePlugins } from "@/features/plugins/store"; import { DEFAULT_SETTINGS } from "../model/schema"; import { languagePageId } from "../pages/editor/colorSchemeMeta"; import { openSettings, stage, stagedValue, useSettingsDialog } from "../model/staging"; @@ -308,6 +309,39 @@ describe("scheme attribute state", () => { expect(value("colorOverrides")).toEqual({ dark: { staticSymbol: { italic: false } } }); }); + it("shows a followed plugin theme's comment style and saves clearing it (#7453)", () => { + _setSchemeSource(readFileSync("src/styles/tokens.css", "utf8")); + const theme = { + id: "one", + name: "Plugin One", + kind: "dark" as const, + tokens: { "--syn-comment": "#5c6370", "--syn-comment-style": "italic" }, + }; + try { + act(() => { + usePlugins.setState({ activeTheme: { pluginId: "p", theme } }); + stage("editor.colorScheme", "global", "editor.colorScheme", "theme-default"); + }); + expect(screen.getByText("Follows the IDE theme: currently Plugin One.")).toBeInTheDocument(); + const preview = screen.getByLabelText("Color scheme preview"); + expect(preview.style.getPropertyValue("--syn-comment-style")).toBe("italic"); + act(() => fireEvent.click(preview.querySelector('span[style*="--syn-comment-italic"]')!)); + expect(screen.getByRole("textbox", { name: "Foreground hex" })).toHaveValue("#5c6370"); + const italic = screen.getByRole("checkbox", { name: "Italic" }); + expect(italic).toBeChecked(); + act(() => fireEvent.click(italic)); + expect(value("colorOverrides")).toEqual({ dark: { lineComment: { italic: false } } }); + expect(preview.style.getPropertyValue("--syn-comment-style")).toBe("normal"); + + // An explicitly chosen scheme replaces the plugin theme's colours, in the editor and here. + act(() => stage("editor.colorScheme", "global", "editor.colorScheme", "dark")); + expect(preview.style.getPropertyValue("--syn-comment")).not.toBe("#5c6370"); + } finally { + usePlugins.setState({ activeTheme: null }); + _setSchemeSource(""); + } + }); + it("shows the red-green colours the editor uses and the setting behind them (#3851)", () => { _setSchemeSource(readFileSync("src/styles/tokens.css", "utf8")); try { diff --git a/src/features/settings/pages/editor/ColorSchemeBody.tsx b/src/features/settings/pages/editor/ColorSchemeBody.tsx index b360520e8c..3d52393c52 100644 --- a/src/features/settings/pages/editor/ColorSchemeBody.tsx +++ b/src/features/settings/pages/editor/ColorSchemeBody.tsx @@ -52,6 +52,7 @@ import { useContributions } from "@/workbench/contributions"; import { parseIcls, SchemeParseError, importScheme, deleteScheme, exportIcls } from "@/platform/icls"; import { tx, txPart, useTx } from "@/platform/i18n"; import { selectPage } from "../../model/staging"; +import { usePlugins } from "@/features/plugins/store"; type Overrides = Record>; @@ -134,9 +135,10 @@ function previewVars( customs: CustomColorSchemes, language?: string, redGreen = false, + themeTokens?: Record, ): CSSProperties { const vars: Record = {}; - const tokens = resolvedSchemeTokens(id, customs, redGreen, o); + const tokens = resolvedSchemeTokens(id, customs, redGreen, o, themeTokens); for (const [k, v] of tokens) if ( k.startsWith("--syn-") || @@ -1779,6 +1781,10 @@ export function ColorSchemePage({ crossPageReveal = null; }); const id = effectiveScheme(scheme, undefined, customs); + // A scheme that follows the IDE theme shows a plugin theme's colours, as the editor does (`schemeCss`). + const pluginTheme = usePlugins((s) => s.activeTheme); + const followed = pluginTheme && id !== scheme ? pluginTheme.theme : undefined; + const themeTokens = followed?.tokens; const mine = overrides?.[id]; /** Keys of every group containing an overridden attribute, so the blue marks the path to the root. */ const modifiedKeys = useMemo(() => { @@ -1797,25 +1803,25 @@ export function ColorSchemePage({ const attr = ATTRIBUTES.find((a) => a.id === selected); const language = attr?.language; const inherited = useMemo( - () => (attr ? resolvedAttribute(id, attr.id, customs, redGreen) : {}), - [id, attr, customs, redGreen], + () => (attr ? resolvedAttribute(id, attr.id, customs, redGreen, themeTokens) : {}), + [id, attr, customs, redGreen, themeTokens], ); const vars = useMemo(() => { const next = { - ...previewVars(id, mine, customs, language, redGreen), + ...previewVars(id, mine, customs, language, redGreen, themeTokens), ...fontWeightVars(weight, boldFontWeight), } as CSSProperties & Record; // Console preview sits on the console background, not the editor surface. if (scopes[0] === "Console Colors" && next["--bg-console"]) next["--bg-editor"] = next["--bg-console"]; return next; - }, [id, mine, customs, language, weight, boldFontWeight, scopes, redGreen]); + }, [id, mine, customs, language, weight, boldFontWeight, scopes, redGreen, themeTokens]); const baseVars = useMemo( () => ({ - ...previewVars(id, mine, customs, undefined, redGreen), + ...previewVars(id, mine, customs, undefined, redGreen, themeTokens), ...fontWeightVars(weight, boldFontWeight), }) as CSSProperties, - [id, mine, customs, weight, boldFontWeight, redGreen], + [id, mine, customs, weight, boldFontWeight, redGreen, themeTokens], ); const update = (attrId: string, v: TextAttributesOverride | undefined) => { const next: Overrides = { ...(overrides ?? {}) }; @@ -1967,7 +1973,9 @@ export function ColorSchemePage({ )} {scheme === "theme-default" && ( -
{tx`Follows the IDE theme: currently ${txPart(schemeName)}.`}
+
{tx`Follows the IDE theme: currently ${txPart(followed?.name ?? schemeName)}.`}
)} {redGreen && (id === "dark" || id === "light") && (
diff --git a/src/platform/colorScheme.test.ts b/src/platform/colorScheme.test.ts index 267ddd974c..5e758e84d7 100644 --- a/src/platform/colorScheme.test.ts +++ b/src/platform/colorScheme.test.ts @@ -7,6 +7,7 @@ import { _setSchemeSource, overrideDeclarations, resolvedAttribute, + resolvedSchemeTokens, schemeAfterThemeChange, schemeCss, schemeTokens, @@ -394,6 +395,21 @@ describe("color schemes", () => { }); }); + it("resolves a followed plugin theme's tokens under the user's overrides", () => { + // A plugin theme with italic comments: the attribute shows italic, so clearing it saves an override. + const theme = { "--syn-comment": "#5c6370", "--syn-comment-style": "italic", "--syn-doc-comment": "#98c379" }; + expect(resolvedAttribute("dark", "lineComment", {}, false, theme)).toMatchObject({ + foreground: "#5c6370", + italic: true, + }); + expect(resolvedAttribute("dark", "go.lineComment", {}, false, theme)).toMatchObject({ italic: true }); + expect(resolvedAttribute("dark", "docComment", {}, false, theme).foreground).toBe("#98c379"); + const tokens = resolvedSchemeTokens("dark", {}, false, { lineComment: { italic: false } }, theme); + expect(tokens.get("--syn-comment")).toBe("#5c6370"); + expect(tokens.get("--syn-comment-style")).toBe("normal"); + expect(tokens.get("--syn-comment-italic")).toBe("normal"); + }); + it("resolves package references from the editor foreground and emits their overrides", () => { for (const theme of ["light", "dark", "dark-hc"] as const) { expect(resolvedAttribute(theme, "packageReference").foreground).toBe(schemeTokens(theme).get("--fg-editor")); diff --git a/src/platform/colorScheme.ts b/src/platform/colorScheme.ts index 3948cbd7c1..a3b52d5318 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -879,17 +879,24 @@ export function listSchemes(customs: CustomColorSchemes = {}) { * A scheme's tokens with a custom scheme's attributes and then `overrides` applied; tokens that * tokens.css derives from an overridden one follow it unless they are overridden themselves. With * `redGreen`, a built-in scheme's red-green variant; a custom scheme keeps its parent's own colours. + * `themeTokens` are those of a plugin theme the scheme follows, over the scheme's and under overrides. */ export function resolvedSchemeTokens( id: string, customs: CustomColorSchemes = {}, redGreen = false, overrides?: Record, + themeTokens?: Record, ): Vars { const custom = customSchemes(customs)[id]; const builtin = isBuiltinTheme(id); const base = builtin ? id : (custom?.parent ?? "light"); - const fixed: Vars = new Map([...overrideDeclarations(custom?.attributes), ...overrideDeclarations(overrides)]); + // A plugin theme the scheme follows paints over the built-in one, as its stylesheet does in the editor. + const fixed: Vars = new Map([ + ...Object.entries(themeTokens ?? {}), + ...overrideDeclarations(custom?.attributes), + ...overrideDeclarations(overrides), + ]); return new Map(schemeTokens(base, source, redGreen && builtin, fixed)); } @@ -929,12 +936,13 @@ export function resolvedAttribute( attrId: string, customs: CustomColorSchemes = {}, redGreen = false, + themeTokens?: Record, ): TextAttributesOverride { const attr = ATTRIBUTES.find((a) => a.id === attrId); if (!attr) return {}; const custom = customSchemes(customs)[id]; const own = custom?.attributes[attr.id]; - const vars = resolvedSchemeTokens(id, customs, redGreen); + const vars = resolvedSchemeTokens(id, customs, redGreen, undefined, themeTokens); if (attr.language) { const scoped = overrideDeclarations(custom?.attributes, attr.language); for (const [key, value] of scoped) vars.set(key, value);