on master help sign in
Add command to merge non-consecutive ranges (#7053)

* Add command for merging non-consecutive ranges * Add `merge_selections` command to book * Simplify `merge_ranges` Heeded the advice of @the-mikedavis to stop iterating over all ranges and simply merge the first and the last range, as the invariants of `Selection` guarantee that the list of ranges is always sorted and never empty. * Clarify doc comment of `merge_ranges`

commit70e4cdbd8e3f0b2a1c5bd2e1cb1c1ec537bdca04 authorSzabin <h.szabin@gmail.com> dateSat 20 May 2023, 01:31 (3 years ago) parente4a9bec
4 files changed+18−2
book/src/keymap.md+2−1 helix-core/src/selection.rs+8−1 helix-term/src/commands.rs+7 helix-term/src/keymap/default.rs+1
book/src/keymap.md+2−1
@@ -111,7 +111,8 @@
111111| `s` | Select all regex matches inside selections | `select_regex` |
112112| `S` | Split selection into sub selections on regex matches | `split_selection` |
113113| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
114−| `Alt-_ ` | Merge consecutive selections | `merge_consecutive_selections` |
114+| `Alt-minus` | Merge selections | `merge_selections` |
115+| `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` |
115116| `&` | Align selection in columns | `align_selections` |
116117| `_` | Trim whitespace from the selection | `trim_selections` |
117118| `;` | Collapse selection onto a single cursor | `collapse_selection` |
helix-core/src/selection.rs+8−1
@@ -522,7 +522,14 @@ impl Selection {
522522 self
523523 }
524524
525− // Merges all ranges that are consecutive
525+ /// Replaces ranges with one spanning from first to last range.
526+ pub fn merge_ranges(self) -> Self {
527+ let first = self.ranges.first().unwrap();
528+ let last = self.ranges.last().unwrap();
529+ Selection::new(smallvec![first.merge(*last)], 0)
530+ }
531+
532+ /// Merges all ranges that are consecutive.
526533 pub fn merge_consecutive_ranges(mut self) -> Self {
527534 let mut primary = self.ranges[self.primary_index];
528535
helix-term/src/commands.rs+7
@@ -275,6 +275,7 @@ impl MappableCommand {
275275 select_regex, "Select all regex matches inside selections",
276276 split_selection, "Split selections on regex matches",
277277 split_selection_on_newline, "Split selection on newlines",
278+ merge_selections, "Merge selections",
278279 merge_consecutive_selections, "Merge consecutive selections",
279280 search, "Search for regex pattern",
280281 rsearch, "Reverse search for regex pattern",
@@ -1737,6 +1738,12 @@ fn split_selection_on_newline(cx: &mut Context) {
17371738 doc.set_selection(view.id, selection);
17381739}
17391740
1741+fn merge_selections(cx: &mut Context) {
1742+ let (view, doc) = current!(cx.editor);
1743+ let selection = doc.selection(view.id).clone().merge_ranges();
1744+ doc.set_selection(view.id, selection);
1745+}
1746+
17401747fn merge_consecutive_selections(cx: &mut Context) {
17411748 let (view, doc) = current!(cx.editor);
17421749 let selection = doc.selection(view.id).clone().merge_consecutive_ranges();
helix-term/src/keymap/default.rs+1
@@ -79,6 +79,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
7979
8080 "s" => select_regex,
8181 "A-s" => split_selection_on_newline,
82+ "A-minus" => merge_selections,
8283 "A-_" => merge_consecutive_selections,
8384 "S" => split_selection,
8485 ";" => collapse_selection,