From 3389746a154db286952e088ba367b44db5f11a44 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:50:06 +0800 Subject: [PATCH] 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. --- src/editor/core/cm/interpolation.test.ts | 34 ++++++++++++++++++++++++ src/editor/core/cm/interpolation.ts | 27 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/editor/core/cm/interpolation.test.ts b/src/editor/core/cm/interpolation.test.ts index dfb48513ad..38627c5304 100644 --- a/src/editor/core/cm/interpolation.test.ts +++ b/src/editor/core/cm/interpolation.test.ts @@ -17,6 +17,40 @@ async function tokens(mode: string, source: string) { return result; } +describe("Kotlin soft keywords", () => { + const styleOf = (out: Awaited>, 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}")'], diff --git a/src/editor/core/cm/interpolation.ts b/src/editor/core/cm/interpolation.ts index e0709d9f8d..cb69380d5e 100644 --- a/src/editor/core/cm/interpolation.ts +++ b/src/editor/core/cm/interpolation.ts @@ -152,7 +152,34 @@ export function interpolationParser(base: StreamParser, 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 === "{"); +}