From 81b357cdceb9866ec033fa0c3129bd9bcfaf02b7 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sun, 22 Nov 2026 01:08:46 +0900 Subject: [PATCH] feat(color-scheme): add Kotlin extension call, package-level call and enum entry entries (#4979) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Kotlin page gets Identifiers › Extension function call, Package-level function call and Classes › Enum entry, as IntelliJ's Kotlin page has. Calls on an explicit receiver to top-level functions are extension calls, without one package-level calls; enum entries no longer take Constant. Each falls back to its parent until set; .icls KOTLIN_* keys fill them. Closes #4059 Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- src/editor/lsp/semantic/kotlin/index.test.ts | 47 +++++++++++++++++++ src/editor/lsp/semantic/kotlin/index.ts | 18 ++++++- src/editor/lsp/theme.ts | 7 +++ .../settings/pages/editor/ColorSchemeBody.tsx | 13 +++++ src/platform/colorScheme.ts | 12 +++++ src/platform/icls.test.ts | 15 ++++++ src/platform/icls.ts | 4 +- src/platform/locales/text/en.json | 5 +- src/platform/locales/text/zh.json | 5 +- 9 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/editor/lsp/semantic/kotlin/index.test.ts b/src/editor/lsp/semantic/kotlin/index.test.ts index 844aea598..4f337ca0e 100644 --- a/src/editor/lsp/semantic/kotlin/index.test.ts +++ b/src/editor/lsp/semantic/kotlin/index.test.ts @@ -44,6 +44,53 @@ describe("Kotlin semantic tokens", () => { ).toBe("cm-sem-property"); }); + it("tells extension and package-level function calls and enum entries apart (#4059)", () => { + // JetBrains Kotlin LSP's legend; top-level functions carry `static`, library ones `defaultLibrary`. + const types = ["function", "method", "enumMember", "class"]; + const modifiers = ["declaration", "static", "defaultLibrary"]; + const lines = [ + "fun Book.summary() = shelfLabel(Genre.NOVEL)", + "val s = book.summary() + book?.let { it } + local() + name.toString()", + "@Outer.Inner fun f() = emptyMap()", + ]; + const doc = Text.of(lines); + const tok = (line: number, word: string, type: string, mods: string[] = [], nth = 0) => { + let col = -1; + for (let i = 0; i <= nth; i++) col = lines[line]!.indexOf(word, col + 1); + return [line, col, word.length, types.indexOf(type), mods.reduce((m, n) => m | (1 << modifiers.indexOf(n)), 0)]; + }; + const data = [ + tok(0, "summary", "function", ["declaration", "static"]), + tok(0, "shelfLabel", "function", ["static"]), + tok(0, "NOVEL", "enumMember"), + tok(1, "summary", "function", ["static"]), + tok(1, "let", "function", ["static", "defaultLibrary"]), + tok(1, "local", "function"), + tok(1, "toString", "method", ["defaultLibrary"]), + tok(2, "Inner", "class"), + tok(2, "emptyMap", "function", ["static", "defaultLibrary"]), + ].flat(); + const ranges = semanticRanges( + doc, + { tokenTypes: types, tokenModifiers: modifiers, data }, + false, + "kotlin", + Tree.empty, + ); + const at = (line: number, word: string) => + ranges.find((r) => r.from === doc.line(line + 1).from + lines[line]!.indexOf(word))?.className.split(" "); + expect(at(0, "summary")).not.toContain("cm-sem-kotlin-extension-call"); + expect(at(0, "shelfLabel")).toContain("cm-sem-kotlin-package-call"); + expect(at(0, "NOVEL")).toEqual(["cm-sem-constant", "cm-sem-kotlin-enum-entry"]); + expect(at(1, "summary")).toContain("cm-sem-kotlin-extension-call"); + // Library functions keep their Predefined function class, which the theme lets win. + expect(at(1, "let")).toEqual(expect.arrayContaining(["cm-sem-builtin", "cm-sem-kotlin-extension-call"])); + expect(at(1, "local")).toEqual(["cm-sem-function"]); + expect(at(1, "toString")).toEqual(["cm-sem-function", "cm-sem-builtin"]); + expect(at(2, "Inner")).toEqual(["cm-sem-annotation"]); + expect(at(2, "emptyMap")).toContain("cm-sem-kotlin-package-call"); + }); + it("colours annotation names as annotations, not as the constructor calls the server reports", () => { const lines = [ '@Column(name = "x")', diff --git a/src/editor/lsp/semantic/kotlin/index.ts b/src/editor/lsp/semantic/kotlin/index.ts index 9b5aa13b4..b65c81b3e 100644 --- a/src/editor/lsp/semantic/kotlin/index.ts +++ b/src/editor/lsp/semantic/kotlin/index.ts @@ -3,6 +3,16 @@ import { registerLanguageSemantics, type LanguageSemantics } from "../languages" const NAME = /[A-Za-z_][\w]*/y; +/** Names right after a `.`: a call's explicit receiver (`book.summary()`, `x?.let`). */ +function receiverNames(doc: Text): Map { + const out = new Map(); + for (const m of doc.toString().matchAll(/\.[ \t]*([A-Za-z_]\w*)/g)) { + const to = m.index + m[0].length; + out.set(`${to - m[1]!.length}:${to}`, "receiver"); + } + return out; +} + /** * The names of annotation usages (`@Column`, `@field:Column`, `@Outer.Inner`), as `"from:to"` → `"annotation"`. * An `@` right after a name is a label (`return@forEach`, `this@Outer`, `loop@`), not an annotation. @@ -35,7 +45,8 @@ function annotationNames(doc: Text): Map { } const kotlinSemantics: LanguageSemantics = { - symbolKinds: (_tree, doc) => annotationNames(doc), + // Annotations last: `@Outer.Inner` is an annotation, not a receiver's member. + symbolKinds: (_tree, doc) => new Map([...receiverNames(doc), ...annotationNames(doc)]), tokenClasses({ type, modifiers, kind, classes }) { // JetBrains Kotlin LSP reports an annotation usage as a constructor call (`method`); // IDEA colours the name as an annotation (Metadata), like its `@`. @@ -49,6 +60,11 @@ const kotlinSemantics: LanguageSemantics = { if ((type === "variable" || type === "property" || type === "parameter") && modifiers.includes("modification")) classes.push("cm-sem-mutable"); if (type === "label") classes.push("cm-sem-constant"); + if (type === "enumMember") classes.push("cm-sem-kotlin-enum-entry"); + // JetBrains Kotlin LSP marks top-level functions static (members are methods): called on a receiver, + // one is an extension. ponytail: an implicit receiver (`with(x) { ext() }`) reads as package-level. + if (type === "function" && modifiers.includes("static") && !modifiers.includes("declaration")) + classes.push(kind === "receiver" ? "cm-sem-kotlin-extension-call" : "cm-sem-kotlin-package-call"); }, }; diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index 17895afb0..996284c06 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -518,6 +518,9 @@ export const lspTheme = EditorView.theme({ ".cm-sem-function": semanticStyle("function-call", "function"), // Keep role selectors at one class so static/deprecated modifiers can override their styles. ".cm-sem-function:where(.cm-sem-declaration)": semanticStyle("function", "function"), + // Kotlin's top-level function calls; a library's own stay Predefined functions (the rule below). + ".cm-sem-kotlin-extension-call": inheritedStyle("kotlin-extension-call", "function-call"), + ".cm-sem-kotlin-package-call": inheritedStyle("kotlin-package-call", "function-call"), // Predefined function / type: unset, they keep the Predefined symbol style. ".cm-sem-function:where(.cm-sem-builtin)": inheritedStyle("builtin-function", "builtin"), ".cm-sem-function.cm-sem-python-method-call": semanticStyle("python-method-call", "function-call"), @@ -527,6 +530,10 @@ export const lspTheme = EditorView.theme({ ".cm-sem-annotation": semanticStyle("decorator", "annotation"), ".cm-sem-namespace": semanticStyle("namespace", "type"), ".cm-sem-constant": { ...semanticStyle("constant", "constant"), fontStyle: "var(--syn-constant-italic, italic)" }, + ".cm-sem-kotlin-enum-entry": { + ...inheritedStyle("kotlin-enum-entry", "constant"), + fontStyle: "var(--syn-kotlin-enum-entry-italic, var(--syn-constant-italic, italic))", + }, ".cm-sem-variable": semanticStyle("variable", "variable"), ".cm-sem-property": semanticStyle("property", "property"), ".cm-sem-parameter": semanticStyle("param", "variable"), diff --git a/src/features/settings/pages/editor/ColorSchemeBody.tsx b/src/features/settings/pages/editor/ColorSchemeBody.tsx index c0c85aecd..fc7d1d530 100644 --- a/src/features/settings/pages/editor/ColorSchemeBody.tsx +++ b/src/features/settings/pages/editor/ColorSchemeBody.tsx @@ -1015,6 +1015,19 @@ const SAMPLES: Record = { ["null", "kotlin.literalKeyword"], [")", "kotlin.operator"], ], + [ + [" ", "text"], + ["log", "kotlin.packageCall"], + ["(", "kotlin.operator"], + ["name", "kotlin.identifier"], + [".", "kotlin.operator"], + ["shout", "kotlin.extensionCall"], + ["(), ", "kotlin.operator"], + ["Mood", "kotlin.className"], + [".", "kotlin.operator"], + ["HAPPY", "kotlin.enumEntry"], + [")", "kotlin.operator"], + ], [["}", "kotlin.operator"]], ], }, diff --git a/src/platform/colorScheme.ts b/src/platform/colorScheme.ts index a42266775..f7607f800 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -432,6 +432,18 @@ export const ATTRIBUTES: readonly AttributeDef[] = [ }), language: JS, }, + // 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). + ...( + [ + ["extensionCall", "Identifiers › Extension function call", "kotlin-extension-call", "function-call"], + ["packageCall", "Identifiers › Package-level function call", "kotlin-package-call", "function-call"], + ["enumEntry", "Classes › Enum entry", "kotlin-enum-entry", "constant"], + ] as const + ).map(([id, name, token, inherit]) => ({ + ...syn(`kotlin.${id}`, name, ["Kotlin"], token, { inherit, defaultItalic: id === "enumEntry" }), + language: "Kotlin", + })), ...[ ["brackets", "Brackets", "bracket"], ["separator", "Comma and semicolon", "separator"], diff --git a/src/platform/icls.test.ts b/src/platform/icls.test.ts index 7e80e18b1..eb3621274 100644 --- a/src/platform/icls.test.ts +++ b/src/platform/icls.test.ts @@ -164,6 +164,21 @@ describe(".icls schemes", () => { expect([applied, skipped]).toEqual([16, 2]); }); + it("imports IntelliJ's Kotlin call and enum entry keys into their own entries (#4059)", () => { + const opt = (name: string, value: string, font = "0") => + ``; + const { scheme } = parseIcls( + `${opt("KOTLIN_FUNCTION_CALL", "bcbec4")}${opt( + "KOTLIN_EXTENSION_FUNCTION_CALL", + "56a8f5", + )}${opt("KOTLIN_PACKAGE_FUNCTION_CALL", "57aaf7", "2")}${opt("KOTLIN_ENUM_ENTRY", "c77dbb", "2")}`, + ); + expect(scheme.attributes["kotlin.functionCall"]?.foreground).toBe("#bcbec4"); + expect(scheme.attributes["kotlin.extensionCall"]?.foreground).toBe("#56a8f5"); + expect(scheme.attributes["kotlin.packageCall"]).toEqual({ foreground: "#57aaf7", bold: false, italic: true }); + expect(scheme.attributes["kotlin.enumEntry"]).toEqual({ foreground: "#c77dbb", bold: false, italic: true }); + }); + 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 3bdd1faf6..40b17ad79 100644 --- a/src/platform/icls.ts +++ b/src/platform/icls.ts @@ -116,7 +116,9 @@ Object.assign(ATTRIBUTE_NAMES, { KOTLIN_MUTABLE_VARIABLE: "kotlin.identifier", KOTLIN_PACKAGE_PROPERTY: "kotlin.field", KOTLIN_SYNTHETIC_EXTENSION_PROPERTY: "kotlin.field", - KOTLIN_EXTENSION_FUNCTION_CALL: "kotlin.functionCall", + KOTLIN_EXTENSION_FUNCTION_CALL: "kotlin.extensionCall", + KOTLIN_PACKAGE_FUNCTION_CALL: "kotlin.packageCall", + KOTLIN_ENUM_ENTRY: "kotlin.enumEntry", KOTLIN_NAMED_ARGUMENT: "kotlin.parameter", KOTLIN_OBJECT: "kotlin.className", JAVA_DOC_TAG: "java.docCommentTag", diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 5d00795fb..97efcec89 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2964,5 +2964,8 @@ "Decorator": "Decorator", "Literal keyword": "Literal keyword", "Predefined type": "Predefined type", - "Predefined function": "Predefined function" + "Predefined function": "Predefined function", + "Extension function call": "Extension function call", + "Package-level function call": "Package-level function call", + "Enum entry": "Enum entry" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 86f12e33c..fcd561963 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9291,5 +9291,8 @@ "Global function": "全局函数", "Literal keyword": "字面量关键字", "Predefined type": "预定义类型", - "Predefined function": "预定义函数" + "Predefined function": "预定义函数", + "Extension function call": "扩展函数调用", + "Package-level function call": "包级函数调用", + "Enum entry": "枚举项" }