on master help sign in
fix: Correctly merge multiple selection ranges together
3007478Blaž Hrastnik
helix-core/src/selection.rs+21−2
@@ -223,7 +223,9 @@ impl Selection {
223
223
224 // TODO: we could do with one vec by removing elements as we mutate
224 // TODO: we could do with one vec by removing elements as we mutate
225
225
226+ let mut i = 0;
227+
226− for (i, range) in ranges.into_iter().enumerate() {
228+ for range in ranges.into_iter() {
227 // if previous value exists
229 // if previous value exists
228 if let Some(prev) = result.last_mut() {
230 if let Some(prev) = result.last_mut() {
229 // and we overlap it
231 // and we overlap it
@@ -250,7 +252,8 @@ impl Selection {
250 }
252 }
251 }
253 }
252
254
253− result.push(range)
255+ result.push(range);
256+ i += 1
254 }
257 }
255
258
256 Self {
259 Self {
@@ -434,6 +437,22 @@ mod test {
434 .join(",");
437 .join(",");
435
438
436 assert_eq!(res, "0/6,6/7,7/8,9/13,13/14");
439 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);
437 }
456 }
438
457
439 #[test]
458 #[test]