on master help sign in
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

commitbd2ab5be4331a2ab6d406378a9c0868eb2f72819 authorMatouš Dzivjak <matousdzivjak@gmail.com> dateWed 29 Dec 2021, 10:17 (5 years ago) parentdc1faa3
3 files changed+27−1
helix-core/src/selection.rs+9 helix-term/src/commands.rs+16−1 helix-term/src/keymap.rs+2
helix-core/src/selection.rs+9
@@ -122,6 +122,15 @@ impl Range {
122122 }
123123 }
124124
125+ // flips the direction of the selection
126+ pub fn flip(&self) -> Self {
127+ Self {
128+ anchor: self.head,
129+ head: self.anchor,
130+ horiz: self.horiz,
131+ }
132+ }
133+
125134 /// Check two ranges for overlap.
126135 #[must_use]
127136 pub fn overlaps(&self, other: &Self) -> bool {
helix-term/src/commands.rs+16−1
@@ -266,6 +266,7 @@ impl MappableCommand {
266266 change_selection_noyank, "Change selection (delete and enter insert mode, without yanking)",
267267 collapse_selection, "Collapse selection onto a single cursor",
268268 flip_selections, "Flip selection cursor and anchor",
269+ ensure_selections_forward, "Ensure the selection is in forward direction",
269270 insert_mode, "Insert before selection",
270271 append_mode, "Insert after selection (append)",
271272 command_mode, "Enter command mode",
@@ -1904,7 +1905,21 @@ fn flip_selections(cx: &mut Context) {
19041905 let selection = doc
19051906 .selection(view.id)
19061907 .clone()
1907− .transform(|range| Range::new(range.head, range.anchor));
1908+ .transform(|range| range.flip());
1909+ doc.set_selection(view.id, selection);
1910+}
1911+
1912+fn ensure_selections_forward(cx: &mut Context) {
1913+ let (view, doc) = current!(cx.editor);
1914+
1915+ let selection = doc
1916+ .selection(view.id)
1917+ .clone()
1918+ .transform(|r| match r.direction() {
1919+ Direction::Forward => r,
1920+ Direction::Backward => r.flip(),
1921+ });
1922+
19081923 doc.set_selection(view.id, selection);
19091924}
19101925
helix-term/src/keymap.rs+2
@@ -617,6 +617,8 @@ impl Default for Keymaps {
617617 "A-(" => rotate_selection_contents_backward,
618618 "A-)" => rotate_selection_contents_forward,
619619
620+ "A-:" => ensure_selections_forward,
621+
620622 "esc" => normal_mode,
621623 "C-b" | "pageup" => page_up,
622624 "C-f" | "pagedown" => page_down,