on master help sign in
Implement selection rotation with `(` and `)`
commit66a90130a5f99d769e9f6034025297f78ecaa3ec authorBlaž Hrastnik <blaz@mxxn.io> dateFri 6 Aug 2021, 11:22 (5 years ago) parent953125d
3 files changed+25−4
book/src/keymap.md+2 helix-term/src/commands.rs+21 helix-term/src/keymap.rs+2−4
book/src/keymap.md+2
@@ -76,6 +76,8 @@
7676| `Alt-;` | Flip selection cursor and anchor |
7777| `C` | Copy selection onto the next line |
7878| `Alt-C` | Copy selection onto the previous line |
79+| `(` | Rotate main selection forward |
80+| `)` | Rotate main selection backward |
7981| `%` | Select entire file |
8082| `x` | Select current line, if already selected, extend to next line |
8183| `X` | Extend selection to line bounds (line-wise selection) |
helix-term/src/commands.rs+21
@@ -275,6 +275,8 @@ impl Command {
275275 completion, "Invoke completion popup",
276276 hover, "Show docs for item under cursor",
277277 toggle_comments, "Comment/uncomment selections",
278+ rotate_selections_forward, "Rotate selections forward",
279+ rotate_selections_backward, "Rotate selections backward",
278280 expand_selection, "Expand selection to parent syntax node",
279281 jump_forward, "Jump forward on jumplist",
280282 jump_backward, "Jump backward on jumplist",
@@ -3747,6 +3749,25 @@ fn toggle_comments(cx: &mut Context) {
37473749 doc.append_changes_to_history(view.id);
37483750}
37493751
3752+fn rotate_selections(cx: &mut Context, direction: Direction) {
3753+ let count = cx.count();
3754+ let (view, doc) = current!(cx.editor);
3755+ let mut selection = doc.selection(view.id).clone();
3756+ let index = selection.primary_index();
3757+ let len = selection.len();
3758+ selection.set_primary_index(match direction {
3759+ Direction::Forward => (index + count) % len,
3760+ Direction::Backward => (index + (len.saturating_sub(count) % len)) % len,
3761+ });
3762+ doc.set_selection(view.id, selection);
3763+}
3764+fn rotate_selections_forward(cx: &mut Context) {
3765+ rotate_selections(cx, Direction::Forward)
3766+}
3767+fn rotate_selections_backward(cx: &mut Context) {
3768+ rotate_selections(cx, Direction::Backward)
3769+}
3770+
37503771// tree sitter node selection
37513772
37523773fn expand_selection(cx: &mut Context) {
helix-term/src/keymap.rs+2−4
@@ -451,7 +451,8 @@ impl Default for Keymaps {
451451 // & align selections
452452 // _ trim selections
453453
454− // C / altC = copy (repeat) selections on prev/next lines
454+ "(" => rotate_selections_backward,
455+ ")" => rotate_selections_forward,
455456
456457 "esc" => normal_mode,
457458 "C-b" | "pageup" => page_up,
@@ -508,9 +509,6 @@ impl Default for Keymaps {
508509 "\"" => select_register,
509510 "C-z" => suspend,
510511 });
511− // TODO: decide whether we want normal mode to also be select mode (kakoune-like), or whether
512− // we keep this separate select mode. More keys can fit into normal mode then, but it's weird
513− // because some selection operations can now be done from normal mode, some from select mode.
514512 let mut select = normal.clone();
515513 select.merge_nodes(keymap!({ "Select mode"
516514 "h" | "left" => extend_char_left,