From 66a90130a5f99d769e9f6034025297f78ecaa3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 6 Aug 2021 11:22:01 +0900 Subject: [PATCH] Implement selection rotation with `(` and `)` --- book/src/keymap.md | 2 ++ helix-term/src/commands.rs | 21 +++++++++++++++++++++ helix-term/src/keymap.rs | 6 ++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index a33d3dcdd..99a76c781 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -76,6 +76,8 @@ | `Alt-;` | Flip selection cursor and anchor | | `C` | Copy selection onto the next line | | `Alt-C` | Copy selection onto the previous line | +| `(` | Rotate main selection forward | +| `)` | Rotate main selection backward | | `%` | Select entire file | | `x` | Select current line, if already selected, extend to next line | | `X` | Extend selection to line bounds (line-wise selection) | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 622a3ebd4..7facb556c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -275,6 +275,8 @@ impl Command { completion, "Invoke completion popup", hover, "Show docs for item under cursor", toggle_comments, "Comment/uncomment selections", + rotate_selections_forward, "Rotate selections forward", + rotate_selections_backward, "Rotate selections backward", expand_selection, "Expand selection to parent syntax node", jump_forward, "Jump forward on jumplist", jump_backward, "Jump backward on jumplist", @@ -3747,6 +3749,25 @@ fn toggle_comments(cx: &mut Context) { doc.append_changes_to_history(view.id); } +fn rotate_selections(cx: &mut Context, direction: Direction) { + let count = cx.count(); + let (view, doc) = current!(cx.editor); + let mut selection = doc.selection(view.id).clone(); + let index = selection.primary_index(); + let len = selection.len(); + selection.set_primary_index(match direction { + Direction::Forward => (index + count) % len, + Direction::Backward => (index + (len.saturating_sub(count) % len)) % len, + }); + doc.set_selection(view.id, selection); +} +fn rotate_selections_forward(cx: &mut Context) { + rotate_selections(cx, Direction::Forward) +} +fn rotate_selections_backward(cx: &mut Context) { + rotate_selections(cx, Direction::Backward) +} + // tree sitter node selection fn expand_selection(cx: &mut Context) { diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index ef283a63d..9a18c3b7e 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -451,7 +451,8 @@ impl Default for Keymaps { // & align selections // _ trim selections - // C / altC = copy (repeat) selections on prev/next lines + "(" => rotate_selections_backward, + ")" => rotate_selections_forward, "esc" => normal_mode, "C-b" | "pageup" => page_up, @@ -508,9 +509,6 @@ impl Default for Keymaps { "\"" => select_register, "C-z" => suspend, }); - // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether - // we keep this separate select mode. More keys can fit into normal mode then, but it's weird - // because some selection operations can now be done from normal mode, some from select mode. let mut select = normal.clone(); select.merge_nodes(keymap!({ "Select mode" "h" | "left" => extend_char_left,