on master help sign in
Drop selection_lines completely, change move_line_start binding
commit9c24f1ec0e325da43a81d654ec91cdf68d4ccafc authorBlaž Hrastnik <blaz@mxxn.io> dateTue 18 May 2021, 18:27 (5 years ago) parentf99a683
3 files changed+24−41
book/src/keymap.md+1−1 helix-term/src/commands.rs+22−39 helix-term/src/keymap.rs+1−1
book/src/keymap.md+1−1
@@ -17,7 +17,7 @@
1717| f | find next char |
1818| T | find 'till previous char |
1919| F | find previous char |
20−| 0 | move to the start of the line |
20+| ^ | move to the start of the line |
2121| $ | move to the end of the line |
2222| m | Jump to matching bracket |
2323| PageUp | Move page up |
helix-term/src/commands.rs+22−39
@@ -186,37 +186,31 @@ pub fn move_line_down(cx: &mut Context) {
186186
187187pub fn move_line_end(cx: &mut Context) {
188188 let (view, doc) = cx.current();
189− let lines = selection_lines(doc.text(), doc.selection(view.id));
190189
191− let positions = lines
192− .into_iter()
193− .map(|index| {
194− // adjust all positions to the end of the line.
195−
196− // Line end is pos at the start of next line - 1
197− // subtract another 1 because the line ends with \n
198− doc.text().line_to_char(index + 1).saturating_sub(2)
199− })
200− .map(|pos| Range::new(pos, pos));
190+ let selection = doc.selection(view.id).transform(|range| {
191+ let text = doc.text();
192+ let line = text.char_to_line(range.head);
201193
202− let selection = Selection::new(positions.collect(), 0);
194+ // Line end is pos at the start of next line - 1
195+ // subtract another 1 because the line ends with \n
196+ let pos = text.line_to_char(line + 1).saturating_sub(2);
197+ Range::new(pos, pos)
198+ });
203199
204200 doc.set_selection(view.id, selection);
205201}
206202
207203pub fn move_line_start(cx: &mut Context) {
208204 let (view, doc) = cx.current();
209− let lines = selection_lines(doc.text(), doc.selection(view.id));
210205
211− let positions = lines
212− .into_iter()
213− .map(|index| {
214− // adjust all positions to the start of the line.
215− doc.text().line_to_char(index)
216− })
217− .map(|pos| Range::new(pos, pos));
206+ let selection = doc.selection(view.id).transform(|range| {
207+ let text = doc.text();
208+ let line = text.char_to_line(range.head);
218209
219− let selection = Selection::new(positions.collect(), 0);
210+ // adjust to start of the line
211+ let pos = text.line_to_char(line);
212+ Range::new(pos, pos)
213+ });
220214
221215 doc.set_selection(view.id, selection);
222216}
@@ -1123,19 +1117,6 @@ pub fn buffer_picker(cx: &mut Context) {
11231117 cx.push_layer(Box::new(picker));
11241118}
11251119
1126−// calculate line numbers for each selection range
1127−fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
1128− let mut lines = selection
1129− .iter()
1130− .map(|range| doc.char_to_line(range.head))
1131− .collect::<Vec<_>>();
1132−
1133− lines.sort_unstable(); // sorting by usize so _unstable is preferred
1134− lines.dedup();
1135−
1136− lines
1137−}
1138−
11391120// I inserts at the start of each line with a selection
11401121pub fn prepend_to_line(cx: &mut Context) {
11411122 move_line_start(cx);
@@ -1169,13 +1150,15 @@ fn open(cx: &mut Context, open: Open) {
11691150 enter_insert_mode(doc);
11701151
11711152 let text = doc.text().slice(..);
1172− let lines = selection_lines(doc.text(), doc.selection(view.id));
1153+ let selection = doc.selection(view.id);
1154+
1155+ let mut ranges = SmallVec::with_capacity(selection.len());
11731156
1174− let mut ranges = SmallVec::with_capacity(lines.len());
1157+ let changes: Vec<Change> = selection
1158+ .iter()
1159+ .map(|range| {
1160+ let line = text.char_to_line(range.head);
11751161
1176− let changes: Vec<Change> = lines
1177− .into_iter()
1178− .map(|line| {
11791162 let line = match open {
11801163 // adjust position to the end of the line (next line - 1)
11811164 Open::Below => line + 1,
helix-term/src/keymap.rs+1−1
@@ -145,7 +145,7 @@ pub fn default() -> Keymaps {
145145 //
146146 key!('r') => commands::replace,
147147
148− key!('0') => commands::move_line_start,
148+ key!('^') => commands::move_line_start,
149149 key!('$') => commands::move_line_end,
150150
151151 key!('w') => commands::move_next_word_start,