on master help sign in
Simplify some more code.
commit96db02742e85648e4ce0c2ab9b7cd239bdb763f8 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 13 Sep 2020, 23:38 (6 years ago) parent0427acd
4 files changed+16−23
helix-core/src/commands.rs+1−1 helix-core/src/selection.rs+10−2 helix-core/src/state.rs+4−11 helix-core/src/transaction.rs+1−9
helix-core/src/commands.rs+1−1
@@ -62,8 +62,8 @@ pub fn append_mode(state: &mut State, _count: usize) {
6262
6363// TODO: I, A, o and O can share a lot of the primitives.
6464
65+// calculate line numbers for each selection range
6566fn selection_lines(state: &State) -> Vec<usize> {
66− // calculate line numbers for each selection range
6767 let mut lines = state
6868 .selection
6969 .ranges()
helix-core/src/selection.rs+10−2
@@ -100,8 +100,8 @@ impl Range {
100100#[derive(Debug, Clone)]
101101pub struct Selection {
102102 // TODO: decide how many ranges to inline SmallVec<[Range; 1]>
103− pub(crate) ranges: SmallVec<[Range; 1]>,
104− pub(crate) primary_index: usize,
103+ ranges: SmallVec<[Range; 1]>,
104+ primary_index: usize,
105105}
106106
107107impl Selection {
@@ -205,6 +205,14 @@ impl Selection {
205205 }
206206 }
207207
208+ // fast path for a single selection (cursor)
209+ if ranges.len() == 1 {
210+ return Selection {
211+ ranges,
212+ primary_index: 0,
213+ };
214+ }
215+
208216 // TODO: only normalize if needed (any ranges out of order)
209217 normalize(ranges, primary_index)
210218 }
helix-core/src/state.rs+4−11
@@ -122,7 +122,7 @@ impl State {
122122 // TODO: move all selections according to normal cursor move semantics by collapsing it
123123 // into cursors and moving them vertically
124124
125− let ranges = self.selection.ranges.iter().map(|range| {
125+ self.selection.transform(|range| {
126126 // let pos = if !range.is_empty() {
127127 // // if selection already exists, bump it to the start or end of current select first
128128 // if dir == Direction::Backward {
@@ -134,10 +134,7 @@ impl State {
134134 let pos = self.move_pos(range.head, dir, granularity, count);
135135 // };
136136 SelectionRange::new(pos, pos)
137− });
138−
139− Selection::new(ranges.collect(), self.selection.primary_index)
140− // TODO: update selection in state via transaction
137+ })
141138 }
142139
143140 pub fn extend_selection(
@@ -146,13 +143,10 @@ impl State {
146143 granularity: Granularity,
147144 count: usize,
148145 ) -> Selection {
149− let ranges = self.selection.ranges.iter().map(|range| {
146+ self.selection.transform(|range| {
150147 let pos = self.move_pos(range.head, dir, granularity, count);
151148 SelectionRange::new(range.anchor, pos)
152− });
153−
154− Selection::new(ranges.collect(), self.selection.primary_index)
155− // TODO: update selection in state via transaction
149+ })
156150 }
157151}
158152
@@ -203,7 +197,6 @@ mod test {
203197 fn test_coords_at_pos() {
204198 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
205199 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
206− // TODO: what is the coordinate of newline?
207200 assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
208201 assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
209202 assert_eq!(coords_at_pos(&text.slice(..), 7), (1, 1)); // position on o
helix-core/src/transaction.rs+1−9
@@ -3,13 +3,6 @@ use crate::{Rope, Selection, SelectionRange, State, Tendril};
33/// (from, to, replacement)
44pub type Change = (usize, usize, Option<Tendril>);
55
6−// TODO: divided into three different operations, I sort of like having just
7−// Splice { extent, Option<text>, distance } better.
8−// insert: Splice { extent: 0, text: Some("a"), distance: 2 }
9−// delete: Splice { extent: 2, text: None, distance: 2 }
10−// replace: Splice { extent: 2, text: Some("abc"), distance: 2 }
11−// unchanged?: Splice { extent: 0, text: None, distance: 2 }
12−// harder to compose though.
136#[derive(Debug, Clone, PartialEq, Eq)]
147enum Operation {
158 /// Move cursor by n characters.
@@ -364,7 +357,6 @@ impl Transaction {
364357
365358 let mut last = 0;
366359 for (from, to, tendril) in changes {
367− // TODO: need to fill the in-between ranges too
368360 // Retain from last "to" to current "from"
369361 acc.push(Operation::Retain(from - last));
370362 match tendril {
@@ -383,7 +375,7 @@ impl Transaction {
383375 where
384376 F: Fn(&SelectionRange) -> Change,
385377 {
386− Self::change(state, state.selection.ranges.iter().map(f))
378+ Self::change(state, state.selection.ranges().iter().map(f))
387379 }
388380
389381 /// Insert text at each selection head.