diff --git a/crates/project/src/search.rs b/crates/project/src/search.rs index 49c7f931142..6a47831d3fc 100644 --- a/crates/project/src/search.rs +++ b/crates/project/src/search.rs @@ -553,8 +553,12 @@ impl SearchQuery { if let std::result::Result::Ok(mat) = mat { let should_push = if *one_match_per_line { - // ensure that only one match per line is returned. - let pos = buffer.offset_to_point(mat.start()); + // Ensure that only one match per line is returned. + // Match offsets are relative to `text`, so convert + // them to buffer offsets before looking up the row, + // otherwise we could end up marking rows before + // `subrange` as seen. + let pos = buffer.offset_to_point(range_offset + mat.start()); seen_lines.insert(pos.row) } else { true diff --git a/crates/project/tests/integration/search.rs b/crates/project/tests/integration/search.rs index 5b94509425a..1b1f4782581 100644 --- a/crates/project/tests/integration/search.rs +++ b/crates/project/tests/integration/search.rs @@ -191,6 +191,33 @@ async fn test_multiline_regex(cx: &mut gpui::TestAppContext) { assert_eq!(results, vec![0..6, 12..18]); } +#[gpui::test] +async fn test_one_match_per_line_with_subrange(cx: &mut gpui::TestAppContext) { + let one_match_per_line = true; + let subrange = 2..5; + + let search_query = SearchQuery::regex( + "a", + false, + false, + false, + one_match_per_line, + Default::default(), + Default::default(), + false, + None, + ) + .expect("Should be able to create a regex SearchQuery"); + + let text = Rope::from("x\naaa"); + let snapshot = cx + .update(|app| Buffer::build_snapshot(text, None, None, None, app)) + .await; + + let results = search_query.search(&snapshot, Some(subrange)).await; + assert_eq!(results, vec![0..1]); +} + #[gpui::test] async fn regex_with_eol_detects_lines() { let re = SearchQuery::regex( diff --git a/crates/vim/src/normal/search.rs b/crates/vim/src/normal/search.rs index 0a28c232901..75953e8f1ce 100644 --- a/crates/vim/src/normal/search.rs +++ b/crates/vim/src/normal/search.rs @@ -1335,6 +1335,29 @@ mod test { }); } + #[gpui::test] + async fn test_replace_without_g_after_first_line(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + cx.set_state( + indoc! { + "x + ˇaaaa" + }, + Mode::Normal, + ); + + cx.simulate_keystrokes(": s / a / b"); + cx.simulate_keystrokes("enter"); + + cx.assert_state( + indoc! { + "x + ˇbaaa" + }, + Mode::Normal, + ); + } + #[gpui::test] async fn test_replace_gdefault(cx: &mut gpui::TestAppContext) { let mut cx = NeovimBackedTestContext::new(cx).await;