@@ -126,6 +126,7 @@ impl Range {
126126}
127127
128128/// A selection consists of one or more selection ranges.
129+/// invariant: A selection can never be empty (always contains at least primary range).
129130#[derive(Debug, Clone)]
130131pub struct Selection {
131132 ranges: SmallVec<[Range; 1]>,
@@ -205,12 +206,14 @@ impl Selection {
205206 // TODO: consume an iterator or a vec to reduce allocations?
206207 #[must_use]
207208 pub fn new(ranges: SmallVec<[Range; 1]>, primary_index: usize) -> Self {
209+ assert!(!ranges.is_empty());
210+
208211 fn normalize(mut ranges: SmallVec<[Range; 1]>, mut primary_index: usize) -> Selection {
209212 let primary = ranges[primary_index];
210213 ranges.sort_unstable_by_key(Range::from);
211214 primary_index = ranges.iter().position(|&range| range == primary).unwrap();
212215
213− let mut result: SmallVec<[Range; 1]> = SmallVec::new();
216+ let mut result = SmallVec::new();
214217
215218 // TODO: we could do with one vec by removing elements as we mutate
216219
@@ -294,7 +297,7 @@ impl<'a> IntoIterator for &'a Selection {
294297 }
295298}
296299
297−// TODO: checkSelection -> check if valid for doc length
300+// TODO: checkSelection -> check if valid for doc length && sorted
298301
299302pub fn keep_matches(
300303 text: RopeSlice,
@@ -387,6 +390,12 @@ pub fn split_on_matches(
387390mod test {
388391 use super::*;
389392
393+ #[test]
394+ #[should_panic]
395+ fn test_new_empty() {
396+ let sel = Selection::new(smallvec![], 0);
397+ }
398+
390399 #[test]
391400 fn test_create_normalizes_and_merges() {
392401 let sel = Selection::new(