Skip to content

Commit

Permalink
Use Iterator::reduce to compose history transactions
Browse files Browse the repository at this point in the history
Co-authored-by: Blaž Hrastnik <[email protected]>
  • Loading branch information
the-mikedavis and archseer committed Nov 23, 2022
1 parent 8f08375 commit 187858f
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions helix-core/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct History {
}

/// A single point in history. See [History] for more information.
#[derive(Debug)]
#[derive(Debug, Clone)]
struct Revision {
parent: usize,
last_child: Option<NonZeroUsize>,
Expand Down Expand Up @@ -126,15 +126,13 @@ impl History {
return None;
}

let mut transaction = self.revisions[revision].transaction.clone();

// The bounds are checked in the if condition above:
// `revision + 1` is known to be `<= self.current`.
for revision in &self.revisions[revision + 1..self.current] {
transaction = transaction.compose(revision.transaction.clone());
}

Some(transaction)
// `revision` is known to be `< self.current`.
self.revisions[revision..self.current]
.iter()
.map(|revision| &revision.transaction)
.cloned()
.reduce(|acc, transaction| acc.compose(transaction))
}

/// Undo the last edit.
Expand Down

0 comments on commit 187858f

Please sign in to comment.