mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(editor): restore the caret of Move Statement on Redo (#5063)
Redo of Move Statement Up / Down left the caret where the move started: CodeMirror's history maps the selection from before the edit through it unless the command records the selection after. Both the structural and the line-based move now record the caret they set, so Undo / Redo put it back.
This commit is contained in:
@@ -514,6 +514,7 @@ function moveStatement(up: boolean): Command {
|
||||
if (plan === "noop") return true;
|
||||
if (plan !== "line") {
|
||||
view.dispatch(plan.spec);
|
||||
recordSelectionAfter(view);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -521,6 +522,14 @@ function moveStatement(up: boolean): Command {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the caret an edit command set as the selection after its edit, which Redo restores;
|
||||
* without it Redo maps the caret from before the edit through it (elsewhere after a move).
|
||||
*/
|
||||
export function recordSelectionAfter(view: EditorView): void {
|
||||
view.dispatch({ selection: view.state.selection });
|
||||
}
|
||||
|
||||
export const moveStatementUp = moveStatement(true);
|
||||
export const moveStatementDown = moveStatement(false);
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { history, redo, undo } from "@codemirror/commands";
|
||||
import { javascript } from "@codemirror/lang-javascript";
|
||||
import { moveStatementDown } from "./editing";
|
||||
|
||||
const views: EditorView[] = [];
|
||||
afterEach(() => views.splice(0).forEach((v) => v.destroy()));
|
||||
|
||||
describe("Move Statement and Redo", () => {
|
||||
it("puts the caret back where the move left it", () => {
|
||||
const doc = "function f() {\n const x = 1;\n const y = 2;\n return x + y;\n}\n";
|
||||
const v = new EditorView({
|
||||
state: EditorState.create({
|
||||
doc,
|
||||
selection: { anchor: doc.indexOf("x = 1") },
|
||||
extensions: [history(), javascript({ typescript: true })],
|
||||
}),
|
||||
parent: document.body,
|
||||
});
|
||||
views.push(v);
|
||||
moveStatementDown(v);
|
||||
const after = v.state.selection.main.head;
|
||||
expect(v.state.doc.line(3).text).toBe(" const x = 1;");
|
||||
undo(v);
|
||||
redo(v);
|
||||
expect(v.state.selection.main.head).toBe(after);
|
||||
});
|
||||
});
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
completeStatementSpec,
|
||||
isBlockHead,
|
||||
lineCommentToken,
|
||||
recordSelectionAfter,
|
||||
unclosedQuote,
|
||||
} from "./editing";
|
||||
|
||||
@@ -447,7 +448,8 @@ export async function moveStatementStructural(
|
||||
up: boolean,
|
||||
q: StructureQueries,
|
||||
): Promise<"done" | "fallback" | "stale"> {
|
||||
return runPlanned(view, async () => {
|
||||
const before = view.state.doc;
|
||||
const result = await runPlanned(view, async () => {
|
||||
const line = view.state.doc.lineAt(view.state.selection.main.head);
|
||||
if (!line.text.trim()) return null;
|
||||
const r = await q.ranges(offsetToPos(view.state.doc, line.from));
|
||||
@@ -464,6 +466,8 @@ export async function moveStatementStructural(
|
||||
const plan = planMoveStatement(view.state, r, up, errors);
|
||||
return plan.kind === "move" ? plan.spec : plan.kind === "noop" ? "noop" : null;
|
||||
});
|
||||
if (result === "done" && view.state.doc !== before) recordSelectionAfter(view);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function completeStatementStructural(
|
||||
|
||||
Reference in New Issue
Block a user