mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
vim: Fix substitute without the g flag replacing the wrong matches (#67015)
## 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 <dinojoaocosta@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user