дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Hovering or Alt-clicking an operator while suspended evaluated nothing, because only identifiers, member chains and calls were taken. 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
73 рядки
3.1 KiB
TypeScript
73 рядки
3.1 KiB
TypeScript
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`. */
|
|
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");
|
|
});
|
|
});
|
|
|
|
/** 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();
|
|
});
|
|
});
|