diff options
author | Blaž Hrastnik | 2020-12-03 04:12:40 +0000 |
---|---|---|
committer | GitHub | 2020-12-03 04:12:40 +0000 |
commit | b7a3e525ed7fed5ed79e8580df2e3496bd994419 (patch) | |
tree | d202637047759b0510a16d8c59fdbbde62b50617 /helix-core/src/history.rs | |
parent | 2e12fc9a7cd221bb7b5f4b5c1ece599089770ccb (diff) | |
parent | 39bf1ca82514e1dc56dfebdce2558cce662367d1 (diff) |
Merge pull request #5 from helix-editor/lsp
LSP: mk1
Diffstat (limited to 'helix-core/src/history.rs')
-rw-r--r-- | helix-core/src/history.rs | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index e6d9a738..66445525 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -57,37 +57,31 @@ impl History { self.cursor == 0 } - pub fn undo(&mut self, state: &mut State) { + // TODO: I'd like to pass Transaction by reference but it fights with the borrowck + + pub fn undo(&mut self) -> Option<Transaction> { if self.at_root() { // We're at the root of undo, nothing to do. - return; + return None; } let current_revision = &self.revisions[self.cursor]; - // TODO: pass the return value through? It should always succeed - let success = current_revision.revert.apply(state); - - if !success { - panic!("Failed to apply undo!"); - } - self.cursor = current_revision.parent; + + Some(current_revision.revert.clone()) } - pub fn redo(&mut self, state: &mut State) { + pub fn redo(&mut self) -> Option<Transaction> { let current_revision = &self.revisions[self.cursor]; // for now, simply pick the latest child (linear undo / redo) if let Some((index, transaction)) = current_revision.children.last() { - let success = transaction.apply(state); - - if !success { - panic!("Failed to apply redo!"); - } - self.cursor = *index; + + return Some(transaction.clone()); } + None } } @@ -120,17 +114,27 @@ mod test { assert_eq!("hello 世界!", state.doc()); // --- + fn undo(history: &mut History, state: &mut State) { + if let Some(transaction) = history.undo() { + transaction.apply(state); + } + } + fn redo(history: &mut History, state: &mut State) { + if let Some(transaction) = history.redo() { + transaction.apply(state); + } + } - history.undo(&mut state); + undo(&mut history, &mut state); assert_eq!("hello world!", state.doc()); - history.redo(&mut state); + redo(&mut history, &mut state); assert_eq!("hello 世界!", state.doc()); - history.undo(&mut state); - history.undo(&mut state); + undo(&mut history, &mut state); + undo(&mut history, &mut state); assert_eq!("hello", state.doc()); // undo at root is a no-op - history.undo(&mut state); + undo(&mut history, &mut state); assert_eq!("hello", state.doc()); } } |