feat(color-scheme): add Kotlin extension call, package-level call and enum entry entries (#4979)

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>
This commit is contained in:
LU XIANGRUI
2026-11-22 00:08:46 +08:00
committed by GitHub
co-authored by wildcatDownstairs
parent 9eaa44e004
commit 81b357cdce
9 changed files with 122 additions and 4 deletions
@@ -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<String, Int>()",
];
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")',
+17 -1
View File
@@ -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<string, string> {
const out = new Map<string, string>();
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<string, string> {
}
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");
},
};
+7
View File
@@ -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"),
@@ -1015,6 +1015,19 @@ const SAMPLES: Record<string, CodeSample> = {
["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"]],
],
},
+12
View File
@@ -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"],
+15
View File
@@ -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") =>
`<option name="${name}"><value><option name="FOREGROUND" value="${value}"/><option name="FONT_TYPE" value="${font}"/></value></option>`;
const { scheme } = parseIcls(
`<scheme name="K"><attributes>${opt("KOTLIN_FUNCTION_CALL", "bcbec4")}${opt(
"KOTLIN_EXTENSION_FUNCTION_CALL",
"56a8f5",
)}${opt("KOTLIN_PACKAGE_FUNCTION_CALL", "57aaf7", "2")}${opt("KOTLIN_ENUM_ENTRY", "c77dbb", "2")}</attributes></scheme>`,
);
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;
+3 -1
View File
@@ -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",
+4 -1
View File
@@ -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"
}
+4 -1
View File
@@ -9291,5 +9291,8 @@
"Global function": "全局函数",
"Literal keyword": "字面量关键字",
"Predefined type": "预定义类型",
"Predefined function": "预定义函数"
"Predefined function": "预定义函数",
"Extension function call": "扩展函数调用",
"Package-level function call": "包级函数调用",
"Enum entry": "枚举项"
}