From acdd88f0f79854b79f30e18fe56ef4a81fd8d167 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 08:15:54 +0800 Subject: [PATCH] fix(debugger): group variable and watch context menu actions (#5093) The debugger variable menu groups Set Value with Copy Value and Copy Name, and watch creation and field watchpoints get their own group when available. The watch menu puts Remove Watch and Remove All Watches first, above value and source commands. --- .../debugger/__tests__/toolWindow.test.tsx | 36 +++++++++++---- src/features/debugger/ui/VariablesView.tsx | 46 ++++++++++--------- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/features/debugger/__tests__/toolWindow.test.tsx b/src/features/debugger/__tests__/toolWindow.test.tsx index f38d317ee..0c2a7115b 100644 --- a/src/features/debugger/__tests__/toolWindow.test.tsx +++ b/src/features/debugger/__tests__/toolWindow.test.tsx @@ -348,10 +348,10 @@ describe("debug tool window", () => { describe("variables the adapter computes after the stop", () => { const tick = () => act(() => new Promise((resolve) => setTimeout(resolve, 0))); - const setup = () => { + const setup = (current = session) => { adapterData.library = "Collecting data…"; adapterData.scopes.mockClear(); - useDebug.setState({ sessions: { 1: session }, active: 1, stopSeq: 1, variablesEpoch: 0 }); + useDebug.setState({ sessions: { 1: current }, active: 1, stopSeq: 1, variablesEpoch: 0 }); return render(); }; @@ -396,22 +396,42 @@ describe("variables the adapter computes after the stop", () => { } }); - it("offers Remove All Watches on a watch's context menu", async () => { + it("groups variable and watch context menu actions", async () => { useDebug.setState({ watches: ["count + 1", "label.upper()"] }); const menu = (text: string) => { fireEvent.contextMenu(screen.getByText(text).closest("[role=treeitem]")!); const items = useContextMenuStore.getState().menu?.items ?? []; act(() => hideContextMenu()); - return items.flatMap((i) => ("label" in i && i.label ? [i] : [])); + return items; }; + const layout = (items: ReturnType) => + items.map((item) => (item.kind === "separator" ? "|" : item.label)); try { - setup(); + setup({ ...session, caps: { dataBreakpoints: true } as DebugSession["caps"] }); await screen.findByText("label.upper()"); await screen.findByText("'hi'"); - expect(menu("label").map((i) => i.label)).not.toContain("Remove All Watches"); + expect(layout(menu("label"))).toEqual([ + "Set Value…", + "Copy Value", + "Copy Name", + "|", + "Add to Watches", + "Add Field Watchpoint", + "|", + "Jump to Source", + ]); const items = menu("count + 1"); - expect(items.map((i) => i.label)).toContain("Remove Watch"); - const removeAll = items.find((i) => i.label === "Remove All Watches"); + expect(layout(items)).toEqual([ + "Remove Watch", + "Remove All Watches", + "|", + "Set Value…", + "Copy Value", + "Copy Name", + "|", + "Jump to Source", + ]); + const removeAll = items.find((i) => "label" in i && i.label === "Remove All Watches"); expect(removeAll).toBeTruthy(); act(() => (removeAll as { run: () => void }).run()); expect(useDebug.getState().watches).toEqual([]); diff --git a/src/features/debugger/ui/VariablesView.tsx b/src/features/debugger/ui/VariablesView.tsx index 157fda34e..301e3f6e3 100644 --- a/src/features/debugger/ui/VariablesView.tsx +++ b/src/features/debugger/ui/VariablesView.tsx @@ -390,36 +390,38 @@ export function VariablesView({ }; const onContextMenu = (node: VarNode | null, _sel: Set, e: React.MouseEvent) => { - if (!node || node.kind === "message") return; + if (!node || node.kind === "message" || node.kind === "scope") return; e.preventDefault(); const items: MenuItem[] = []; - if (editable(node)) - items.push( - { label: "Set Value…", icon: "edit", shortcut: "F2", run: () => startEdit(node) }, - { kind: "separator" }, - ); if (node.kind === "watch") items.push( { label: "Remove Watch", icon: "minus", run: () => removeWatch(node.name) }, { label: "Remove All Watches", run: removeAllWatches }, - ); - if (node.kind !== "scope") { - items.push({ label: "Copy Value", icon: "copy", run: () => void copyValue(node) }); - items.push({ label: "Copy Name", run: () => void writeClipboardText(node.name) }); - if (node.kind === "var" && node.evaluateName) - items.push({ label: "Add to Watches", icon: "eye", run: () => addWatch(node.evaluateName!) }); - if (canWatch(node)) - items.push({ - label: "Add Field Watchpoint", - icon: "breakpoint-field", - run: () => void addWatchpointFor(session!, node), - }); - items.push( { kind: "separator" }, - { label: "Jump to Source", shortcut: "F4", run: () => void jumpToDeclaration(node) }, ); - } - if (items.length) showContextMenu(e, items); + if (editable(node)) items.push({ label: "Set Value…", icon: "edit", shortcut: "F2", run: () => startEdit(node) }); + items.push( + { label: "Copy Value", icon: "copy", run: () => void copyValue(node) }, + { label: "Copy Name", run: () => void writeClipboardText(node.name) }, + ); + + const watchActions: MenuItem[] = []; + const expression = node.evaluateName; + if (node.kind === "var" && expression) + watchActions.push({ label: "Add to Watches", icon: "eye", run: () => addWatch(expression) }); + if (canWatch(node)) + watchActions.push({ + label: "Add Field Watchpoint", + icon: "breakpoint-field", + run: () => void addWatchpointFor(session!, node), + }); + if (watchActions.length) items.push({ kind: "separator" }, ...watchActions); + + items.push( + { kind: "separator" }, + { label: "Jump to Source", shortcut: "F4", run: () => void jumpToDeclaration(node) }, + ); + showContextMenu(e, items); }; const onKeyDown = (e: KeyboardEvent) => {