Skip to content

Feature: Add Undo/Revert Functionality with Ctrl+Z#28

Open
susbakun wants to merge 9 commits intopflenker:mainfrom
susbakun:feature/revert
Open

Feature: Add Undo/Revert Functionality with Ctrl+Z#28
susbakun wants to merge 9 commits intopflenker:mainfrom
susbakun:feature/revert

Conversation

@susbakun
Copy link

@susbakun susbakun commented Dec 27, 2025

Summary

This PR implements undo/revert functionality for the editor, allowing users to undo their last edit operation using Ctrl+Z. The feature supports reverting all edit operations including character insertions, deletions (both forward and backward), newlines, and range deletions.

demo

Features

  • Undo with Ctrl+Z: Press Ctrl+Z to revert the last edit operation
  • Revert buffer: Maintains a history of up to 100 edit operations
  • Full operation support:
    • Character insertions
    • Character deletions (Delete key)
    • Backward deletions (Backspace)
    • Newline insertions
    • Range deletions (selected text)
  • Multi-character grapheme support: Correctly handles Unicode graphemes that span multiple characters
  • Proper text extraction: Range deletions preserve indentation and formatting when reverted

Implementation Details

Key Changes

  1. New Edit::Revert command: Added to the Edit enum, triggered by Ctrl+Z
  2. New Edit::InsertText variant: Added to support reverting multi-character deletions and range deletions
  3. Revert buffer: Added revert_buffer: Vec<RevertCommand> to View struct to store edit history
  4. Text extraction methods:
    • Buffer::get_a_grapheme(): Extracts a single grapheme at a location
    • Buffer::get_range_grapheme(): Extracts text from a selection range
    • Line::get_a_grapheme(): Public method to extract graphemes from a line
    • Line::get_grapheme_range(): Public method to extract grapheme ranges from a line
  5. Skip revert buffer flag: Added skip_revert_buffer parameter to prevent infinite loops when applying revert commands

Technical Highlights

  • Grapheme-aware: Uses grapheme indices instead of byte indices to correctly handle Unicode characters
  • Memory management: Limits revert buffer to 100 entries to prevent unbounded memory growth
  • Cursor position restoration: Restores cursor to the correct location when reverting operations

Usage

  1. Perform any edit operation (insert, delete, etc.)
  2. Press Ctrl+Z to undo the last operation
  3. Repeat Ctrl+Z to undo previous operations (up to 100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant