aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-06-30 07:08:41 +0000
committerBlaž Hrastnik2021-06-30 07:08:41 +0000
commit3007478567c45274e405ec3b57a273bfa0025cc9 (patch)
tree5ea77b774d4214f1ba5320475e4ece2486c6bcfc
parente9159887a9119f146095fec9603e654c549a688c (diff)
fix: Correctly merge multiple selection ranges together
Fixes #391
-rw-r--r--helix-core/src/selection.rs23
1 files changed, 21 insertions, 2 deletions
diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs
index de4879d6..370a1f6e 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::<Vec<String>>()
+ .join(",");
+
+ assert_eq!(res, "0/7");
+ assert_eq!(sel.primary_index, 0);
}
#[test]