diff --git a/book/src/keymap.md b/book/src/keymap.md index 4c7c83060..9c8c23d6e 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -17,7 +17,7 @@ ### Movement | f | find next char | | T | find 'till previous char | | F | find previous char | -| 0 | move to the start of the line | +| ^ | move to the start of the line | | $ | move to the end of the line | | m | Jump to matching bracket | | PageUp | Move page up | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 39022a063..45f8420ef 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -186,37 +186,31 @@ pub fn move_line_down(cx: &mut Context) { pub fn move_line_end(cx: &mut Context) { let (view, doc) = cx.current(); - let lines = selection_lines(doc.text(), doc.selection(view.id)); - let positions = lines - .into_iter() - .map(|index| { - // adjust all positions to the end of the line. + let selection = doc.selection(view.id).transform(|range| { + let text = doc.text(); + let line = text.char_to_line(range.head); - // Line end is pos at the start of next line - 1 - // subtract another 1 because the line ends with \n - doc.text().line_to_char(index + 1).saturating_sub(2) - }) - .map(|pos| Range::new(pos, pos)); - - let selection = Selection::new(positions.collect(), 0); + // Line end is pos at the start of next line - 1 + // subtract another 1 because the line ends with \n + let pos = text.line_to_char(line + 1).saturating_sub(2); + Range::new(pos, pos) + }); doc.set_selection(view.id, selection); } pub fn move_line_start(cx: &mut Context) { let (view, doc) = cx.current(); - let lines = selection_lines(doc.text(), doc.selection(view.id)); - let positions = lines - .into_iter() - .map(|index| { - // adjust all positions to the start of the line. - doc.text().line_to_char(index) - }) - .map(|pos| Range::new(pos, pos)); + let selection = doc.selection(view.id).transform(|range| { + let text = doc.text(); + let line = text.char_to_line(range.head); - let selection = Selection::new(positions.collect(), 0); + // adjust to start of the line + let pos = text.line_to_char(line); + Range::new(pos, pos) + }); doc.set_selection(view.id, selection); } @@ -1123,19 +1117,6 @@ pub fn buffer_picker(cx: &mut Context) { cx.push_layer(Box::new(picker)); } -// calculate line numbers for each selection range -fn selection_lines(doc: &Rope, selection: &Selection) -> Vec { - let mut lines = selection - .iter() - .map(|range| doc.char_to_line(range.head)) - .collect::>(); - - lines.sort_unstable(); // sorting by usize so _unstable is preferred - lines.dedup(); - - lines -} - // I inserts at the start of each line with a selection pub fn prepend_to_line(cx: &mut Context) { move_line_start(cx); @@ -1169,13 +1150,15 @@ fn open(cx: &mut Context, open: Open) { enter_insert_mode(doc); let text = doc.text().slice(..); - let lines = selection_lines(doc.text(), doc.selection(view.id)); + let selection = doc.selection(view.id); - let mut ranges = SmallVec::with_capacity(lines.len()); + let mut ranges = SmallVec::with_capacity(selection.len()); + + let changes: Vec = selection + .iter() + .map(|range| { + let line = text.char_to_line(range.head); - let changes: Vec = lines - .into_iter() - .map(|line| { let line = match open { // adjust position to the end of the line (next line - 1) Open::Below => line + 1, diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index a1d483c78..044d97ebf 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -145,7 +145,7 @@ pub fn default() -> Keymaps { // key!('r') => commands::replace, - key!('0') => commands::move_line_start, + key!('^') => commands::move_line_start, key!('$') => commands::move_line_end, key!('w') => commands::move_next_word_start,