Compare commits

...
Author SHA1 Message Date
l0ng-ai 5b151180de fix(debugger): quick-evaluate the whole method call under the mouse
Quick Evaluate took only the identifier and its member chain, so Alt+Click on
getX in o.getX() sent o.getX, which a Java adapter resolves as a field. It also
asked with the hover context, in which adapters refuse to call methods.

Quick Evaluate and the Evaluate dialog's seed now take the call of the clicked
identifier (balanced arguments, strings skipped) and member chains through calls
and indexing (a.getB().c(), items[i].name), and Quick Evaluate evaluates in the
watch context. Plain hovers and value-on-selection keep the side-effect-free
identifier and hover context.
2026-11-20 19:52:35 +08:00
4 changed files with 101 additions and 8 deletions

No files matched your search

@@ -0,0 +1,37 @@
import { describe, expect, it } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { expressionAt } from "../editor/valueTooltip";
/** The expression around the `|` in `line`. */
function at(line: string, calls: boolean) {
const pos = line.indexOf("|");
const view = new EditorView({ state: EditorState.create({ doc: line.replace("|", "") }) });
const text = expressionAt(view, pos, { calls })?.text ?? null;
view.destroy();
return text;
}
describe("expression at the mouse for Quick Evaluate (#4772)", () => {
it("takes the call of the clicked method, arguments included", () => {
expect(at("String n = vo.getChd|rnum();", true)).toBe("vo.getChdrnum()");
expect(at('x = f|(a, g(b), "c)");', true)).toBe('f(a, g(b), "c)")');
expect(at("x = list.g|et (i);", true)).toBe("list.get (i)");
});
it("follows member chains through calls and indexing to the clicked member", () => {
expect(at("a.getB().c|()", true)).toBe("a.getB().c()");
expect(at("a.get|B().c()", true)).toBe("a.getB()");
expect(at("items[i + 1].na|me", true)).toBe("items[i + 1].name");
expect(at("a.b()[0].c|", true)).toBe("a.b()[0].c");
});
it("leaves an unbalanced call as the bare member", () => {
expect(at("vo.getChd|rnum(a,", true)).toBe("vo.getChdrnum");
});
it("keeps plain hovers free of calls", () => {
expect(at("String n = vo.getChd|rnum();", false)).toBe("vo.getChdrnum");
expect(at("a.getB().c|()", false)).toBe("c");
});
});
@@ -91,7 +91,7 @@ describe("paused editor evaluation", () => {
view.dispatch({ selection: { anchor: 0, head: 5 } });
expect(click().defaultPrevented).toBe(true);
await vi.waitFor(() => expect(document.querySelector(".cm-lsp-hint")?.textContent).toContain("order.items = 42"));
expect(ipc.debugEvaluateIn).toHaveBeenCalledExactlyOnceWith(7, 10, "order.items", "hover");
expect(ipc.debugEvaluateIn).toHaveBeenCalledExactlyOnceWith(7, 10, "order.items", "watch");
expect(view.state.selection.main.head).toBe(16);
});
@@ -101,7 +101,7 @@ describe("paused editor evaluation", () => {
session();
view.dispatch({ selection: { anchor: 0, head: doc.length } });
click();
await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledWith(7, 10, doc, "hover"));
await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledWith(7, 10, doc, "watch"));
expect(view.state.selection.main.from).toBe(0);
expect(view.state.selection.main.to).toBe(doc.length);
await vi.waitFor(() =>
@@ -114,7 +114,7 @@ describe("paused editor evaluation", () => {
const text = "(total +\n order.items)";
view.dispatch({ changes: { from: 0, to: doc.length, insert: text }, selection: { anchor: 0, head: text.length } });
click();
await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledWith(7, 10, text, "hover"));
await vi.waitFor(() => expect(ipc.debugEvaluateIn).toHaveBeenCalledWith(7, 10, text, "watch"));
});
it("ignores an object response that arrives after the debugger resumes", async () => {
+3 -2
View File
@@ -208,7 +208,7 @@ function evaluateSeed(view: EditorView | undefined = activeEditor()?.view, pos?:
const { state } = view;
const sel = state.selection.main;
if (!sel.empty && (pos == null || (sel.from <= pos && pos <= sel.to))) return state.sliceDoc(sel.from, sel.to);
return expressionAt(view, pos ?? sel.head)?.text ?? "";
return expressionAt(view, pos ?? sel.head, { calls: true })?.text ?? "";
}
async function quickEvaluate(arg?: unknown) {
@@ -234,7 +234,8 @@ async function quickEvaluate(arg?: unknown) {
);
};
try {
const value = await evaluate(expression, "hover", session);
// Unlike a plain hover, an explicit Quick Evaluate may call methods (adapters refuse that for "hover").
const value = await evaluate(expression, "watch", session);
if (session && stillCurrent()) showQuickValue(view, pos, expression, value, session.id);
} catch (e) {
if (stillCurrent()) showHint(view, e instanceof Error ? e.message : String(e), { pos, tone: "error" });
+58 -3
View File
@@ -19,8 +19,45 @@ 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 OPEN: Record<string, string> = { ")": "(", "]": "[" };
const CLOSE: Record<string, string> = { "(": ")", "[": "]" };
/** Index just past the bracket group opening at `i` (strings skipped), or -1 when it doesn't close on the line. */
function closeOf(t: string, i: number): number {
const stack: string[] = [];
for (let j = i; j < t.length; j++) {
const c = t[j]!;
if (c === '"' || c === "'" || c === "`") {
j++;
while (j < t.length && t[j] !== c) j += t[j] === "\\" ? 2 : 1;
if (j >= t.length) return -1;
} else if (CLOSE[c]) stack.push(CLOSE[c]);
else if (c === ")" || c === "]") {
if (stack.pop() !== c) return -1;
if (!stack.length) return j + 1;
}
}
return -1;
}
/** Index of the bracket opening the group that closes at `i - 1`, or -1. */
function openOf(t: string, i: number): number {
for (let start = i - 1; start >= 0; start--) {
if (t[start] === OPEN[t[i - 1]!] && closeOf(t, start) === i) return start;
}
return -1;
}
/**
* Identifier (with `a.b.c` member access) around `pos`, or the selection containing it. With
* `calls`, as for Quick Evaluate: member chains through calls and indexing (`a.getB().c`,
* `items[i].name`) and the call of the identifier itself (`o.getX()`, `f(a, b)`).
*/
export function expressionAt(
view: EditorView,
pos: number,
{ calls = false } = {},
): { from: number; to: number; text: string } | null {
const { state } = view;
const sel = state.selection.main;
if (!sel.empty && pos >= sel.from && pos <= sel.to) {
@@ -33,7 +70,25 @@ export function expressionAt(view: EditorView, pos: number): { from: number; to:
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--;
for (;;) {
while (start > 0 && IDENT.test(t[start - 1]!)) start--;
if (t[start - 1] === "." && IDENT.test(t[start - 2] ?? "")) start--;
else if (calls && t[start - 1] === "." && OPEN[t[start - 2] ?? ""]) {
let open = openOf(t, start - 1);
// `a.b()[0].c`: walk back over every group, then the name they follow.
while (open > 0 && OPEN[t[open - 1]!]) open = openOf(t, open);
if (open <= 0 || !IDENT.test(t[open - 1]!)) break;
start = open;
} else break;
}
if (calls) {
let next = end;
while (t[next] === " ") next++;
if (t[next] === "(") {
const close = closeOf(t, next);
if (close > 0) end = close;
}
}
const text = t.slice(start, end);
if (!text || /^\d/.test(text)) return null;
return { from: line.from + start, to: line.from + end, text };