editor: Make SelectNext and SelectPrevious preserve cursor direction (#29293)

Closes #27652

Now, if the last selection is reversed, subsequent `SelectNext` or
`SelectPrevious` selection will also be reversed.


https://github.com/user-attachments/assets/dff31abf-ac9e-4d35-bd2c-34e7b0f3ca23

Release Notes:

- Fixed an issue where `SelectNext` and `SelectPrevious` did not
preserve the last selection's cursor direction.
This commit is contained in:
Smit Barmase 2025-04-24 00:17:17 +05:30 committed by GitHub
parent 45d3f5168a
commit 636c6e7f2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 8 deletions

View file

@ -11834,6 +11834,7 @@ impl Editor {
fn select_next_match_ranges(
this: &mut Editor,
range: Range<usize>,
reversed: bool,
replace_newest: bool,
auto_scroll: Option<Autoscroll>,
window: &mut Window,
@ -11844,7 +11845,11 @@ impl Editor {
if replace_newest {
s.delete(s.newest_anchor().id);
}
s.insert_range(range.clone());
if reversed {
s.insert_range(range.end..range.start);
} else {
s.insert_range(range);
}
});
}
@ -11895,6 +11900,7 @@ impl Editor {
select_next_match_ranges(
self,
next_selected_range,
last_selection.reversed,
replace_newest,
autoscroll,
window,
@ -11953,6 +11959,7 @@ impl Editor {
select_next_match_ranges(
self,
selection.start..selection.end,
selection.reversed,
replace_newest,
autoscroll,
window,
@ -12120,7 +12127,11 @@ impl Editor {
if action.replace_newest {
s.delete(s.newest_anchor().id);
}
s.insert_range(next_selected_range);
if last_selection.reversed {
s.insert_range(next_selected_range.end..next_selected_range.start);
} else {
s.insert_range(next_selected_range);
}
});
} else {
select_prev_state.done = true;

View file

@ -5825,6 +5825,13 @@ async fn test_select_next(cx: &mut TestAppContext) {
cx.update_editor(|e, window, cx| e.select_next(&SelectNext::default(), window, cx))
.unwrap();
cx.assert_editor_state("«abcˇ»\n«abcˇ» «abcˇ»\ndefabc\n«abcˇ»");
// Test selection direction should be preserved
cx.set_state("abc\n«ˇabc» abc\ndefabc\nabc");
cx.update_editor(|e, window, cx| e.select_next(&SelectNext::default(), window, cx))
.unwrap();
cx.assert_editor_state("abc\n«ˇabc» «ˇabc»\ndefabc\nabc");
}
#[gpui::test]
@ -6011,6 +6018,25 @@ let «fooˇ» = 2;
let foo = 2;
let foo = «2ˇ»;"#,
);
// Test last selection direction should be preserved
cx.set_state(
r#"let foo = 2;
let foo = 2;
let «fooˇ» = 2;
let «ˇfoo» = 2;
let foo = 2;"#,
);
cx.update_editor(|e, window, cx| e.select_next(&SelectNext::default(), window, cx))
.unwrap();
cx.assert_editor_state(
r#"let foo = 2;
let foo = 2;
let «fooˇ» = 2;
let «ˇfoo» = 2;
let «ˇfoo» = 2;"#,
);
}
#[gpui::test]
@ -6138,25 +6164,26 @@ async fn test_select_previous_with_single_selection(cx: &mut TestAppContext) {
cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx))
.unwrap();
cx.assert_editor_state("«abcˇ»\n«ˇabc» abc\ndefabc\nabc");
// selection direction is preserved
cx.assert_editor_state("«ˇabc»\n«ˇabc» abc\ndefabc\nabc");
cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx))
.unwrap();
cx.assert_editor_state("«abcˇ»\n«ˇabc» abc\ndefabc\n«abcˇ»");
cx.assert_editor_state("«ˇabc»\n«ˇabc» abc\ndefabc\n«ˇabc»");
cx.update_editor(|editor, window, cx| editor.undo_selection(&UndoSelection, window, cx));
cx.assert_editor_state("«abcˇ»\n«ˇabc» abc\ndefabc\nabc");
cx.assert_editor_state("«ˇabc»\n«ˇabc» abc\ndefabc\nabc");
cx.update_editor(|editor, window, cx| editor.redo_selection(&RedoSelection, window, cx));
cx.assert_editor_state("«abcˇ»\n«ˇabc» abc\ndefabc\n«abcˇ»");
cx.assert_editor_state("«ˇabc»\n«ˇabc» abc\ndefabc\n«ˇabc»");
cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx))
.unwrap();
cx.assert_editor_state("«abcˇ»\n«ˇabc» abc\ndef«abcˇ»\n«abcˇ»");
cx.assert_editor_state("«ˇabc»\n«ˇabc» abc\ndef«ˇabc»\n«ˇabc»");
cx.update_editor(|e, window, cx| e.select_previous(&SelectPrevious::default(), window, cx))
.unwrap();
cx.assert_editor_state("«abcˇ»\n«ˇabc» «abcˇ»\ndef«abcˇ»\n«abcˇ»");
cx.assert_editor_state("«ˇabc»\n«ˇabc» «ˇabc»\ndef«ˇabc»\n«ˇabc»");
}
#[gpui::test]