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"],