From 3677ffa0868f2ba371bafac2e3c1630d1b400f0c Mon Sep 17 00:00:00 2001 From: Axlefublr <101342105+Axlefublr@users.noreply.github.com> Date: Mon, 9 Dec 2024 23:10:38 +0800 Subject: [PATCH] merge 9483 by scdailey Added kakoune's A-S command Small formatting change Updated to use grapheme helpers Change to Range::point and fix keymap formatting --- book/src/generated/static-cmd.md | 1 + book/src/keymap.md | 1 + helix-term/src/commands.rs | 16 ++++++++++++++++ helix-term/src/keymap/default.rs | 1 + 4 files changed, 19 insertions(+) diff --git a/book/src/generated/static-cmd.md b/book/src/generated/static-cmd.md index 85f4d1d5a444..9b9b50b01981 100644 --- a/book/src/generated/static-cmd.md +++ b/book/src/generated/static-cmd.md @@ -65,6 +65,7 @@ | `page_cursor_half_up` | Move page and cursor half up | normal: `` ``, `` Z ``, `` z ``, `` Z ``, `` z ``, select: `` ``, `` Z ``, `` z ``, `` Z ``, `` z `` | | `page_cursor_half_down` | Move page and cursor half down | normal: `` ``, `` Z ``, `` z ``, `` Z ``, `` z ``, select: `` ``, `` Z ``, `` z ``, `` Z ``, `` z `` | | `select_all` | Select whole document | normal: `` % ``, select: `` % `` | +| `select_first_and_last_chars` | Select first and last characters of each selection | normal: `` ``, select: `` `` | | `select_regex` | Select all regex matches inside selections | normal: `` s ``, select: `` s `` | | `split_selection` | Split selections on regex matches | normal: `` S ``, select: `` S `` | | `split_selection_on_newline` | Split selection on newlines | normal: `` ``, select: `` `` | diff --git a/book/src/keymap.md b/book/src/keymap.md index 2797eaee2908..4de35ab09f14 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -117,6 +117,7 @@ Normal mode is the default mode when you launch helix. You can return to it from | `s` | Select all regex matches inside selections | `select_regex` | | `S` | Split selection into sub selections on regex matches | `split_selection` | | `Alt-s` | Split selection on newlines | `split_selection_on_newline` | +| `Alt-S` | Select first and last characters of each selection | `select_first_and_last_chars` | | `Alt-minus` | Merge selections | `merge_selections` | | `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` | | `&` | Align selection in columns | `align_selections` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 61d495d7c9d8..f2b07c467ede 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -349,6 +349,7 @@ impl MappableCommand { page_cursor_half_up, "Move page and cursor half up", page_cursor_half_down, "Move page and cursor half down", select_all, "Select whole document", + select_first_and_last_chars, "Select first and last characters of each selection", select_regex, "Select all regex matches inside selections", split_selection, "Split selections on regex matches", split_selection_on_newline, "Split selection on newlines", @@ -2004,6 +2005,21 @@ fn select_all(cx: &mut Context) { doc.set_selection(view.id, Selection::single(0, end)) } +fn select_first_and_last_chars(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + + let selection = doc.selection(view.id).clone().transform_iter(|range| { + [ + Range::point(range.from()), + Range::point(graphemes::prev_grapheme_boundary(text, range.to())), + ] + .into_iter() + }); + + doc.set_selection(view.id, selection); +} + fn select_regex(cx: &mut Context) { let reg = cx.register.unwrap_or('/'); ui::regex_prompt( diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index c6cefd927574..8a7e0db29090 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -79,6 +79,7 @@ pub fn default() -> HashMap { "s" => select_regex, + "A-S" => select_first_and_last_chars, "A-s" => split_selection_on_newline, "A-minus" => merge_selections, "A-_" => merge_consecutive_selections,