fix: retain current occurrence when submitting search

This commit is contained in:
Valdemar Lorenzen
2026-10-15 12:31:13 +02:00
parent adc44d22db
commit f5c6d098f8
5 changed files with 66 additions and 14 deletions
+34 -2
View File
@@ -120,6 +120,36 @@ fn wait_until(session: &mut EditorSession, ready: impl Fn(&EditorSession) -> boo
}
}
#[test]
fn submitting_search_keeps_each_caret_inside_a_current_match() {
let fixture = Fixture::new();
for (text, query, column, end) in [
("jj next jj", "jj", 0, 2),
("jj next jj", "jj", 1, 2),
("界e\u{301} next 界e\u{301}", "界e\u{301}", 1, 2),
("aaaa next aa", "aa", 1, 3),
] {
let mut editor = fixture.editor(text);
keys(&mut editor, &"l".repeat(column));
let cursor = editor.cursor();
keys(&mut editor, &format!("/{query}"));
editor.handle(Input::Enter);
assert_eq!(editor.cursor(), cursor, "{text:?}");
assert_eq!(editor.search_match().unwrap().end_column, end);
keys(&mut editor, "n");
assert_ne!(editor.cursor(), cursor);
assert!(!editor.dirty());
}
let mut editor = fixture.editor("jj next jj\n-- next jj");
editor.handle(Input::Ctrl('e'));
keys(&mut editor, "/jj");
editor.handle(Input::Enter);
assert_eq!(
editor.carets(),
[Cursor::default(), Cursor { row: 1, column: 8 }]
);
}
#[test]
fn normal_escape_hides_highlights_but_retains_search_and_rearms_star() {
let fixture = Fixture::new();
@@ -145,7 +175,7 @@ fn normal_escape_hides_highlights_but_retains_search_and_rearms_star() {
editor.handle(Input::Escape);
editor.handle(Input::Char('/'));
editor.handle(Input::Enter);
assert_eq!(editor.cursor(), Cursor { row: 0, column: 4 });
assert_eq!(editor.cursor(), Cursor::default());
assert_eq!(editor.search_matches_on_row(0).count(), 2);
assert!(!editor.dirty());
}
@@ -192,7 +222,7 @@ fn star_search_keeps_whole_word_counts_history_and_literal_search_distinct() {
editor.handle(Input::Escape);
editor.handle(Input::Char('/'));
editor.handle(Input::Enter);
assert_eq!(editor.cursor(), Cursor { row: 6, column: 0 });
assert_eq!(editor.cursor(), Cursor::default());
keys(&mut editor, "gb*");
assert_eq!(editor.cursor(), Cursor { row: 9, column: 0 });
keys(&mut editor, "n");
@@ -200,6 +230,7 @@ fn star_search_keeps_whole_word_counts_history_and_literal_search_distinct() {
keys(&mut editor, "/cat");
editor.handle(Input::Enter);
keys(&mut editor, "n");
assert_eq!(editor.cursor(), Cursor { row: 1, column: 1 });
assert_eq!(editor.search_match().unwrap().end_column, 4);
assert_eq!(editor.context().text, text);
@@ -260,6 +291,7 @@ fn star_search_rebinding_moves_all_carets_and_remains_view_local() {
assert_eq!(session.editor().cursor(), Cursor { row: 1, column: 0 });
session_keys(&mut session, "/cat");
session.handle(Input::Enter);
session_keys(&mut session, "n");
assert_eq!(session.editor().cursor(), Cursor { row: 1, column: 4 });
session.handle(Input::Alt('h'));
session_keys(&mut session, "2N");