diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index 7d2526b00..f1625f996 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -290,11 +290,12 @@ impl Selection { } } + /// Adds a new range to the selection and makes it the primary range. pub fn push(mut self, range: Range) -> Self { self.ranges.push(range); + self.set_primary_index(self.ranges().len() - 1); self.normalize() } - // replace_range /// Map selections over a set of changes. Useful for adjusting the selection position after /// applying changes to a document. @@ -320,6 +321,11 @@ impl Selection { self.primary_index } + pub fn set_primary_index(&mut self, idx: usize) { + assert!(idx < self.ranges.len()); + self.primary_index = idx; + } + #[must_use] /// Constructs a selection holding a single range. pub fn single(anchor: usize, head: usize) -> Self {