Skip to content

Commit

Permalink
feat: escape reverts text edits
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Dec 3, 2024
1 parent 601808b commit 33d01f7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ fn make_tree_keymap() {

fn make_text_keymap() {
let keymap = new_keymap();
keymap.bind_key("esc", "ExitText", || s::text_nav_exit());
keymap.bind_key("enter", "ExitText", || s::text_nav_exit());
keymap.bind_key("enter", "Confirm", || s::text_nav_exit());
keymap.bind_key("esc", "Revert", || s::revert());
keymap.bind_key("left", "Left", || s::text_nav_left());
keymap.bind_key("right", "Right", || s::text_nav_right());
keymap.bind_key("bksp", "Backspace", || s::text_ed_backspace());
Expand Down
2 changes: 1 addition & 1 deletion src/engine/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum TextNavCommand {
Beginning,
/// Move the cursor to the end of the text.
End,
/// Exit text mode, placing the cursor after the texty node.
/// Exit text mode, keeping the edits.
ExitText,
}

Expand Down
11 changes: 11 additions & 0 deletions src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ impl Doc {
}
}

/// Instead of ending the current undo group, permanently revert its changes.
pub fn revert_undo_group(&mut self, s: &mut Storage) {
if let Some(recent) = self.recent.take() {
let redos = recent.execute(s, &mut self.cursor);
redos.delete_trees(s);
if self.save_point == SavePoint::Recent {
self.save_point = SavePoint::None;
}
}
}

/// Undoes the last undo group on the undo stack and moves it to the redo stack.
/// Returns `Err(EditError::NothingToUndo)` if the undo stack is empty.
/// If there were recent edits _not_ completed with a call to end_undo_group(),
Expand Down
9 changes: 9 additions & 0 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ impl Engine {
Ok(())
}

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

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

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

pub fn insert_node(&mut self, construct: Construct) -> Result<(), SynlessError> {
let node = Node::new_with_auto_fill(self.engine.raw_storage_mut(), construct);
self.engine.execute(TreeEdCommand::Insert(node))?;
Expand Down Expand Up @@ -895,6 +899,7 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
// Editing: Meta
register!(module, rt.undo()?);
register!(module, rt.redo()?);
register!(module, rt.revert()?);

// Command Line Interface
register!(module, rt.cli_args());
Expand Down

0 comments on commit 33d01f7

Please sign in to comment.