diff --git a/scripts/theme/theme.test.ts b/scripts/theme/theme.test.ts index 10710bf76d..3fd67c2908 100644 --- a/scripts/theme/theme.test.ts +++ b/scripts/theme/theme.test.ts @@ -199,6 +199,25 @@ describe("IntelliJ theme import", () => { expect(convertIcls(``).tokens).toEqual({}); }); + it("reads .icls colours written with a leading # or 0x, as IntelliJ does (#5619)", () => { + // One Dark writes most of its values as `#rrggbb`: its class names became the base theme's. + const s = convertIcls(` + + + + + `); + expect(s.tokens).toMatchObject({ + "--caret": "#528bff", + "--fg-line-number": "#495162", + "--syn-type": "#e5c07b", + "--syn-keyword": "#c678dd", + "--syn-string": "#98c37980", + }); + }); + it("only targets tokens that exist in tokens.css", () => { const declared = new Set([...css.matchAll(/(--[a-z0-9-]+)\s*:/g)].map((m) => m[1]!)); // Read with a fallback by the editor (no tokens.css default), still themeable. diff --git a/src/features/plugins/intellijTheme.ts b/src/features/plugins/intellijTheme.ts index 00d9ef300e..8951adf4b7 100644 --- a/src/features/plugins/intellijTheme.ts +++ b/src/features/plugins/intellijTheme.ts @@ -9,6 +9,7 @@ * and so does the command line (`scripts/theme/import-intellij-theme.ts`). */ +import { iclsColor } from "../../platform/iclsColor.ts"; import { DARCULA_TOKENS } from "./intellijDarcula.ts"; /** OpenIDEA token ← IntelliJ `ui` keys, first present wins. `*.prop` wildcards apply too. */ @@ -304,19 +305,12 @@ function resolveColor(value: string, named: Map, depth = 0): str return ref !== undefined && depth < 8 ? resolveColor(ref, named, depth + 1) : null; } -/** `.icls` hex: RGB without `#`, leading zeros dropped (`"0"` is black, `"ff"` is #0000ff). */ -function iclsColor(value: string): string | null { - const v = value.trim().toLowerCase(); - if (!/^[0-9a-f]{1,8}$/.test(v)) return null; - return `#${v.length <= 6 ? v.padStart(6, "0") : v.padStart(8, "0")}`; -} - const attr = (tag: string, name: string) => new RegExp(`${name}="([^"]*)"`).exec(tag)?.[1]; /** Parses the colours and attributes of an `.icls` file into tokens. */ export function convertIcls(xml: string): { name: string; dark: boolean | null; tokens: Record } { const tokens: Record = {}; - const set = (names: string[], color: string | null) => { + const set = (names: string[], color: string | undefined) => { if (color) for (const n of names) tokens[n] ??= color; }; const schemeTag = /]*>/.exec(xml)?.[0] ?? ""; @@ -346,7 +340,7 @@ export function convertIcls(xml: string): { name: string; dark: boolean | null; } attributes.set(m[1]!, parts); } - const textForeground = iclsColor(attributes.get("TEXT")?.get("FOREGROUND") ?? ""); + const textForeground = iclsColor(attributes.get("TEXT")?.get("FOREGROUND")); for (const [key, parts] of attributes) { const map = ICLS_ATTRIBUTES[key]; if (!map) continue; @@ -355,7 +349,7 @@ export function convertIcls(xml: string): { name: string; dark: boolean | null; for (const [part, token] of map) if (part === "FOREGROUND") set([token], textForeground); continue; } - for (const [part, token] of map) set([token], iclsColor(parts.get(part) ?? "")); + for (const [part, token] of map) set([token], iclsColor(parts.get(part))); // FONT_TYPE: 1 bold, 2 italic, 3 bold italic. if (key === "DEFAULT_LINE_COMMENT" && parts.has("FONT_TYPE")) { tokens["--syn-comment-style"] = Number(parts.get("FONT_TYPE")) & 2 ? "italic" : "normal"; diff --git a/src/platform/icls.test.ts b/src/platform/icls.test.ts index 50bb59b301..8c8e107680 100644 --- a/src/platform/icls.test.ts +++ b/src/platform/icls.test.ts @@ -205,6 +205,25 @@ describe(".icls schemes", () => { expect(scheme.attributes["kotlin.enumEntry"]).toEqual({ foreground: "#c77dbb", bold: false, italic: true }); }); + it("reads colours written with a leading # or 0x, as IntelliJ does (#5619)", () => { + // One Dark writes most of its values as `#rrggbb`. + const { scheme } = parseIcls(` + + + + + `); + const fg = (id: string) => scheme.attributes[id]?.foreground; + expect(fg("caret")).toBe("#528bff"); + expect(fg("lineNumber")).toBe("#495162"); + expect(fg("className")).toBe("#e5c07b"); + expect(fg("keyword")).toBe("#c678dd"); + // No alpha in schemes here: an RRGGBBAA value is left out, with or without `#`. + expect(fg("string")).toBeUndefined(); + }); + it("maps WebStorm's JavaScript and TypeScript keys to their page, TypeScript first, then JavaScript", () => { const { scheme, applied, skipped } = parseIcls(fixture("webstorm-darcula")); const fg = (id: string) => scheme.attributes[id]?.foreground; diff --git a/src/platform/icls.ts b/src/platform/icls.ts index abeb2563b5..dcbb6ed98f 100644 --- a/src/platform/icls.ts +++ b/src/platform/icls.ts @@ -10,6 +10,7 @@ import { } from "./colorScheme"; import { isBuiltinTheme, themeKind } from "./themes"; import type { CustomColorScheme, CustomColorSchemes, TextAttributesOverride } from "./editorSettings"; +import { iclsColor } from "./iclsColor"; import { DARCULA_ATTRIBUTES } from "./iclsDarcula"; // Many language keys share the editor's language-default tokens. @@ -324,8 +325,11 @@ const EFFECT_CODES: Partial strikeout: "3", }; const children = (el: Element, tag: string) => Array.from(el.children).filter((e) => e.tagName === tag); -const color = (v: string | null): string | undefined => - v && /^[0-9a-f]{1,6}$/i.test(v) ? `#${v.padStart(6, "0").toLowerCase()}` : undefined; +// Schemes here have no alpha: an RRGGBBAA value is left out, as before. +const color = (v: string | null): string | undefined => { + const c = iclsColor(v); + return c?.length === 7 ? c : undefined; +}; function attributeEntries(section: Element): Element[] { const entries = children(section, "option"); diff --git a/src/platform/iclsColor.test.ts b/src/platform/iclsColor.test.ts new file mode 100644 index 0000000000..fa761ac592 --- /dev/null +++ b/src/platform/iclsColor.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import { iclsColor } from "./iclsColor"; + +describe("iclsColor", () => { + it("reads values the way IntelliJ's scheme reader does", () => { + const cases: Array<[string | null | undefined, string | undefined]> = [ + ["a9b7c6", "#a9b7c6"], + ["#A9B7C6", "#a9b7c6"], + ["0xa9b7c6", "#a9b7c6"], + [" #a9b7c6 ", "#a9b7c6"], + ["a9b7c680", "#a9b7c680"], + ["#a9b7c680", "#a9b7c680"], + // Short values are a number with its leading zeros dropped. + ["0", "#000000"], + ["ff", "#0000ff"], + ["abc", "#000abc"], + // IntelliJ reads neither of these as a colour. + ["#abc", undefined], + ["#ff", undefined], + ["1234567", undefined], + ["red", undefined], + ["", undefined], + [null, undefined], + [undefined, undefined], + ]; + expect(cases.map(([v]) => iclsColor(v))).toEqual(cases.map(([, c]) => c)); + }); +}); diff --git a/src/platform/iclsColor.ts b/src/platform/iclsColor.ts new file mode 100644 index 0000000000..3e08934cf8 --- /dev/null +++ b/src/platform/iclsColor.ts @@ -0,0 +1,12 @@ +/** + * An `.icls` colour value as IntelliJ reads it (`ValueElementReader.toColor`): six or eight hex digits + * (`RRGGBB`, `RRGGBBAA`), with or without a leading `#` or `0x`, else a hex number with its leading zeros + * dropped (`"0"` is black, `"ff"` is `#0000ff`). Returns lower-case `#rrggbb` / `#rrggbbaa`. + */ +export function iclsColor(value: string | null | undefined): string | undefined { + const v = value?.trim().toLowerCase(); + if (!v) return undefined; + const full = /^(?:#|0x)?([0-9a-f]{6}|[0-9a-f]{8})$/.exec(v); + if (full) return `#${full[1]}`; + return /^[0-9a-f]{1,5}$/.test(v) ? `#${v.padStart(6, "0")}` : undefined; +}