From 0f12dd98150a90f2ab7398b9b92cbd24b93f98d9 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Tue, 12 Jul 2022 17:09:14 +0800 Subject: [PATCH 1/4] let extend-line respect range direction --- helix-term/src/commands.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 193d5d405fb2..834fd0ed0555 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1922,7 +1922,12 @@ enum Extend { } fn extend_line(cx: &mut Context) { - extend_line_impl(cx, Extend::Below); + let (view, doc) = current_ref!(cx.editor); + let extend = match doc.selection(view.id).primary().direction() { + Direction::Forward => Extend::Below, + Direction::Backward => Extend::Above, + }; + extend_line_impl(cx, extend); } fn extend_line_above(cx: &mut Context) { From d365f3c937ea2ffde691aff4e65f1050122c0d84 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Tue, 12 Jul 2022 17:30:45 +0800 Subject: [PATCH 2/4] fix extend above logic --- helix-term/src/commands.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 834fd0ed0555..d159163c5538 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1942,20 +1942,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) { let selection = doc.selection(view.id).clone().transform(|range| { let (start_line, end_line) = range.line_range(text.slice(..)); - let start = text.line_to_char(start_line); - let end = text.line_to_char((end_line + count).min(text.len_lines())); + let start = text.line_to_char(match extend { + Extend::Above => start_line.saturating_sub(count), + Extend::Below => start_line, + }); + let end = text.line_to_char( + match extend { + Extend::Above => end_line + 1, // the start of next line + Extend::Below => (end_line + count), + } + .min(text.len_lines()), + ); // extend to previous/next line if current line is selected let (anchor, head) = if range.from() == start && range.to() == end { match extend { - Extend::Above => (end, text.line_to_char(start_line.saturating_sub(1))), + Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count + 1))), Extend::Below => ( start, text.line_to_char((end_line + count + 1).min(text.len_lines())), ), } } else { - (start, end) + match extend { + Extend::Above => (end, start), + Extend::Below => (start, end), + } }; Range::new(anchor, head) From ceeae91f417003826d6681e9eb927ea0934a7539 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Wed, 13 Jul 2022 07:28:31 +0800 Subject: [PATCH 3/4] keep `x` existing binding --- book/src/keymap.md | 2 +- helix-term/src/commands.rs | 7 ++++++- helix-term/src/keymap/default.rs | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 5daeec3fa3b5..2b5e4cd76526 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -105,7 +105,7 @@ | `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` | | `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` | | `%` | Select entire file | `select_all` | -| `x` | Select current line, if already selected, extend to next line | `extend_line` | +| `x` | Select current line, if already selected, extend to next line | `extend_line_below` | | `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` | | `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` | | `J` | Join lines inside selection | `join_selections` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d159163c5538..54a97952306c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -246,7 +246,8 @@ impl MappableCommand { extend_search_prev, "Add previous search match to selection", search_selection, "Use current selection as search pattern", global_search, "Global Search in workspace folder", - extend_line, "Select current line, if already selected, extend to next line", + extend_line, "Select current line, if already selected, extend to another line based on the anchor", + extend_line_below, "Select current line, if already selected, extend to next line", extend_line_above, "Select current line, if already selected, extend to previous line", extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)", shrink_to_line_bounds, "Shrink selection to line bounds (line-wise selection)", @@ -1930,6 +1931,10 @@ fn extend_line(cx: &mut Context) { extend_line_impl(cx, extend); } +fn extend_line_below(cx: &mut Context) { + extend_line_impl(cx, Extend::Below); +} + fn extend_line_above(cx: &mut Context) { extend_line_impl(cx, Extend::Above); } diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 9b1447581b98..99e233402127 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -85,7 +85,7 @@ pub fn default() -> HashMap { "A-n" | "A-right" => select_next_sibling, "%" => select_all, - "x" => extend_line, + "x" => extend_line_below, "X" => extend_to_line_bounds, "A-x" => shrink_to_line_bounds, From e65961eb7f61a037d70095c8bdee2cf925e31b33 Mon Sep 17 00:00:00 2001 From: Bob Date: Wed, 10 Aug 2022 12:46:24 +0800 Subject: [PATCH 4/4] Update book/src/keymap.md Co-authored-by: Ivan Tham --- book/src/keymap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index fa3b1828d143..bb1dec692a15 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -125,7 +125,7 @@ | `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` | | `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` | | `%` | Select entire file | `select_all` | -| `x` | Select current line, if already selected, extend to next line | `extend_line_below` | +| `x` | Select current line, if already selected, extend to next line | `extend_line_below` | | `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` | | `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` | | `J` | Join lines inside selection | `join_selections` |