on master help sign in
Validate compose len after applying a is same as before applying b.
commit0b74d423d017b8b201cf9a471f8c1fb1e96de148 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 14 Oct 2020, 11:55 (6 years ago) parent7fcc6f8
1 file changed+17−5
helix-core/src/transaction.rs+17−5
helix-core/src/transaction.rs+17−5
@@ -41,11 +41,26 @@ impl ChangeSet {
4141
4242 // TODO: from iter
4343
44+ #[must_use]
45+ fn len_after(&self) -> usize {
46+ use Operation::*;
47+
48+ let mut len = 0;
49+ for change in &self.changes {
50+ match change {
51+ Retain(i) => len += i,
52+ Insert(s) => len += s.chars().count(),
53+ Delete(_) => (),
54+ }
55+ }
56+ len
57+ }
58+
4459 /// Combine two changesets together.
4560 /// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
4661 /// returned value will represent the change `docA` → `docC`.
4762 pub fn compose(self, other: ChangeSet) -> Result<Self, ()> {
48− // TODO: len before b should match len after a
63+ debug_assert!(self.len_after() == other.len);
4964
5065 let len = self.changes.len();
5166
@@ -182,10 +197,7 @@ impl ChangeSet {
182197 /// Returns a new changeset that reverts this one. Useful for `undo` implementation.
183198 /// The document parameter expects the original document before this change was applied.
184199 pub fn invert(&self, original_doc: &Rope) -> Self {
185− if original_doc.len_chars() != self.len {
186− panic!("Document length mismatch");
187− // return false;
188− }
200+ assert!(original_doc.len_chars() == self.len);
189201
190202 let mut changes = Vec::with_capacity(self.changes.len());
191203 let mut pos = 0;