@@ -451,15 +451,8 @@ impl Selection {
451451
452452 /// Map selections over a set of changes. Useful for adjusting the selection position after
453453 /// applying changes to a document.
454- pub fn map ( mut self , changes : & ChangeSet ) -> Self {
455- if changes. is_empty ( ) {
456- return self ;
457- }
458- self = self . map_no_normalize ( changes) ;
459- // TODO: only normalize if needed (any ranges out of order)
460- self = self . normalize ( ) ;
461-
462- self
454+ pub fn map ( self , changes : & ChangeSet ) -> Self {
455+ self . map_no_normalize ( changes) . normalize ( )
463456 }
464457
465458 /// Map selections over a set of changes. Useful for adjusting the selection position after
@@ -524,6 +517,9 @@ impl Selection {
524517
525518 /// Normalizes a `Selection`.
526519 fn normalize ( mut self ) -> Self {
520+ if self . len ( ) < 2 {
521+ return self ;
522+ }
527523 let mut primary = self . ranges [ self . primary_index ] ;
528524 self . ranges . sort_unstable_by_key ( Range :: from) ;
529525
@@ -588,17 +584,12 @@ impl Selection {
588584 assert ! ( !ranges. is_empty( ) ) ;
589585 debug_assert ! ( primary_index < ranges. len( ) ) ;
590586
591- let mut selection = Self {
587+ let selection = Self {
592588 ranges,
593589 primary_index,
594590 } ;
595591
596- if selection. ranges . len ( ) > 1 {
597- // TODO: only normalize if needed (any ranges out of order)
598- selection = selection. normalize ( ) ;
599- }
600-
601- selection
592+ selection. normalize ( )
602593 }
603594
604595 /// Takes a closure and maps each `Range` over the closure.
0 commit comments