From 09ced2017c79896e25b836f45c6ce56963aaa199 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 22 Nov 2022 21:13:50 -0600 Subject: [PATCH] Use Iterator::reduce to compose history transactions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-core/src/history.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 8a8702b9cc6aa..2b07bd8f7f3b1 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -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, @@ -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() + .cloned() + .map(|revision| revision.transaction) + .reduce(|acc, transaction| acc.compose(transaction)) } /// Undo the last edit.