@@ -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 {