Compact diffs

This commit is contained in:
Andras Schmelczer 2024-11-24 22:32:17 +00:00
commit 8b8733c1e3
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -251,70 +251,6 @@ impl Operation {
}
}
}
/// Merges the operation with another operation that is consequtive to this operation.
/// The other operation must start where this operation ends.
/// The two operations must be of the same type, otherwise panics.
pub fn extend(self, other: &Self) -> Self {
match (self, other) {
(
Operation::Insert { index, text },
Operation::Insert {
text: other_text, ..
},
) => {
let end_index = index + text.chars().count();
debug_assert!(
end_index == other.start_index(),
"Cannot merge non-consequtive inserts with index {} and {}",
end_index,
other.start_index()
);
Operation::Insert {
index,
text: text + other_text,
}
}
(
Operation::Delete {
index,
deleted_character_count,
#[cfg(debug_assertions)]
deleted_text,
},
Operation::Delete {
index: other_index,
deleted_character_count: other_deleted_character_count,
#[cfg(debug_assertions)]
deleted_text: other_deleted_text,
},
) => {
debug_assert!(
index == *other_index,
"Cannot merge non-consequtive deletes",
);
Operation::Delete {
index,
deleted_character_count: deleted_character_count
+ other_deleted_character_count,
#[cfg(debug_assertions)]
deleted_text: deleted_text
.into_iter()
.flat_map(|t1| other_deleted_text.as_ref().map(|t2| t1 + t2).into_iter())
.last(),
}
}
(this, other) => panic!(
"Cannot merge operations of different type: {:?} and {:?}",
&this, &other
),
}
}
}
impl Display for Operation {