From 71e5c04c1cfbe13daa4ad5beae2c83fe79728c4a Mon Sep 17 00:00:00 2001 From: Szabin Hamrik Date: Mon, 15 May 2023 18:19:46 +0200 Subject: [PATCH 1/4] Add command for merging non-consecutive ranges --- helix-core/src/selection.rs | 10 ++++++++++ helix-term/src/commands.rs | 7 +++++++ helix-term/src/keymap/default.rs | 1 + 3 files changed, 18 insertions(+) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 259b131a4202..00926729353d 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -522,6 +522,16 @@ impl Selection { self } + // Replaces ranges with one spanning from leftmost to rightmost selection + pub fn merge_ranges(mut self) -> Self { + if !self.ranges.is_empty() { + let merged_range = self.ranges.iter().fold(self.ranges[0], |a, b| a.merge(*b)); + self.ranges = smallvec![merged_range]; + self.primary_index = 0; + } + self + } + // Merges all ranges that are consecutive pub fn merge_consecutive_ranges(mut self) -> Self { let mut primary = self.ranges[self.primary_index]; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7d86bc0b33bf..46e8189aff4a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -274,6 +274,7 @@ impl MappableCommand { select_regex, "Select all regex matches inside selections", split_selection, "Split selections on regex matches", split_selection_on_newline, "Split selection on newlines", + merge_selections, "Merge selections", merge_consecutive_selections, "Merge consecutive selections", search, "Search for regex pattern", rsearch, "Reverse search for regex pattern", @@ -1740,6 +1741,12 @@ fn split_selection_on_newline(cx: &mut Context) { doc.set_selection(view.id, selection); } +fn merge_selections(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + let selection = doc.selection(view.id).clone().merge_ranges(); + doc.set_selection(view.id, selection); +} + fn merge_consecutive_selections(cx: &mut Context) { let (view, doc) = current!(cx.editor); let selection = doc.selection(view.id).clone().merge_consecutive_ranges(); diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 9bd002809d61..d80ab1ffb7fb 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -79,6 +79,7 @@ pub fn default() -> HashMap { "s" => select_regex, "A-s" => split_selection_on_newline, + "A-minus" => merge_selections, "A-_" => merge_consecutive_selections, "S" => split_selection, ";" => collapse_selection, From a3916066ec0919387a25237ac9c9c7abc0880333 Mon Sep 17 00:00:00 2001 From: Szabin Hamrik Date: Mon, 15 May 2023 19:38:40 +0200 Subject: [PATCH 2/4] Add `merge_selections` command to book --- book/src/keymap.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index f530bf6c5d42..e8b140c856cc 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -111,7 +111,8 @@ | `s` | Select all regex matches inside selections | `select_regex` | | `S` | Split selection into sub selections on regex matches | `split_selection` | | `Alt-s` | Split selection on newlines | `split_selection_on_newline` | -| `Alt-_ ` | Merge consecutive selections | `merge_consecutive_selections` | +| `Alt-minus` | Merge selections | `merge_selections` | +| `Alt-_` | Merge consecutive selections | `merge_consecutive_selections` | | `&` | Align selection in columns | `align_selections` | | `_` | Trim whitespace from the selection | `trim_selections` | | `;` | Collapse selection onto a single cursor | `collapse_selection` | From 7ee7805b713b9db287a58b76ae2822800317aa54 Mon Sep 17 00:00:00 2001 From: Szabin Date: Fri, 19 May 2023 08:02:00 +0200 Subject: [PATCH 3/4] 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. --- helix-core/src/selection.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 00926729353d..a78fd051723e 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -523,13 +523,10 @@ impl Selection { } // Replaces ranges with one spanning from leftmost to rightmost selection - pub fn merge_ranges(mut self) -> Self { - if !self.ranges.is_empty() { - let merged_range = self.ranges.iter().fold(self.ranges[0], |a, b| a.merge(*b)); - self.ranges = smallvec![merged_range]; - self.primary_index = 0; - } - self + pub fn merge_ranges(self) -> Self { + let first = self.ranges.first().unwrap(); + let last = self.ranges.last().unwrap(); + Selection::new(smallvec![first.merge(*last)], 0) } // Merges all ranges that are consecutive From 4ce9daa06644b876c6ab6ae5a41fae167413114f Mon Sep 17 00:00:00 2001 From: Szabin Hamrik Date: Fri, 19 May 2023 08:14:04 +0200 Subject: [PATCH 4/4] Clarify doc comment of `merge_ranges` --- helix-core/src/selection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index a78fd051723e..9e366c33701e 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -522,14 +522,14 @@ impl Selection { self } - // Replaces ranges with one spanning from leftmost to rightmost selection + /// Replaces ranges with one spanning from first to last range. pub fn merge_ranges(self) -> Self { let first = self.ranges.first().unwrap(); let last = self.ranges.last().unwrap(); Selection::new(smallvec![first.merge(*last)], 0) } - // Merges all ranges that are consecutive + /// Merges all ranges that are consecutive. pub fn merge_consecutive_ranges(mut self) -> Self { let mut primary = self.ranges[self.primary_index];