@@ -126,6 +126,7 @@ impl Range {
128/// A selection consists of one or more selection ranges.
128/// A selection consists of one or more selection ranges.
129+/// invariant: A selection can never be empty (always contains at least primary range).
129#[derive(Debug, Clone)]
130#[derive(Debug, Clone)]
130pub struct Selection {
131pub struct Selection {
131 ranges: SmallVec<[Range; 1]>,
132 ranges: SmallVec<[Range; 1]>,
@@ -205,12 +206,14 @@ impl Selection {
205 // TODO: consume an iterator or a vec to reduce allocations?
206 // TODO: consume an iterator or a vec to reduce allocations?
206 #[must_use]
207 #[must_use]
207 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
208 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
209+ assert!(!ranges.is_empty());
208 fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
211 fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
209 let primary = ranges[primary_index];
212 let primary = ranges[primary_index];
210 ranges.sort_unstable_by_key(Range::from);
213 ranges.sort_unstable_by_key(Range::from);
211 primary_index = ranges.iter().position(|&range| range == primary).unwrap();
214 primary_index = ranges.iter().position(|&range| range == primary).unwrap();
213− let mut result: SmallVec<[Range; 1]> = SmallVec::new();
216+ let mut result = SmallVec::new();
215 // TODO: we could do with one vec by removing elements as we mutate
218 // TODO: we could do with one vec by removing elements as we mutate
@@ -294,7 +297,7 @@ impl<'a> IntoIterator for &'a Selection {
297−// TODO: checkSelection -> check if valid for doc length
300+// TODO: checkSelection -> check if valid for doc length && sorted
299pub fn keep_matches(
302pub fn keep_matches(
300 text: RopeSlice,
303 text: RopeSlice,
@@ -387,6 +390,12 @@ pub fn split_on_matches(
387mod test {
390mod test {
388 use super::*;
391 use super::*;
395+ fn test_new_empty() {
396+ let sel = Selection::new(smallvec![], 0);
391 fn test_create_normalizes_and_merges() {
400 fn test_create_normalizes_and_merges() {
392 let sel = Selection::new(
401 let sel = Selection::new(