From f098166571fd8fc2046d49355dffc0d2aad29e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sat, 12 Sep 2020 23:52:38 +0900 Subject: [PATCH] Get rid of a bunch of clones. --- helix-core/src/commands.rs | 35 +++++------------------------------ helix-core/src/selection.rs | 7 +++++-- helix-core/src/state.rs | 10 ++++------ 3 files changed, 14 insertions(+), 38 deletions(-) diff --git a/helix-core/src/commands.rs b/helix-core/src/commands.rs index 37d59946..fdb3fab6 100644 --- a/helix-core/src/commands.rs +++ b/helix-core/src/commands.rs @@ -10,47 +10,23 @@ pub type Command = fn(state: &mut State, count: usize); pub fn move_char_left(state: &mut State, count: usize) { // TODO: use a transaction - let selection = state.move_selection( - // TODO: remove the clone here - state.selection.clone(), - Direction::Backward, - Granularity::Character, - count, - ); + let selection = state.move_selection(Direction::Backward, Granularity::Character, count); state.selection = selection; } pub fn move_char_right(state: &mut State, count: usize) { // TODO: use a transaction - state.selection = state.move_selection( - // TODO: remove the clone here - state.selection.clone(), - Direction::Forward, - Granularity::Character, - count, - ); + state.selection = state.move_selection(Direction::Forward, Granularity::Character, count); } pub fn move_line_up(state: &mut State, count: usize) { // TODO: use a transaction - state.selection = state.move_selection( - // TODO: remove the clone here - state.selection.clone(), - Direction::Backward, - Granularity::Line, - count, - ); + state.selection = state.move_selection(Direction::Backward, Granularity::Line, count); } pub fn move_line_down(state: &mut State, count: usize) { // TODO: use a transaction - state.selection = state.move_selection( - // TODO: remove the clone here - state.selection.clone(), - Direction::Forward, - Granularity::Line, - count, - ); + state.selection = state.move_selection(Direction::Forward, Granularity::Line, count); } // avoid select by default by having a visual mode switch that makes movements into selects @@ -66,7 +42,6 @@ pub fn insert_mode(state: &mut State, _count: usize) { state.selection = state .selection - .clone() .transform(|range| Range::new(range.to(), range.from())) } @@ -76,7 +51,7 @@ pub fn append_mode(state: &mut State, _count: usize) { // TODO: as transaction let text = &state.doc.slice(..); - state.selection = state.selection.clone().transform(|range| { + state.selection = state.selection.transform(|range| { // TODO: to() + next char Range::new(range.from(), next_grapheme_boundary(text, range.to())) }) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index a196417a..f6ca424f 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -211,11 +211,14 @@ impl Selection { } /// Takes a closure and maps each selection over the closure. - pub fn transform(self, f: F) -> Self + pub fn transform(&self, f: F) -> Self where F: Fn(Range) -> Range, { - Self::new(self.ranges.into_iter().map(f).collect(), self.primary_index) + Self::new( + self.ranges.iter().copied().map(f).collect(), + self.primary_index, + ) } } diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs index 5f941fb5..511df8fe 100644 --- a/helix-core/src/state.rs +++ b/helix-core/src/state.rs @@ -115,7 +115,6 @@ impl State { pub fn move_selection( &self, - sel: Selection, dir: Direction, granularity: Granularity, count: usize, @@ -123,7 +122,7 @@ impl State { // TODO: move all selections according to normal cursor move semantics by collapsing it // into cursors and moving them vertically - let ranges = sel.ranges.into_iter().map(|range| { + let ranges = self.selection.ranges.iter().map(|range| { // let pos = if !range.is_empty() { // // if selection already exists, bump it to the start or end of current select first // if dir == Direction::Backward { @@ -137,23 +136,22 @@ impl State { SelectionRange::new(pos, pos) }); - Selection::new(ranges.collect(), sel.primary_index) + Selection::new(ranges.collect(), self.selection.primary_index) // TODO: update selection in state via transaction } pub fn extend_selection( &self, - sel: Selection, dir: Direction, granularity: Granularity, count: usize, ) -> Selection { - let ranges = sel.ranges.into_iter().map(|range| { + let ranges = self.selection.ranges.iter().map(|range| { let pos = self.move_pos(range.head, dir, granularity, count); SelectionRange::new(range.anchor, pos) }); - Selection::new(ranges.collect(), sel.primary_index) + Selection::new(ranges.collect(), self.selection.primary_index) // TODO: update selection in state via transaction } }