@@ -223,7 +223,9 @@ impl Selection {
223223
224224 // TODO: we could do with one vec by removing elements as we mutate
225225
226− for (i, range) in ranges.into_iter().enumerate() {
226+ let mut i = 0;
227+
228+ for range in ranges.into_iter() {
227229 // if previous value exists
228230 if let Some(prev) = result.last_mut() {
229231 // and we overlap it
@@ -250,7 +252,8 @@ impl Selection {
250252 }
251253 }
252254
253− result.push(range)
255+ result.push(range);
256+ i += 1
254257 }
255258
256259 Self {
@@ -434,6 +437,22 @@ mod test {
434437 .join(",");
435438
436439 assert_eq!(res, "0/6,6/7,7/8,9/13,13/14");
440+
441+ // it correctly calculates a new primary index
442+ let sel = Selection::new(
443+ smallvec![Range::new(0, 2), Range::new(1, 5), Range::new(4, 7)],
444+ 2,
445+ );
446+
447+ let res = sel
448+ .ranges
449+ .into_iter()
450+ .map(|range| format!("{}/{}", range.anchor, range.head))
451+ .collect::<Vec<String>>()
452+ .join(",");
453+
454+ assert_eq!(res, "0/7");
455+ assert_eq!(sel.primary_index, 0);
437456 }
438457
439458 #[test]