aboutsummaryrefslogtreecommitdiff
path: root/helix-core
diff options
context:
space:
mode:
authorBlaž Hrastnik2020-10-14 02:55:24 +0000
committerBlaž Hrastnik2020-10-14 04:35:54 +0000
commit0b74d423d017b8b201cf9a471f8c1fb1e96de148 (patch)
tree3660e89aa047d20d98cf96fba9dfcfe5abe49768 /helix-core
parent7fcc6f8f1baab380c3c11a7fa4ca4018f073d52e (diff)
Validate compose len after applying a is same as before applying b.
Diffstat (limited to 'helix-core')
-rw-r--r--helix-core/src/transaction.rs22
1 files changed, 17 insertions, 5 deletions
diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs
index c888de3a..6f3956aa 100644
--- a/helix-core/src/transaction.rs
+++ b/helix-core/src/transaction.rs
@@ -41,11 +41,26 @@ impl ChangeSet {
// TODO: from iter
+ #[must_use]
+ fn len_after(&self) -> usize {
+ use Operation::*;
+
+ let mut len = 0;
+ for change in &self.changes {
+ match change {
+ Retain(i) => len += i,
+ Insert(s) => len += s.chars().count(),
+ Delete(_) => (),
+ }
+ }
+ len
+ }
+
/// Combine two changesets together.
/// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
/// returned value will represent the change `docA` → `docC`.
pub fn compose(self, other: ChangeSet) -> Result<Self, ()> {
- // TODO: len before b should match len after a
+ debug_assert!(self.len_after() == other.len);
let len = self.changes.len();
@@ -182,10 +197,7 @@ impl ChangeSet {
/// Returns a new changeset that reverts this one. Useful for `undo` implementation.
/// The document parameter expects the original document before this change was applied.
pub fn invert(&self, original_doc: &Rope) -> Self {
- if original_doc.len_chars() != self.len {
- panic!("Document length mismatch");
- // return false;
- }
+ assert!(original_doc.len_chars() == self.len);
let mut changes = Vec::with_capacity(self.changes.len());
let mut pos = 0;