diff --git a/src/editor/lsp/semantic/java/index.test.ts b/src/editor/lsp/semantic/java/index.test.ts index 1d4ded8eee..6dac5924b9 100644 --- a/src/editor/lsp/semantic/java/index.test.ts +++ b/src/editor/lsp/semantic/java/index.test.ts @@ -17,13 +17,27 @@ const token = (type: string, modifiers: string[], language: string) => describe("Java semantic tokens", () => { it.each([ ["class", ["cm-sem-type"]], - ["interface", ["cm-sem-type"]], + ["interface", ["cm-sem-type", "cm-sem-java-interface"]], ["method", ["cm-sem-function"]], ])("colours JDK %s references like library and workspace ones", (type, classes) => { expect(token(type, ["defaultLibrary"], "java")).toEqual(classes); expect(token(type, ["defaultLibrary"], "java")).toEqual(token(type, [], "java")); }); + it.each([ + ["interface", [], "cm-sem-java-interface"], + ["enum", [], "cm-sem-java-enum"], + ["typeParameter", [], "cm-sem-java-type-parameter"], + ["class", ["abstract"], "cm-sem-java-abstract-class"], + ])("gives %s names IntelliJ's own colour (#6415)", (type, modifiers, cls) => { + expect(token(type, modifiers, "java")).toContain(cls); + }); + + it("leaves concrete classes and abstract methods the class and method colours", () => { + expect(token("class", [], "java")).toEqual(["cm-sem-type"]); + expect(token("method", ["abstract"], "java")?.join(" ")).not.toMatch(/java-/); + }); + it("keeps the other modifiers of JDK symbols", () => { expect(token("property", ["static", "defaultLibrary"], "java")?.sort()).toEqual([ "cm-sem-property", @@ -32,7 +46,7 @@ describe("Java semantic tokens", () => { }); it.each(["class", "interface", "enum", "record"])("does not italicise static nested %s names", (type) => { - expect(token(type, ["static"], "java")).toEqual(["cm-sem-type"]); + expect(token(type, ["static"], "java")).not.toContain("cm-sem-static"); }); it.each([ diff --git a/src/editor/lsp/semantic/java/index.ts b/src/editor/lsp/semantic/java/index.ts index d130099cab..344f0e53a1 100644 --- a/src/editor/lsp/semantic/java/index.ts +++ b/src/editor/lsp/semantic/java/index.ts @@ -8,6 +8,13 @@ import { javaUnterminatedLiteralRanges } from "./unterminated"; const TYPE_TOKENS = new Set(["class", "interface", "enum", "record", "annotation", "type", "typeParameter"]); +/** IntelliJ's Java kinds of type, each with its own colour (unset, the class name's). */ +const TYPE_KIND_CLASS: Record = { + interface: "cm-sem-java-interface", + enum: "cm-sem-java-enum", + typeParameter: "cm-sem-java-type-parameter", +}; + /** Contextual keywords `@lezer/java` parses as their own nodes but leaves uncoloured (`var` is coloured, but jdtls sends it as a type). */ const KEYWORD_NODES = new Set(["sealed", "permits", "record", "var"]); const NAME_NODES = new Set(["Identifier", "Definition", "TypeName"]); @@ -64,7 +71,7 @@ const javaSemantics: LanguageSemantics = { ...javaUnterminatedLiteralRanges(tree, doc), ], fallbackRanges: (tree) => javaDeclarationRanges(tree), - tokenClasses({ type, classes, from, doc, tree }) { + tokenClasses({ type, modifiers, classes, from, doc, tree }) { // Relies on jdtls sending only contextual keywords (`sealed`, `permits`, `record`, `non-sealed`) as `modifier` tokens. if (type === "modifier" || type === "keyword") classes.push("cm-sem-keyword"); // Java colours JDK types and calls like any other: `defaultLibrary` (sent for JDK symbols) gets no colour of its own. @@ -75,6 +82,8 @@ const javaSemantics: LanguageSemantics = { const s = classes.indexOf("cm-sem-static"); if (s >= 0) classes.splice(s, 1); } + if (Object.hasOwn(TYPE_KIND_CLASS, type)) classes.push(TYPE_KIND_CLASS[type]!); + else if (type === "class" && modifiers.includes("abstract")) classes.push("cm-sem-java-abstract-class"); if (tree && isReassignedAt(tree, doc, from)) classes.push("cm-sem-mutable"); }, }; diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index 5e0e43c458..e5119180ee 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -623,6 +623,11 @@ export const lspTheme = EditorView.theme({ ".cm-sem-js-interface": inheritedStyle("javascript-interface", "type"), ".cm-sem-js-type-alias": inheritedStyle("javascript-type-alias", "type"), ".cm-sem-js-type-parameter": inheritedStyle("javascript-type-parameter", "type"), + // IntelliJ's Java Interface / Abstract class / Enum / Type parameter: unset, they keep the type colour. + ".cm-sem-java-interface": inheritedStyle("java-interface", "type"), + ".cm-sem-java-abstract-class": inheritedStyle("java-abstract-class", "type"), + ".cm-sem-java-enum": inheritedStyle("java-enum", "type"), + ".cm-sem-java-type-parameter": inheritedStyle("java-type-parameter", "type"), // IDEA colours declarations, calls stay (nearly) plain text. ".cm-sem-function": semanticStyle("function-call", "function"), // Keep role selectors at one class so static/deprecated modifiers can override their styles. diff --git a/src/features/settings/pages/editor/ColorSchemeBody.tsx b/src/features/settings/pages/editor/ColorSchemeBody.tsx index 371461b1b6..3aee75e6b3 100644 --- a/src/features/settings/pages/editor/ColorSchemeBody.tsx +++ b/src/features/settings/pages/editor/ColorSchemeBody.tsx @@ -752,8 +752,21 @@ const SAMPLES: Record = { [ ["public class ", "java.keyword"], ["Greeter", "java.className"], + ["<", "java.operator"], + ["T", "java.typeParameter"], + ["> ", "java.operator"], + ["extends ", "java.keyword"], + ["Shape", "java.abstractClass"], + [" implements ", "java.keyword"], + ["Named", "java.interface"], [" {", "java.operator"], ], + [ + [" private ", "java.keyword"], + ["Mode", "java.enum"], + [" mode", "java.field"], + [";", "java.operator"], + ], [ [" private static final ", "java.keyword"], ["int", "java.keyword"], diff --git a/src/platform/colorScheme.test.ts b/src/platform/colorScheme.test.ts index 86fa5e499c..267ddd974c 100644 --- a/src/platform/colorScheme.test.ts +++ b/src/platform/colorScheme.test.ts @@ -150,6 +150,19 @@ describe("color schemes", () => { expect(css).not.toMatch(/:root \{[^}]*--syn-javascript-interface/); }); + it("offers Java's interface, abstract class, enum and type parameter, in the class name's style until set (#6415)", () => { + const ids = ["java.interface", "java.abstractClass", "java.enum", "java.typeParameter"]; + const page = ATTRIBUTES.filter((a) => a.path[0] === "Java").map((a) => a.id); + expect(page).toEqual(expect.arrayContaining(ids)); + const customs = { + "custom-j": { name: "J", parent: "dark" as const, attributes: { "java.className": { foreground: "#123456" } } }, + }; + for (const id of ids) expect(resolvedAttribute("custom-j", id, customs)?.foreground).toBe("#123456"); + const css = schemeCss("dark", { dark: { "java.enum": { foreground: "#4ec9b0" } } }); + expect(css).toContain("--syn-java-enum: #4ec9b0 !important"); + expect(css).not.toMatch(/:root \{[^}]*--syn-java-enum/); + }); + it("shows an unset Global function in the function declaration style, then its own (#4059)", () => { const fn = resolvedAttribute("dark", "functionDeclaration"); expect(resolvedAttribute("dark", "javascript.globalFunction")).toEqual(fn); diff --git a/src/platform/colorScheme.ts b/src/platform/colorScheme.ts index cb9b4706ee..ef34302a12 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -496,6 +496,18 @@ export const ATTRIBUTES: readonly AttributeDef[] = [ ...syn(`javascript.${id}`, name, [JS], token, { inherit: "type" }), language: JS, })), + // IntelliJ's Java kinds of type: unset, they keep the class name's colour. + ...( + [ + ["interface", "Classes › Interface name", "java-interface"], + ["abstractClass", "Classes › Abstract class name", "java-abstract-class"], + ["enum", "Classes › Enum name", "java-enum"], + ["typeParameter", "Classes › Type parameter", "java-type-parameter"], + ] as const + ).map(([id, name, token]) => ({ + ...syn(`java.${id}`, name, ["Java"], token, { inherit: "type" }), + language: "Java", + })), // IntelliJ's Kotlin page: calls of top-level functions, and enum entries (IntelliJ falls back to // static method and static field, which are a function call and a constant here). ...( diff --git a/src/platform/icls.test.ts b/src/platform/icls.test.ts index cbd5204145..b09de3d23b 100644 --- a/src/platform/icls.test.ts +++ b/src/platform/icls.test.ts @@ -297,6 +297,29 @@ describe(".icls schemes", () => { expect(explicit["javascript.interface"]?.foreground).toBe("#4ec9b0"); }); + it("maps IntelliJ's Java interface, abstract class, enum and type parameter keys (#6415)", () => { + const opt = (name: string, value: string) => + ``; + const parse = (...options: string[]) => + parseIcls(`${options.join("")}`).scheme.attributes; + const all = parse( + opt("CLASS_NAME_ATTRIBUTES", "000000"), + opt("INTERFACE_NAME_ATTRIBUTES", "111111"), + opt("ABSTRACT_CLASS_NAME_ATTRIBUTES", "222222"), + opt("ENUM_NAME_ATTRIBUTES", "333333"), + opt("TYPE_PARAMETER_NAME_ATTRIBUTES", "20999d"), + ); + expect(all["java.interface"]?.foreground).toBe("#111111"); + expect(all["java.abstractClass"]?.foreground).toBe("#222222"); + expect(all["java.enum"]?.foreground).toBe("#333333"); + expect(all["java.typeParameter"]?.foreground).toBe("#20999d"); + // The default Interface name colours Java's interface until INTERFACE_NAME_ATTRIBUTES does, listed first or not. + expect(parse(opt("DEFAULT_INTERFACE_NAME", "ebcb8b"))["java.interface"]?.foreground).toBe("#ebcb8b"); + const explicit = parse(opt("INTERFACE_NAME_ATTRIBUTES", "111111"), opt("DEFAULT_INTERFACE_NAME", "ebcb8b")); + expect(explicit["java.interface"]?.foreground).toBe("#111111"); + expect(explicit.className?.foreground).toBe("#ebcb8b"); + }); + it("maps punctuation only from keys that do not spread one colour to other punctuation", () => { const opt = (name: string, c: string) => ``; diff --git a/src/platform/icls.ts b/src/platform/icls.ts index 907185439b..22fffd28fb 100644 --- a/src/platform/icls.ts +++ b/src/platform/icls.ts @@ -115,6 +115,13 @@ for (const [name, id] of Object.entries(LANGUAGE_KEYS)) { ATTRIBUTE_NAMES[`KOTLIN_${name}`] = `kotlin.${id}`; ATTRIBUTE_NAMES[name] = id; } +/** IntelliJ's Java kinds of type; they outrank the shared defaults that also colour them. */ +const JAVA_TYPE_KEYS: Record = { + INTERFACE_NAME_ATTRIBUTES: "java.interface", + ABSTRACT_CLASS_NAME_ATTRIBUTES: "java.abstractClass", + ENUM_NAME_ATTRIBUTES: "java.enum", + TYPE_PARAMETER_NAME_ATTRIBUTES: "java.typeParameter", +}; Object.assign(ATTRIBUTE_NAMES, { KOTLIN_MUTABLE_VARIABLE: "kotlin.identifier", KOTLIN_PACKAGE_PROPERTY: "kotlin.field", @@ -130,6 +137,7 @@ Object.assign(ATTRIBUTE_NAMES, { KDOC_TAG_NAME: "kotlin.docCommentTag", KDOC_LINK: "kotlin.docCommentTagValue", CLASS_NAME_ATTRIBUTES: "className", + ...JAVA_TYPE_KEYS, // IDEA DiffColors (Diff & Merge page): Import/export the four change types by their // exchange names, so a scheme exported here carries the same keys IDEA reads. DIFF_MODIFIED: "diffChanged", @@ -304,17 +312,22 @@ const PARENT_ANNOTATIONS: Record = { Default: "#808000", Darcula const KEY_ORDER = new Map(Object.keys(ATTRIBUTE_NAMES).map((name, i) => [name, i])); const LANGUAGE_PRECEDENCE = ["TS.", "JS.", "PY."]; const precedence = (name: string) => { - const language = LANGUAGE_PRECEDENCE.findIndex((prefix) => name.startsWith(prefix)); - return (language < 0 ? LANGUAGE_PRECEDENCE.length : language) * KEY_ORDER.size + KEY_ORDER.get(name)!; + const prefixed = LANGUAGE_PRECEDENCE.findIndex((prefix) => name.startsWith(prefix)); + const language = + prefixed >= 0 ? prefixed : LANGUAGE_PRECEDENCE.length + (Object.hasOwn(JAVA_TYPE_KEYS, name) ? 0 : 1); + return language * KEY_ORDER.size + KEY_ORDER.get(name)!; +}; +// Keys that also colour other attributes as their fallback. TypeScript's and Java's interfaces fall +// back to the language default Interface name, as they do in WebStorm and IntelliJ; an explicit +// JS./TS.INTERFACE or INTERFACE_NAME_ATTRIBUTES outranks it. +const ALSO_TARGETS: Record = { + DEFAULT_INTERFACE_NAME: ["javascript.interface", "java.interface"], }; -// Keys that also colour another attribute as its fallback. TypeScript's interface falls back to the -// language default Interface name, as it does in WebStorm; an explicit JS./TS.INTERFACE outranks it. -const ALSO_TARGETS: Record = { DEFAULT_INTERFACE_NAME: "javascript.interface" }; const targets = (name: string): string[] => Object.hasOwn(ATTRIBUTE_NAMES, name) ? ALSO_CALLS.has(name) ? [ATTRIBUTE_NAMES[name]!, "javascript.functionCall"] - : [ATTRIBUTE_NAMES[name]!, ...(Object.hasOwn(ALSO_TARGETS, name) ? [ALSO_TARGETS[name]!] : [])] + : [ATTRIBUTE_NAMES[name]!, ...(Object.hasOwn(ALSO_TARGETS, name) ? ALSO_TARGETS[name]! : [])] : []; // IntelliJ's EFFECT_TYPE codes. The box (0) and faded text (6) have no counterpart here; the bold and the diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index 2c43eaf7d8..5183e88b29 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -9284,6 +9284,8 @@ "Interface name": "介面名稱", "Type alias": "型別別名", "Type parameter": "型別參數", + "Abstract class name": "抽象類別名稱", + "Enum name": "列舉名稱", "Change Project Icon…": "變更專案圖示…", "Reset Project Icon": "重設專案圖示", "Cannot change the project icon": "無法變更專案圖示", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index f4497afad0..fdf1b93d43 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -10472,6 +10472,8 @@ "Interface name": "接口名", "Type alias": "类型别名", "Type parameter": "类型参数", + "Abstract class name": "抽象类名", + "Enum name": "枚举名", "Change Project Icon…": "更改项目图标…", "Reset Project Icon": "重置项目图标", "Cannot change the project icon": "无法更改项目图标",