on master help sign in
Get rid of a bunch of clones.
f098166Blaž Hrastnik
helix-core/src/commands.rs+5−30
@@ -10,47 +10,23 @@ pub type Command = fn(state: &mut State, count: usize);
10
10
11pub fn move_char_left(state: &mut State, count: usize) {
11pub fn move_char_left(state: &mut State, count: usize) {
12 // TODO: use a transaction
12 // TODO: use a transaction
13− let selection = state.move_selection(
13+ let selection = state.move_selection(Direction::Backward, Granularity::Character, count);
14− // TODO: remove the clone here
15− state.selection.clone(),
16− Direction::Backward,
17− Granularity::Character,
18− count,
19− );
20 state.selection = selection;
14 state.selection = selection;
21}
15}
22
16
23pub fn move_char_right(state: &mut State, count: usize) {
17pub fn move_char_right(state: &mut State, count: usize) {
24 // TODO: use a transaction
18 // TODO: use a transaction
25− state.selection = state.move_selection(
19+ state.selection = state.move_selection(Direction::Forward, Granularity::Character, count);
26− // TODO: remove the clone here
27− state.selection.clone(),
28− Direction::Forward,
29− Granularity::Character,
30− count,
31− );
32}
20}
33
21
34pub fn move_line_up(state: &mut State, count: usize) {
22pub fn move_line_up(state: &mut State, count: usize) {
35 // TODO: use a transaction
23 // TODO: use a transaction
36− state.selection = state.move_selection(
24+ state.selection = state.move_selection(Direction::Backward, Granularity::Line, count);
37− // TODO: remove the clone here
38− state.selection.clone(),
39− Direction::Backward,
40− Granularity::Line,
41− count,
42− );
43}
25}
44
26
45pub fn move_line_down(state: &mut State, count: usize) {
27pub fn move_line_down(state: &mut State, count: usize) {
46 // TODO: use a transaction
28 // TODO: use a transaction
47− state.selection = state.move_selection(
29+ state.selection = state.move_selection(Direction::Forward, Granularity::Line, count);
48− // TODO: remove the clone here
49− state.selection.clone(),
50− Direction::Forward,
51− Granularity::Line,
52− count,
53− );
54}
30}
55
31
56// avoid select by default by having a visual mode switch that makes movements into selects
32// 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) {
66
42
67 state.selection = state
43 state.selection = state
68 .selection
44 .selection
69− .clone()
70 .transform(|range| Range::new(range.to(), range.from()))
45 .transform(|range| Range::new(range.to(), range.from()))
71}
46}
72
47
@@ -76,7 +51,7 @@ pub fn append_mode(state: &mut State, _count: usize) {
76
51
77 // TODO: as transaction
52 // TODO: as transaction
78 let text = &state.doc.slice(..);
53 let text = &state.doc.slice(..);
79− state.selection = state.selection.clone().transform(|range| {
54+ state.selection = state.selection.transform(|range| {
80 // TODO: to() + next char
55 // TODO: to() + next char
81 Range::new(range.from(), next_grapheme_boundary(text, range.to()))
56 Range::new(range.from(), next_grapheme_boundary(text, range.to()))
82 })
57 })
helix-core/src/selection.rs+5−2
@@ -211,11 +211,14 @@ impl Selection {
211 }
211 }
212
212
213 /// Takes a closure and maps each selection over the closure.
213 /// 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
215 where
215 where
216 F: Fn(Range) -> Range,
216 F: Fn(Range) -> Range,
217 {
217 {
218+ Self::new(
218− Self::new(self.ranges.into_iter().map(f).collect(), self.primary_index)
219+ self.ranges.iter().copied().map(f).collect(),
220+ self.primary_index,
221+ )
219 }
222 }
220}
223}
221
224
helix-core/src/state.rs+4−6
@@ -115,7 +115,6 @@ impl State {
115
115
116 pub fn move_selection(
116 pub fn move_selection(
117 &self,
117 &self,
118− sel: Selection,
119 dir: Direction,
118 dir: Direction,
120 granularity: Granularity,
119 granularity: Granularity,
121 count: usize,
120 count: usize,
@@ -123,7 +122,7 @@ impl State {
123 // TODO: move all selections according to normal cursor move semantics by collapsing it
122 // TODO: move all selections according to normal cursor move semantics by collapsing it
124 // into cursors and moving them vertically
123 // into cursors and moving them vertically
125
124
126− let ranges = sel.ranges.into_iter().map(|range| {
125+ let ranges = self.selection.ranges.iter().map(|range| {
127 // let pos = if !range.is_empty() {
126 // let pos = if !range.is_empty() {
128 // // if selection already exists, bump it to the start or end of current select first
127 // // if selection already exists, bump it to the start or end of current select first
129 // if dir == Direction::Backward {
128 // if dir == Direction::Backward {
@@ -137,23 +136,22 @@ impl State {
137 SelectionRange::new(pos, pos)
136 SelectionRange::new(pos, pos)
138 });
137 });
139
138
140− Selection::new(ranges.collect(), sel.primary_index)
139+ Selection::new(ranges.collect(), self.selection.primary_index)
141 // TODO: update selection in state via transaction
140 // TODO: update selection in state via transaction
142 }
141 }
143
142
144 pub fn extend_selection(
143 pub fn extend_selection(
145 &self,
144 &self,
146− sel: Selection,
147 dir: Direction,
145 dir: Direction,
148 granularity: Granularity,
146 granularity: Granularity,
149 count: usize,
147 count: usize,
150 ) -> Selection {
148 ) -> Selection {
151− let ranges = sel.ranges.into_iter().map(|range| {
149+ let ranges = self.selection.ranges.iter().map(|range| {
152 let pos = self.move_pos(range.head, dir, granularity, count);
150 let pos = self.move_pos(range.head, dir, granularity, count);
153 SelectionRange::new(range.anchor, pos)
151 SelectionRange::new(range.anchor, pos)
154 });
152 });
155
153
156− Selection::new(ranges.collect(), sel.primary_index)
154+ Selection::new(ranges.collect(), self.selection.primary_index)
157 // TODO: update selection in state via transaction
155 // TODO: update selection in state via transaction
158 }
156 }
159}
157}