From 721635d115510004803d258097e6523a6ea1d03d Mon Sep 17 00:00:00 2001 From: kirawi <67773714+kirawi@users.noreply.github.com> Date: Thu, 4 Jan 2024 18:56:50 -0500 Subject: [PATCH] fix merge logic --- helix-core/src/history.rs | 17 +++++++++-------- helix-core/src/transaction.rs | 4 +--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 1db4eb22902f6..0c56f8f9c4b7b 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -266,7 +266,7 @@ impl History { /// E -> F /// ``` pub fn merge(&mut self, mut other: History) -> anyhow::Result<()> { - let after_n = self + let n = self .revisions .iter() .zip(other.revisions.iter()) @@ -275,14 +275,15 @@ impl History { }) .count(); - let revisions = self.revisions.split_off(after_n); - other.revisions.reserve_exact(revisions.len()); + let new_revs = self.revisions.split_off(n); + if new_revs.is_empty() { + return Ok(()); + } - // Converts the number of new elements to an index offset - let offset = (other.revisions.len() - after_n) - 1; - for mut r in revisions { - // Update parents of new revisions - if r.parent >= after_n { + // Only unique revisions in `self` matter, so saturating_sub(1) is sound as it going to 0 means there are no new revisions in the other history that aren't in `self` + let offset = (other.revisions.len() - n).saturating_sub(1); + for mut r in new_revs { + if r.parent >= n { r.parent += offset; } debug_assert!(r.parent < other.revisions.len()); diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index c34aedc0aa5d3..4ac1e05105203 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -379,9 +379,7 @@ impl ChangeSet { macro_rules! map { ($map: expr, $i: expr) => { loop { - let Some((pos, assoc)) = positions.peek_mut() else { - return; - }; + let Some((pos, assoc)) = positions.peek_mut() else { return; }; if **pos < old_pos { // Positions are not sorted, revert to the last Operation that // contains this position and continue iterating from there.