From bc85c85501306f4d3e5e6c802590816cdaf29c13 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Wed, 21 Jul 2021 09:23:01 -0700 Subject: [PATCH] Fix selections not being modified quite correctly with text edits. --- helix-core/src/selection.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index beade27a3..3d4ee7683 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -109,8 +109,21 @@ impl Range { /// Map a range through a set of changes. Returns a new range representing the same position /// after the changes are applied. pub fn map(self, changes: &ChangeSet) -> Self { - let anchor = changes.map_pos(self.anchor, Assoc::After); - let head = changes.map_pos(self.head, Assoc::After); + use std::cmp::Ordering; + let (anchor, head) = match self.anchor.cmp(&self.head) { + Ordering::Equal => ( + changes.map_pos(self.anchor, Assoc::After), + changes.map_pos(self.head, Assoc::After), + ), + Ordering::Less => ( + changes.map_pos(self.anchor, Assoc::After), + changes.map_pos(self.head, Assoc::Before), + ), + Ordering::Greater => ( + changes.map_pos(self.anchor, Assoc::Before), + changes.map_pos(self.head, Assoc::After), + ), + }; // We want to return a new `Range` with `horiz == None` every time, // even if the anchor and head haven't changed, because we don't