move normalize fastpath into normalize function

This commit is contained in:
Pascal Kuthe 2023-06-22 23:37:39 +02:00 committed by Blaž Hrastnik
parent d491e234f4
commit b33516fb16

View file

@ -451,15 +451,8 @@ pub fn replace(mut self, index: usize, range: Range) -> Self {
/// 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
@ -524,6 +517,9 @@ pub fn point(pos: usize) -> Self {
/// 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);
@ -588,17 +584,12 @@ pub fn merge_consecutive_ranges(mut self) -> Self {
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.