From 9eaa44e00431c8b0cd0df385bb4eea66409e256a Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sun, 22 Nov 2026 01:07:39 +0900 Subject: [PATCH] feat(color-scheme): split predefined types and functions and separate literal keywords (#4978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Language Defaults and each language page get Identifiers › Predefined type and Predefined function next to Predefined symbol, and Literal keyword next to Keyword. The new entries show their parent until set, so existing overrides keep applying. true / false / null in the clike modes (Kotlin, Scala, Dart, Groovy, C#) are bool / null tokens taking Literal keyword instead of Constant. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- src/editor/core/cm/highlight.ts | 18 +++++-------- src/editor/core/cm/interpolation.test.ts | 14 +++++++++++ src/editor/core/cm/interpolation.ts | 6 +++++ src/editor/lsp/theme.ts | 4 ++- .../settings/pages/editor/ColorSchemeBody.tsx | 17 +++++++++++-- src/platform/colorScheme.test.ts | 25 +++++++++++++++++++ src/platform/colorScheme.ts | 5 ++++ src/platform/locales/text/en.json | 5 +++- src/platform/locales/text/zh.json | 5 +++- 9 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/editor/core/cm/highlight.ts b/src/editor/core/cm/highlight.ts index 36100fc13..eb6959b5e 100644 --- a/src/editor/core/cm/highlight.ts +++ b/src/editor/core/cm/highlight.ts @@ -40,19 +40,11 @@ export const openideaHighlightStyle = HighlightStyle.define([ token(t.link, "link", { effect: "underline" }), token(t.quote, "string"), token( - [ - t.keyword, - t.controlKeyword, - t.moduleKeyword, - t.operatorKeyword, - t.definitionKeyword, - t.modifier, - t.null, - t.bool, - t.unit, - ], + [t.keyword, t.controlKeyword, t.moduleKeyword, t.operatorKeyword, t.definitionKeyword, t.modifier, t.unit], "keyword", ), + // JetBrains colours them as keywords; Literal keyword sets them apart. + token([t.null, t.bool], "literal", { color: "keyword" }), token(t.self, "self"), token([t.string, t.special(t.string), t.character, t.attributeValue], "string"), token(t.docString, "docstring", { italic: "italic" }), @@ -60,7 +52,9 @@ export const openideaHighlightStyle = HighlightStyle.define([ token(t.regexp, "regexp"), token([t.number, t.integer, t.float], "number"), token([t.typeName, t.className, t.namespace, t.definition(t.typeName)], "type"), - token([t.standard(t.typeName), t.standard(t.variableName), t.standard(t.function(t.variableName))], "builtin"), + token(t.standard(t.variableName), "builtin"), + token(t.standard(t.typeName), "builtin-type", { color: "builtin" }), + token(t.standard(t.function(t.variableName)), "builtin-function", { color: "builtin" }), token([t.function(t.variableName), t.function(t.propertyName)], "function-call"), token(shellCommand, "shell-command", { color: "function-call" }), token( diff --git a/src/editor/core/cm/interpolation.test.ts b/src/editor/core/cm/interpolation.test.ts index 38627c530..2671c0fd6 100644 --- a/src/editor/core/cm/interpolation.test.ts +++ b/src/editor/core/cm/interpolation.test.ts @@ -51,6 +51,20 @@ describe("Kotlin soft keywords", () => { }); }); +describe("literal keywords (#4059)", () => { + it.each(["Kotlin", "Scala", "Dart", "Groovy", "C#"])( + "tags true, false and null in %s as literals, not as constants", + async (mode) => { + const out = await tokens(mode, "x = f(true, false, null)"); + const styleOf = (text: string) => out.find((t) => t.text === text)?.style ?? ""; + expect(styleOf("true")).toBe("tok-bool"); + expect(styleOf("false")).toBe("tok-bool"); + // `tags.null` derives from keyword, which classHighlighter prints; an atom prints tok-atom. + expect(styleOf("null")).toBe("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 cb69380d5..957d400e8 100644 --- a/src/editor/core/cm/interpolation.ts +++ b/src/editor/core/cm/interpolation.ts @@ -154,6 +154,12 @@ export function interpolationParser(base: StreamParser, dialect: Interpola } if (dialect === "Kotlin" && result === "keyword" && !kotlinKeywordAt(stream.string, from, stream.pos)) return "variable"; + // The clike modes call these atoms, like enum-ish constants; JetBrains colours them as keywords. + if (result === "atom") { + const word = stream.string.slice(from, stream.pos); + if (word === "true" || word === "false") return "bool"; + if (word === "null") return "null"; + } return result; }, }; diff --git a/src/editor/lsp/theme.ts b/src/editor/lsp/theme.ts index c83395061..17895afb0 100644 --- a/src/editor/lsp/theme.ts +++ b/src/editor/lsp/theme.ts @@ -518,7 +518,8 @@ 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"), - ".cm-sem-function:where(.cm-sem-builtin)": semanticStyle("builtin", "function"), + // 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"), // WebStorm's Global function: a module-level function, its declaration and its calls. ".cm-sem-global-function": inheritedStyle("global-function", "function"), @@ -535,6 +536,7 @@ export const lspTheme = EditorView.theme({ // Before the builtin rule: library globals (`print`, `string`) keep the predefined colour. ".cm-sem-global:where(:not(.cm-sem-function))": semanticStyle("global", "variable"), ".cm-sem-builtin:where(:not(.cm-sem-function))": semanticStyle("builtin", "variable"), + ".cm-sem-builtin:where(.cm-sem-type)": inheritedStyle("builtin-type", "builtin"), ".cm-sem-builtin.cm-sem-python-builtin": semanticStyle("python-builtin", "builtin"), ".cm-sem-static": { fontStyle: "var(--syn-static-italic, italic)" }, ".cm-sem-property.cm-sem-static, .cm-sem-property.cm-sem-static *": { diff --git a/src/features/settings/pages/editor/ColorSchemeBody.tsx b/src/features/settings/pages/editor/ColorSchemeBody.tsx index 78a9a2f65..c0c85aecd 100644 --- a/src/features/settings/pages/editor/ColorSchemeBody.tsx +++ b/src/features/settings/pages/editor/ColorSchemeBody.tsx @@ -377,6 +377,17 @@ const SAMPLES: Record = { ["predefined", "predefined"], [", ", "operator"], ["local", "identifier"], + [", ", "operator"], + ["true", "literalKeyword"], + [")", "operator"], + ], + [ + [" ", "text"], + ["String", "predefinedType"], + [".", "operator"], + ["format", "predefinedFunction"], + ["(", "operator"], + ["null", "literalKeyword"], [")", "operator"], ], [ @@ -966,7 +977,7 @@ const SAMPLES: Record = { ["val", "kotlin.keyword"], [" name", "kotlin.parameter"], [": ", "text"], - ["String", "kotlin.className"], + ["String", "kotlin.predefinedType"], [")", "kotlin.operator"], [" {", "kotlin.operator"], ], @@ -997,9 +1008,11 @@ const SAMPLES: Record = { ], [ [" ", "text"], - ["println", "kotlin.predefined"], + ["println", "kotlin.predefinedFunction"], ["(", "kotlin.operator"], ["name", "kotlin.identifier"], + [" ?: ", "kotlin.operator"], + ["null", "kotlin.literalKeyword"], [")", "kotlin.operator"], ], [["}", "kotlin.operator"]], diff --git a/src/platform/colorScheme.test.ts b/src/platform/colorScheme.test.ts index b6b63187e..8d5052b7c 100644 --- a/src/platform/colorScheme.test.ts +++ b/src/platform/colorScheme.test.ts @@ -106,6 +106,31 @@ describe("color schemes", () => { expect(css).not.toMatch(/:root \{[^}]*--syn-decorator/); }); + it("splits Predefined symbol into types and functions and keeps literals apart from Constant (#4059)", () => { + for (const [child, parent] of [ + ["predefinedType", "predefined"], + ["predefinedFunction", "predefined"], + ["literalKeyword", "keyword"], + ["kotlin.predefinedType", "kotlin.predefined"], + ["kotlin.literalKeyword", "kotlin.keyword"], + ] as const) + expect(resolvedAttribute("dark", child)).toEqual(resolvedAttribute("dark", parent)); + expect(resolvedAttribute("dark", "kotlin.literalKeyword")).not.toEqual( + resolvedAttribute("dark", "kotlin.constant"), + ); + // A language's Predefined symbol still reaches unset types and functions: they are not set on :root. + const css = schemeCss("dark", { dark: { "kotlin.predefined": { foreground: "#224466" } } }); + expect(css).not.toContain("--syn-builtin-type"); + const split = schemeCss("dark", { + dark: { + "kotlin.predefinedType": { foreground: "#bcbec4" }, + "kotlin.predefinedFunction": { foreground: "#56a8f5" }, + }, + }); + expect(split).toContain("--syn-builtin-type: #bcbec4 !important"); + expect(split).toContain("--syn-builtin-function: #56a8f5 !important"); + }); + 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 37f56f211..a42266775 100644 --- a/src/platform/colorScheme.ts +++ b/src/platform/colorScheme.ts @@ -300,6 +300,8 @@ const BASE_ATTRIBUTES: readonly AttributeDef[] = [ }, syn("keyword", "Keyword", [LD], "keyword"), + // `true`, `false`, `null`: keywords in JetBrains schemes, which have no key of their own for them. + syn("literalKeyword", "Literal keyword", [LD], "literal", { inherit: "keyword" }), syn("identifier", "Identifiers › Local variable", [LD], "variable"), syn("parameter", "Identifiers › Parameter", [LD], "param"), syn("globalVariable", "Identifiers › Global variable", [LD], "global"), @@ -317,6 +319,9 @@ const BASE_ATTRIBUTES: readonly AttributeDef[] = [ syn("functionDeclaration", "Identifiers › Function declaration", [LD], "function"), syn("functionCall", "Identifiers › Function call", [LD], "function-call"), syn("predefined", "Identifiers › Predefined symbol", [LD], "builtin"), + // A standard library's types and functions (String, let), apart from its other symbols. + syn("predefinedType", "Identifiers › Predefined type", [LD], "builtin-type", { inherit: "builtin" }), + syn("predefinedFunction", "Identifiers › Predefined function", [LD], "builtin-function", { inherit: "builtin" }), syn("className", "Classes › Class name", [LD], "type"), syn("number", "Number", [LD], "number"), syn("string", "String › String text", [LD], "string"), diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 5ca09c718..5d00795fb 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2961,5 +2961,8 @@ "Cannot find the source of {0}": "Cannot find the source of {0}", "JavaScript and TypeScript": "JavaScript and TypeScript", "Global function": "Global function", - "Decorator": "Decorator" + "Decorator": "Decorator", + "Literal keyword": "Literal keyword", + "Predefined type": "Predefined type", + "Predefined function": "Predefined function" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index eff0938f4..86f12e33c 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9288,5 +9288,8 @@ "This CLI discovers the folder itself: off hides the skill from OpenIDEA's / menu and applies only where the CLI supports disabling.": "该 CLI 会自己发现这个文件夹:关闭只会让 OpenIDEA 的 / 菜单不再提供该技能,并且只在 CLI 支持禁用时生效。", "Cannot find the source of {0}": "找不到 {0} 的源代码", "JavaScript and TypeScript": "JavaScript 和 TypeScript", - "Global function": "全局函数" + "Global function": "全局函数", + "Literal keyword": "字面量关键字", + "Predefined type": "预定义类型", + "Predefined function": "预定义函数" }