From ff6c6eb13f9e27bf75009dc7e697f625e37761d2 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 22 Nov 2026 02:50:09 +0800 Subject: [PATCH] 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. --- src/editor/core/cm/editing.ts | 9 ++++++ src/editor/core/cm/moveStatementRedo.test.ts | 30 ++++++++++++++++++++ src/editor/core/cm/structural.ts | 6 +++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/editor/core/cm/moveStatementRedo.test.ts diff --git a/src/editor/core/cm/editing.ts b/src/editor/core/cm/editing.ts index e5ec289d3..bab53c81a 100644 --- a/src/editor/core/cm/editing.ts +++ b/src/editor/core/cm/editing.ts @@ -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); diff --git a/src/editor/core/cm/moveStatementRedo.test.ts b/src/editor/core/cm/moveStatementRedo.test.ts new file mode 100644 index 000000000..413061202 --- /dev/null +++ b/src/editor/core/cm/moveStatementRedo.test.ts @@ -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); + }); +}); diff --git a/src/editor/core/cm/structural.ts b/src/editor/core/cm/structural.ts index bd067d0b0..ed00aab1b 100644 --- a/src/editor/core/cm/structural.ts +++ b/src/editor/core/cm/structural.ts @@ -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(