diff options
author | Michael Davis | 2022-11-27 18:46:36 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2022-11-29 16:15:20 +0000 |
commit | 9387dfafedfc7051e7682cfdbec09b5b91c76918 (patch) | |
tree | 09a82a69790f8dbb095596392e0eaa99b9cd6134 /helix-core/src | |
parent | 9a9e462183cb60bff6450f17173e6b18eadbbfb2 (diff) |
Use lowest common ancestor search in History::changes_since
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/history.rs | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 82509242..b99e969f 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -122,32 +122,16 @@ impl History { /// 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> { - use std::cmp::Ordering::*; + 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()); - match revision.cmp(&self.current) { - Equal => None, - Less => { - let mut child = self.revisions[revision].last_child?.get(); - let mut transaction = self.revisions[child].transaction.clone(); - while child != self.current { - child = self.revisions[child].last_child?.get(); - transaction = transaction.compose(self.revisions[child].transaction.clone()); - } - Some(transaction) - } - Greater => { - let mut inversion = self.revisions[revision].inversion.clone(); - let mut parent = self.revisions[revision].parent; - while parent != self.current { - parent = self.revisions[parent].parent; - if parent == 0 { - return None; - } - inversion = inversion.compose(self.revisions[parent].inversion.clone()); - } - Some(inversion) - } - } + up_txns.chain(down_txns).reduce(|acc, tx| tx.compose(acc)) } /// Undo the last edit. |