on master help sign in
Validate compose len after applying a is same as before applying b.
0b74d42Blaž Hrastnik
helix-core/src/transaction.rs+17−5
@@ -41,11 +41,26 @@ impl ChangeSet {
41
41
42 // TODO: from iter
42 // TODO: from iter
43
43
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+
44 /// Combine two changesets together.
59 /// Combine two changesets together.
45 /// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
60 /// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the
46 /// returned value will represent the change `docA` → `docC`.
61 /// returned value will represent the change `docA` → `docC`.
47 pub fn compose(self, other: ChangeSet) -> Result<Self, ()> {
62 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);
49
64
50 let len = self.changes.len();
65 let len = self.changes.len();
51
66
@@ -182,10 +197,7 @@ impl ChangeSet {
182 /// Returns a new changeset that reverts this one. Useful for `undo` implementation.
197 /// Returns a new changeset that reverts this one. Useful for `undo` implementation.
183 /// The document parameter expects the original document before this change was applied.
198 /// The document parameter expects the original document before this change was applied.
184 pub fn invert(&self, original_doc: &Rope) -> Self {
199 pub fn invert(&self, original_doc: &Rope) -> Self {
185− if original_doc.len_chars() != self.len {
200+ assert!(original_doc.len_chars() == self.len);
186− panic!("Document length mismatch");
187− // return false;
188− }
189
201
190 let mut changes = Vec::with_capacity(self.changes.len());
202 let mut changes = Vec::with_capacity(self.changes.len());
191 let mut pos = 0;
203 let mut pos = 0;