From 3f574806f445eeb99d513022ab0996ef130c3bc5 Mon Sep 17 00:00:00 2001 From: "zed-zippy[bot]" <234243425+zed-zippy[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:10:46 +0000 Subject: [PATCH] vim: Fix substitute without the g flag replacing the wrong matches (#67015) (cherry-pick to stable) (#67049) Cherry-pick of #67015 to stable ---- ## Summary `SearchQuery::search` searches a slice of the buffer and returns offsets into that slice. With `one_match_per_line`, it found each match's row by passing the slice offset to `buffer.offset_to_point`, which takes a buffer offset. When the slice started after the first line, matches got the rows of lines nearer the top of the buffer, so the wrong matches were kept. It now adds `range_offset`. Only Vim's `:s` sets `one_match_per_line`, when it replaces the first match on each line. It searches only the lines in its range, so it was correct only when the range started on the first line. With `x` on line 1 and `aaaa` on line 2, `:s/a/b/` on line 2 gave `baba` instead of `baaa`. ## Testing - `test_replace_without_g_after_first_line` is new and fails without the fix. - `cargo test -p vim --lib` and `cargo test -p search --lib` pass. - `cargo test -p project --features test-support --test integration search` passes. - `./script/clippy -p project -p vim` passes. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed Vim's `:s` without the `g` flag replacing the wrong matches when used after the first line --------- Co-authored-by: dino Co-authored-by: Parker Loflin Co-authored-by: dino --- crates/project/src/search.rs | 8 +++++-- crates/project/tests/integration/search.rs | 27 ++++++++++++++++++++++ crates/vim/src/normal/search.rs | 23 ++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) 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 57c872a2666..a60efb5f590 100644 --- a/crates/vim/src/normal/search.rs +++ b/crates/vim/src/normal/search.rs @@ -1310,6 +1310,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;