Compare commits

...
Author SHA1 Message Date
l0ng-ai 022a1a54c5 fix(debugger): keep an expanded value hint open
Expanding an object in the debugger's value hint grows it into a
variables tree that no longer fits above the line, so the tooltip moved
below it, away from the mouse, and closed as soon as the mouse moved
towards it. An expanded hover hint is now shown pinned at the expression:
it stays until Escape, a click in the editor or the next step. Hints
shown with a timeout (selection, quick evaluate, inline values) are kept
once clicked or focused instead of closing after their few seconds.

Refs #3970
2026-11-17 20:38:45 +08:00
3 changed files with 115 additions and 11 deletions

No files matched your search

+9 -2
View File
@@ -35,9 +35,14 @@ const hintField = StateField.define<Hint | null>({
pos: h.pos,
above: true,
strictSide: false,
create: () => {
create: (view) => {
const dom = document.createElement("div");
dom.className = `cm-lsp-hint cm-lsp-hint-${h.tone}`;
// Clicked or focused (a value tree expanded in it): the user works in the hint, so
// it stays until an edit, a caret move or Escape.
const keep = () => clearTimeout(timers.get(view));
dom.addEventListener("pointerdown", keep);
dom.addEventListener("focusin", keep);
dom.setAttribute("role", "status");
if (h.pending) dom.dataset.pending = "true";
Object.assign(dom.dataset, h.data);
@@ -63,7 +68,8 @@ const timers = new WeakMap<EditorView, ReturnType<typeof setTimeout>>();
export function showHint(
view: EditorView,
content: Hint["content"],
opts: { pos?: number; tone?: Hint["tone"]; timeout?: number; pending?: boolean; data?: Hint["data"] } = {},
/** `timeout: null`: the hint stays until an edit, a caret move or Escape. */
opts: { pos?: number; tone?: Hint["tone"]; timeout?: number | null; pending?: boolean; data?: Hint["data"] } = {},
) {
const pos = opts.pos ?? view.state.selection.main.head;
const effects: StateEffect<unknown>[] = [
@@ -72,6 +78,7 @@ export function showHint(
if (!view.state.field(hintField, false)) effects.push(StateEffect.appendConfig.of(hintExtension()));
view.dispatch({ effects });
clearTimeout(timers.get(view));
if (opts.timeout === null) return;
timers.set(
view,
// Also while the view is hidden: it would show the hint again when its tab is shown.
@@ -0,0 +1,55 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import type { DebugSessionId, Variable } from "@/bindings";
import { showHint } from "@/editor/lsp/hint";
import { valueHint } from "../editor/valueTooltip";
vi.mock("../ui/ValueHintTree", () => ({
mountValueTree: (host: HTMLElement) => {
host.append(Object.assign(document.createElement("div"), { className: "tree" }));
return { unmount: () => {} };
},
}));
const session = 7 as DebugSessionId;
const order: Variable = {
name: "order",
value: "Order@12",
typeName: "Order",
variablesReference: 3,
evaluateName: "order",
} as Variable;
let view: EditorView;
const expand = () => {
const button = document.querySelector<HTMLButtonElement>("[aria-label='Expand value']")!;
button.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
button.click();
};
beforeEach(() => {
vi.useFakeTimers();
view = new EditorView({ state: EditorState.create({ doc: "order.total" }), parent: document.body });
});
afterEach(() => {
view.destroy();
vi.useRealTimers();
});
describe("value hint", () => {
it("keeps a hint whose value was expanded past the hint's timeout", () => {
showHint(view, () => valueHint("order", order, session), { pos: 0, timeout: 4000 });
expand();
expect(document.querySelector(".cm-lsp-hint .tree")).not.toBeNull();
vi.advanceTimersByTime(60_000);
expect(document.querySelector(".cm-lsp-hint .tree")).not.toBeNull();
});
it("hands an expanded hover hint to its pin instead of growing in place", () => {
const pin = vi.fn();
showHint(view, () => valueHint("order", order, session, 400, { pin }), { pos: 0 });
expand();
expect(pin).toHaveBeenCalled();
expect(document.querySelector(".tree")).toBeNull();
});
});
+51 -9
View File
@@ -5,11 +5,11 @@
*/
import type { EditorState, Extension } from "@codemirror/state";
import type { DebugSessionId, Variable } from "@/bindings";
import { hoverTooltip, ViewPlugin, type EditorView, type ViewUpdate } from "@codemirror/view";
import { showHint } from "@/editor/lsp/hint";
import { closeHoverTooltips, hoverTooltip, ViewPlugin, type EditorView, type ViewUpdate } from "@codemirror/view";
import { hideHint, showHint } from "@/editor/lsp/hint";
import { workflowSettings } from "@/platform/workflowSettings";
import { navigationModifierHeld } from "@/platform/navigationModifier";
import { activeSession, evaluate } from "../state";
import { activeSession, evaluate, useDebug } from "../state";
import { inlineValue, shownValue } from "../values";
import { tx } from "@/platform/i18n";
import { iconMarkup, selectableText } from "@/ui";
@@ -45,10 +45,20 @@ export function valueHint(
value: Variable,
session: DebugSessionId,
max: number | null = 400,
opts: { expanded?: boolean; pin?: () => void } = {},
): { dom: HTMLElement; destroy: () => void } {
const dom = document.createElement("div");
dom.className = `${styles.valueTooltip} ${selectableText}`;
let tree: { unmount(): void } | null = null;
const expandTree = () => {
dom.classList.replace(selectableText, styles.valueTooltipTree!);
dom.replaceChildren();
tree = mountValueTree(dom, expression, value, session);
};
if (value.variablesReference > 0 && opts.expanded) {
expandTree();
return { dom, destroy: () => tree?.unmount() };
}
if (value.variablesReference > 0) {
// IDEA's value hint: the "+" expands the object into a variables tree in place.
const expand = document.createElement("button");
@@ -60,10 +70,9 @@ export function valueHint(
expand.addEventListener("mousedown", (e) => e.preventDefault());
expand.addEventListener("click", () => {
if (tree) return;
if (opts.pin) return opts.pin();
expand.remove();
dom.classList.replace(selectableText, styles.valueTooltipTree!);
dom.replaceChildren();
tree = mountValueTree(dom, expression, value, session);
expandTree();
});
dom.append(expand);
}
@@ -109,15 +118,48 @@ export function valueTooltip(): Extension {
pos: expr.from,
end: expr.to,
above: true,
create: () => {
return valueHint(expr.text, value, session.id);
},
create: (view) =>
valueHint(expr.text, value, session.id, 400, {
// 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,
});
},
}),
};
},
{ hoverTime: BASE_HOVER },
);
}
/** 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;
const unsubscribe = useDebug.subscribe((s) => {
if (s.stopSeq !== stop || s.sessions[session]?.status !== "paused") hideHint(view);
});
const hint = valueHint(expression, value, session, 400, { expanded: true });
// 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;
e.preventDefault();
hideHint(view);
view.focus();
});
return {
dom: hint.dom,
destroy: () => {
unsubscribe();
hint.destroy();
},
};
}
const SELECTION_DELAY = 400;
const MAX_SELECTION = 200;