diff --git a/src/features/todo/TodoView.test.tsx b/src/features/todo/TodoView.test.tsx index 2407abe2e..9be249e6e 100644 --- a/src/features/todo/TodoView.test.tsx +++ b/src/features/todo/TodoView.test.tsx @@ -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()), + 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(); + 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(); diff --git a/src/features/todo/TodoView.tsx b/src/features/todo/TodoView.tsx index aec895a0a..280c9bab7 100644 --- a/src/features/todo/TodoView.tsx +++ b/src/features/todo/TodoView.tsx @@ -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 { } /** Groups for a tab (exported for the badge and tests). */ -export function useTodoGroups(tab: Tab): TodoGroup[] { +export function useTodoGroups(tab: Tab, openPaths?: ReadonlySet): 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) { 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(null); const [lead, setLead] = useState(null); @@ -447,7 +446,7 @@ export function TodoView() { tab === "file" && !current ? ( ) : tab === "scope" ? ( - scopedScanning || scopedKey !== scopeKey ? ( + scopedScanning || scopedKey !== scope.key ? (
Scanning for TODO items…