From 3007478567c45274e405ec3b57a273bfa0025cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 30 Jun 2021 16:08:41 +0900 Subject: [PATCH] fix: Correctly merge multiple selection ranges together Fixes #391 --- helix-core/src/selection.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index de4879d6f..370a1f6e1 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -223,7 +223,9 @@ impl Selection { // TODO: we could do with one vec by removing elements as we mutate - for (i, range) in ranges.into_iter().enumerate() { + let mut i = 0; + + for range in ranges.into_iter() { // if previous value exists if let Some(prev) = result.last_mut() { // and we overlap it @@ -250,7 +252,8 @@ impl Selection { } } - result.push(range) + result.push(range); + i += 1 } Self { @@ -434,6 +437,22 @@ mod test { .join(","); assert_eq!(res, "0/6,6/7,7/8,9/13,13/14"); + + // it correctly calculates a new primary index + let sel = Selection::new( + smallvec![Range::new(0, 2), Range::new(1, 5), Range::new(4, 7)], + 2, + ); + + let res = sel + .ranges + .into_iter() + .map(|range| format!("{}/{}", range.anchor, range.head)) + .collect::>() + .join(","); + + assert_eq!(res, "0/7"); + assert_eq!(sel.primary_index, 0); } #[test]