diff --git a/src/editor/fileMoves.ts b/src/editor/fileMoves.ts index 08e8747f0e..c74147e803 100644 --- a/src/editor/fileMoves.ts +++ b/src/editor/fileMoves.ts @@ -1,7 +1,7 @@ /** IDEA keeps editors on files that a refactoring moves or renames: they reopen at the new paths. */ import type { FileMove } from "@/bindings"; import { openEditorPaths } from "@/editor/api"; -import { notifyFilesMoved, trackFileMove } from "@/platform/fileOps"; +import { movedPath, notifyFilesMoved, trackFileMove } from "@/platform/fileOps"; import { emitLocalIdeEvent } from "@/platform/ipc"; import { isSameOrInside } from "@/platform/paths"; import { useProject } from "@/platform/project"; @@ -15,13 +15,6 @@ export async function saveAllQuietly(editor: { saveAll(): Promise }) { } } -/** `path` after `moves`. */ -export function movedPath(path: string, moves: readonly FileMove[]): string { - let p = path; - for (const m of moves) if (isSameOrInside(p, m.from)) p = m.to + p.slice(m.from.length); - return p; -} - /** Moves in the opposite direction, last first: what Undo of `moves` does. */ export function reversedMoves(moves: readonly FileMove[]): FileMove[] { return [...moves].reverse().map((m) => ({ from: m.to, to: m.from })); diff --git a/src/features/debugger/__tests__/breakpointMoves.test.ts b/src/features/debugger/__tests__/breakpointMoves.test.ts new file mode 100644 index 0000000000..cc0b30d4b3 --- /dev/null +++ b/src/features/debugger/__tests__/breakpointMoves.test.ts @@ -0,0 +1,178 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { reversedMoves, withEditorsFollowing } from "@/editor/fileMoves"; +import { useProject } from "@/platform/project"; +import type { Breakpoint, FileMove } from "@/platform/ipc"; +import { activate } from "../index"; +import { + addBreakpoint, + breakpointsOf, + consumeTemporary, + flushBreakpoints, + loadBreakpoints, + setMuted, + useBreakpoints, +} from "../breakpoints/store"; +import { + addFieldWatchpoint, + addMethodBreakpoint, + loadSpecialBreakpoints, + specialAt, + useSpecialBreakpoints, +} from "../breakpoints/special"; +import { openBreakpointPopover, useOverlays } from "../ui/popoverStore"; + +const ipc = vi.hoisted(() => ({ + debugSetBreakpoints: vi.fn(async (_path: string, list: Breakpoint[]) => ({ + status: "ok", + data: list.map((b) => ({ line: b.line, verified: true, message: null })), + })), + debugReplaceBreakpoints: vi.fn(async (_files: Record) => ({ status: "ok", data: null })), + debugSetFunctionBreakpoints: vi.fn(async () => ({ status: "ok", data: [] })), + debugSetDataBreakpoints: vi.fn(async () => ({ status: "ok", data: [] })), +})); +vi.mock("@/platform/ipc", async (original) => { + const real = await original(); + return { + ...real, + isTauri: () => true, + onIdeEvent: () => () => {}, + commands: { ...real.commands, ...ipc }, + }; +}); + +const root = "/p"; +const from = `${root}/src/Order.java`; +const to = `${root}/src/Purchase.java`; + +/** An explicit rename / move, as the project tree and refactorings run it. */ +const move = (...moves: FileMove[]) => + withEditorsFollowing( + () => Promise.resolve(), + () => moves, + ); +const files = () => Object.keys(useBreakpoints.getState().byFile).sort(); +const reloaded = () => { + loadBreakpoints(root); + loadSpecialBreakpoints(root); +}; + +describe("breakpoints follow renamed and moved files", () => { + let dispose: () => void; + + beforeEach(() => { + localStorage.clear(); + useProject.setState({ project: { root } as never }); + dispose = activate(); + }); + afterEach(() => { + dispose(); + useProject.setState({ project: null }); + loadBreakpoints(null); + loadSpecialBreakpoints(null); + vi.clearAllMocks(); + }); + + it("keeps a breakpoint's properties, its status and the stored list", async () => { + const bp = addBreakpoint(from, 12, { condition: "total > 10", enabled: false }); + addBreakpoint(from, 20, { temporary: true, suspend: false, logHit: true }); + useBreakpoints.setState({ status: { [bp.id]: { verified: true, message: null, line: 13 } } }); + const before = breakpointsOf(from); + await move({ from, to }); + expect(files()).toEqual([to]); + expect(breakpointsOf(to)).toEqual(before); + expect(useBreakpoints.getState().status[bp.id]).toEqual({ verified: true, message: null, line: 13 }); + expect(consumeTemporary(to, 20)).toBe(true); + reloaded(); + expect(breakpointsOf(to)).toEqual([bp]); + }); + + it("follows a folder, and only it", async () => { + addBreakpoint(`${root}/src/shop/a/Order.java`, 1); + addBreakpoint(`${root}/src/shop-old/Order.java`, 2); + await move({ from: `${root}/src/shop`, to: `${root}/src/store` }); + expect(files()).toEqual([`${root}/src/shop-old/Order.java`, `${root}/src/store/a/Order.java`]); + }); + + it("follows a file through several moves of one refactoring, and back on Undo", async () => { + const bp = addBreakpoint(from, 3); + const moves = [ + { from: `${root}/src`, to: `${root}/lib` }, + { from: `${root}/lib/Order.java`, to: `${root}/lib/Purchase.java` }, + ]; + await move(...moves); + expect(files()).toEqual([`${root}/lib/Purchase.java`]); + await move(...reversedMoves(moves)); + expect(breakpointsOf(from)).toEqual([bp]); + expect(files()).toEqual([from]); + }); + + it("moves method breakpoints and field watchpoints, and leaves those without a source", async () => { + addMethodBreakpoint("shop.Order#pay", { path: from, line: 4 }); + addFieldWatchpoint({ dataId: "shop.Order#total", label: "Order.total" }, { path: from, line: 2 }); + addFieldWatchpoint({ dataId: "0x10", label: "value" }); + await move({ from, to }); + expect(specialAt(from)).toEqual([]); + expect(specialAt(to).map((b) => [b.kind, b.line])).toEqual([ + ["method", 4], + ["field", 2], + ]); + reloaded(); + expect(specialAt(to)).toHaveLength(2); + expect(useSpecialBreakpoints.getState().fields.map((f) => f.path)).toEqual([to, null]); + }); + + it("keeps one breakpoint per line of a path that had some: the moved file's", async () => { + const moved = [addBreakpoint(from, 5, { condition: "moved" }), addBreakpoint(from, 9)]; + addBreakpoint(to, 5, { condition: "stale" }); + const other = addBreakpoint(to, 7); + await move({ from, to }); + expect(breakpointsOf(to)).toEqual([moved[0], other, moved[1]]); + expect(files()).toEqual([to]); + }); + + it("a method breakpoint on the same line of the new path keeps working without a source line", async () => { + addMethodBreakpoint("shop.Order#pay", { path: from, line: 4 }); + addMethodBreakpoint("shop.Purchase#pay", { path: to, line: 4 }); + await move({ from, to }); + expect(useSpecialBreakpoints.getState().methods.map((m) => [m.name, m.path, m.line])).toEqual([ + ["shop.Order#pay", to, 4], + ["shop.Purchase#pay", null, null], + ]); + }); + + it("clears the old path in running sessions and sets the new one, muted or not", async () => { + const bp = addBreakpoint(from, 12, { suspend: false, logHit: true }); + setMuted(true); + await flushBreakpoints(); + vi.clearAllMocks(); + useBreakpoints.setState({ status: {} }); + await move({ from, to }); + await vi.waitFor(() => expect(useBreakpoints.getState().status[bp.id]?.verified).toBe(true)); + const sent = { condition: null, hitCondition: null, logMessage: "Breakpoint reached at Purchase.java:13" }; + expect(ipc.debugSetBreakpoints.mock.calls).toEqual([ + [from, []], + [to, [{ line: 12, enabled: false, ...sent }]], + ]); + expect(useBreakpoints.getState().muted).toBe(true); + // A session that starts later gets no trace of the old path. + await flushBreakpoints(); + expect(Object.keys(ipc.debugReplaceBreakpoints.mock.calls.at(-1)![0])).toEqual([to]); + }); + + it("keeps the open properties popover on its breakpoint", async () => { + const bp = addBreakpoint(from, 1); + openBreakpointPopover(from, bp.id, { x: 1, y: 2 }); + await move({ from, to }); + expect(useOverlays.getState().popover).toMatchObject({ path: to, id: bp.id, x: 1, y: 2 }); + useOverlays.setState({ popover: null }); + }); + + it("does nothing for files without breakpoints", async () => { + addBreakpoint(from, 1); + await flushBreakpoints(); + vi.clearAllMocks(); + const state = useBreakpoints.getState().byFile; + await move({ from: `${root}/README.md`, to: `${root}/docs.md` }); + expect(useBreakpoints.getState().byFile).toBe(state); + }); +}); diff --git a/src/features/debugger/breakpoints/special.ts b/src/features/debugger/breakpoints/special.ts index 81aa819bae..bda5c4268a 100644 --- a/src/features/debugger/breakpoints/special.ts +++ b/src/features/debugger/breakpoints/special.ts @@ -5,8 +5,9 @@ * them. Entries remember the source line they were created on for the gutter icon. */ import { create } from "zustand"; +import { movedPath } from "@/platform/fileOps"; import { useProject } from "@/platform/project"; -import { call, commands, isTauri, type DataBreakpoint, type FunctionBreakpoint } from "@/platform/ipc"; +import { call, commands, isTauri, type DataBreakpoint, type FileMove, type FunctionBreakpoint } from "@/platform/ipc"; import { newBreakpointId, type BreakpointStatus } from "./model"; import { useBreakpoints } from "./store"; @@ -228,6 +229,25 @@ export function removeSpecialIn(path: string) { setLists({ methods: methods.filter((m) => m.path !== path), fields: fields.filter((f) => f.path !== path) }); } +/** + * Files were renamed or moved: the source lines follow. One that was on the same line of the new + * path keeps working without a source line, as after an edit that merges two lines. + */ +export function moveSpecial(moves: readonly FileMove[]) { + const { methods, fields } = useSpecialBreakpoints.getState(); + const follow = (list: T[]): T[] => { + const next = list.map((b) => (b.path === null ? b : { ...b, path: movedPath(b.path, moves) })); + const moved = next.filter((b, i) => b.path !== list[i]!.path); + return next.map((b) => + !moved.includes(b) && moved.some((m) => m.path === b.path && m.line === b.line) + ? { ...b, path: null, line: null } + : b, + ); + }; + if (![...methods, ...fields].some((b) => b.path !== null && movedPath(b.path, moves) !== b.path)) return; + setLists({ methods: follow(methods), fields: follow(fields) }); +} + /** Method breakpoints / watchpoints whose gutter icon is in `path`. */ export function specialAt(path: string): SpecialBreakpoint[] { const { methods, fields } = useSpecialBreakpoints.getState(); diff --git a/src/features/debugger/breakpoints/store.ts b/src/features/debugger/breakpoints/store.ts index 40a8ccf9fb..90f1633ef8 100644 --- a/src/features/debugger/breakpoints/store.ts +++ b/src/features/debugger/breakpoints/store.ts @@ -4,8 +4,9 @@ * The backend keeps them across sessions and returns verification states. */ import { create } from "zustand"; +import { movedPath } from "@/platform/fileOps"; import { useProject } from "@/platform/project"; -import { call, commands, isTauri } from "@/platform/ipc"; +import { call, commands, isTauri, type FileMove } from "@/platform/ipc"; import { makeBreakpoint, sameEntries, @@ -253,6 +254,24 @@ export function setFileBreakpoints(path: string, list: BreakpointEntry[]) { setFile(path, list); } +/** Files were renamed or moved: their breakpoints follow. On a line both paths had one, the moved file's stays. */ +export function moveBreakpoints(moves: readonly FileMove[]) { + const { byFile } = useBreakpoints.getState(); + const paths = Object.keys(byFile); + const moved = paths.filter((path) => movedPath(path, moves) !== path); + if (!moved.length) return; + const next = Object.fromEntries(paths.filter((p) => !moved.includes(p)).map((p) => [p, byFile[p]!])); + for (const from of moved) { + const to = movedPath(from, moves); + const list = byFile[from]!; + const kept = (next[to] ?? []).filter((b) => !list.some((m) => m.line === b.line)); + next[to] = sortByLine([...kept, ...list]); + } + useBreakpoints.setState({ byFile: next }); + persist(); + for (const path of new Set([...moved, ...moved.map((p) => movedPath(p, moves))])) scheduleSync(path); +} + /** * A suspend at `path:line` hit the temporary breakpoint there: remove it, like IDEA. * Returns whether one was removed. diff --git a/src/features/debugger/index.ts b/src/features/debugger/index.ts index 2159f85c9e..c5888a72e9 100644 --- a/src/features/debugger/index.ts +++ b/src/features/debugger/index.ts @@ -4,6 +4,7 @@ * execution point + inline values, Evaluate / View Breakpoints dialogs. */ import { lazyComponent, lazyWhen } from "@/ui"; +import { onFilesMoved } from "@/platform/fileOps"; import { onIdeEvent } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { selectionValueTooltip, valueTooltip } from "./editor/valueTooltip"; @@ -20,14 +21,14 @@ import { import { registerDebugActions } from "./actions"; import { registerDevToolsWindows } from "./flutter/devTools"; import { registerDebugTabActions } from "./tabActions"; -import { loadBreakpoints } from "./breakpoints/store"; +import { loadBreakpoints, moveBreakpoints } from "./breakpoints/store"; import { loadExceptionBreakpoints } from "./breakpoints/exceptions"; import { loadExceptionClasses } from "./breakpoints/exceptionClasses"; import { loadJsBreakpoints, watchPageSessions } from "./js/eventBreakpoints"; import { watchNetworkSessions } from "./js/networkSessions"; import { loadResponseOverrides, watchOverrideSessions } from "./js/responseOverrides"; import { watchProfiledSessions } from "./js/profile"; -import { loadSpecialBreakpoints } from "./breakpoints/special"; +import { loadSpecialBreakpoints, moveSpecial } from "./breakpoints/special"; import { breakpointGutter } from "./editor/breakpointGutter"; import { executionPoint } from "./editor/execution"; import { runToCursorInlay } from "./editor/runToCursor"; @@ -46,7 +47,7 @@ import { stopSession, useDebug, } from "./state"; -import { useOverlays } from "./ui/popoverStore"; +import { moveBreakpointPopover, useOverlays } from "./ui/popoverStore"; import { REMOTE_TYPES } from "./remote"; import { jsBrowserType } from "./browser"; import { forgetInstances } from "./memory/instancesState"; @@ -167,6 +168,11 @@ export function activate(): () => void { installSmartStepPicking(), installFlutterHotReloadOnSave(), installBuildHotSwap(), + onFilesMoved((moves) => { + moveBreakpoints(moves); + moveSpecial(moves); + moveBreakpointPopover(moves); + }), onIdeEvent("debug", (e) => { if (e.event.kind === "hotCodeReplace") onHotCodeReplace(e.session, e.event); else handleDebugEvent(e.session, e.event); diff --git a/src/features/debugger/ui/popoverStore.ts b/src/features/debugger/ui/popoverStore.ts index 87a4b70860..a6586585c1 100644 --- a/src/features/debugger/ui/popoverStore.ts +++ b/src/features/debugger/ui/popoverStore.ts @@ -1,6 +1,14 @@ /** Open/close state of the debugger's overlays (rendered by `Overlays.tsx`). */ import { create } from "zustand"; -import type { AttachableProcess, DebugSessionId, RunConfig, StepInTarget, StreamChainInfo } from "@/platform/ipc"; +import { movedPath } from "@/platform/fileOps"; +import type { + AttachableProcess, + DebugSessionId, + FileMove, + RunConfig, + StepInTarget, + StreamChainInfo, +} from "@/platform/ipc"; /** A Smart Step Into target placed on the paused line (UTF-16 offsets within the line). */ export interface InlineStepTarget { @@ -64,6 +72,12 @@ export function closeBreakpointPopover() { useOverlays.setState({ popover: null }); } +/** The popover stays on its breakpoint when the file is renamed or moved. */ +export function moveBreakpointPopover(moves: readonly FileMove[]) { + const { popover } = useOverlays.getState(); + if (popover) useOverlays.setState({ popover: { ...popover, path: movedPath(popover.path, moves) } }); +} + export function openTableView(expression: string, session: DebugSessionId) { useOverlays.setState({ tableView: { expression, session } }); } diff --git a/src/features/project-tree/fileRefactor.test.tsx b/src/features/project-tree/fileRefactor.test.tsx index c65416c463..bb6a02a6f2 100644 --- a/src/features/project-tree/fileRefactor.test.tsx +++ b/src/features/project-tree/fileRefactor.test.tsx @@ -1,8 +1,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import type { FileMove, WorkspaceEdit } from "@/bindings"; -import { movedPath } from "@/editor/fileMoves"; -import { onFilesMoved } from "@/platform/fileOps"; +import { movedPath, onFilesMoved } from "@/platform/fileOps"; import { provideNotificationService } from "@/workbench/contributions"; import { lastFileRefactoring, diff --git a/src/platform/fileOps.ts b/src/platform/fileOps.ts index ba6375ab78..0b1384a655 100644 --- a/src/platform/fileOps.ts +++ b/src/platform/fileOps.ts @@ -4,6 +4,7 @@ * this for Settings › Version Control › Confirmation. */ import type { FileMove } from "./ipc"; +import { isSameOrInside } from "./paths"; export interface FileOperation { kind: "created" | "deleted"; @@ -28,6 +29,13 @@ export function onFileOperation(listener: Listener): () => void { return () => listeners.delete(listener); } +/** `path` after `moves`. */ +export function movedPath(path: string, moves: readonly FileMove[]): string { + let p = path; + for (const m of moves) if (isSameOrInside(p, m.from)) p = m.to + p.slice(m.from.length); + return p; +} + const moveListeners = new Set<(moves: readonly FileMove[]) => void | Promise>(); const pendingMoves = new Map, string | null>();