From 1f8eaec00a0f718962bcf70bbcc28ab27fd5fd4c Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Tue, 24 Nov 2026 21:53:46 +0900 Subject: [PATCH] fix(editor): colour Java primitive types as keywords (#5543) @lezer/java tags PrimitiveType as standard(typeName), so int/long/boolean/... used the built-in colour instead of the keyword colour IntelliJ uses (void already had it). JAVA_TAGS now tags PrimitiveType as keyword; this covers Java files, JSP scriptlets and java code blocks in Markdown. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- src/editor/core/cm/jsp/jsp.test.ts | 2 +- src/editor/core/cm/languages.test.ts | 16 ++++++++++++++++ src/editor/core/cm/languages.ts | 6 +++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/editor/core/cm/jsp/jsp.test.ts b/src/editor/core/cm/jsp/jsp.test.ts index 64d09bab8..9cd2bb146 100644 --- a/src/editor/core/cm/jsp/jsp.test.ts +++ b/src/editor/core/cm/jsp/jsp.test.ts @@ -120,7 +120,7 @@ describe("JSP pages", () => { "%>:pi", "<%!:pi", "private:keyword", - "int:type", + "int:keyword", "count:def", "0:num", "%>:pi", diff --git a/src/editor/core/cm/languages.test.ts b/src/editor/core/cm/languages.test.ts index 805c11196..2a14963ec 100644 --- a/src/editor/core/cm/languages.test.ts +++ b/src/editor/core/cm/languages.test.ts @@ -510,6 +510,22 @@ describe("language modes", () => { ]); }); + it("colours Java primitive types as keywords", async () => { + const highlighter = tagHighlighter([ + { tag: t.keyword, class: "keyword" }, + { tag: t.standard(t.typeName), class: "builtin-type" }, + ]); + const primitives = ["int", "long", "double", "float", "boolean", "char", "byte", "short"]; + const doc = `class A { ${primitives.map((p, i) => `${p} f${i};`).join(" ")} void m() { int[] a = new int[1]; } }`; + const support = await loadSupport(findLanguageDescription("Java", "/p/A.java")!); + const styles = new Map>(); + highlightTree(support!.language.parser.parse(doc), highlighter, (from, to, style) => { + const text = doc.slice(from, to); + styles.set(text, (styles.get(text) ?? new Set()).add(style)); + }); + for (const word of [...primitives, "void"]) expect([...(styles.get(word) ?? [])], word).toEqual(["keyword"]); + }); + it("recognizes go.mod and go.work files by mode and filename", async () => { for (const [mode, path] of [ ["GoMod", "/project/go.mod"], diff --git a/src/editor/core/cm/languages.ts b/src/editor/core/cm/languages.ts index 00b3a1f8f..cee2e58c4 100644 --- a/src/editor/core/cm/languages.ts +++ b/src/editor/core/cm/languages.ts @@ -495,11 +495,11 @@ const CSS_TAGS: Parameters[0] = { "ClassName IdName PseudoClassName ColorLiteral CallTag": t.tagName, }; -// @lezer/java tags annotations as plain identifiers, and `this` as `self` (Python's `self` colour); -// IntelliJ colours `this` as a keyword. +// @lezer/java tags annotations as plain identifiers, `this` as `self` (Python's `self` colour) and +// primitive types as built-in types; IntelliJ colours `this` and `int`, `boolean`… as keywords. export const JAVA_TAGS: Parameters[0] = { "MarkerAnnotation/Identifier Annotation/Identifier MarkerAnnotation Annotation": t.annotation, - this: t.keyword, + "this PrimitiveType": t.keyword, // IntelliJ has no Java label colour: `outer:` stays plain text, like `break outer`. Label: t.variableName, };