From a92674b1a041c786ef39bc885fb4474b07edeff1 Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Mon, 9 May 2022 23:48:37 -0400 Subject: [PATCH 1/5] Add shrink equivalent of extend_to_line_bounds --- helix-term/src/commands.rs | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f0b54e0b7fa4..8ab4a8273f95 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -247,6 +247,7 @@ impl MappableCommand { extend_line, "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)", delete_selection, "Delete selection", delete_selection_noyank, "Delete selection, without yanking", change_selection, "Change selection (delete and enter insert mode)", @@ -1937,6 +1938,46 @@ fn extend_to_line_bounds(cx: &mut Context) { ); } +fn shrink_to_line_bounds(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + + doc.set_selection( + view.id, + doc.selection(view.id).clone().transform(|range| { + let text = doc.text(); + + let (start_line, end_line) = range.line_range(text.slice(..)); + + // Do nothing if the selection is within one line to prevent + // conditional logic for the behavior of this command + if start_line == end_line { + return range; + } + + let mut start = text.line_to_char(start_line); + + // line_to_char gives us the start position of the line, so + // we need to get the start position of the next line, then + // backtrack to get the end position of the last line. + let mut end = text.line_to_char(end_line + 1) - 1; + + if start != range.from() { + start = text.line_to_char((start_line + 1).min(text.len_lines())); + } + + if end != range.to() { + end = text.line_to_char(end_line).saturating_sub(1); + } + + if range.anchor <= range.head { + Range::new(start, end) + } else { + Range::new(end, start) + } + }), + ); +} + enum Operation { Delete, Change, From ac848624f815c17b7b18ed90fa03c0a17b3dd8df Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Sat, 14 May 2022 20:43:30 -0400 Subject: [PATCH 2/5] Add a check for being past rope end in end position calc --- helix-term/src/commands.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8ab4a8273f95..3b246772a0de 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1958,8 +1958,8 @@ fn shrink_to_line_bounds(cx: &mut Context) { // line_to_char gives us the start position of the line, so // we need to get the start position of the next line, then - // backtrack to get the end position of the last line. - let mut end = text.line_to_char(end_line + 1) - 1; + // backtrack to get the end position + 1 of the last line. + let mut end = text.line_to_char((end_line + 1).min(text.len_lines())) - 1; if start != range.from() { start = text.line_to_char((start_line + 1).min(text.len_lines())); From 829086da7f4946aa7f7e59375318b23cf68370db Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Sat, 14 May 2022 20:53:01 -0400 Subject: [PATCH 3/5] Include the EOL character in calculations --- helix-term/src/commands.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3b246772a0de..1d58e29ece83 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1957,16 +1957,17 @@ fn shrink_to_line_bounds(cx: &mut Context) { let mut start = text.line_to_char(start_line); // line_to_char gives us the start position of the line, so - // we need to get the start position of the next line, then - // backtrack to get the end position + 1 of the last line. - let mut end = text.line_to_char((end_line + 1).min(text.len_lines())) - 1; + // we need to get the start position of the next line. In + // the editor, this will correspond to the cursor being on + // the EOL whitespace charactor, which is what we want. + let mut end = text.line_to_char((end_line + 1).min(text.len_lines())); if start != range.from() { start = text.line_to_char((start_line + 1).min(text.len_lines())); } if end != range.to() { - end = text.line_to_char(end_line).saturating_sub(1); + end = text.line_to_char(end_line); } if range.anchor <= range.head { From 48958179ab7d5f148c41dca9adb9b32e0824c63e Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Sat, 14 May 2022 21:00:50 -0400 Subject: [PATCH 4/5] Bind to `A-x` for now --- helix-term/src/keymap/default.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index a8ff8be91bf6..82bddf642841 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -87,7 +87,7 @@ pub fn default() -> HashMap { "%" => select_all, "x" => extend_line, "X" => extend_to_line_bounds, - // crop_to_whole_line + "A-x" => shrink_to_line_bounds, "m" => { "Match" "m" => match_brackets, From 649c64e553eb93604a1acf2321b82b4e4f890189 Mon Sep 17 00:00:00 2001 From: Daniel Poulin Date: Sat, 14 May 2022 22:18:20 -0400 Subject: [PATCH 5/5] Document new keybind --- book/src/keymap.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/keymap.md b/book/src/keymap.md index 9d5d08417789..4844c1f74118 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -107,6 +107,7 @@ | `%` | Select entire file | `select_all` | | `x` | Select current line, if already selected, extend to next line | `extend_line` | | `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` | | `K` | Keep selections matching the regex | `keep_selections` | | `Alt-K` | Remove selections matching the regex | `remove_selections` |