on master help sign in
Drop selection_lines completely, change move_line_start binding
9c24f1eBlaž Hrastnik
book/src/keymap.md+1−1
@@ -17,7 +17,7 @@
17| f | find next char |
17| f | find next char |
18| T | find 'till previous char |
18| T | find 'till previous char |
19| F | find previous char |
19| F | find previous char |
20−| 0 | move to the start of the line |
20+| ^ | move to the start of the line |
21| $ | move to the end of the line |
21| $ | move to the end of the line |
22| m | Jump to matching bracket |
22| m | Jump to matching bracket |
23| PageUp | Move page up |
23| PageUp | Move page up |
helix-term/src/commands.rs+22−39
@@ -186,37 +186,31 @@ pub fn move_line_down(cx: &mut Context) {
186
186
187pub fn move_line_end(cx: &mut Context) {
187pub fn move_line_end(cx: &mut Context) {
188 let (view, doc) = cx.current();
188 let (view, doc) = cx.current();
189− let lines = selection_lines(doc.text(), doc.selection(view.id));
190
189
191− let positions = lines
190+ let selection = doc.selection(view.id).transform(|range| {
192− .into_iter()
191+ let text = doc.text();
193− .map(|index| {
192+ let line = text.char_to_line(range.head);
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));
201
193
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+ });
203
199
204 doc.set_selection(view.id, selection);
200 doc.set_selection(view.id, selection);
205}
201}
206
202
207pub fn move_line_start(cx: &mut Context) {
203pub fn move_line_start(cx: &mut Context) {
208 let (view, doc) = cx.current();
204 let (view, doc) = cx.current();
209− let lines = selection_lines(doc.text(), doc.selection(view.id));
210
205
211− let positions = lines
206+ let selection = doc.selection(view.id).transform(|range| {
212− .into_iter()
207+ let text = doc.text();
213− .map(|index| {
208+ let line = text.char_to_line(range.head);
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));
218
209
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+ });
220
214
221 doc.set_selection(view.id, selection);
215 doc.set_selection(view.id, selection);
222}
216}
@@ -1123,19 +1117,6 @@ pub fn buffer_picker(cx: &mut Context) {
1123 cx.push_layer(Box::new(picker));
1117 cx.push_layer(Box::new(picker));
1124}
1118}
1125
1119
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−
1139// I inserts at the start of each line with a selection
1120// I inserts at the start of each line with a selection
1140pub fn prepend_to_line(cx: &mut Context) {
1121pub fn prepend_to_line(cx: &mut Context) {
1141 move_line_start(cx);
1122 move_line_start(cx);
@@ -1169,13 +1150,15 @@ fn open(cx: &mut Context, open: Open) {
1169 enter_insert_mode(doc);
1150 enter_insert_mode(doc);
1170
1151
1171 let text = doc.text().slice(..);
1152 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());
1173
1156
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);
1175
1161
1176− let changes: Vec<Change> = lines
1177− .into_iter()
1178− .map(|line| {
1179 let line = match open {
1162 let line = match open {
1180 // adjust position to the end of the line (next line - 1)
1163 // adjust position to the end of the line (next line - 1)
1181 Open::Below => line + 1,
1164 Open::Below => line + 1,
helix-term/src/keymap.rs+1−1
@@ -145,7 +145,7 @@ pub fn default() -> Keymaps {
145 //
145 //
146 key!('r') => commands::replace,
146 key!('r') => commands::replace,
147
147
148− key!('0') => commands::move_line_start,
148+ key!('^') => commands::move_line_start,
149 key!('$') => commands::move_line_end,
149 key!('$') => commands::move_line_end,
150
150
151 key!('w') => commands::move_next_word_start,
151 key!('w') => commands::move_next_word_start,