Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): ensure_selections_forward #1393

Merged
merged 3 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ impl Range {
}
}

// flips the direction of the selection
pub fn flip(&self) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Range is Copy so self can be used:

Suggested change
pub fn flip(&self) -> Self {
pub fn flip(self) -> Self {

Doesn't seem to change the compiler output though. I also attempted to use

    pub fn flip(mut self) -> Self {
        std::mem::swap(&mut self.head, &mut self.anchor);
        self
    }

to see if it would generate better assembly but it ended up worse: https://rust.godbolt.org/z/rP8vva9G6

Self {
anchor: self.head,
head: self.anchor,
horiz: self.horiz,
}
}

/// Check two ranges for overlap.
#[must_use]
pub fn overlaps(&self, other: &Self) -> bool {
Expand Down
15 changes: 15 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ impl MappableCommand {
change_selection_noyank, "Change selection (delete and enter insert mode, without yanking)",
collapse_selection, "Collapse selection onto a single cursor",
flip_selections, "Flip selection cursor and anchor",
ensure_selections_forward, "Ensure the selection is in forward direction",
insert_mode, "Insert before selection",
append_mode, "Insert after selection (append)",
command_mode, "Enter command mode",
Expand Down Expand Up @@ -1908,6 +1909,20 @@ fn flip_selections(cx: &mut Context) {
doc.set_selection(view.id, selection);
}

fn ensure_selections_forward(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

let selection = doc
.selection(view.id)
.clone()
.transform(|r| match r.direction() {
Direction::Forward => r,
Direction::Backward => r.flip(),
});

doc.set_selection(view.id, selection);
}

fn enter_insert_mode(doc: &mut Document) {
doc.mode = Mode::Insert;
}
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 @@ -617,6 +617,8 @@ impl Default for Keymaps {
"A-(" => rotate_selection_contents_backward,
"A-)" => rotate_selection_contents_forward,

"A-:" => ensure_selections_forward,

"esc" => normal_mode,
"C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down,
Expand Down