on master help sign in
Add command to merge non-consecutive ranges (#7053)
70e4cdbSzabin
book/src/keymap.md+2−1
@@ -111,7 +111,8 @@
111| `s` | Select all regex matches inside selections | `select_regex` |
111| `s` | Select all regex matches inside selections | `select_regex` |
112| `S` | Split selection into sub selections on regex matches | `split_selection` |
112| `S` | Split selection into sub selections on regex matches | `split_selection` |
113| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
113| `Alt-s` | Split selection on newlines | `split_selection_on_newline` |
114+| `Alt-minus` | Merge selections | `merge_selections` |
114−| `Alt-_ ` | Merge consecutive selections | `merge_consecutive_selections` |
115+| `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` |
115| `&` | Align selection in columns | `align_selections` |
116| `&` | Align selection in columns | `align_selections` |
116| `_` | Trim whitespace from the selection | `trim_selections` |
117| `_` | Trim whitespace from the selection | `trim_selections` |
117| `;` | Collapse selection onto a single cursor | `collapse_selection` |
118| `;` | Collapse selection onto a single cursor | `collapse_selection` |
helix-core/src/selection.rs+8−1
@@ -522,7 +522,14 @@ impl Selection {
522 self
522 self
523 }
523 }
524
524
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+
525− // Merges all ranges that are consecutive
532+ /// Merges all ranges that are consecutive.
526 pub fn merge_consecutive_ranges(mut self) -> Self {
533 pub fn merge_consecutive_ranges(mut self) -> Self {
527 let mut primary = self.ranges[self.primary_index];
534 let mut primary = self.ranges[self.primary_index];
528
535
helix-term/src/commands.rs+7
@@ -275,6 +275,7 @@ impl MappableCommand {
275 select_regex, "Select all regex matches inside selections",
275 select_regex, "Select all regex matches inside selections",
276 split_selection, "Split selections on regex matches",
276 split_selection, "Split selections on regex matches",
277 split_selection_on_newline, "Split selection on newlines",
277 split_selection_on_newline, "Split selection on newlines",
278+ merge_selections, "Merge selections",
278 merge_consecutive_selections, "Merge consecutive selections",
279 merge_consecutive_selections, "Merge consecutive selections",
279 search, "Search for regex pattern",
280 search, "Search for regex pattern",
280 rsearch, "Reverse search for regex pattern",
281 rsearch, "Reverse search for regex pattern",
@@ -1737,6 +1738,12 @@ fn split_selection_on_newline(cx: &mut Context) {
1737 doc.set_selection(view.id, selection);
1738 doc.set_selection(view.id, selection);
1738}
1739}
1739
1740
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+
1740fn merge_consecutive_selections(cx: &mut Context) {
1747fn merge_consecutive_selections(cx: &mut Context) {
1741 let (view, doc) = current!(cx.editor);
1748 let (view, doc) = current!(cx.editor);
1742 let selection = doc.selection(view.id).clone().merge_consecutive_ranges();
1749 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> {
79
79
80 "s" => select_regex,
80 "s" => select_regex,
81 "A-s" => split_selection_on_newline,
81 "A-s" => split_selection_on_newline,
82+ "A-minus" => merge_selections,
82 "A-_" => merge_consecutive_selections,
83 "A-_" => merge_consecutive_selections,
83 "S" => split_selection,
84 "S" => split_selection,
84 ";" => collapse_selection,
85 ";" => collapse_selection,