mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(todo): include live matches from all files in Open Files scope (#4279)
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { act, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { _provideOpenEditorPaths } from "@/editor/api";
|
||||
import { TodoView, useTodoView, useTodoViewOptions } from "./TodoView";
|
||||
import { useTodos } from "./store";
|
||||
import { setLiveTodos, useTodos } from "./store";
|
||||
import { setLocalChanges } from "@/workbench/contributions";
|
||||
|
||||
vi.mock("./store", async (original) => ({
|
||||
...(await original<typeof import("./store")>()),
|
||||
scanScope: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("TodoView", () => {
|
||||
it("renders grouped items with the keyword emphasised and filters by keyword", () => {
|
||||
act(() =>
|
||||
@@ -60,6 +66,54 @@ describe("TodoView", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("shows new, changed and removed live TODOs in Open Files", () => {
|
||||
const fresh = "/p/src/fresh.ts";
|
||||
const saved = "/p/src/saved.ts";
|
||||
const outside = "/p/src/outside.ts";
|
||||
const openPaths = [fresh, saved];
|
||||
const scoped = new Map([[saved, [{ path: saved, line: 0, col: 3, keyword: "TODO", text: "TODO saved" }]]]);
|
||||
const items = (path: string, text: string) => [{ path, line: 0, col: 3, keyword: "TODO", text: `TODO ${text}` }];
|
||||
const initialView = useTodoView.getState();
|
||||
const initialTodos = useTodos.getState();
|
||||
_provideOpenEditorPaths(() => openPaths);
|
||||
try {
|
||||
act(() => {
|
||||
useTodoView.setState({ tab: "scope", scope: "openFiles" });
|
||||
useTodos.setState({ scoped, scopedKey: JSON.stringify({ kind: "files", paths: openPaths }) });
|
||||
setLiveTodos(outside, items(outside, "outside the scope"));
|
||||
});
|
||||
render(<TodoView />);
|
||||
expect(screen.getByText("saved.ts")).toBeVisible();
|
||||
expect(screen.queryByText("fresh.ts")).toBeNull();
|
||||
|
||||
act(() => setLiveTodos(fresh, items(fresh, "first unsaved match")));
|
||||
expect(screen.getByText("fresh.ts")).toBeVisible();
|
||||
expect(screen.getByText("first unsaved match")).toBeVisible();
|
||||
expect(screen.queryByText("outside.ts")).toBeNull();
|
||||
|
||||
act(() => setLiveTodos(fresh, items(fresh, "edited match")));
|
||||
expect(screen.getByText("edited match")).toBeVisible();
|
||||
expect(screen.queryByText("first unsaved match")).toBeNull();
|
||||
act(() => useTodoView.setState({ scope: "productionFiles" }));
|
||||
expect(screen.queryByText("fresh.ts")).toBeNull();
|
||||
expect(screen.getByText("saved.ts")).toBeVisible();
|
||||
|
||||
act(() => {
|
||||
useTodoView.setState({ scope: "openFiles" });
|
||||
setLiveTodos(saved, []);
|
||||
setLiveTodos(fresh, []);
|
||||
});
|
||||
expect(screen.queryByText("fresh.ts")).toBeNull();
|
||||
expect(screen.queryByText("saved.ts")).toBeNull();
|
||||
} finally {
|
||||
_provideOpenEditorPaths(() => []);
|
||||
act(() => {
|
||||
useTodoView.setState(initialView);
|
||||
useTodos.setState(initialTodos);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("shows the preview pane and does not autoscroll by default, as IDEA", () => {
|
||||
expect(useTodoViewOptions.getState()).toEqual({ preview: true, autoscroll: false });
|
||||
render(<TodoView />);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/** TODO tool window: "Project", "Current File", "Scope Based" and "Changes" tabs, grouped by file. */
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { create } from "zustand";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import clsx from "clsx";
|
||||
import { openEditorPaths, useEditorRegistry } from "@/editor/api";
|
||||
import { executeAction } from "@/platform/actions";
|
||||
@@ -156,7 +157,7 @@ function useChangedPaths(): Set<string> {
|
||||
}
|
||||
|
||||
/** Groups for a tab (exported for the badge and tests). */
|
||||
export function useTodoGroups(tab: Tab): TodoGroup[] {
|
||||
export function useTodoGroups(tab: Tab, openPaths?: ReadonlySet<string>): TodoGroup[] {
|
||||
const scanned = useTodos((s) => s.scanned);
|
||||
const scoped = useTodos((s) => s.scoped);
|
||||
const live = useTodos((s) => s.live);
|
||||
@@ -170,6 +171,8 @@ export function useTodoGroups(tab: Tab): TodoGroup[] {
|
||||
patterns.map((p) => p.keyword.trim()),
|
||||
);
|
||||
if (tab === "scope") {
|
||||
// Open Files membership comes from the tabs, even when the last scan found no matches there.
|
||||
if (openPaths) return groupTodos(mergeLive(scoped, live), { keywords, paths: openPaths });
|
||||
// Open editors' unsaved text, for the files the scope scan found.
|
||||
const inScope = new Map([...live].filter(([path]) => scoped.has(path)));
|
||||
return groupTodos(mergeLive(scoped, inScope), { keywords });
|
||||
@@ -177,26 +180,22 @@ export function useTodoGroups(tab: Tab): TodoGroup[] {
|
||||
const merged = mergeLive(scanned, live);
|
||||
if (tab === "changes") return groupTodos(merged, { keywords, paths: changed });
|
||||
return groupTodos(merged, tab === "file" ? { keywords, path: current } : { keywords });
|
||||
}, [scanned, scoped, live, hidden, patterns, tab, current, changed]);
|
||||
}, [scanned, scoped, live, hidden, patterns, tab, current, changed, openPaths]);
|
||||
}
|
||||
|
||||
/** The Scope Based tab's scope as the backend scope, with a key that changes when it does. */
|
||||
function useScopeQuery() {
|
||||
const scope = useTodoView((s) => s.scope);
|
||||
const custom = useCustomScopeList();
|
||||
// Open tabs change with the active editor (or its count): re-read them then.
|
||||
const openKey = useEditorRegistry((s) =>
|
||||
scope === "openFiles" ? `${s.editors.size}:${s.active?.ctx.path ?? ""}` : "",
|
||||
);
|
||||
const openPaths = useEditorRegistry(useShallow(() => (scope === "openFiles" ? openEditorPaths() : [])));
|
||||
return useMemo(() => {
|
||||
const search = todoSearchScope(scope, custom, scope === "openFiles" ? openEditorPaths() : []);
|
||||
return { search, key: JSON.stringify(search) };
|
||||
}, [scope, custom, openKey]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
const search = todoSearchScope(scope, custom, openPaths);
|
||||
return { search, key: JSON.stringify(search), openPaths: scope === "openFiles" ? new Set(openPaths) : undefined };
|
||||
}, [scope, custom, openPaths]);
|
||||
}
|
||||
|
||||
/** Rescans the chosen scope when it changes, and (debounced) when the project index does. */
|
||||
function useScopeScan(active: boolean) {
|
||||
const { search, key } = useScopeQuery();
|
||||
function useScopeScan(active: boolean, { search, key }: ReturnType<typeof useScopeQuery>) {
|
||||
const scanned = useTodos((s) => s.scanned);
|
||||
const scopedKey = useTodos((s) => s.scopedKey);
|
||||
const fresh = scopedKey === key;
|
||||
@@ -391,9 +390,9 @@ export function TodoView() {
|
||||
const root = useProject((s) => s.project?.root ?? "");
|
||||
const current = useEditorRegistry((s) => s.active?.ctx.path ?? null);
|
||||
const { scanning, error, limitHit, scannedOnce, scopedScanning, scopedKey } = useTodos();
|
||||
const groups = useTodoGroups(tab);
|
||||
useScopeScan(tab === "scope");
|
||||
const scopeKey = useScopeQuery().key;
|
||||
const scope = useScopeQuery();
|
||||
const groups = useTodoGroups(tab, scope.openPaths);
|
||||
useScopeScan(tab === "scope", scope);
|
||||
const showPreview = useTodoViewOptions((o) => o.preview);
|
||||
const tree = useRef<TreeHandle>(null);
|
||||
const [lead, setLead] = useState<Node | null>(null);
|
||||
@@ -447,7 +446,7 @@ export function TodoView() {
|
||||
tab === "file" && !current ? (
|
||||
<EmptyState icon="todo" title="No file is open" />
|
||||
) : tab === "scope" ? (
|
||||
scopedScanning || scopedKey !== scopeKey ? (
|
||||
scopedScanning || scopedKey !== scope.key ? (
|
||||
<div className={styles.loading}>
|
||||
<Spinner size={14} /> <T>Scanning for TODO items…</T>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user