on master help sign in
core: transaction: Resolve some TODOs
376d99aBlaž Hrastnik
helix-core/src/transaction.rs+9−16
@@ -21,7 +21,6 @@ pub enum Assoc {
21 After,
21 After,
22}
22}
23
23
24−// ChangeSpec = Change | ChangeSet | Vec<Change>
25#[derive(Debug, Default, Clone, PartialEq, Eq)]
24#[derive(Debug, Default, Clone, PartialEq, Eq)]
26pub struct ChangeSet {
25pub struct ChangeSet {
27 pub(crate) changes: Vec<Operation>,
26 pub(crate) changes: Vec<Operation>,
@@ -50,7 +49,6 @@ impl ChangeSet {
50 }
49 }
51
50
52 // TODO: from iter
51 // TODO: from iter
53− //
54
52
55 #[doc(hidden)] // used by lsp to convert to LSP changes
53 #[doc(hidden)] // used by lsp to convert to LSP changes
56 pub fn changes(&self) -> &[Operation] {
54 pub fn changes(&self) -> &[Operation] {
@@ -415,8 +413,6 @@ impl ChangeSet {
415pub struct Transaction {
413pub struct Transaction {
416 changes: ChangeSet,
414 changes: ChangeSet,
417 selection: Option<Selection>,
415 selection: Option<Selection>,
418− // effects, annotations
419− // scroll_into_view
420}
416}
421
417
422impl Transaction {
418impl Transaction {
@@ -440,14 +436,12 @@ impl Transaction {
440
436
441 /// Returns true if applied successfully.
437 /// Returns true if applied successfully.
442 pub fn apply(&self, doc: &mut Rope) -> bool {
438 pub fn apply(&self, doc: &mut Rope) -> bool {
443− if !self.changes.is_empty() {
439+ if self.changes.is_empty() {
444− // apply changes to the document
445− if !self.changes.apply(doc) {
446− return false;
440+ return true;
447− }
448 }
441 }
449
442
450− true
443+ // apply changes to the document
444+ self.changes.apply(doc)
451 }
445 }
452
446
453 /// Generate a transaction that reverts this one.
447 /// Generate a transaction that reverts this one.
@@ -475,7 +469,7 @@ impl Transaction {
475 /// Generate a transaction from a set of changes.
469 /// Generate a transaction from a set of changes.
476 pub fn change<I>(doc: &Rope, changes: I) -> Self
470 pub fn change<I>(doc: &Rope, changes: I) -> Self
477 where
471 where
478− I: IntoIterator<Item = Change> + Iterator,
472+ I: Iterator<Item = Change>,
479 {
473 {
480 let len = doc.len_chars();
474 let len = doc.len_chars();
481
475
@@ -483,12 +477,11 @@ impl Transaction {
483 let size = upper.unwrap_or(lower);
477 let size = upper.unwrap_or(lower);
484 let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
478 let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
485
479
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−
490 let mut last = 0;
480 let mut last = 0;
491 for (from, to, tendril) in changes {
481 for (from, to, tendril) in changes {
482+ // Verify ranges are ordered and not overlapping
483+ debug_assert!(last <= from);
484+
492 // Retain from last "to" to current "from"
485 // Retain from last "to" to current "from"
493 changeset.retain(from - last);
486 changeset.retain(from - last);
494 let span = to - from;
487 let span = to - from;
@@ -694,7 +687,7 @@ mod test {
694 let mut doc = Rope::from("hello world!\ntest 123");
687 let mut doc = Rope::from("hello world!\ntest 123");
695 let transaction = Transaction::change(
688 let transaction = Transaction::change(
696 &doc,
689 &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
698 vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
691 vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
699 );
692 );
700 transaction.apply(&mut doc);
693 transaction.apply(&mut doc);