@@ -109,8 +109,21 @@ impl Range {
109109 /// Map a range through a set of changes. Returns a new range representing the same position
110110 /// after the changes are applied.
111111 pub fn map(self, changes: &ChangeSet) -> Self {
112− let anchor = changes.map_pos(self.anchor, Assoc::After);
113− let head = changes.map_pos(self.head, Assoc::After);
112+ use std::cmp::Ordering;
113+ let (anchor, head) = match self.anchor.cmp(&self.head) {
114+ Ordering::Equal => (
115+ changes.map_pos(self.anchor, Assoc::After),
116+ changes.map_pos(self.head, Assoc::After),
117+ ),
118+ Ordering::Less => (
119+ changes.map_pos(self.anchor, Assoc::After),
120+ changes.map_pos(self.head, Assoc::Before),
121+ ),
122+ Ordering::Greater => (
123+ changes.map_pos(self.anchor, Assoc::Before),
124+ changes.map_pos(self.head, Assoc::After),
125+ ),
126+ };
114127
115128 // We want to return a new `Range` with `horiz == None` every time,
116129 // even if the anchor and head haven't changed, because we don't