From d90b2b7bbe182d142aef06e1e6f65a8f1a4de811 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:21:27 +0800 Subject: [PATCH] fix(editor): show usage counts that came back incomplete for a moment (#2857) Servers call an empty references answer incomplete while any of their progress runs, and jdtls reports progress for a few milliseconds around many requests with no settle afterwards, so a usage lens whose count came back incomplete could stay hidden. An incomplete count is now asked again on the next passes, up to three times, before waiting for the server to settle as before. Java lens evals: 4 of 36 runs lost a lens before, 0 of 36 after. --- src/editor/lsp/codeLens.test.ts | 23 +++++++++++++++++++++++ src/editor/lsp/codeLens.ts | 22 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/editor/lsp/codeLens.test.ts b/src/editor/lsp/codeLens.test.ts index 1650b0368..1ad5773b1 100644 --- a/src/editor/lsp/codeLens.test.ts +++ b/src/editor/lsp/codeLens.test.ts @@ -141,6 +141,29 @@ describe("background usage counts", () => { expect(titles(view)).toEqual(["2 usages"]); }); + it("asks again for a count that came back incomplete during a short busy spell", async () => { + // jdtls reports progress for a few milliseconds around many requests: no settle follows it. + ipc.lspReferences.mockResolvedValueOnce(refs(0, "indexing")); + const view = editor(); + await settle(); + expect(titles(view)).toEqual([]); + await settle(); + expect(ipc.lspReferences).toHaveBeenCalledTimes(2); + expect(titles(view)).toEqual(["2 usages"]); + }); + + it("stops asking again while the server keeps saying it is indexing, until it settles", async () => { + ipc.lspReferences.mockResolvedValue(refs(0, "indexing")); + const view = editor(); + for (let i = 0; i < 10; i++) await settle(); + expect(ipc.lspReferences).toHaveBeenCalledTimes(4); + expect(titles(view)).toEqual([]); + ipc.lspReferences.mockResolvedValue(refs(2)); + emitLocalIdeEvent({ type: "lspRefresh", kind: "codeLens", server: 1 }); + await settle(); + expect(titles(view)).toEqual(["2 usages"]); + }); + it("cancels obsolete requests immediately on edits and ignores their late replies", async () => { const old = deferred>(); ipc.lspReferences.mockReturnValueOnce(old.promise); diff --git a/src/editor/lsp/codeLens.ts b/src/editor/lsp/codeLens.ts index 5c8c8c886..2dcd648dc 100644 --- a/src/editor/lsp/codeLens.ts +++ b/src/editor/lsp/codeLens.ts @@ -36,6 +36,12 @@ const MAX_PER_PASS = 40; const CONCURRENCY = 4; /** Lines above/below the viewport that are counted too, so scrolling feels instant. */ const MARGIN_LINES = 10; +/** + * Passes that ask again for a count that came back incomplete. Servers call an empty answer + * incomplete while any of their progress runs, and jdtls reports progress for a few + * milliseconds around many requests without settling afterwards. + */ +const INCOMPLETE_RETRIES = 3; const usagesBackoff = featureBackoff(); export interface LensTarget { @@ -116,6 +122,8 @@ class UsagesLensPlugin { private pending = new Set(); /** Keys whose count came back incomplete (the server was indexing): not shown, asked again once it settles. */ private incomplete = new Set(); + /** Times each key's count came back incomplete, for the passes that ask again before it settles. */ + private retries = new Map(); private unsubscribe: () => void; private offReady: () => void; private offSettled: () => void; @@ -131,11 +139,13 @@ class UsagesLensPlugin { this.offReady = onLspRefresh("codeLens", () => { this.cache.clear(); this.incomplete.clear(); + this.retries.clear(); this.refresh(); }); this.offSettled = onServerSettled(() => { if (!this.incomplete.size) return; this.incomplete.clear(); + this.retries.clear(); this.refresh(); }); this.offBlame = onBlameInvalidated(() => this.refresh()); @@ -176,6 +186,7 @@ class UsagesLensPlugin { if (this.cacheDoc !== text) { this.cache = new Map(); this.incomplete = new Set(); + this.retries = new Map(); this.cacheDoc = text; } try { @@ -193,6 +204,7 @@ class UsagesLensPlugin { const key = (t: LensTarget) => `${t.line}:${t.col}:${t.name}`; const todo = usages ? visible.filter((t) => !this.cache.has(key(t)) && !this.incomplete.has(key(t))) : []; let unavailable = false; + let retry = false; for (let i = 0; i < todo.length && !unavailable; i += CONCURRENCY) { await Promise.all( todo.slice(i, i + CONCURRENCY).map(async (t) => { @@ -213,8 +225,13 @@ class UsagesLensPlugin { ); }, wanted); if (wanted()) { - if (incomplete) this.incomplete.add(key(t)); - else this.cache.set(key(t), refs.length); + if (!incomplete) this.cache.set(key(t), refs.length); + else { + const tries = (this.retries.get(key(t)) ?? 0) + 1; + this.retries.set(key(t), tries); + if (tries > INCOMPLETE_RETRIES) this.incomplete.add(key(t)); + else retry = true; + } } } catch (e) { if (wanted() && isUnavailable(e)) unavailable = true; @@ -226,6 +243,7 @@ class UsagesLensPlugin { if (!wanted()) return; } if (unavailable) usagesBackoff.mark(doc.doc); + if (retry) this.schedule(); const lines = await blame; if (!wanted()) return; const lenses: CodeLens[] = [];