From 70e4cdbd8e3f0b2a1c5bd2e1cb1c1ec537bdca04 Mon Sep 17 00:00:00 2001 From: Szabin Date: Sat, 20 May 2023 01:31:39 +0200 Subject: 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`--- helix-core/src/selection.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'helix-core') diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 259b131a..9e366c33 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -522,7 +522,14 @@ impl Selection { self } - // Merges all ranges that are consecutive + /// 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. pub fn merge_consecutive_ranges(mut self) -> Self { let mut primary = self.ranges[self.primary_index]; -- cgit v1.2.3-70-g09d2