From 8d2d2d27e619fa2f39e908673906b589e8852c7a Mon Sep 17 00:00:00 2001
From: audichuang <72485427+audichuang@users.noreply.github.com>
Date: Fri, 4 Dec 2026 23:56:58 +0800
Subject: [PATCH] fix(code-insight): keep Show Usages' scope chooser on Project
Files while that search takes a while (#8064)
* fix(code-insight): keep Show Usages' scope chooser on Project Files while that search takes a while
* test(code-insight): Show Usages widens when invoked again during the chooser's slow Project Files search
---
src/features/code-insight/usages.ts | 4 +-
.../code-insight/usagesPopup.test.tsx | 57 +++++++++++++++++++
2 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/src/features/code-insight/usages.ts b/src/features/code-insight/usages.ts
index 15e4fb2fc5..6d85248e81 100644
--- a/src/features/code-insight/usages.ts
+++ b/src/features/code-insight/usages.ts
@@ -538,7 +538,9 @@ async function searchPopup(c: CaretContext, libraries: boolean, widenable: boole
early = true;
shown ||= !!found;
const so = found ?? { groups: [], note: null };
- useUsagesPopup.setState({ groups: so.groups, note: so.note, loading: true, scope: widenable ? "libraries" : null });
+ // The scope searched: the chooser can have switched a widenable search back to Project Files.
+ const scope = widenable ? (libraries ? "libraries" : "project") : null;
+ useUsagesPopup.setState({ groups: so.groups, note: so.note, loading: true, scope });
};
const timer = setTimeout(openEarly, EARLY_POPUP_DELAY_MS);
const again = shortcutLabel("find.showUsages");
diff --git a/src/features/code-insight/usagesPopup.test.tsx b/src/features/code-insight/usagesPopup.test.tsx
index e1fcd3ecd6..7bd779caab 100644
--- a/src/features/code-insight/usagesPopup.test.tsx
+++ b/src/features/code-insight/usagesPopup.test.tsx
@@ -405,6 +405,63 @@ it("shows the count once a search in the open popup ends with none", async () =>
act(() => dismissUsagesPopup());
});
+it("keeps the chosen scope while the scope chooser's search takes a while", async () => {
+ render();
+ const at = (line: number) => ({ ...loc, range: { start: { line, col: 4 }, end: { line, col: 8 } } });
+ referenceResponse.mockResolvedValueOnce({ locations: [], incomplete: null, librariesSkipped: true });
+ await showUsagesPopup();
+ referenceResponse.mockResolvedValueOnce({ locations: [at(1), at(2)], incomplete: null });
+ await act(() => showUsagesPopup());
+ expect(useUsagesPopup.getState()).toMatchObject({ open: true, scope: "libraries" });
+ let finish!: (value: { locations: (typeof loc)[]; incomplete: null; librariesSkipped: boolean }) => void;
+ referenceResponse.mockReturnValueOnce(new Promise((r) => (finish = r)));
+ await act(async () => setUsagesPopupScope("project"));
+ // Past the early-open delay, the chooser still names the scope being searched.
+ await act(() => new Promise((r) => setTimeout(r, 350)));
+ expect(useUsagesPopup.getState()).toMatchObject({ loading: true, scope: "project" });
+ expect(screen.getByRole("button", { name: "Scope" })).toHaveTextContent("Project Files");
+ await act(async () => finish({ locations: [at(1)], incomplete: null, librariesSkipped: true }));
+ await vi.waitFor(() => expect(useUsagesPopup.getState()).toMatchObject({ loading: false, scope: "project" }));
+ act(() => dismissUsagesPopup());
+});
+
+it("widens when invoked again while the scope chooser's Project Files search takes a while", async () => {
+ render();
+ const at = (line: number) => ({ ...loc, range: { start: { line, col: 4 }, end: { line, col: 8 } } });
+ const libraries = () => (lspReferences.mock.lastCall as unknown[])[5];
+ referenceResponse.mockResolvedValueOnce({ locations: [], incomplete: null, librariesSkipped: true });
+ await showUsagesPopup();
+ referenceResponse.mockResolvedValueOnce({ locations: [at(1), at(2)], incomplete: null });
+ await act(() => showUsagesPopup());
+ referenceResponse.mockReturnValueOnce(new Promise(() => {}));
+ await act(async () => setUsagesPopupScope("project"));
+ await act(() => new Promise((r) => setTimeout(r, 350)));
+ expect(libraries()).toBe(false);
+ referenceResponse.mockResolvedValueOnce({ locations: [at(1), at(2), at(3)], incomplete: null });
+ await act(() => showUsagesPopup());
+ expect(libraries()).toBe(true);
+ expect(useUsagesPopup.getState()).toMatchObject({ loading: false, scope: "libraries" });
+ act(() => dismissUsagesPopup());
+});
+
+it("shows no scope chooser while a search that can't be widened takes a while", async () => {
+ render();
+ const at = (line: number) => ({ ...loc, range: { start: { line, col: 4 }, end: { line, col: 8 } } });
+ referenceResponse.mockResolvedValueOnce({ locations: [at(1), at(2)], incomplete: null });
+ await act(() => showUsagesPopup());
+ expect(useUsagesPopup.getState()).toMatchObject({ open: true, scope: null });
+ // Invoked again: the chooser shows while it searches, and picks Project and Libraries.
+ referenceResponse.mockReturnValueOnce(new Promise(() => {}));
+ await act(() => void showUsagesPopup());
+ let finish!: (value: { locations: (typeof loc)[]; incomplete: null }) => void;
+ referenceResponse.mockReturnValueOnce(new Promise((r) => (finish = r)));
+ await act(async () => setUsagesPopupScope("libraries"));
+ await act(() => new Promise((r) => setTimeout(r, 350)));
+ expect(useUsagesPopup.getState()).toMatchObject({ loading: true, scope: null });
+ await act(async () => finish({ locations: [at(1), at(2)], incomplete: null }));
+ act(() => dismissUsagesPopup());
+});
+
it("searches Project Files, and Project and Libraries when invoked again", async () => {
render();
const at = (line: number) => ({ ...loc, range: { start: { line, col: 4 }, end: { line, col: 8 } } });