Undo/redo support in ViEditor

This commit is contained in:
Jeremy Soller 2023-11-13 13:31:06 -07:00
parent 7830f4107c
commit 5352fdee94
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
5 changed files with 198 additions and 68 deletions

View file

@ -93,15 +93,41 @@ pub enum Action {
GotoLine(usize),
}
/// A unique change to an editor
#[derive(Clone, Debug)]
pub enum ChangeItem {
Delete(Cursor, String),
Insert(Cursor, String),
pub struct ChangeItem {
/// Cursor indicating start of change
pub start: Cursor,
/// Cursor indicating end of change
pub end: Cursor,
/// Text to be inserted or deleted
pub text: String,
/// Insert if true, delete if false
pub insert: bool,
}
impl ChangeItem {
// Reverse change item (in place)
pub fn reverse(&mut self) {
self.insert = !self.insert;
}
}
/// A set of change items grouped into one logical change
#[derive(Clone, Debug, Default)]
pub struct Change {
items: Vec<ChangeItem>,
/// Change items grouped into one change
pub items: Vec<ChangeItem>,
}
impl Change {
// Reverse change (in place)
pub fn reverse(&mut self) {
self.items.reverse();
for item in self.items.iter_mut() {
item.reverse();
}
}
}
/// A trait to allow easy replacements of [`Editor`], like `SyntaxEditor`
@ -158,6 +184,9 @@ pub trait Edit {
/// attributes, or with the previous character's attributes if None is given.
fn insert_string(&mut self, data: &str, attrs_list: Option<AttrsList>);
/// Apply a change
fn apply_change(&mut self, change: &Change) -> bool;
/// Start collecting change
fn start_change(&mut self);