on master help sign in
Implement Selection::replace to replace a single range
6431b26Blaž Hrastnik
helix-core/src/selection.rs+11
@@ -362,6 +362,11 @@ impl Selection {
362
362
363 /// Adds a new range to the selection and makes it the primary range.
363 /// Adds a new range to the selection and makes it the primary range.
364 pub fn remove(mut self, index: usize) -> Self {
364 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+
365 self.ranges.remove(index);
370 self.ranges.remove(index);
366 if index < self.primary_index || self.primary_index == self.ranges.len() {
371 if index < self.primary_index || self.primary_index == self.ranges.len() {
367 self.primary_index -= 1;
372 self.primary_index -= 1;
@@ -369,6 +374,12 @@ impl Selection {
369 self
374 self
370 }
375 }
371
376
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+
372 /// Map selections over a set of changes. Useful for adjusting the selection position after
383 /// Map selections over a set of changes. Useful for adjusting the selection position after
373 /// applying changes to a document.
384 /// applying changes to a document.
374 pub fn map(self, changes: &ChangeSet) -> Self {
385 pub fn map(self, changes: &ChangeSet) -> Self {
helix-term/src/commands.rs+1−2
@@ -1227,8 +1227,7 @@ fn search_impl(
1227 Movement::Extend => selection.clone().push(Range::new(start, end)),
1227 Movement::Extend => selection.clone().push(Range::new(start, end)),
1228 Movement::Move => selection
1228 Movement::Move => selection
1229 .clone()
1229 .clone()
1230− .remove(selection.primary_index())
1230+ .replace(selection.primary_index(), Range::new(start, end)),
1231− .push(Range::new(start, end)),
1232 };
1231 };
1233
1232
1234 doc.set_selection(view.id, selection);
1233 doc.set_selection(view.id, selection);