Skip to content

Commit

Permalink
Add reverse_selection_contents (helix-editor#7329)
Browse files Browse the repository at this point in the history
  • Loading branch information
clo4 authored and rozaliev committed Jun 13, 2023
1 parent 8b44bcf commit fc7036f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ impl MappableCommand {
rotate_selections_backward, "Rotate selections backward",
rotate_selection_contents_forward, "Rotate selection contents forward",
rotate_selection_contents_backward, "Rotate selections contents backward",
reverse_selection_contents, "Reverse selections contents",
expand_selection, "Expand selection to parent syntax node",
shrink_selection, "Shrink selection to previously expanded syntax node",
select_next_sibling, "Select next sibling in syntax tree",
Expand Down Expand Up @@ -4491,7 +4492,13 @@ fn rotate_selections_backward(cx: &mut Context) {
rotate_selections(cx, Direction::Backward)
}

fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
enum ReorderStrategy {
RotateForward,
RotateBackward,
Reverse,
}

fn reorder_selection_contents(cx: &mut Context, strategy: ReorderStrategy) {
let count = cx.count;
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand All @@ -4509,9 +4516,10 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {

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),
match strategy {
ReorderStrategy::RotateForward => chunk.rotate_right(1),
ReorderStrategy::RotateBackward => chunk.rotate_left(1),
ReorderStrategy::Reverse => chunk.reverse(),
};
}

Expand All @@ -4528,10 +4536,13 @@ fn rotate_selection_contents(cx: &mut Context, direction: Direction) {
}

fn rotate_selection_contents_forward(cx: &mut Context) {
rotate_selection_contents(cx, Direction::Forward)
reorder_selection_contents(cx, ReorderStrategy::RotateForward)
}
fn rotate_selection_contents_backward(cx: &mut Context) {
rotate_selection_contents(cx, Direction::Backward)
reorder_selection_contents(cx, ReorderStrategy::RotateBackward)
}
fn reverse_selection_contents(cx: &mut Context) {
reorder_selection_contents(cx, ReorderStrategy::Reverse)
}

// tree sitter node selection
Expand Down

0 comments on commit fc7036f

Please sign in to comment.