Files
openidea/src/features/debugger/editor/valueTooltip.ts
T
l0ng-ai 83a949ffe9 fix(debugger): keep an expanded value hint open (#4004)
Expanding an object in the debugger's value hint grew it into a variables tree that no longer fit above the line, so the tooltip moved below it, away from the mouse, and the hover tooltip closed as soon as the mouse moved towards it. An expanded hover hint is now shown pinned at the expression until Escape, a click in the editor, an edit or the next step; hints shown with a timeout (selection, quick evaluate, inline values) are kept once clicked or focused.

Refs #3970
2026-11-17 20:39:19 +08:00

218 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Settings › Debugger › Data Views › "Show value tooltip on mouse hover":
* while suspended, hovering an expression (or the selection under the mouse)
* evaluates it in the selected frame.
*/
import type { EditorState, Extension } from "@codemirror/state";
import type { DebugSessionId, Variable } from "@/bindings";
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, useDebug } from "../state";
import { inlineValue, shownValue } from "../values";
import { tx } from "@/platform/i18n";
import { iconMarkup, selectableText } from "@/ui";
import { mountValueTree } from "../ui/ValueHintTree";
import styles from "./editor.module.css";
const BASE_HOVER = 100;
const IDENT = /[\p{L}\p{N}_$]/u;
/** Identifier (with `a.b.c` member access) around `pos`, or the selection containing it. */
export function expressionAt(view: EditorView, pos: number): { from: number; to: number; text: string } | null {
const { state } = view;
const sel = state.selection.main;
if (!sel.empty && pos >= sel.from && pos <= sel.to) {
const text = state.sliceDoc(sel.from, sel.to).trim();
return text && !text.includes("\n") ? { from: sel.from, to: sel.to, text } : null;
}
const line = state.doc.lineAt(pos);
const t = line.text;
let start = pos - line.from;
let end = start;
if (!IDENT.test(t[end] ?? "") && !IDENT.test(t[start - 1] ?? "")) return null;
while (end < t.length && IDENT.test(t[end]!)) end++;
while (start > 0 && (IDENT.test(t[start - 1]!) || (t[start - 1] === "." && IDENT.test(t[start - 2] ?? "")))) start--;
const text = t.slice(start, end);
if (!text || /^\d/.test(text)) return null;
return { from: line.from + start, to: line.from + end, text };
}
/** IDEA's value hint: `expression = value {type}`, objects expandable into a variables tree in place. */
export function valueHint(
expression: string,
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");
expand.type = "button";
expand.className = styles.valueExpand!;
expand.setAttribute("aria-label", tx("Expand value"));
expand.setAttribute("aria-expanded", "false");
expand.innerHTML = iconMarkup("chevron-right", 12) ?? "";
expand.addEventListener("mousedown", (e) => e.preventDefault());
expand.addEventListener("click", () => {
if (tree) return;
if (opts.pin) return opts.pin();
expand.remove();
expandTree();
});
dom.append(expand);
}
const name = document.createElement("span");
name.className = styles.valueName!;
name.textContent = `${expression} = `;
const shown = shownValue(value.value, value.typeName);
// A JVM object's `{Type@id}` leads its value; other types follow it.
const leading = shown.label?.includes("@") ?? false;
const type = (text: string) => {
const span = document.createElement("span");
span.className = styles.valueType!;
span.textContent = text;
return span;
};
dom.append(name);
if (leading) dom.append(type(`${shown.label} `));
dom.append(max === null ? shown.text : inlineValue(shown.text, max));
if (shown.label && !leading) dom.append(type(` ${shown.label}`));
return { dom, destroy: () => tree?.unmount() };
}
export function valueTooltip(): Extension {
return hoverTooltip(
async (view, pos) => {
const dbg = workflowSettings().debugger;
// ⌘/Ctrl-hover shows the navigation info instead (closed on press by the LSP hover).
if (!dbg.valueTooltip || navigationModifierHeld()) return null;
const session = activeSession();
if (!session || session.status !== "paused") return null;
const expr = expressionAt(view, pos);
if (!expr) return null;
if (dbg.valueTooltipDelay > BASE_HOVER)
await new Promise((r) => setTimeout(r, dbg.valueTooltipDelay - BASE_HOVER));
let value;
try {
value = await evaluate(expr.text, "hover", session);
} catch {
return null;
}
if (navigationModifierHeld()) return null;
return {
pos: expr.from,
end: expr.to,
above: true,
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;
/** The selected text when it can be evaluated: one non-blank line, not too long. */
export function selectionExpression(state: EditorState): { from: number; text: string } | null {
const sel = state.selection;
if (sel.ranges.length !== 1 || sel.main.empty) return null;
const text = state.sliceDoc(sel.main.from, sel.main.to).trim();
if (!text || text.length > MAX_SELECTION || text.includes("\n")) return null;
return { from: sel.main.from, text };
}
/**
* Settings › Debugger › Data Views › "Show value tooltip on code selection": while
* suspended, selecting an expression shows its value once the selection settles.
*/
export function selectionValueTooltip(): Extension {
return ViewPlugin.fromClass(
class {
private timer: ReturnType<typeof setTimeout> | undefined;
private seq = 0;
constructor(readonly view: EditorView) {}
update(u: ViewUpdate) {
if (!u.selectionSet) return;
clearTimeout(this.timer);
this.seq++;
if (!u.transactions.some((tr) => tr.isUserEvent("select"))) return;
const seq = this.seq;
this.timer = setTimeout(() => void this.show(seq), SELECTION_DELAY);
}
private async show(seq: number) {
if (!workflowSettings().debugger.valueTooltipOnSelection) return;
const session = activeSession();
if (!session || session.status !== "paused") return;
const expr = selectionExpression(this.view.state);
if (!expr) return;
let value;
try {
value = await evaluate(expr.text, "hover", session);
} catch {
return;
}
// The selection moved on while evaluating.
if (seq !== this.seq) return;
showHint(this.view, () => valueHint(expr.text, value, session.id), { pos: expr.from, timeout: 60_000 });
}
destroy() {
clearTimeout(this.timer);
}
},
);
}