on master help sign in
Fix `shrink_selection` with multiple cursors. (#6093)

* Fix #6092 Cause were some incorrect assumptions that missed an edge case in the `Selection.contains()` calculation. Tests were added accordingly. * Fix Selection.contains() edge-case handling. Removing the len check short-circuit was the only thing needed as pointed out by @dead10ck.

commit2cf4ce235662fcb272c684751b844b2ebc1b757f authorgibbz00 <gabrielhansson00@gmail.com> dateThu 9 Mar 2023, 05:08 (4 years ago) parentaabc8af
2 files changed+6−6
helix-core/src/selection.rs+6−5 helix-term/src/commands.rs−1
helix-core/src/selection.rs+6−5
@@ -625,11 +625,6 @@ impl Selection {
625625
626626 // returns true if self ⊇ other
627627 pub fn contains(&self, other: &Selection) -> bool {
628− // can't contain other if it is larger
629− if other.len() > self.len() {
630− return false;
631− }
632−
633628 let (mut iter_self, mut iter_other) = (self.iter(), other.iter());
634629 let (mut ele_self, mut ele_other) = (iter_self.next(), iter_other.next());
635630
@@ -1240,5 +1235,11 @@ mod test {
12401235 vec!((3, 4), (7, 9))
12411236 ));
12421237 assert!(!contains(vec!((1, 1), (5, 6)), vec!((1, 6))));
1238+
1239+ // multiple ranges of other are all contained in some ranges of self,
1240+ assert!(contains(
1241+ vec!((1, 4), (7, 10)),
1242+ vec!((1, 2), (3, 4), (7, 9))
1243+ ));
12431244 }
12441245}
helix-term/src/commands.rs−1
@@ -4371,7 +4371,6 @@ fn shrink_selection(cx: &mut Context) {
43714371 // try to restore previous selection
43724372 if let Some(prev_selection) = view.object_selections.pop() {
43734373 if current_selection.contains(&prev_selection) {
4374− // allow shrinking the selection only if current selection contains the previous object selection
43754374 doc.set_selection(view.id, prev_selection);
43764375 return;
43774376 } else {