From 5beeacedf96578c27ffef0198d1f1a7c80c8570c Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Tue, 24 Nov 2026 21:54:51 +0900 Subject: [PATCH] fix(editor): colour Java declarations before jdtls answers (#5551) Until jdtls returned semantic tokens (startup, slow Maven/Gradle import, jdtls missing), Java field, enum constant and method/constructor declaration names were default text and changed colour later. java/declarations.ts colours them from the Lezer tree as fallbackRanges with the classes jdtls's tokens produce (static from modifiers; interface fields and enum constants static), so jdtls tokens replace them without a colour change. Composed with the contextual keyword syntax ranges from #5544. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- .../lsp/semantic/java/declarations.test.ts | 64 +++++++++++++++++++ src/editor/lsp/semantic/java/declarations.ts | 43 +++++++++++++ src/editor/lsp/semantic/java/index.ts | 2 + 3 files changed, 109 insertions(+) create mode 100644 src/editor/lsp/semantic/java/declarations.test.ts create mode 100644 src/editor/lsp/semantic/java/declarations.ts diff --git a/src/editor/lsp/semantic/java/declarations.test.ts b/src/editor/lsp/semantic/java/declarations.test.ts new file mode 100644 index 0000000000..0718e9b79c --- /dev/null +++ b/src/editor/lsp/semantic/java/declarations.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, it } from "vitest"; +import { Text } from "@codemirror/state"; +import { javaLanguage } from "@codemirror/lang-java"; +import { semanticRanges } from "../index"; +import { javaDeclarationRanges } from "./declarations"; + +const SRC = `class A { + private int count, total = 1; + static String name; + private final int code = 0; + final static long SERIAL = 1L; + public static final int MAX = 10; + A() {} + static void helper() { int local = 0; count = local; } + T id(T t) { return t; } + enum Level { LOW, HIGH(2) { void f() {} }; Level() {} } + interface I { int C = 1; void m(); static void s() {} } +}`; + +const tree = javaLanguage.parser.parse(SRC); +const doc = Text.of(SRC.split("\n")); +const named = (ranges: Array<{ from: number; to: number; className: string }>) => + Object.fromEntries(ranges.map((r) => [SRC.slice(r.from, r.to), r.className])); + +describe("javaDeclarationRanges", () => { + it("colours declaration names as jdtls does, and nothing else", () => { + expect(named(javaDeclarationRanges(tree))).toEqual({ + count: "cm-sem-property cm-sem-declaration", + total: "cm-sem-property cm-sem-declaration", + name: "cm-sem-property cm-sem-static cm-sem-declaration", + code: "cm-sem-property cm-sem-declaration", + SERIAL: "cm-sem-property cm-sem-static cm-sem-declaration", + MAX: "cm-sem-property cm-sem-static cm-sem-declaration", + A: "cm-sem-function cm-sem-declaration", + helper: "cm-sem-function cm-sem-static cm-sem-declaration", + id: "cm-sem-function cm-sem-declaration", + LOW: "cm-sem-constant cm-sem-static cm-sem-declaration", + HIGH: "cm-sem-constant cm-sem-static cm-sem-declaration", + f: "cm-sem-function cm-sem-declaration", + Level: "cm-sem-function cm-sem-declaration", + C: "cm-sem-property cm-sem-static cm-sem-declaration", + m: "cm-sem-function cm-sem-declaration", + s: "cm-sem-function cm-sem-static cm-sem-declaration", + }); + }); + + it("is shown without server tokens and gives way to them", () => { + const empty = { data: [], tokenTypes: [], tokenModifiers: [] }; + expect(named(semanticRanges(doc, empty, false, "java", tree)).count).toBe("cm-sem-property cm-sem-declaration"); + // A jdtls token over `count` (line 1, col 14) replaces the fallback. + const tokens = { data: [1, 14, 5, 0, 0], tokenTypes: ["property"], tokenModifiers: [] }; + const count = semanticRanges(doc, tokens, false, "java", tree).filter((r) => SRC.slice(r.from, r.to) === "count"); + expect(count.map((r) => r.className)).toEqual(["cm-sem-property"]); + }); + + it("matches the classes of jdtls's own declaration tokens", () => { + // MAX (line 5, col 26) and C (line 10, col 20), as jdtls sends them; IDEA colours `static final` fields as static fields. + const mods = ["static", "readonly", "declaration"]; + const tokens = { data: [5, 26, 3, 0, 7, 10, 20, 1, 0, 7], tokenTypes: ["property"], tokenModifiers: mods }; + const fallback = named(javaDeclarationRanges(tree)); + const server = named(semanticRanges(doc, tokens, false, "java", tree).filter((r) => !("source" in r))); + expect(server).toEqual({ MAX: fallback.MAX, C: fallback.C }); + }); +}); diff --git a/src/editor/lsp/semantic/java/declarations.ts b/src/editor/lsp/semantic/java/declarations.ts new file mode 100644 index 0000000000..de560f3987 --- /dev/null +++ b/src/editor/lsp/semantic/java/declarations.ts @@ -0,0 +1,43 @@ +import type { SyntaxNode, Tree } from "@lezer/common"; +import type { SemanticRange } from "../languages"; + +// The classes jdtls's token for the declared name produces: [instance, static]. +const DECLARATIONS: Record = { + FieldDeclaration: ["cm-sem-property cm-sem-declaration", "cm-sem-property cm-sem-static cm-sem-declaration"], + // Interface fields: implicitly `static final`. + ConstantDeclaration: [ + "cm-sem-property cm-sem-static cm-sem-declaration", + "cm-sem-property cm-sem-static cm-sem-declaration", + ], + EnumConstant: [ + "cm-sem-constant cm-sem-static cm-sem-declaration", + "cm-sem-constant cm-sem-static cm-sem-declaration", + ], + MethodDeclaration: ["cm-sem-function cm-sem-declaration", "cm-sem-function cm-sem-static cm-sem-declaration"], + ConstructorDeclaration: ["cm-sem-function cm-sem-declaration", "cm-sem-function cm-sem-declaration"], +}; + +const isStatic = (decl: SyntaxNode) => decl.getChild("Modifiers")?.getChild("static") != null; + +/** + * Declaration names coloured from the parse tree while jdtls has not answered (startup, project import, + * jdtls missing), with the classes its tokens give them, so nothing changes colour when they arrive. + * References are left alone: a bare name can be a field or a local, and only jdtls knows which. + */ +export function javaDeclarationRanges(tree: Tree): SemanticRange[] { + const out: SemanticRange[] = []; + tree.iterate({ + enter(cursor) { + const classes = Object.hasOwn(DECLARATIONS, cursor.name) ? DECLARATIONS[cursor.name] : undefined; + if (!classes) return; + const decl = cursor.node; + const className = classes[isStatic(decl) ? 1 : 0]; + const declarators = decl.getChildren("VariableDeclarator"); + for (const owner of declarators.length ? declarators : [decl]) { + const name = owner.getChild("Definition"); + if (name) out.push({ from: name.from, to: name.to, className, source: "fallback" }); + } + }, + }); + return out; +} diff --git a/src/editor/lsp/semantic/java/index.ts b/src/editor/lsp/semantic/java/index.ts index e51c743ef0..fe0f6d412d 100644 --- a/src/editor/lsp/semantic/java/index.ts +++ b/src/editor/lsp/semantic/java/index.ts @@ -1,6 +1,7 @@ import type { Tree } from "@lezer/common"; import type { Text } from "@codemirror/state"; import { registerLanguageSemantics, type LanguageSemantics, type SemanticRange } from "../languages"; +import { javaDeclarationRanges } from "./declarations"; /** Contextual keywords `@lezer/java` parses as their own nodes but leaves uncoloured (`var` is coloured, but jdtls sends it as a type). */ const KEYWORD_NODES = new Set(["sealed", "permits", "record", "var"]); @@ -53,6 +54,7 @@ export function javaKeywordRanges(tree: Tree, doc: Text): SemanticRange[] { const javaSemantics: LanguageSemantics = { syntaxRanges: (tree, doc) => javaKeywordRanges(tree, doc), + fallbackRanges: (tree) => javaDeclarationRanges(tree), tokenClasses({ type, classes }) { // Relies on jdtls sending only contextual keywords (`sealed`, `permits`, `record`, `non-sealed`) as `modifier` tokens. if (type === "modifier" || type === "keyword") classes.push("cm-sem-keyword");