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 | 56 |
1 files changed, 14 insertions, 42 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 28d6dd6e..bb95213c 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 @@ -28,10 +22,10 @@ pub struct State { /// /// The current revision is the one currently displayed in the buffer. /// -/// Committing a new revision to the history will update the last child of the +/// Commiting a new revision to the history will update the last child of the /// current revision, and push a new revision to the end of the vector. /// -/// Revisions are committed with a timestamp. :earlier and :later can be used +/// Revisions are commited with a timestamp. :earlier and :later can be used /// to jump to the closest revision to a moment in time relative to the timestamp /// of the current revision plus (:later) or minus (:earlier) the duration /// given to the command. If a single integer is given, the editor will instead @@ -39,7 +33,7 @@ pub struct State { /// /// Limitations: /// * Changes in selections currently don't commit history changes. The selection -/// will only be updated to the state after a committed buffer change. +/// will only be updated to the state after a commited buffer change. /// * The vector of history revisions is currently unbounded. This might /// cause the memory consumption to grow significantly large during long /// editing sessions. @@ -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,13 +282,13 @@ 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), } -/// A subset of systemd.time time span syntax units. +/// A subset of sytemd.time time span syntax units. const TIME_UNITS: &[(&[&str], &str, u64)] = &[ (&["seconds", "second", "sec", "s"], "seconds", 1), (&["minutes", "minute", "min", "m"], "minutes", 60), @@ -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. |