Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/history.rs')
| -rw-r--r-- | helix-core/src/history.rs | 48 |
1 files changed, 10 insertions, 38 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 28d6dd6e..3f324e34 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -1,15 +1,9 @@ -use crate::{Assoc, ChangeSet, Range, Rope, Selection, Transaction}; +use crate::{Assoc, ChangeSet, Range, Rope, State, Transaction}; use once_cell::sync::Lazy; use regex::Regex; use std::num::NonZeroUsize; use std::time::{Duration, Instant}; -#[derive(Debug, Clone)] -pub struct State { - pub doc: Rope, - pub selection: Selection, -} - /// Stores the history of changes to a buffer. /// /// Currently the history is represented as a vector of revisions. The vector @@ -54,7 +48,7 @@ pub struct History { } /// A single point in history. See [History] for more information. -#[derive(Debug, Clone)] +#[derive(Debug)] struct Revision { parent: usize, last_child: Option<NonZeroUsize>, @@ -72,8 +66,8 @@ impl Default for History { revisions: vec![Revision { parent: 0, last_child: None, - transaction: Transaction::from(ChangeSet::new("".into())), - inversion: Transaction::from(ChangeSet::new("".into())), + transaction: Transaction::from(ChangeSet::new(&Rope::new())), + inversion: Transaction::from(ChangeSet::new(&Rope::new())), timestamp: Instant::now(), }], current: 0, @@ -119,21 +113,6 @@ impl History { self.current == 0 } - /// Returns the changes since the given revision composed into a transaction. - /// Returns None if there are no changes between the current and given revisions. - pub fn changes_since(&self, revision: usize) -> Option<Transaction> { - let lca = self.lowest_common_ancestor(revision, self.current); - let up = self.path_up(revision, lca); - let down = self.path_up(self.current, lca); - let up_txns = up - .iter() - .rev() - .map(|&n| self.revisions[n].inversion.clone()); - let down_txns = down.iter().map(|&n| self.revisions[n].transaction.clone()); - - down_txns.chain(up_txns).reduce(|acc, tx| tx.compose(acc)) - } - /// Undo the last edit. pub fn undo(&mut self) -> Option<&Transaction> { if self.at_root() { @@ -198,7 +177,7 @@ impl History { } } - /// List of nodes on the way from `n` to 'a`. Doesn't include `a`. + /// List of nodes on the way from `n` to 'a`. Doesn`t include `a`. /// Includes `n` unless `a == n`. `a` must be an ancestor of `n`. fn path_up(&self, mut n: usize, a: usize) -> Vec<usize> { let mut path = Vec::new(); @@ -303,7 +282,7 @@ impl History { } /// Whether to undo by a number of edits or a duration of time. -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy)] pub enum UndoKind { Steps(usize), TimePeriod(std::time::Duration), @@ -387,16 +366,12 @@ impl std::str::FromStr for UndoKind { #[cfg(test)] mod test { use super::*; - use crate::Selection; #[test] fn test_undo_redo() { let mut history = History::default(); let doc = Rope::from("hello"); - let mut state = State { - doc, - selection: Selection::point(0), - }; + let mut state = State::new(doc); let transaction1 = Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter()); @@ -445,10 +420,7 @@ mod test { fn test_earlier_later() { let mut history = History::default(); let doc = Rope::from("a\n"); - let mut state = State { - doc, - selection: Selection::point(0), - }; + let mut state = State::new(doc); fn undo(history: &mut History, state: &mut State) { if let Some(transaction) = history.undo() { @@ -574,8 +546,8 @@ mod test { // Units are validated. assert_eq!( - "1 millennium".parse::<UndoKind>(), - Err("incorrect time unit: millennium".to_string()) + "1 millenium".parse::<UndoKind>(), + Err("incorrect time unit: millenium".to_string()) ); // Units can't be specified twice. |