дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
The Variables view showed the Java debugger's text under its type: args = {String[]} String[0]@9, map = {HashMap} HashMap@10 size=1, and a map entry as 0 = {HashMap$Node} HashMap$Node@31 "a":"b". A JVM object now reads {String[0]@9} [] and {HashMap@10} size = 1, a map entry as "a" -> "b" without its index, and a String without its type. Inline values and the value tooltip use the same text (args: [], map: size = 1). Values from other debuggers keep their text and type.
176 рядки
6.7 KiB
TypeScript
176 рядки
6.7 KiB
TypeScript
/**
|
||
* 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 { hoverTooltip, ViewPlugin, type EditorView, type ViewUpdate } from "@codemirror/view";
|
||
import { showHint } from "@/editor/lsp/hint";
|
||
import { workflowSettings } from "@/platform/workflowSettings";
|
||
import { navigationModifierHeld } from "@/platform/navigationModifier";
|
||
import { activeSession, evaluate } 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,
|
||
): { dom: HTMLElement; destroy: () => void } {
|
||
const dom = document.createElement("div");
|
||
dom.className = `${styles.valueTooltip} ${selectableText}`;
|
||
let tree: { unmount(): void } | null = null;
|
||
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;
|
||
expand.remove();
|
||
dom.classList.replace(selectableText, styles.valueTooltipTree!);
|
||
dom.replaceChildren();
|
||
tree = mountValueTree(dom, expression, value, session);
|
||
});
|
||
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: () => {
|
||
return valueHint(expr.text, value, session.id);
|
||
},
|
||
};
|
||
},
|
||
{ hoverTime: BASE_HOVER },
|
||
);
|
||
}
|
||
|
||
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);
|
||
}
|
||
},
|
||
);
|
||
}
|