Skip to content

Commit

Permalink
Simplify merge_ranges
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hamrik authored May 19, 2023
1 parent a391606 commit 7ee7805
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ee7805

Please sign in to comment.