@@ -451,15 +451,8 @@ impl Selection {
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() {
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();
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 {
525 /// Normalizes a `Selection`.
518 /// Normalizes a `Selection`.
526 fn normalize(mut self) -> Self {
519 fn normalize(mut self) -> Self {
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);
@@ -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());
591− let mut selection = Self {
587+ let selection = Self {
593 primary_index,
589 primary_index,
596− if selection.ranges.len() > 1 {
592+ selection.normalize()
597− // TODO: only normalize if needed (any ranges out of order)
598− selection = selection.normalize();
604 /// Takes a closure and maps each `Range` over the closure.
595 /// Takes a closure and maps each `Range` over the closure.