Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 7ca439e315 feat(debugger): evaluate the expression of an operator on hover and Quick Evaluate
Hovering or Alt-clicking an operator while suspended evaluated nothing, because only identifiers, member chains and calls were taken. As in IDEA, an operator now gives the expression it belongs to, from the syntax tree: && gives a > 1 && !flag, > gives a > 1, ! gives !flag. A hover leaves out expressions with calls, increments or assignments, which Quick Evaluate still evaluates; an identifier is still just the identifier.

Fixes #7000
2026-11-30 21:19:00 +08:00
2 змінених файлів з 59 додано та 2 видалено

No files matched your search

+35
Переглянути файл
@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { ensureSyntaxTree } from "@codemirror/language";
import { java } from "@codemirror/lang-java";
import { expressionAt } from "../editor/valueTooltip";
/** The expression around the `|` in `line`. */
@@ -35,3 +37,36 @@ describe("expression at the mouse for Quick Evaluate (#4772)", () => {
expect(at("a.getB().c|()", false)).toBe("c");
});
});
/** The expression around the `|` in a Java statement. */
function inJava(statement: string, calls: boolean) {
const prefix = "class A { void f() { ";
const doc = `${prefix}${statement} } }`;
const pos = doc.indexOf("|");
const view = new EditorView({ state: EditorState.create({ doc: doc.replace("|", ""), extensions: java() }) });
ensureSyntaxTree(view.state, view.state.doc.length, 1000);
const text = expressionAt(view, pos, { calls })?.text ?? null;
view.destroy();
return text;
}
describe("expression of an operator (#7000)", () => {
it("takes the expression the operator belongs to, as IDEA does", () => {
for (const calls of [false, true]) {
expect(inJava("if (a > 1 |&& !flag) {}", calls)).toBe("a > 1 && !flag");
expect(inJava("if (a |> 1 && !flag) {}", calls)).toBe("a > 1");
expect(inJava("if (a > 1 && |!flag) {}", calls)).toBe("!flag");
expect(inJava("x = c |? 1 : 2;", calls)).toBe("c ? 1 : 2");
// An identifier is still just the identifier.
expect(inJava("if (|a > 1 && !flag) {}", calls)).toBe("a");
}
});
it("leaves calls, increments and assignments to Quick Evaluate", () => {
expect(inJava("if (s.length() |> 0) {}", false)).toBeNull();
expect(inJava("if (s.length() |> 0) {}", true)).toBe("s.length() > 0");
expect(inJava("if (a |!= b) {}", false)).toBe("a != b");
expect(inJava("x = a |+ i++;", false)).toBeNull();
expect(inJava("x |= 1;", true)).toBeNull();
});
});
+24 -2
Переглянути файл
@@ -3,6 +3,7 @@
* while suspended, hovering an expression (or the selection under the mouse)
* evaluates it in the selected frame.
*/
import { syntaxTree } from "@codemirror/language";
import type { EditorState, Extension } from "@codemirror/state";
import type { DebugSessionId, Variable } from "@/bindings";
import { closeHoverTooltips, hoverTooltip, ViewPlugin, type EditorView, type ViewUpdate } from "@codemirror/view";
@@ -49,10 +50,29 @@ function openOf(t: string, i: number): number {
return -1;
}
/** Syntax nodes of an operator's expression (Lezer's Java, JS, Python, C++, Rust, Go grammars). */
const OPERATOR_EXPRESSION = /^(Binary|Unary|Ternary|Conditional)Expr(ession)?$/;
/** A call, an increment or an assignment: a plain hover doesn't evaluate those. */
const SIDE_EFFECTS = /[\p{L}\p{N}_$)\]]\s*\(|\+\+|--|(^|[^=!<>])=(?!=)/u;
/**
* As in IDEA, on an operator the expression it belongs to: `&&` gives `a > 1 && !flag`, `>` gives
* `a > 1`, `!` gives `!flag`. Without `calls` (a hover), not when evaluating it could change state.
*/
function operatorExpressionAt(state: EditorState, pos: number, calls: boolean) {
const token = syntaxTree(state).resolveInner(pos, 1);
const parent = token.parent;
if (token.firstChild || !parent || !OPERATOR_EXPRESSION.test(parent.name)) return null;
const text = state.sliceDoc(parent.from, parent.to);
if (text.includes("\n") || (!calls && SIDE_EFFECTS.test(text))) return null;
return { from: parent.from, to: parent.to, text };
}
/**
* 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)`).
* `items[i].name`) and the call of the identifier itself (`o.getX()`, `f(a, b)`). On an operator,
* the expression it belongs to.
*/
export function expressionAt(
view: EditorView,
@@ -69,7 +89,9 @@ export function expressionAt(
const t = line.text;
let start = pos - line.from;
let end = start;
if (!IDENT.test(t[end] ?? "") && !IDENT.test(t[start - 1] ?? "")) return null;
if (!IDENT.test(t[end] ?? "") && !IDENT.test(t[start - 1] ?? "")) {
return /\s/.test(t[end] ?? " ") ? null : operatorExpressionAt(state, pos, calls);
}
while (end < t.length && IDENT.test(t[end]!)) end++;
for (;;) {
while (start > 0 && IDENT.test(t[start - 1]!)) start--;