diff options
author | Blaž Hrastnik | 2021-08-08 04:26:13 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-08-08 04:26:13 +0000 |
commit | 02cba2a7f403f48eccb18100fb751f7b42373dba (patch) | |
tree | 5459041f2d91e75cdd0eea4f4d96f3ec67244cd7 /helix-term/src | |
parent | ba729349b8b443ab362f2f8090a573194b032710 (diff) |
Implement alt-( and alt-) to rotate selection contents
Diffstat (limited to 'helix-term/src')
-rw-r--r-- | helix-term/src/commands.rs | 45 | ||||
-rw-r--r-- | helix-term/src/keymap.rs | 2 |
2 files changed, 47 insertions, 0 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7facb556..95d387ec 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -277,6 +277,8 @@ impl Command { toggle_comments, "Comment/uncomment selections", rotate_selections_forward, "Rotate selections forward", rotate_selections_backward, "Rotate selections backward", + rotate_selection_contents_forward, "Rotate selection contents forward", + rotate_selection_contents_backward, "Rotate selections contents backward", expand_selection, "Expand selection to parent syntax node", jump_forward, "Jump forward on jumplist", jump_backward, "Jump backward on jumplist", @@ -3768,6 +3770,49 @@ fn rotate_selections_backward(cx: &mut Context) { rotate_selections(cx, Direction::Backward) } +fn rotate_selection_contents(cx: &mut Context, direction: Direction) { + let count = cx.count; + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + + let selection = doc.selection(view.id); + let mut fragments: Vec<_> = selection + .fragments(text) + .map(|fragment| Tendril::from_slice(&fragment)) + .collect(); + + let group = count + .map(|count| count.get()) + .unwrap_or(fragments.len()) // default to rotating everything as one group + .min(fragments.len()); + + for chunk in fragments.chunks_mut(group) { + // TODO: also modify main index + match direction { + Direction::Forward => chunk.rotate_right(1), + Direction::Backward => chunk.rotate_left(1), + }; + } + + let transaction = Transaction::change( + doc.text(), + selection + .ranges() + .iter() + .zip(fragments) + .map(|(range, fragment)| (range.from(), range.to(), Some(fragment))), + ); + + doc.apply(&transaction, view.id); + doc.append_changes_to_history(view.id); +} +fn rotate_selection_contents_forward(cx: &mut Context) { + rotate_selection_contents(cx, Direction::Forward) +} +fn rotate_selection_contents_backward(cx: &mut Context) { + rotate_selection_contents(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 9a18c3b7..ca28a894 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -453,6 +453,8 @@ impl Default for Keymaps { "(" => rotate_selections_backward, ")" => rotate_selections_forward, + "A-(" => rotate_selection_contents_backward, + "A-)" => rotate_selection_contents_forward, "esc" => normal_mode, "C-b" | "pageup" => page_up, |