diff --git a/src/editor/lsp/semantic/index.ts b/src/editor/lsp/semantic/index.ts index 3009d54bb0..79a8e8b9a4 100644 --- a/src/editor/lsp/semantic/index.ts +++ b/src/editor/lsp/semantic/index.ts @@ -114,6 +114,9 @@ export function rainbowIndex(name: string): number { const RAINBOW_TYPES = new Set(["variable", "parameter"]); const NOT_LOCAL = new Set(["static", "constant", "defaultLibrary", "global", "classScope", "fileScope"]); +/** A name being typed parses as a zero-width recovery node, and a mark decoration may not be empty. */ +const nonEmpty = (r: { from: number; to: number }) => r.from < r.to; + function rangeCoverage(ranges: Array<{ from: number; to: number }>) { ranges.sort((a, b) => a.from - b.from || a.to - b.to); const ends: number[] = []; @@ -202,7 +205,7 @@ export function semanticRanges( ); } out.push(...syntax); - return out.sort((a, b) => a.from - b.from || a.to - b.to); + return out.filter(nonEmpty).sort((a, b) => a.from - b.from || a.to - b.to); } const markCache = new Map(); @@ -305,7 +308,7 @@ const semanticPlugin = ViewPlugin.fromClass( : []; const covered = rangeCoverage(syntax); const retained: Array<{ from: number; to: number }> = []; - const marks = syntax.map((r) => markFor(r.className, r.source).range(r.from, r.to)); + const marks = syntax.filter(nonEmpty).map((r) => markFor(r.className, r.source).range(r.from, r.to)); this.view.state.field(semanticField).between(0, doc.length, (from, to, decoration) => { if ( !decoration.spec.languageSyntax && @@ -324,7 +327,7 @@ const semanticPlugin = ViewPlugin.fromClass( this.rainbow ? (name) => `cm-sem-rainbow-${rainbowIndex(name)}` : undefined, symbols ?? new Map(), )) - if (!covered(r.from, r.to) && !retainedCovered(r.from, r.to)) + if (nonEmpty(r) && !covered(r.from, r.to) && !retainedCovered(r.from, r.to)) marks.push(markFor(r.className, r.source).range(r.from, r.to)); } this.view.dispatch({ effects: setSemantic.of(Decoration.set(marks, true)) }); diff --git a/src/editor/lsp/semantic/java/declarations.test.ts b/src/editor/lsp/semantic/java/declarations.test.ts index 7ae6c6c3fb..3338fc1728 100644 --- a/src/editor/lsp/semantic/java/declarations.test.ts +++ b/src/editor/lsp/semantic/java/declarations.test.ts @@ -23,6 +23,18 @@ 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.each([ + "class A { void (){} }", + "class A { () {} }", + "class A { static int = 1; }", + "class A { int x, = 2; }", + "interface I { int = 1; }", + "record R() { void (){} }", + ])("does not color zero-width recovery nodes in %j", (source) => { + const ranges = javaDeclarationRanges(javaLanguage.parser.parse(source)); + expect(ranges.filter((range) => range.from >= range.to)).toEqual([]); + }); + it("colours declaration names as jdtls does, and nothing else", () => { expect(named(javaDeclarationRanges(tree))).toEqual({ count: "cm-sem-property cm-sem-declaration cm-sem-java-instance-field", diff --git a/src/editor/lsp/semantic/java/declarations.ts b/src/editor/lsp/semantic/java/declarations.ts index 7a022c8234..482bd76386 100644 --- a/src/editor/lsp/semantic/java/declarations.ts +++ b/src/editor/lsp/semantic/java/declarations.ts @@ -50,7 +50,8 @@ export function javaDeclarationRanges(tree: Tree): SemanticRange[] { 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" }); + // A declaration being typed has a zero-width recovery node for its missing name. + if (name && name.from < name.to) out.push({ from: name.from, to: name.to, className, source: "fallback" }); } }, }); diff --git a/src/editor/lsp/semantic/typing.test.ts b/src/editor/lsp/semantic/typing.test.ts index be754f1ea9..9e74bdc06a 100644 --- a/src/editor/lsp/semantic/typing.test.ts +++ b/src/editor/lsp/semantic/typing.test.ts @@ -24,6 +24,31 @@ afterEach(() => { }); describe("semantic colours while typing", () => { + it.each([ + ["TypeScript", "demo.ts", "typescript", "@x.() class C {}", "x"], + ["Java", "A.java", "java", "class A { void run() {} }", "run"], + ])("ignores the empty recovery node of a name deleted in %s", async (name, file, language, source, deleted) => { + const support = await loadSupport(findLanguageDescription(name, file)!); + vi.useFakeTimers(); + const view = new EditorView({ + parent: document.body, + state: EditorState.create({ + doc: source, + extensions: [support!, lspDoc.of({ doc: 93, language } as EditorDocContext), semanticExtension()], + }), + }); + try { + await vi.advanceTimersByTimeAsync(0); + const from = source.indexOf(deleted); + view.dispatch({ changes: { from, to: from + deleted.length } }); + // The syntax colours refresh in a microtask; an empty mark range there was an uncaught RangeError. + await vi.advanceTimersByTimeAsync(0); + expect(view.state.doc.toString()).toBe(source.replace(deleted, "")); + } finally { + view.destroy(); + } + }); + it("colours a newly completed builtin and parameters before any LSP response", async () => { const support = await loadSupport(findLanguageDescription("Python", "demo.py")!); vi.useFakeTimers();