on master help sign in
core: transaction: Resolve some TODOs
commit376d99a51d35c258182a088515285a99cc109073 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 3 Mar 2022, 17:03 (5 years ago) parentadf97e0
1 file changed+9−16
helix-core/src/transaction.rs+9−16
helix-core/src/transaction.rs+9−16
@@ -21,7 +21,6 @@ pub enum Assoc {
2121 After,
2222}
2323
24−// ChangeSpec = Change | ChangeSet | Vec<Change>
2524#[derive(Debug, Default, Clone, PartialEq, Eq)]
2625pub struct ChangeSet {
2726 pub(crate) changes: Vec<Operation>,
@@ -50,7 +49,6 @@ impl ChangeSet {
5049 }
5150
5251 // TODO: from iter
53− //
5452
5553 #[doc(hidden)] // used by lsp to convert to LSP changes
5654 pub fn changes(&self) -> &[Operation] {
@@ -415,8 +413,6 @@ impl ChangeSet {
415413pub struct Transaction {
416414 changes: ChangeSet,
417415 selection: Option<Selection>,
418− // effects, annotations
419− // scroll_into_view
420416}
421417
422418impl Transaction {
@@ -440,14 +436,12 @@ impl Transaction {
440436
441437 /// Returns true if applied successfully.
442438 pub fn apply(&self, doc: &mut Rope) -> bool {
443− if !self.changes.is_empty() {
444− // apply changes to the document
445− if !self.changes.apply(doc) {
446− return false;
447− }
439+ if self.changes.is_empty() {
440+ return true;
448441 }
449442
450− true
443+ // apply changes to the document
444+ self.changes.apply(doc)
451445 }
452446
453447 /// Generate a transaction that reverts this one.
@@ -475,7 +469,7 @@ impl Transaction {
475469 /// Generate a transaction from a set of changes.
476470 pub fn change<I>(doc: &Rope, changes: I) -> Self
477471 where
478− I: IntoIterator<Item = Change> + Iterator,
472+ I: Iterator<Item = Change>,
479473 {
480474 let len = doc.len_chars();
481475
@@ -483,12 +477,11 @@ impl Transaction {
483477 let size = upper.unwrap_or(lower);
484478 let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
485479
486− // TODO: verify ranges are ordered and not overlapping or change will panic.
487−
488− // TODO: test for (pos, pos, None) to factor out as nothing
489−
490480 let mut last = 0;
491481 for (from, to, tendril) in changes {
482+ // Verify ranges are ordered and not overlapping
483+ debug_assert!(last <= from);
484+
492485 // Retain from last "to" to current "from"
493486 changeset.retain(from - last);
494487 let span = to - from;
@@ -694,7 +687,7 @@ mod test {
694687 let mut doc = Rope::from("hello world!\ntest 123");
695688 let transaction = Transaction::change(
696689 &doc,
697− // (1, 1, None) is a useless 0-width delete
690+ // (1, 1, None) is a useless 0-width delete that gets factored out
698691 vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
699692 );
700693 transaction.apply(&mut doc);