search.rs

  1use std::{ops::Range, sync::OnceLock, time::Duration};
  2
  3use gpui::{actions, impl_actions, ViewContext};
  4use language::Point;
  5use regex::Regex;
  6use search::{buffer_search, BufferSearchBar, SearchOptions};
  7use serde_derive::Deserialize;
  8use workspace::{searchable::Direction, Workspace};
  9
 10use crate::{
 11    motion::{search_motion, Motion},
 12    normal::move_cursor,
 13    state::{Mode, SearchState},
 14    Vim,
 15};
 16
 17#[derive(Clone, Deserialize, PartialEq)]
 18#[serde(rename_all = "camelCase")]
 19pub(crate) struct MoveToNext {
 20    #[serde(default)]
 21    partial_word: bool,
 22}
 23
 24#[derive(Clone, Deserialize, PartialEq)]
 25#[serde(rename_all = "camelCase")]
 26pub(crate) struct MoveToPrev {
 27    #[serde(default)]
 28    partial_word: bool,
 29}
 30
 31#[derive(Clone, Deserialize, PartialEq)]
 32pub(crate) struct Search {
 33    #[serde(default)]
 34    backwards: bool,
 35}
 36
 37#[derive(Debug, Clone, PartialEq, Deserialize)]
 38pub struct FindCommand {
 39    pub query: String,
 40    pub backwards: bool,
 41}
 42
 43#[derive(Debug, Clone, PartialEq, Deserialize)]
 44pub struct ReplaceCommand {
 45    pub query: String,
 46}
 47
 48#[derive(Debug, Default)]
 49struct Replacement {
 50    search: String,
 51    replacement: String,
 52    should_replace_all: bool,
 53    is_case_sensitive: bool,
 54    range: Option<Range<usize>>,
 55}
 56
 57actions!(vim, [SearchSubmit, MoveToNextMatch, MoveToPrevMatch]);
 58impl_actions!(
 59    vim,
 60    [FindCommand, ReplaceCommand, Search, MoveToPrev, MoveToNext]
 61);
 62
 63static RANGE_REGEX: OnceLock<Regex> = OnceLock::new();
 64pub(crate) fn range_regex() -> &'static Regex {
 65    RANGE_REGEX.get_or_init(|| Regex::new(r"^(\d+),(\d+)s(.*)").unwrap())
 66}
 67
 68pub(crate) fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
 69    workspace.register_action(move_to_next);
 70    workspace.register_action(move_to_prev);
 71    workspace.register_action(move_to_next_match);
 72    workspace.register_action(move_to_prev_match);
 73    workspace.register_action(search);
 74    workspace.register_action(search_submit);
 75    workspace.register_action(search_deploy);
 76
 77    workspace.register_action(find_command);
 78    workspace.register_action(replace_command);
 79}
 80
 81fn move_to_next(workspace: &mut Workspace, action: &MoveToNext, cx: &mut ViewContext<Workspace>) {
 82    move_to_internal(workspace, Direction::Next, !action.partial_word, cx)
 83}
 84
 85fn move_to_prev(workspace: &mut Workspace, action: &MoveToPrev, cx: &mut ViewContext<Workspace>) {
 86    move_to_internal(workspace, Direction::Prev, !action.partial_word, cx)
 87}
 88
 89fn move_to_next_match(
 90    workspace: &mut Workspace,
 91    _: &MoveToNextMatch,
 92    cx: &mut ViewContext<Workspace>,
 93) {
 94    move_to_match_internal(workspace, Direction::Next, cx)
 95}
 96
 97fn move_to_prev_match(
 98    workspace: &mut Workspace,
 99    _: &MoveToPrevMatch,
100    cx: &mut ViewContext<Workspace>,
101) {
102    move_to_match_internal(workspace, Direction::Prev, cx)
103}
104
105fn search(workspace: &mut Workspace, action: &Search, cx: &mut ViewContext<Workspace>) {
106    let pane = workspace.active_pane().clone();
107    let direction = if action.backwards {
108        Direction::Prev
109    } else {
110        Direction::Next
111    };
112    Vim::update(cx, |vim, cx| {
113        let count = vim.take_count(cx).unwrap_or(1);
114        let prior_selections = vim.editor_selections(cx);
115        pane.update(cx, |pane, cx| {
116            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
117                search_bar.update(cx, |search_bar, cx| {
118                    if !search_bar.show(cx) {
119                        return;
120                    }
121                    let query = search_bar.query(cx);
122
123                    search_bar.select_query(cx);
124                    cx.focus_self();
125
126                    if query.is_empty() {
127                        search_bar.set_replacement(None, cx);
128                        search_bar.set_search_options(SearchOptions::REGEX, cx);
129                    }
130                    vim.workspace_state.search = SearchState {
131                        direction,
132                        count,
133                        initial_query: query.clone(),
134                        prior_selections,
135                        prior_operator: vim.active_operator(),
136                        prior_mode: vim.state().mode,
137                    };
138                });
139            }
140        })
141    })
142}
143
144// hook into the existing to clear out any vim search state on cmd+f or edit -> find.
145fn search_deploy(_: &mut Workspace, _: &buffer_search::Deploy, cx: &mut ViewContext<Workspace>) {
146    Vim::update(cx, |vim, _| vim.workspace_state.search = Default::default());
147    cx.propagate();
148}
149
150fn search_submit(workspace: &mut Workspace, _: &SearchSubmit, cx: &mut ViewContext<Workspace>) {
151    let mut motion = None;
152    Vim::update(cx, |vim, cx| {
153        let pane = workspace.active_pane().clone();
154        pane.update(cx, |pane, cx| {
155            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
156                search_bar.update(cx, |search_bar, cx| {
157                    let state = &mut vim.workspace_state.search;
158                    let mut count = state.count;
159                    let direction = state.direction;
160
161                    // in the case that the query has changed, the search bar
162                    // will have selected the next match already.
163                    if (search_bar.query(cx) != state.initial_query)
164                        && state.direction == Direction::Next
165                    {
166                        count = count.saturating_sub(1)
167                    }
168                    state.count = 1;
169                    search_bar.select_match(direction, count, cx);
170                    search_bar.focus_editor(&Default::default(), cx);
171
172                    let mut prior_selections: Vec<_> = state.prior_selections.drain(..).collect();
173                    let prior_mode = state.prior_mode;
174                    let prior_operator = state.prior_operator.take();
175                    let new_selections = vim.editor_selections(cx);
176
177                    // If the active editor has changed during a search, don't panic.
178                    if prior_selections.iter().any(|s| {
179                        vim.update_active_editor(cx, |_vim, editor, cx| {
180                            !s.start.is_valid(&editor.snapshot(cx).buffer_snapshot)
181                        })
182                        .unwrap_or(true)
183                    }) {
184                        prior_selections.clear();
185                    }
186
187                    if prior_mode != vim.state().mode {
188                        vim.switch_mode(prior_mode, true, cx);
189                    }
190                    if let Some(operator) = prior_operator {
191                        vim.push_operator(operator, cx);
192                    };
193                    motion = Some(Motion::ZedSearchResult {
194                        prior_selections,
195                        new_selections,
196                    });
197                });
198            }
199        });
200    });
201
202    if let Some(motion) = motion {
203        search_motion(motion, cx)
204    }
205}
206
207pub fn move_to_match_internal(
208    workspace: &mut Workspace,
209    direction: Direction,
210    cx: &mut ViewContext<Workspace>,
211) {
212    let mut motion = None;
213    Vim::update(cx, |vim, cx| {
214        let pane = workspace.active_pane().clone();
215        let count = vim.take_count(cx).unwrap_or(1);
216        let prior_selections = vim.editor_selections(cx);
217
218        pane.update(cx, |pane, cx| {
219            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
220                search_bar.update(cx, |search_bar, cx| {
221                    search_bar.select_match(direction, count, cx);
222
223                    let new_selections = vim.editor_selections(cx);
224                    motion = Some(Motion::ZedSearchResult {
225                        prior_selections,
226                        new_selections,
227                    });
228                })
229            }
230        })
231    });
232    if let Some(motion) = motion {
233        search_motion(motion, cx);
234    }
235}
236
237pub fn move_to_internal(
238    workspace: &mut Workspace,
239    direction: Direction,
240    whole_word: bool,
241    cx: &mut ViewContext<Workspace>,
242) {
243    Vim::update(cx, |vim, cx| {
244        let pane = workspace.active_pane().clone();
245        let count = vim.take_count(cx).unwrap_or(1);
246        let prior_selections = vim.editor_selections(cx);
247
248        pane.update(cx, |pane, cx| {
249            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
250                let search = search_bar.update(cx, |search_bar, cx| {
251                    let options = SearchOptions::CASE_SENSITIVE | SearchOptions::REGEX;
252                    if !search_bar.show(cx) {
253                        return None;
254                    }
255                    let Some(query) = search_bar.query_suggestion(cx) else {
256                        vim.clear_operator(cx);
257                        let _ = search_bar.search("", None, cx);
258                        return None;
259                    };
260                    let mut query = regex::escape(&query);
261                    if whole_word {
262                        query = format!(r"\<{}\>", query);
263                    }
264                    Some(search_bar.search(&query, Some(options), cx))
265                });
266
267                if let Some(search) = search {
268                    let search_bar = search_bar.downgrade();
269                    cx.spawn(|_, mut cx| async move {
270                        search.await?;
271                        search_bar.update(&mut cx, |search_bar, cx| {
272                            search_bar.select_match(direction, count, cx);
273
274                            let new_selections =
275                                Vim::update(cx, |vim, cx| vim.editor_selections(cx));
276                            search_motion(
277                                Motion::ZedSearchResult {
278                                    prior_selections,
279                                    new_selections,
280                                },
281                                cx,
282                            )
283                        })?;
284                        anyhow::Ok(())
285                    })
286                    .detach_and_log_err(cx);
287                }
288            }
289        });
290
291        if vim.state().mode.is_visual() {
292            vim.switch_mode(Mode::Normal, false, cx)
293        }
294    });
295}
296
297fn find_command(workspace: &mut Workspace, action: &FindCommand, cx: &mut ViewContext<Workspace>) {
298    let pane = workspace.active_pane().clone();
299    pane.update(cx, |pane, cx| {
300        if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
301            let search = search_bar.update(cx, |search_bar, cx| {
302                if !search_bar.show(cx) {
303                    return None;
304                }
305                let mut query = action.query.clone();
306                if query == "" {
307                    query = search_bar.query(cx);
308                };
309
310                Some(search_bar.search(
311                    &query,
312                    Some(SearchOptions::CASE_SENSITIVE | SearchOptions::REGEX),
313                    cx,
314                ))
315            });
316            let Some(search) = search else { return };
317            let search_bar = search_bar.downgrade();
318            let direction = if action.backwards {
319                Direction::Prev
320            } else {
321                Direction::Next
322            };
323            cx.spawn(|_, mut cx| async move {
324                search.await?;
325                search_bar.update(&mut cx, |search_bar, cx| {
326                    search_bar.select_match(direction, 1, cx)
327                })?;
328                anyhow::Ok(())
329            })
330            .detach_and_log_err(cx);
331        }
332    })
333}
334
335fn replace_command(
336    workspace: &mut Workspace,
337    action: &ReplaceCommand,
338    cx: &mut ViewContext<Workspace>,
339) {
340    let replacement = parse_replace_all(&action.query);
341    let pane = workspace.active_pane().clone();
342    let mut editor = Vim::read(cx)
343        .active_editor
344        .as_ref()
345        .and_then(|editor| editor.upgrade());
346    if let Some(range) = &replacement.range {
347        if let Some(editor) = editor.as_mut() {
348            editor.update(cx, |editor, cx| {
349                let snapshot = &editor.snapshot(cx).buffer_snapshot;
350                let range = snapshot
351                    .anchor_before(Point::new(range.start.saturating_sub(1) as u32, 0))
352                    ..snapshot.anchor_before(Point::new(range.end as u32, 0));
353
354                editor.set_search_within_ranges(&[range], cx)
355            })
356        }
357    }
358    pane.update(cx, |pane, cx| {
359        let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
360            return;
361        };
362        let search = search_bar.update(cx, |search_bar, cx| {
363            if !search_bar.show(cx) {
364                return None;
365            }
366
367            let mut options = SearchOptions::REGEX;
368            if replacement.is_case_sensitive {
369                options.set(SearchOptions::CASE_SENSITIVE, true)
370            }
371            let search = if replacement.search == "" {
372                search_bar.query(cx)
373            } else {
374                replacement.search
375            };
376
377            search_bar.set_replacement(Some(&replacement.replacement), cx);
378            Some(search_bar.search(&search, Some(options), cx))
379        });
380        let Some(search) = search else { return };
381        let search_bar = search_bar.downgrade();
382        cx.spawn(|_, mut cx| async move {
383            search.await?;
384            search_bar.update(&mut cx, |search_bar, cx| {
385                if replacement.should_replace_all {
386                    search_bar.select_last_match(cx);
387                    search_bar.replace_all(&Default::default(), cx);
388                    if let Some(editor) = editor {
389                        cx.spawn(|_, mut cx| async move {
390                            cx.background_executor()
391                                .timer(Duration::from_millis(200))
392                                .await;
393                            editor
394                                .update(&mut cx, |editor, cx| {
395                                    editor.set_search_within_ranges(&[], cx)
396                                })
397                                .ok();
398                        })
399                        .detach();
400                    }
401                    Vim::update(cx, |vim, cx| {
402                        move_cursor(
403                            vim,
404                            Motion::StartOfLine {
405                                display_lines: false,
406                            },
407                            None,
408                            cx,
409                        )
410                    })
411                }
412            })?;
413            anyhow::Ok(())
414        })
415        .detach_and_log_err(cx);
416    })
417}
418
419// convert a vim query into something more usable by zed.
420// we don't attempt to fully convert between the two regex syntaxes,
421// but we do flip \( and \) to ( and ) (and vice-versa) in the pattern,
422// and convert \0..\9 to $0..$9 in the replacement so that common idioms work.
423fn parse_replace_all(query: &str) -> Replacement {
424    let mut chars = query.chars();
425    let mut range = None;
426    let maybe_line_range_and_rest: Option<(Range<usize>, &str)> =
427        range_regex().captures(query).map(|captures| {
428            (
429                captures.get(1).unwrap().as_str().parse().unwrap()
430                    ..captures.get(2).unwrap().as_str().parse().unwrap(),
431                captures.get(3).unwrap().as_str(),
432            )
433        });
434    if maybe_line_range_and_rest.is_some() {
435        let (line_range, rest) = maybe_line_range_and_rest.unwrap();
436        range = Some(line_range);
437        chars = rest.chars();
438    } else if Some('%') != chars.next() || Some('s') != chars.next() {
439        return Replacement::default();
440    }
441
442    let Some(delimiter) = chars.next() else {
443        return Replacement::default();
444    };
445
446    let mut search = String::new();
447    let mut replacement = String::new();
448    let mut flags = String::new();
449
450    let mut buffer = &mut search;
451
452    let mut escaped = false;
453    // 0 - parsing search
454    // 1 - parsing replacement
455    // 2 - parsing flags
456    let mut phase = 0;
457
458    for c in chars {
459        if escaped {
460            escaped = false;
461            if phase == 1 && c.is_digit(10) {
462                buffer.push('$')
463            // unescape escaped parens
464            } else if phase == 0 && c == '(' || c == ')' {
465            } else if c != delimiter {
466                buffer.push('\\')
467            }
468            buffer.push(c)
469        } else if c == '\\' {
470            escaped = true;
471        } else if c == delimiter {
472            if phase == 0 {
473                buffer = &mut replacement;
474                phase = 1;
475            } else if phase == 1 {
476                buffer = &mut flags;
477                phase = 2;
478            } else {
479                break;
480            }
481        } else {
482            // escape unescaped parens
483            if phase == 0 && c == '(' || c == ')' {
484                buffer.push('\\')
485            }
486            buffer.push(c)
487        }
488    }
489
490    let mut replacement = Replacement {
491        search,
492        replacement,
493        should_replace_all: true,
494        is_case_sensitive: true,
495        range,
496    };
497
498    for c in flags.chars() {
499        match c {
500            'g' | 'I' => {}
501            'c' | 'n' => replacement.should_replace_all = false,
502            'i' => replacement.is_case_sensitive = false,
503            _ => {}
504        }
505    }
506
507    replacement
508}
509
510#[cfg(test)]
511mod test {
512    use editor::DisplayPoint;
513    use indoc::indoc;
514    use search::BufferSearchBar;
515
516    use crate::{
517        state::Mode,
518        test::{NeovimBackedTestContext, VimTestContext},
519    };
520
521    #[gpui::test]
522    async fn test_move_to_next(cx: &mut gpui::TestAppContext) {
523        let mut cx = VimTestContext::new(cx, true).await;
524        cx.set_state("ˇhi\nhigh\nhi\n", Mode::Normal);
525
526        cx.simulate_keystrokes(["*"]);
527        cx.run_until_parked();
528        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
529
530        cx.simulate_keystrokes(["*"]);
531        cx.run_until_parked();
532        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
533
534        cx.simulate_keystrokes(["#"]);
535        cx.run_until_parked();
536        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
537
538        cx.simulate_keystrokes(["#"]);
539        cx.run_until_parked();
540        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
541
542        cx.simulate_keystrokes(["2", "*"]);
543        cx.run_until_parked();
544        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
545
546        cx.simulate_keystrokes(["g", "*"]);
547        cx.run_until_parked();
548        cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
549
550        cx.simulate_keystrokes(["n"]);
551        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
552
553        cx.simulate_keystrokes(["g", "#"]);
554        cx.run_until_parked();
555        cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
556    }
557
558    #[gpui::test]
559    async fn test_search(cx: &mut gpui::TestAppContext) {
560        let mut cx = VimTestContext::new(cx, true).await;
561
562        cx.set_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
563        cx.simulate_keystrokes(["/", "c", "c"]);
564
565        let search_bar = cx.workspace(|workspace, cx| {
566            workspace
567                .active_pane()
568                .read(cx)
569                .toolbar()
570                .read(cx)
571                .item_of_type::<BufferSearchBar>()
572                .expect("Buffer search bar should be deployed")
573        });
574
575        cx.update_view(search_bar, |bar, cx| {
576            assert_eq!(bar.query(cx), "cc");
577        });
578
579        cx.run_until_parked();
580
581        cx.update_editor(|editor, cx| {
582            let highlights = editor.all_text_background_highlights(cx);
583            assert_eq!(3, highlights.len());
584            assert_eq!(
585                DisplayPoint::new(2, 0)..DisplayPoint::new(2, 2),
586                highlights[0].0
587            )
588        });
589
590        cx.simulate_keystrokes(["enter"]);
591        cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
592
593        // n to go to next/N to go to previous
594        cx.simulate_keystrokes(["n"]);
595        cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
596        cx.simulate_keystrokes(["shift-n"]);
597        cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
598
599        // ?<enter> to go to previous
600        cx.simulate_keystrokes(["?", "enter"]);
601        cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
602        cx.simulate_keystrokes(["?", "enter"]);
603        cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
604
605        // /<enter> to go to next
606        cx.simulate_keystrokes(["/", "enter"]);
607        cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
608
609        // ?{search}<enter> to search backwards
610        cx.simulate_keystrokes(["?", "b", "enter"]);
611        cx.assert_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
612
613        // works with counts
614        cx.simulate_keystrokes(["4", "/", "c"]);
615        cx.simulate_keystrokes(["enter"]);
616        cx.assert_state("aa\nbb\ncc\ncˇc\ncc\n", Mode::Normal);
617
618        // check that searching resumes from cursor, not previous match
619        cx.set_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
620        cx.simulate_keystrokes(["/", "d"]);
621        cx.simulate_keystrokes(["enter"]);
622        cx.assert_state("aa\nbb\nˇdd\ncc\nbb\n", Mode::Normal);
623        cx.update_editor(|editor, cx| editor.move_to_beginning(&Default::default(), cx));
624        cx.assert_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
625        cx.simulate_keystrokes(["/", "b"]);
626        cx.simulate_keystrokes(["enter"]);
627        cx.assert_state("aa\nˇbb\ndd\ncc\nbb\n", Mode::Normal);
628
629        // check that searching switches to normal mode if in visual mode
630        cx.set_state("ˇone two one", Mode::Normal);
631        cx.simulate_keystrokes(["v", "l", "l"]);
632        cx.assert_editor_state("«oneˇ» two one");
633        cx.simulate_keystrokes(["*"]);
634        cx.assert_state("one two ˇone", Mode::Normal);
635    }
636
637    #[gpui::test]
638    async fn test_non_vim_search(cx: &mut gpui::TestAppContext) {
639        let mut cx = VimTestContext::new(cx, false).await;
640        cx.set_state("ˇone one one one", Mode::Normal);
641        cx.simulate_keystrokes(["cmd-f"]);
642        cx.run_until_parked();
643
644        cx.assert_editor_state("«oneˇ» one one one");
645        cx.simulate_keystrokes(["enter"]);
646        cx.assert_editor_state("one «oneˇ» one one");
647        cx.simulate_keystrokes(["shift-enter"]);
648        cx.assert_editor_state("«oneˇ» one one one");
649    }
650
651    #[gpui::test]
652    async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
653        let mut cx = NeovimBackedTestContext::new(cx).await;
654
655        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
656        cx.simulate_shared_keystrokes(["v", "3", "l", "*"]).await;
657        cx.assert_shared_state("a.c. abcd ˇa.c. abcd").await;
658        cx.assert_shared_mode(Mode::Normal).await;
659    }
660
661    #[gpui::test]
662    async fn test_d_search(cx: &mut gpui::TestAppContext) {
663        let mut cx = NeovimBackedTestContext::new(cx).await;
664
665        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
666        cx.simulate_shared_keystrokes(["d", "/", "c", "d"]).await;
667        cx.simulate_shared_keystrokes(["enter"]).await;
668        cx.assert_shared_state("ˇcd a.c. abcd").await;
669    }
670
671    #[gpui::test]
672    async fn test_v_search(cx: &mut gpui::TestAppContext) {
673        let mut cx = NeovimBackedTestContext::new(cx).await;
674
675        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
676        cx.simulate_shared_keystrokes(["v", "/", "c", "d"]).await;
677        cx.simulate_shared_keystrokes(["enter"]).await;
678        cx.assert_shared_state("«a.c. abcˇ»d a.c. abcd").await;
679
680        cx.set_shared_state("a a aˇ a a a").await;
681        cx.simulate_shared_keystrokes(["v", "/", "a"]).await;
682        cx.simulate_shared_keystrokes(["enter"]).await;
683        cx.assert_shared_state("a a a« aˇ» a a").await;
684        cx.simulate_shared_keystrokes(["/", "enter"]).await;
685        cx.assert_shared_state("a a a« a aˇ» a").await;
686        cx.simulate_shared_keystrokes(["?", "enter"]).await;
687        cx.assert_shared_state("a a a« aˇ» a a").await;
688        cx.simulate_shared_keystrokes(["?", "enter"]).await;
689        cx.assert_shared_state("a a «ˇa »a a a").await;
690        cx.simulate_shared_keystrokes(["/", "enter"]).await;
691        cx.assert_shared_state("a a a« aˇ» a a").await;
692        cx.simulate_shared_keystrokes(["/", "enter"]).await;
693        cx.assert_shared_state("a a a« a aˇ» a").await;
694    }
695
696    #[gpui::test]
697    async fn test_visual_block_search(cx: &mut gpui::TestAppContext) {
698        let mut cx = NeovimBackedTestContext::new(cx).await;
699
700        cx.set_shared_state(indoc! {
701            "ˇone two
702             three four
703             five six
704             "
705        })
706        .await;
707        cx.simulate_shared_keystrokes(["ctrl-v", "j", "/", "f"])
708            .await;
709        cx.simulate_shared_keystrokes(["enter"]).await;
710        cx.assert_shared_state(indoc! {
711            "«one twoˇ»
712             «three fˇ»our
713             five six
714             "
715        })
716        .await;
717    }
718
719    // cargo test -p vim --features neovim test_replace_with_range
720    #[gpui::test]
721    async fn test_replace_with_range(cx: &mut gpui::TestAppContext) {
722        let mut cx = NeovimBackedTestContext::new(cx).await;
723
724        cx.set_shared_state(indoc! {
725            "ˇa
726            a
727            a
728            a
729            a
730            a
731            a
732             "
733        })
734        .await;
735        cx.simulate_shared_keystrokes([":", "2", ",", "5", "s", "/", "a", "/", "b"])
736            .await;
737        cx.simulate_shared_keystrokes(["enter"]).await;
738        cx.assert_shared_state(indoc! {
739            "a
740            b
741            b
742            b
743            ˇb
744            a
745            a
746             "
747        })
748        .await;
749    }
750}