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
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-12-04 23:56:58 +08:00
1 джерело 72db7ecb97
коміт 8d2d2d27e6
2 files changed
+60 -1

No files matched your search

+3 -1
Переглянути файл
@@ -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");
+57
Переглянути файл
@@ -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(<UsagesPopup />);
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(<UsagesPopup />);
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(<UsagesPopup />);
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(<UsagesPopup />);
const at = (line: number) => ({ ...loc, range: { start: { line, col: 4 }, end: { line, col: 8 } } });