on master help sign in
Get rid of a bunch of clones.
commitf098166571fd8fc2046d49355dffc0d2aad29e50 authorBlaž Hrastnik <blaz@mxxn.io> dateSat 12 Sep 2020, 23:52 (6 years ago) parentf9348d7
3 files changed+14−38
helix-core/src/commands.rs+5−30 helix-core/src/selection.rs+5−2 helix-core/src/state.rs+4−6
helix-core/src/commands.rs+5−30
@@ -10,47 +10,23 @@ pub type Command = fn(state: &mut State, count: usize);
1010
1111pub fn move_char_left(state: &mut State, count: usize) {
1212 // TODO: use a transaction
13− let selection = state.move_selection(
14− // TODO: remove the clone here
15− state.selection.clone(),
16− Direction::Backward,
17− Granularity::Character,
18− count,
19− );
13+ let selection = state.move_selection(Direction::Backward, Granularity::Character, count);
2014 state.selection = selection;
2115}
2216
2317pub fn move_char_right(state: &mut State, count: usize) {
2418 // TODO: use a transaction
25− state.selection = state.move_selection(
26− // TODO: remove the clone here
27− state.selection.clone(),
28− Direction::Forward,
29− Granularity::Character,
30− count,
31− );
19+ state.selection = state.move_selection(Direction::Forward, Granularity::Character, count);
3220}
3321
3422pub fn move_line_up(state: &mut State, count: usize) {
3523 // TODO: use a transaction
36− state.selection = state.move_selection(
37− // TODO: remove the clone here
38− state.selection.clone(),
39− Direction::Backward,
40− Granularity::Line,
41− count,
42− );
24+ state.selection = state.move_selection(Direction::Backward, Granularity::Line, count);
4325}
4426
4527pub fn move_line_down(state: &mut State, count: usize) {
4628 // TODO: use a transaction
47− state.selection = state.move_selection(
48− // TODO: remove the clone here
49− state.selection.clone(),
50− Direction::Forward,
51− Granularity::Line,
52− count,
53− );
29+ state.selection = state.move_selection(Direction::Forward, Granularity::Line, count);
5430}
5531
5632// 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) {
6642
6743 state.selection = state
6844 .selection
69− .clone()
7045 .transform(|range| Range::new(range.to(), range.from()))
7146}
7247
@@ -76,7 +51,7 @@ pub fn append_mode(state: &mut State, _count: usize) {
7651
7752 // TODO: as transaction
7853 let text = &state.doc.slice(..);
79− state.selection = state.selection.clone().transform(|range| {
54+ state.selection = state.selection.transform(|range| {
8055 // TODO: to() + next char
8156 Range::new(range.from(), next_grapheme_boundary(text, range.to()))
8257 })
helix-core/src/selection.rs+5−2
@@ -211,11 +211,14 @@ impl Selection {
211211 }
212212
213213 /// Takes a closure and maps each selection over the closure.
214− pub fn transform<F>(self, f: F) -> Self
214+ pub fn transform<F>(&self, f: F) -> Self
215215 where
216216 F: Fn(Range) -> Range,
217217 {
218− Self::new(self.ranges.into_iter().map(f).collect(), self.primary_index)
218+ Self::new(
219+ self.ranges.iter().copied().map(f).collect(),
220+ self.primary_index,
221+ )
219222 }
220223}
221224
helix-core/src/state.rs+4−6
@@ -115,7 +115,6 @@ impl State {
115115
116116 pub fn move_selection(
117117 &self,
118− sel: Selection,
119118 dir: Direction,
120119 granularity: Granularity,
121120 count: usize,
@@ -123,7 +122,7 @@ impl State {
123122 // TODO: move all selections according to normal cursor move semantics by collapsing it
124123 // into cursors and moving them vertically
125124
126− let ranges = sel.ranges.into_iter().map(|range| {
125+ let ranges = self.selection.ranges.iter().map(|range| {
127126 // let pos = if !range.is_empty() {
128127 // // if selection already exists, bump it to the start or end of current select first
129128 // if dir == Direction::Backward {
@@ -137,23 +136,22 @@ impl State {
137136 SelectionRange::new(pos, pos)
138137 });
139138
140− Selection::new(ranges.collect(), sel.primary_index)
139+ Selection::new(ranges.collect(), self.selection.primary_index)
141140 // TODO: update selection in state via transaction
142141 }
143142
144143 pub fn extend_selection(
145144 &self,
146− sel: Selection,
147145 dir: Direction,
148146 granularity: Granularity,
149147 count: usize,
150148 ) -> Selection {
151− let ranges = sel.ranges.into_iter().map(|range| {
149+ let ranges = self.selection.ranges.iter().map(|range| {
152150 let pos = self.move_pos(range.head, dir, granularity, count);
153151 SelectionRange::new(range.anchor, pos)
154152 });
155153
156− Selection::new(ranges.collect(), sel.primary_index)
154+ Selection::new(ranges.collect(), self.selection.primary_index)
157155 // TODO: update selection in state via transaction
158156 }
159157}