on master help sign in
Fix `Selection::push()` to make the pushed range primary.
c400a60Nathan Vegdahl
helix-core/src/selection.rs+7−1
@@ -290,11 +290,12 @@ impl Selection {
290 }
290 }
291 }
291 }
292
292
293+ /// Adds a new range to the selection and makes it the primary range.
293 pub fn push(mut self, range: Range) -> Self {
294 pub fn push(mut self, range: Range) -> Self {
294 self.ranges.push(range);
295 self.ranges.push(range);
296+ self.set_primary_index(self.ranges().len() - 1);
295 self.normalize()
297 self.normalize()
296 }
298 }
297− // replace_range
298
299
299 /// Map selections over a set of changes. Useful for adjusting the selection position after
300 /// Map selections over a set of changes. Useful for adjusting the selection position after
300 /// applying changes to a document.
301 /// applying changes to a document.
@@ -320,6 +321,11 @@ impl Selection {
320 self.primary_index
321 self.primary_index
321 }
322 }
322
323
324+ pub fn set_primary_index(&mut self, idx: usize) {
325+ assert!(idx < self.ranges.len());
326+ self.primary_index = idx;
327+ }
328+
323 #[must_use]
329 #[must_use]
324 /// Constructs a selection holding a single range.
330 /// Constructs a selection holding a single range.
325 pub fn single(anchor: usize, head: usize) -> Self {
331 pub fn single(anchor: usize, head: usize) -> Self {