feat(commands): ensure_selections_forward (#1393)

* feat(commands): ensure_selections_forward

Add command that ensures that selections are in forward direction.

Fixes: https://github.com/helix-editor/helix/issues/1332

* Add keybinding for ensure_selections_forward

Add `A-:` keybinding for the ensure_selections_forward command.

* Re-use range.flip for flip_selections command
pull/1399/head
Matouš Dzivjak 3 years ago committed by GitHub
parent dc1faa35cb
commit bd2ab5be43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -122,6 +122,15 @@ impl Range {
} }
} }
// flips the direction of the selection
pub fn flip(&self) -> Self {
Self {
anchor: self.head,
head: self.anchor,
horiz: self.horiz,
}
}
/// Check two ranges for overlap. /// Check two ranges for overlap.
#[must_use] #[must_use]
pub fn overlaps(&self, other: &Self) -> bool { pub fn overlaps(&self, other: &Self) -> bool {

@ -266,6 +266,7 @@ impl MappableCommand {
change_selection_noyank, "Change selection (delete and enter insert mode, without yanking)", change_selection_noyank, "Change selection (delete and enter insert mode, without yanking)",
collapse_selection, "Collapse selection onto a single cursor", collapse_selection, "Collapse selection onto a single cursor",
flip_selections, "Flip selection cursor and anchor", flip_selections, "Flip selection cursor and anchor",
ensure_selections_forward, "Ensure the selection is in forward direction",
insert_mode, "Insert before selection", insert_mode, "Insert before selection",
append_mode, "Insert after selection (append)", append_mode, "Insert after selection (append)",
command_mode, "Enter command mode", command_mode, "Enter command mode",
@ -1904,7 +1905,21 @@ fn flip_selections(cx: &mut Context) {
let selection = doc let selection = doc
.selection(view.id) .selection(view.id)
.clone() .clone()
.transform(|range| Range::new(range.head, range.anchor)); .transform(|range| range.flip());
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); doc.set_selection(view.id, selection);
} }

@ -617,6 +617,8 @@ impl Default for Keymaps {
"A-(" => rotate_selection_contents_backward, "A-(" => rotate_selection_contents_backward,
"A-)" => rotate_selection_contents_forward, "A-)" => rotate_selection_contents_forward,
"A-:" => ensure_selections_forward,
"esc" => normal_mode, "esc" => normal_mode,
"C-b" | "pageup" => page_up, "C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down, "C-f" | "pagedown" => page_down,

Loading…
Cancel
Save