Skip to content

✨ Restore Cursor and Scroll Position on Undo/Redo #95

Closed

Description

@austincondiff

Description

When performing an undo or redo, the cursor and scroll position should return to the state they were in at the time of the change. Currently, only the text content is restored, but the selection and scroll position are not preserved, which can disrupt the editing experience—especially during rapid or successive edits.

Expected Behavior

  • When a change is undone, the insertion point and scroll position should move to where they were when the change originally occurred.
  • When a change is redone, the insertion point and scroll position should likewise be restored to their prior state.
  • This behavior should feel seamless and natural, mirroring the expectations of professional editors on macOS.

Proposed Implementation

  • Extend the existing Mutation or UndoGroup struct to include the selected range and visible scroll rect at the time of the change.
  • Capture these properties during registerMutation(_:).
  • Restore the stored selected range and scroll rect in undo() and redo() methods after applying the text mutations.

Benefits

  • Improves consistency and usability of the editor during undo/redo workflows.
  • Reduces disorientation from insertion point jumps or loss of context.
  • Brings CodeEditTextView in line with native Mac editor expectations.

Additional Notes

  • Consider edge cases such as when the associated text range no longer exists.

Activity

  1. moved this from 🆕 New to 📋 Todo in CodeEdit Projecton May 30, 2025
  2. thecoolwinter commented on Jun 18, 2025

    @thecoolwinter
    Contributor

    I'd like to note that I don't think we should exactly restore the user's selection on an undo. The macOS way to do this is to select the undone/redone text. That lets the user both see the contents that they just changed, and take quick action like deleting it, moving to the left or right of it, etc.

    Screen.Recording.2025-06-18.at.11.08.02.AM.mov
  3. austincondiff commented on Jun 18, 2025

    @austincondiff
    ContributorAuthor

    @thecoolwinter Good catch! Let's do that then and follow the pattern Apple set forth.

  4. added a commit that references this issue on Jun 18, 2025
    7d63a64
  5. moved this from 📋 Todo to 🏁 Complete in CodeEdit Projecton Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions