Skip to content

Commit

Permalink
feat: register undo and redo
Browse files Browse the repository at this point in the history
  • Loading branch information
e-matteson committed Apr 20, 2024
1 parent 083deda commit 6e40ace
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ tree_keymap.bind_key("l", "Inorder next", || s::tree_nav_inorder_next());
tree_keymap.bind_key("down", "Last child", || s::tree_nav_last_child());
tree_keymap.bind_key("up", "Parent", || s::tree_nav_parent());
tree_keymap.bind_key("enter", "Enter text", || s::tree_nav_enter_text());
tree_keymap.bind_key("bksp", "Backspace", || s::tree_ed_backspace());
tree_keymap.bind_key("d", "Delete", || s::tree_ed_delete());
tree_keymap.bind_key("u", "Undo", || s::undo());
tree_keymap.bind_key("r", "Redo", || s::redo());

tree_keymap.bind_key("m", "Save bookmark", || s::save_bookmark('a'));
tree_keymap.bind_key("'", "Go to bookmark", || s::goto_bookmark('a'));

Expand Down
27 changes: 27 additions & 0 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ impl Engine {
Ok(())
}

pub fn undo(&mut self) -> Result<(), SynlessError> {
let doc = self
.doc_set
.visible_doc_mut()
.ok_or(DocError::NoVisibleDoc)?;
doc.undo(&mut self.storage)?;
Ok(())
}

pub fn redo(&mut self) -> Result<(), SynlessError> {
let doc = self
.doc_set
.visible_doc_mut()
.ok_or(DocError::NoVisibleDoc)?;
doc.redo(&mut self.storage)?;
Ok(())
}

pub fn end_undo_group(&mut self) -> Result<(), SynlessError> {
let doc = self
.doc_set
.visible_doc_mut()
.ok_or(DocError::NoVisibleDoc)?;
doc.end_undo_group();
Ok(())
}

/**********************
* Raw Storage Access *
**********************/
Expand Down
11 changes: 11 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
self.engine.execute(cmd)
}

pub fn undo(&mut self) -> Result<(), SynlessError> {
self.engine.undo()
}

pub fn redo(&mut self) -> Result<(), SynlessError> {
self.engine.redo()
}

/***********
* Private *
***********/
Expand Down Expand Up @@ -507,6 +515,9 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
register!(module, rt, BookmarkCommand::Save(ch: char) as save_bookmark);
register!(module, rt, BookmarkCommand::Goto(ch: char) as goto_bookmark);

register!(module, rt.undo()?);
register!(module, rt.redo()?);

// Logging
rhai::FuncRegistration::new("log_trace")
.in_internal_namespace()
Expand Down

0 comments on commit 6e40ace

Please sign in to comment.