From 02cba2a7f403f48eccb18100fb751f7b42373dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 8 Aug 2021 13:26:13 +0900 Subject: [PATCH] Implement alt-( and alt-) to rotate selection contents --- helix-term/src/commands.rs | 45 ++++++++++++++++++++++++++++++++++++++ helix-term/src/keymap.rs | 2 ++ 2 files changed, 47 insertions(+) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7facb556c..95d387ec5 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 9a18c3b7e..ca28a8945 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,