diff --git a/e2e/quickEvaluateLayout.spec.ts b/e2e/quickEvaluateLayout.spec.ts new file mode 100644 index 000000000..9b05141e5 --- /dev/null +++ b/e2e/quickEvaluateLayout.spec.ts @@ -0,0 +1,88 @@ +import type { EditorView } from "@codemirror/view"; +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +test("Quick Evaluate opens an object tree that fits the editor and returns focus on Escape", async ({ + workbench: page, +}) => { + await page.setViewportSize({ width: 445, height: 500 }); + await stubDesktop(page); + await page.evaluate(async () => { + const path = "/dev/acme-api/settlement.py"; + const h = window.__ipc.handlers; + const value = (name: string, text: string, typeName: string, variablesReference = 0) => ({ + name, + value: text, + typeName, + variablesReference, + evaluateName: name, + }); + h.fs_probe = () => ({ size: 48, binary: false, tooLarge: false }); + h.fs_read_text = () => "summary = {}\nsubtotal = 42\n"; + h.debug_evaluate_in = ({ expression }) => + expression === "summary" ? value("summary", "{...}", "dict", 101) : value("subtotal", "42", "int"); + h.debug_variables = ({ reference }) => + reference === 101 + ? [value("audit", "{...}", "dict", 102), value("total", "47.74", "float")] + : Array.from({ length: 22 }, (_, i) => value(`entry${i.toString().padStart(2, "0")}`, String(i), "int")); + h.debug_scopes = () => []; + const servicesModule = "/src/workbench/contributions.ts"; + const { editorService } = await import(/* @vite-ignore */ servicesModule); + await editorService().openFile(path, { focus: true }); + const debugModule = "/src/features/debugger/state.ts"; + const { adoptSession, publishContext } = await import(/* @vite-ignore */ debugModule); + adoptSession(91, { + name: "Checkout", + adapter: "debugpy", + status: "paused", + selectedFrame: 11, + frames: [{ id: 11, name: "checkout", path, line: 0, column: 0, presentationHint: null }], + }); + publishContext(); + }); + const editor = page.locator(".cm-editor"); + const content = editor.locator(".cm-content"); + await expect(content).toBeVisible(); + await content.evaluate((el) => { + const { view } = (el as HTMLElement & { cmTile: { view: EditorView } }).cmTile; + view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "summary = {}\nsubtotal = 42\n" } }); + }); + const evaluate = (line: number) => + content.evaluate(async (el, lineNo) => { + const { view } = (el as HTMLElement & { cmTile: { view: EditorView } }).cmTile; + view.dispatch({ selection: { anchor: view.state.doc.line(lineNo).from + 2 } }); + const actionModule = "/src/platform/actions.ts"; + await (await import(/* @vite-ignore */ actionModule)).executeAction("debug.quickEvaluate"); + }, line); + + await evaluate(1); + const hint = page.locator(".cm-lsp-hint"); + await expect(hint).toBeVisible(); + await expect(hint.getByRole("button", { name: "Expand value" })).toHaveCount(0); + const tree = hint.getByRole("tree", { name: "summary" }); + await expect(tree).toBeFocused(); + await expect(tree.getByText("audit", { exact: true })).toBeVisible(); + await page.setViewportSize({ width: 400, height: 360 }); + await expect + .poll(async () => { + const editorBox = (await editor.boundingBox())!; + const hintBox = (await hint.boundingBox())!; + return hintBox.x >= editorBox.x - 1 && hintBox.x + hintBox.width <= editorBox.x + editorBox.width + 1; + }) + .toBe(true); + + await tree.getByText("audit", { exact: true }).click(); + await tree.press("ArrowRight"); + await expect(tree.getByText("entry00", { exact: true })).toBeVisible(); + expect(await tree.evaluate((el) => el.scrollHeight > el.clientHeight)).toBe(true); + await tree.press("Escape"); + await expect(hint).toBeHidden(); + await expect(content).toBeFocused(); + + await evaluate(2); + await expect(hint.getByRole("tree")).toHaveCount(0); + await expect(hint).toHaveText(/subtotal\s*=\s*42/); + expect(await ipcCalls(page, "debug_evaluate_in")).toEqual([ + { session: 91, frameId: 11, expression: "summary", context: "hover" }, + { session: 91, frameId: 11, expression: "subtotal", context: "hover" }, + ]); +}); diff --git a/src/features/debugger/__tests__/quickEvaluate.test.ts b/src/features/debugger/__tests__/quickEvaluate.test.ts index c040b6d41..fa287d87d 100644 --- a/src/features/debugger/__tests__/quickEvaluate.test.ts +++ b/src/features/debugger/__tests__/quickEvaluate.test.ts @@ -117,6 +117,23 @@ describe("paused editor evaluation", () => { await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledWith(7, 10, text, "hover")); }); + it("ignores an object response that arrives after the debugger resumes", async () => { + session(); + view.dispatch({ selection: { anchor: 16 } }); + type Response = Awaited>; + let reply!: (result: Response) => void; + ipc.debugEvaluateIn.mockImplementationOnce(() => new Promise((resolve) => (reply = resolve))); + const evaluating = executeAction("debug.quickEvaluate"); + await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledOnce()); + useDebug.setState((s) => ({ sessions: { 7: { ...s.sessions[7]!, status: "running" } } })); + reply({ + status: "ok", + data: { name: "order.items", value: "Array(2)", typeName: "Array", variablesReference: 5, evaluateName: null }, + }); + await evaluating; + expect(document.querySelector(".cm-lsp-hint")).toBeNull(); + }); + it("lets a higher priority custom mouse action win the shared gesture", () => { session(); const run = vi.fn(); diff --git a/src/features/debugger/actions.ts b/src/features/debugger/actions.ts index 99d9c6939..9a2a1e2d7 100644 --- a/src/features/debugger/actions.ts +++ b/src/features/debugger/actions.ts @@ -85,7 +85,7 @@ import { } from "./state"; import { focusedDebugView } from "./ui/focusedView"; import { browseFocusedConsoleHistory } from "./ui/expressionHistory"; -import { expressionAt, valueHint } from "./editor/valueTooltip"; +import { expressionAt, showQuickValue } from "./editor/valueTooltip"; import { mouseEvaluateTarget } from "./editor/quickEvaluate"; import { showHint } from "@/editor/lsp/hint"; import { pausedLineTargets, startInlineSmartStep } from "./editor/smartStep"; @@ -215,17 +215,29 @@ async function quickEvaluate(arg?: unknown) { const target = mouseEvaluateTarget(arg); const view = target?.view ?? activeEditor()?.view; if (!view) return; - const sel = view.state.selection.main; - const pos = target?.pos ?? sel.from; + const { doc, selection } = view.state; + const pos = target?.pos ?? selection.main.from; const expression = evaluateSeed(view, target?.pos).trim(); if (!expression) return showHint(view, "Nothing to evaluate at the caret"); const session = activeSession(); + const stop = useDebug.getState().stopSeq; + const stillCurrent = () => { + const current = activeSession(); + return ( + view.dom.isConnected && + view.state.doc === doc && + view.state.selection.eq(selection) && + current?.status === "paused" && + current.id === session?.id && + current.selectedFrame === session?.selectedFrame && + useDebug.getState().stopSeq === stop + ); + }; try { const value = await evaluate(expression, "hover", session); - if (!session) return; - showHint(view, () => valueHint(expression, value, session.id), { pos, timeout: 60_000 }); + if (session && stillCurrent()) showQuickValue(view, pos, expression, value, session.id); } catch (e) { - showHint(view, e instanceof Error ? e.message : String(e), { pos, tone: "error" }); + if (stillCurrent()) showHint(view, e instanceof Error ? e.message : String(e), { pos, tone: "error" }); } } diff --git a/src/features/debugger/editor/editor.module.css b/src/features/debugger/editor/editor.module.css index 8f8d191b7..40cb4f876 100644 --- a/src/features/debugger/editor/editor.module.css +++ b/src/features/debugger/editor/editor.module.css @@ -180,9 +180,22 @@ word-break: break-word; } .valueTooltipTree { - padding: 2px 0; + display: flex; + flex: 1 1 auto; + min-height: 0; + width: min(460px, calc(var(--debug-value-editor-width, 100vw) - var(--space-2))); + max-width: 100%; + height: min(260px, 50vh); + padding: 0; + font-family: var(--font-ui); white-space: normal; } +:global(.cm-lsp-hint):has(> .valueTooltipTree) { + display: flex; + flex-direction: column; + padding: 0; + overflow: hidden; +} .valueExpand { display: inline-flex; align-items: center; diff --git a/src/features/debugger/editor/valueTooltip.ts b/src/features/debugger/editor/valueTooltip.ts index ec53863eb..b374eefd8 100644 --- a/src/features/debugger/editor/valueTooltip.ts +++ b/src/features/debugger/editor/valueTooltip.ts @@ -95,6 +95,17 @@ export function valueHint( return { dom, destroy: () => tree?.unmount() }; } +export function showQuickValue( + view: EditorView, + pos: number, + expression: string, + value: Variable, + session: DebugSessionId, +) { + if (value.variablesReference > 0) showValueTree(view, pos, expression, value, session); + else showHint(view, () => valueHint(expression, value, session), { pos, timeout: 60_000 }); +} + export function valueTooltip(): Extension { return hoverTooltip( async (view, pos) => { @@ -123,13 +134,7 @@ export function valueTooltip(): Extension { // The tree outgrows the hint and may move to the other side of the line, away from the // mouse, which closed the hover tooltip before it could be reached. Like IDEA's value // popup it stays until Escape, a click in the editor or the next step. - pin: () => { - view.dispatch({ effects: closeHoverTooltips }); - showHint(view, () => pinnedTree(view, expr.text, value, session.id), { - pos: expr.from, - timeout: null, - }); - }, + pin: () => showValueTree(view, expr.from, expr.text, value, session.id), }), }; }, @@ -137,6 +142,11 @@ export function valueTooltip(): Extension { ); } +function showValueTree(view: EditorView, pos: number, expression: string, value: Variable, session: DebugSessionId) { + view.dispatch({ effects: closeHoverTooltips }); + showHint(view, () => pinnedTree(view, expression, value, session), { pos, timeout: null }); +} + /** An expanded value tree, closed when its session leaves the stop it was evaluated in. */ function pinnedTree(view: EditorView, expression: string, value: Variable, session: DebugSessionId) { const stop = useDebug.getState().stopSeq; @@ -144,6 +154,20 @@ function pinnedTree(view: EditorView, expression: string, value: Variable, sessi if (s.stopSeq !== stop || s.sessions[session]?.status !== "paused") hideHint(view); }); const hint = valueHint(expression, value, session, 400, { expanded: true }); + let width = 0; + let resizeFrame = 0; + const updateWidth = () => { + const next = view.dom.clientWidth; + if (!next || next === width) return; + width = next; + hint.dom.style.setProperty("--debug-value-editor-width", `${width}px`); + }; + updateWidth(); + const resize = new ResizeObserver(() => { + cancelAnimationFrame(resizeFrame); + resizeFrame = requestAnimationFrame(updateWidth); + }); + resize.observe(view.dom); // The tree has the focus, so the editor's Escape doesn't reach the hint. hint.dom.addEventListener("keydown", (e) => { if (e.key !== "Escape" || e.defaultPrevented) return; @@ -154,6 +178,8 @@ function pinnedTree(view: EditorView, expression: string, value: Variable, sessi return { dom: hint.dom, destroy: () => { + resize.disconnect(); + cancelAnimationFrame(resizeFrame); unsubscribe(); hint.destroy(); }, diff --git a/src/features/debugger/ui/VariablesView.module.css b/src/features/debugger/ui/VariablesView.module.css index a95c84e46..1c1438ab7 100644 --- a/src/features/debugger/ui/VariablesView.module.css +++ b/src/features/debugger/ui/VariablesView.module.css @@ -175,8 +175,9 @@ /* Expanded value hint in the editor. */ .hint { display: flex; + flex: 1; flex-direction: column; min-width: 0; - width: 460px; - height: 260px; + min-height: 0; + padding-block: var(--space-1); }