From 99fdea5c9a2eeb2ad20df527291b9decfda9171d Mon Sep 17 00:00:00 2001 From: zhzhsx Date: Sat, 28 Nov 2026 20:09:32 +0800 Subject: [PATCH] fix(lsp): request semantic highlighting when a file opens (#6706) The first semantic-tokens request waited for the 600 ms edit debounce, so a newly opened file showed only syntax colours and then changed colour. The view now requests tokens as soon as it opens; edits still refresh through the debounce. Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com> --- src/editor/lsp/semantic/index.ts | 8 +++++--- src/editor/lsp/semantic/typing.test.ts | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/editor/lsp/semantic/index.ts b/src/editor/lsp/semantic/index.ts index 22235c9e77..72cfd5d5c2 100644 --- a/src/editor/lsp/semantic/index.ts +++ b/src/editor/lsp/semantic/index.ts @@ -1,8 +1,8 @@ /** * Semantic highlighting: `lsp_semantic_tokens` → mark decorations with * `cm-sem-*` classes (styled with the --syn-* tokens) layered over the Lezer - * highlighting. Refreshed after edits (debounced); in between, existing marks - * are mapped through the changes. + * highlighting. The view requests tokens as soon as it opens; edits refresh + * them after a debounce, and existing marks are mapped through the changes. * * Semantic highlighting (Color Scheme › Language Defaults, IDEA's rainbow * identifiers): local variables and parameters additionally get @@ -232,8 +232,10 @@ const semanticPlugin = ViewPlugin.fromClass( queueMicrotask(() => { if (!this.destroyed) this.decorate(this.view.state.doc, null); }); - this.refresh(); this.offReady = onLspRefresh("semanticTokens", () => this.refresh()); + // Requests semantic tokens while the view opens. + // Later refreshes still use the debounced `refresh`. + void this.run(); } update(u: ViewUpdate) { const tree = syntaxTree(u.state); diff --git a/src/editor/lsp/semantic/typing.test.ts b/src/editor/lsp/semantic/typing.test.ts index d026eec7c5..3f7f45ad3b 100644 --- a/src/editor/lsp/semantic/typing.test.ts +++ b/src/editor/lsp/semantic/typing.test.ts @@ -35,10 +35,11 @@ describe("semantic colours while typing", () => { }); try { await vi.advanceTimersByTimeAsync(0); + expect(requests).toHaveLength(1); expect(view.dom.querySelector(".cm-sem-python-builtin")).toBeNull(); view.dispatch({ changes: { from: view.state.doc.length, insert: "nt(x)" } }); await vi.advanceTimersByTimeAsync(0); - expect(requests).toHaveLength(0); + expect(requests).toHaveLength(1); expect(view.dom.querySelector(".cm-sem-python-builtin")?.textContent).toBe("print"); expect(view.dom.querySelectorAll(".cm-sem-parameter")).toHaveLength(2); } finally { @@ -57,7 +58,8 @@ describe("semantic colours while typing", () => { }), }); try { - await vi.advanceTimersByTimeAsync(600); + await vi.advanceTimersByTimeAsync(0); + expect(requests).toHaveLength(1); requests.shift()!({ tokenTypes: ["function", "parameter"], tokenModifiers: ["defaultLibrary"],