fix(theme): read .icls colours written with a leading # (#5625)

Both .icls readers (the theme converter and Settings > Color Scheme > Import) accepted only bare hex, so schemes like One Dark that write #rrggbb lost most colours. One iclsColor helper now follows IntelliJ's rules (bare, # or 0x prefixes); the settings import still leaves out RRGGBBAA values.

Co-authored-by: Rhombicdodecahedron <[email protected]>
Цей коміт міститься в:
Alexis authored and GitHub committed 2026-11-24 20:52:43 +08:00
1 джерело af60c1c5b9
коміт f25df55a53
6 files changed
+88 -12

No files matched your search

+19
Переглянути файл
@@ -199,6 +199,25 @@ describe("IntelliJ theme import", () => {
expect(convertIcls(`<scheme name="I" parent_scheme="Islands Dark"></scheme>`).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(`<scheme name="One" parent_scheme="Darcula"><colors>
<option name="CARET_COLOR" value="#528bff"/>
<option name="LINE_NUMBERS_COLOR" value="0x495162"/>
</colors><attributes>
<option name="DEFAULT_CLASS_NAME"><value><option name="FOREGROUND" value="#E5C07B"/></value></option>
<option name="DEFAULT_KEYWORD"><value><option name="FOREGROUND" value="c678dd"/></value></option>
<option name="DEFAULT_STRING"><value><option name="FOREGROUND" value="#98c37980"/></value></option>
</attributes></scheme>`);
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.
+4 -10
Переглянути файл
@@ -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<string, string>, 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<string, string> } {
const tokens: Record<string, string> = {};
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 = /<scheme\b[^>]*>/.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";
+19
Переглянути файл
@@ -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(`<scheme name="One" parent_scheme="Darcula"><colors>
<option name="CARET_COLOR" value="#528bff"/>
<option name="LINE_NUMBERS_COLOR" value="0x495162"/>
</colors><attributes>
<option name="DEFAULT_CLASS_NAME"><value><option name="FOREGROUND" value="#E5C07B"/></value></option>
<option name="DEFAULT_KEYWORD"><value><option name="FOREGROUND" value="c678dd"/></value></option>
<option name="DEFAULT_STRING"><value><option name="FOREGROUND" value="#98c37980"/></value></option>
</attributes></scheme>`);
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;
+6 -2
Переглянути файл
@@ -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<Record<NonNullable<TextAttributesOverride["effect"]>
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");
+28
Переглянути файл
@@ -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));
});
});
+12
Переглянути файл
@@ -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;
}