on master help sign in
Implement Selection::replace to replace a single range

Fixes #985 Co-authored-by: Daniel S Poulin <crimsonmage+github@gmail.com>

commit6431b26a6a5fa4be5b91008f21537721d2ff4ba2 authorBlaž Hrastnik <blaz@mxxn.io> dateSat 6 Nov 2021, 17:37 (5 years ago) parent911b9b3
2 files changed+12−2
helix-core/src/selection.rs+11 helix-term/src/commands.rs+1−2
helix-core/src/selection.rs+11
@@ -362,6 +362,11 @@ impl Selection {
362362
363363 /// Adds a new range to the selection and makes it the primary range.
364364 pub fn remove(mut self, index: usize) -> Self {
365+ assert!(
366+ self.ranges.len() > 1,
367+ "can't remove the last range from a selection!"
368+ );
369+
365370 self.ranges.remove(index);
366371 if index < self.primary_index || self.primary_index == self.ranges.len() {
367372 self.primary_index -= 1;
@@ -369,6 +374,12 @@ impl Selection {
369374 self
370375 }
371376
377+ /// Replace a range in the selection with a new range.
378+ pub fn replace(mut self, index: usize, range: Range) -> Self {
379+ self.ranges[index] = range;
380+ self.normalize()
381+ }
382+
372383 /// Map selections over a set of changes. Useful for adjusting the selection position after
373384 /// applying changes to a document.
374385 pub fn map(self, changes: &ChangeSet) -> Self {
helix-term/src/commands.rs+1−2
@@ -1227,8 +1227,7 @@ fn search_impl(
12271227 Movement::Extend => selection.clone().push(Range::new(start, end)),
12281228 Movement::Move => selection
12291229 .clone()
1230− .remove(selection.primary_index())
1231− .push(Range::new(start, end)),
1230+ .replace(selection.primary_index(), Range::new(start, end)),
12321231 };
12331232
12341233 doc.set_selection(view.id, selection);