on master help sign in
move normalize fastpath into normalize function
b33516fPascal Kuthe
helix-core/src/selection.rs+7−16
@@ -451,15 +451,8 @@ impl Selection {
451
451
452 /// Map selections over a set of changes. Useful for adjusting the selection position after
452 /// Map selections over a set of changes. Useful for adjusting the selection position after
453 /// applying changes to a document.
453 /// applying changes to a document.
454− pub fn map(mut self, changes: &ChangeSet) -> Self {
454+ pub fn map(self, changes: &ChangeSet) -> Self {
455− if changes.is_empty() {
456− return self;
457− }
458− self = self.map_no_normalize(changes);
455+ self.map_no_normalize(changes).normalize()
459− // TODO: only normalize if needed (any ranges out of order)
460− self = self.normalize();
461−
462− self
463 }
456 }
464
457
465 /// Map selections over a set of changes. Useful for adjusting the selection position after
458 /// Map selections over a set of changes. Useful for adjusting the selection position after
@@ -524,6 +517,9 @@ impl Selection {
524
517
525 /// Normalizes a `Selection`.
518 /// Normalizes a `Selection`.
526 fn normalize(mut self) -> Self {
519 fn normalize(mut self) -> Self {
520+ if self.len() < 2 {
521+ return self;
522+ }
527 let mut primary = self.ranges[self.primary_index];
523 let mut primary = self.ranges[self.primary_index];
528 self.ranges.sort_unstable_by_key(Range::from);
524 self.ranges.sort_unstable_by_key(Range::from);
529
525
@@ -588,17 +584,12 @@ impl Selection {
588 assert!(!ranges.is_empty());
584 assert!(!ranges.is_empty());
589 debug_assert!(primary_index < ranges.len());
585 debug_assert!(primary_index < ranges.len());
590
586
591− let mut selection = Self {
587+ let selection = Self {
592 ranges,
588 ranges,
593 primary_index,
589 primary_index,
594 };
590 };
595
591
596− if selection.ranges.len() > 1 {
592+ selection.normalize()
597− // TODO: only normalize if needed (any ranges out of order)
598− selection = selection.normalize();
599− }
600−
601− selection
602 }
593 }
603
594
604 /// Takes a closure and maps each `Range` over the closure.
595 /// Takes a closure and maps each `Range` over the closure.