fix(editor): colour Kotlin soft keywords only where they act as keywords

The Kotlin stream mode treats every soft keyword as a keyword, so a
package segment (import top.ttb.data.Model), a member (result.data) or a
name (val data = ..., fun g(data: String)) was coloured like a keyword.
A word after . or ?. is now always a name, and a soft keyword is a
keyword only before a declaration or another modifier (data class, open
fun, by lazy) or, for catch / finally / init / constructor / get / set,
before ( or {. Hard keywords are unchanged.
Цей коміт міститься в:
l0ng-ai committed 2026-11-17 14:50:06 +08:00
1 джерело 25d6e8ac7c
коміт 3389746a15
2 files changed
+61

No files matched your search

+34
Переглянути файл
@@ -17,6 +17,40 @@ async function tokens(mode: string, source: string) {
return result;
}
describe("Kotlin soft keywords", () => {
const styleOf = (out: Awaited<ReturnType<typeof tokens>>, text: string, nth = 0) =>
out.filter((t) => t.text === text)[nth]?.style ?? "";
it("colours a soft keyword only where it acts as one", async () => {
const source = [
"import top.ttb.data.Model",
"data class Point(val x: Int)",
"open class Base { val data = result.data; private fun f() {} }",
"val open: Int get() = 1",
"fun g(data: String) = try { } catch (e: Exception) { } finally { }",
].join("\n");
const out = await tokens("Kotlin", source);
// Package path, property access and names.
expect(styleOf(out, "data", 0)).not.toContain("tok-keyword");
expect(styleOf(out, "data", 2)).not.toContain("tok-keyword");
expect(styleOf(out, "data", 3)).not.toContain("tok-keyword");
expect(styleOf(out, "data", 4)).not.toContain("tok-keyword");
expect(styleOf(out, "open", 1)).not.toContain("tok-keyword");
// Modifiers and accessors.
expect(styleOf(out, "import")).toContain("tok-keyword");
expect(styleOf(out, "data", 1)).toContain("tok-keyword");
expect(styleOf(out, "open", 0)).toContain("tok-keyword");
expect(styleOf(out, "private")).toContain("tok-keyword");
expect(styleOf(out, "get")).toContain("tok-keyword");
expect(styleOf(out, "catch")).toContain("tok-keyword");
expect(styleOf(out, "finally")).toContain("tok-keyword");
// Hard keywords stay keywords.
expect(styleOf(out, "class")).toContain("tok-keyword");
expect(styleOf(out, "val")).toContain("tok-keyword");
expect(styleOf(out, "fun")).toContain("tok-keyword");
});
});
describe("frontend string interpolation", () => {
it.each([
["Kotlin", 'println("$i + 1 = ${i + 1}")'],
+27
Переглянути файл
@@ -152,7 +152,34 @@ export function interpolationParser<S>(base: StreamParser<S>, dialect: Interpola
frame.doc = depth > 0 && doc;
return doc ? "docComment" : result;
}
if (dialect === "Kotlin" && result === "keyword" && !kotlinKeywordAt(stream.string, from, stream.pos))
return "variable";
return result;
},
};
}
/** Kotlin's hard keywords: never names, wherever they appear (except after a dot). */
const KOTLIN_HARD_KEYWORDS = new Set(
"as as? break class continue do else false for fun if in !in interface is !is null object package return super this throw true try typealias typeof val var when while".split(
" ",
),
);
/** Soft keywords that open a block or a parameter list rather than precede a name. */
const KOTLIN_BLOCK_SOFT_KEYWORDS = new Set(["catch", "finally", "init", "constructor", "get", "set"]);
/**
* Whether the word the base mode calls a keyword is one here. A word after `.` is a member or
* package name (`import a.data.b`, `result.data`). Soft keywords (`data`, `open`, `get`…) are
* keywords only where they act as one, before a declaration or another modifier (`data class`,
* `get() = …`); elsewhere they are ordinary names (`val data = …`), as IDEA colours them.
*/
function kotlinKeywordAt(line: string, from: number, to: number): boolean {
if (/\??\.\s*$/.test(line.slice(0, from))) return false;
const word = line.slice(from, to);
if (KOTLIN_HARD_KEYWORDS.has(word)) return true;
const next = line.slice(to).match(/^\s*(.)/u)?.[1];
if (next === undefined) return false;
if (/[\p{L}_`@]/u.test(next)) return true;
return KOTLIN_BLOCK_SOFT_KEYWORDS.has(word) && (next === "(" || next === "{");
}