fix(settings): keep the annotation colour of imported color schemes (#6130)

Importing a color scheme mapped only DEFAULT_METADATA to the annotation colour. Java annotations use ANNOTATION_NAME_ATTRIBUTES, which the bundled Default and Darcula schemes set themselves, so a scheme based on them that only changes DEFAULT_METADATA lost its annotation colour on import. The importer now reads ANNOTATION_NAME_ATTRIBUTES ahead of DEFAULT_METADATA, applies the parent's value when a Default- or Darcula-based file leaves it out, and exports the colour under that key.

Fixes #6108
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-26 17:08:47 +08:00
1 джерело 925d474274
коміт d437cd46f5
2 files changed
+41

No files matched your search

+33
Переглянути файл
@@ -712,3 +712,36 @@ describe(".icls schemes", () => {
}
});
});
describe("annotation colour (#6108)", () => {
const opt = (name: string, value: string) =>
`<option name="${name}"><value><option name="FOREGROUND" value="${value}"/></value></option>`;
const scheme = (parent: string | null, ...options: string[]) =>
parseIcls(
`<scheme name="A"${parent ? ` parent_scheme="${parent}"` : ""}><attributes>${options.join("")}</attributes></scheme>`,
).scheme;
it.each([
["Default", "#808000"],
["Darcula", "#bbb529"],
])("keeps %s's own annotation colour when the file only changes DEFAULT_METADATA", (parent, colour) => {
// IntelliJ shows Java annotations with the parent's ANNOTATION_NAME_ATTRIBUTES, not the file's default.
expect(scheme(parent, opt("DEFAULT_METADATA", "1d8711")).attributes.metadata?.foreground).toBe(colour);
});
it("takes the file's annotation key over its DEFAULT_METADATA", () => {
const s = scheme("Default", opt("DEFAULT_METADATA", "1d8711"), opt("ANNOTATION_NAME_ATTRIBUTES", "9e880d"));
expect(s.attributes.metadata?.foreground).toBe("#9e880d");
});
it("uses DEFAULT_METADATA without a bundled parent", () => {
expect(scheme(null, opt("DEFAULT_METADATA", "1d8711")).attributes.metadata?.foreground).toBe("#1d8711");
});
it("exports the annotation colour under the annotation key, so it survives a re-import", () => {
const imported = scheme("Default", opt("ANNOTATION_NAME_ATTRIBUTES", "9e880d"));
const xml = exportIcls("custom-a", { "custom-a": imported });
expect(xml).toContain('<option name="ANNOTATION_NAME_ATTRIBUTES">');
expect(parseIcls(xml).scheme.attributes.metadata?.foreground).toBe("#9e880d");
});
});
+8
Переглянути файл
@@ -40,6 +40,8 @@ const ATTRIBUTE_NAMES: Record<string, string> = {
DEFAULT_DOC_COMMENT_TAG: "docCommentTag",
DEFAULT_DOC_COMMENT_TAG_VALUE: "docCommentTagValue",
DEFAULT_DOC_MARKUP: "docCommentMarkup",
// Java's annotation key outranks the default it falls back to.
ANNOTATION_NAME_ATTRIBUTES: "metadata",
DEFAULT_METADATA: "metadata",
DEFAULT_OPERATION_SIGN: "operator",
DEFAULT_PARENTHS: "operator",
@@ -297,6 +299,8 @@ for (const id of [
// its fallback's. TypeScript keys win over JavaScript (TS.* falls back to JS.*), then Python, then the
// shared defaults; within a language, the key listed first above. DEFAULT_DOT, _COMMA and _SEMICOLON are
// not mapped: the one punctuation colour here would spread them to every brace and operator.
/** ANNOTATION_NAME_ATTRIBUTES as IntelliJ's bundled Default and Darcula schemes set it. */
const PARENT_ANNOTATIONS: Record<string, string> = { Default: "#808000", Darcula: "#bbb529" };
const KEY_ORDER = new Map(Object.keys(ATTRIBUTE_NAMES).map((name, i) => [name, i]));
const LANGUAGE_PRECEDENCE = ["TS.", "JS.", "PY."];
const precedence = (name: string) => {
@@ -488,6 +492,10 @@ export function parseIcls(
const own = new Set([...entries.keys()].flatMap((name) => (name ? targets(name) : [])));
for (const [id, value] of Object.entries(DARCULA_ATTRIBUTES)) if (!own.has(id)) attributes[id] ??= { ...value };
}
// Both bundled parents set Java's annotation key itself, so a file that leaves it out shows the parent's
// colour there, not its own DEFAULT_METADATA.
if (Object.hasOwn(PARENT_ANNOTATIONS, parentName) && !entries.has("ANNOTATION_NAME_ATTRIBUTES"))
attributes.metadata = { bold: false, italic: false, foreground: PARENT_ANNOTATIONS[parentName]! };
const bg = attributes.text?.background;
const rgb = bg ? [1, 3, 5].map((i) => parseInt(bg.slice(i, i + 2), 16)) : [255, 255, 255];
const dark = rgb[0]! * 0.299 + rgb[1]! * 0.587 + rgb[2]! * 0.114 < 128;