on master help sign in
Implement selection rotation with `(` and `)`
66a9013Blaž Hrastnik
book/src/keymap.md+2
@@ -76,6 +76,8 @@
76| `Alt-;` | Flip selection cursor and anchor |
76| `Alt-;` | Flip selection cursor and anchor |
77| `C` | Copy selection onto the next line |
77| `C` | Copy selection onto the next line |
78| `Alt-C` | Copy selection onto the previous line |
78| `Alt-C` | Copy selection onto the previous line |
79+| `(` | Rotate main selection forward |
80+| `)` | Rotate main selection backward |
79| `%` | Select entire file |
81| `%` | Select entire file |
80| `x` | Select current line, if already selected, extend to next line |
82| `x` | Select current line, if already selected, extend to next line |
81| `X` | Extend selection to line bounds (line-wise selection) |
83| `X` | Extend selection to line bounds (line-wise selection) |
helix-term/src/commands.rs+21
@@ -275,6 +275,8 @@ impl Command {
275 completion, "Invoke completion popup",
275 completion, "Invoke completion popup",
276 hover, "Show docs for item under cursor",
276 hover, "Show docs for item under cursor",
277 toggle_comments, "Comment/uncomment selections",
277 toggle_comments, "Comment/uncomment selections",
278+ rotate_selections_forward, "Rotate selections forward",
279+ rotate_selections_backward, "Rotate selections backward",
278 expand_selection, "Expand selection to parent syntax node",
280 expand_selection, "Expand selection to parent syntax node",
279 jump_forward, "Jump forward on jumplist",
281 jump_forward, "Jump forward on jumplist",
280 jump_backward, "Jump backward on jumplist",
282 jump_backward, "Jump backward on jumplist",
@@ -3747,6 +3749,25 @@ fn toggle_comments(cx: &mut Context) {
3747 doc.append_changes_to_history(view.id);
3749 doc.append_changes_to_history(view.id);
3748}
3750}
3749
3751
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+
3750// tree sitter node selection
3771// tree sitter node selection
3751
3772
3752fn expand_selection(cx: &mut Context) {
3773fn expand_selection(cx: &mut Context) {
helix-term/src/keymap.rs+2−4
@@ -451,7 +451,8 @@ impl Default for Keymaps {
451 // & align selections
451 // & align selections
452 // _ trim selections
452 // _ trim selections
453
453
454− // C / altC = copy (repeat) selections on prev/next lines
454+ "(" => rotate_selections_backward,
455+ ")" => rotate_selections_forward,
455
456
456 "esc" => normal_mode,
457 "esc" => normal_mode,
457 "C-b" | "pageup" => page_up,
458 "C-b" | "pageup" => page_up,
@@ -508,9 +509,6 @@ impl Default for Keymaps {
508 "\"" => select_register,
509 "\"" => select_register,
509 "C-z" => suspend,
510 "C-z" => suspend,
510 });
511 });
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.
514 let mut select = normal.clone();
512 let mut select = normal.clone();
515 select.merge_nodes(keymap!({ "Select mode"
513 select.merge_nodes(keymap!({ "Select mode"
516 "h" | "left" => extend_char_left,
514 "h" | "left" => extend_char_left,