Skip to content

Commit

Permalink
move normalize fastpath into normalize function
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe authored and archseer committed Jun 25, 2023
1 parent d491e23 commit b33516f
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions helix-core/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,8 @@ impl Selection {

/// Map selections over a set of changes. Useful for adjusting the selection position after
/// applying changes to a document.
pub fn map(mut self, changes: &ChangeSet) -> Self {
if changes.is_empty() {
return self;
}
self = self.map_no_normalize(changes);
// TODO: only normalize if needed (any ranges out of order)
self = self.normalize();

self
pub fn map(self, changes: &ChangeSet) -> Self {
self.map_no_normalize(changes).normalize()
}

/// Map selections over a set of changes. Useful for adjusting the selection position after
Expand Down Expand Up @@ -524,6 +517,9 @@ impl Selection {

/// Normalizes a `Selection`.
fn normalize(mut self) -> Self {
if self.len() < 2 {
return self;
}
let mut primary = self.ranges[self.primary_index];
self.ranges.sort_unstable_by_key(Range::from);

Expand Down Expand Up @@ -588,17 +584,12 @@ impl Selection {
assert!(!ranges.is_empty());
debug_assert!(primary_index < ranges.len());

let mut selection = Self {
let selection = Self {
ranges,
primary_index,
};

if selection.ranges.len() > 1 {
// TODO: only normalize if needed (any ranges out of order)
selection = selection.normalize();
}

selection
selection.normalize()
}

/// Takes a closure and maps each `Range` over the closure.
Expand Down

0 comments on commit b33516f

Please sign in to comment.