Skip to content

Commit

Permalink
Implement alt-( and alt-) to rotate selection contents
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Aug 8, 2021
1 parent ba72934 commit 02cba2a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 02cba2a

Please sign in to comment.