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                    if !search_bar.has_active_match() || !search_bar.show(cx) {
222                        return;
223                    }
224                    search_bar.select_match(direction, count, cx);
225
226                    let new_selections = vim.editor_selections(cx);
227                    motion = Some(Motion::ZedSearchResult {
228                        prior_selections,
229                        new_selections,
230                    });
231                })
232            }
233        })
234    });
235    if let Some(motion) = motion {
236        search_motion(motion, cx);
237    }
238}
239
240pub fn move_to_internal(
241    workspace: &mut Workspace,
242    direction: Direction,
243    whole_word: bool,
244    cx: &mut ViewContext<Workspace>,
245) {
246    Vim::update(cx, |vim, cx| {
247        let pane = workspace.active_pane().clone();
248        let count = vim.take_count(cx).unwrap_or(1);
249        let prior_selections = vim.editor_selections(cx);
250
251        pane.update(cx, |pane, cx| {
252            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
253                let search = search_bar.update(cx, |search_bar, cx| {
254                    let options = SearchOptions::CASE_SENSITIVE | SearchOptions::REGEX;
255                    if !search_bar.show(cx) {
256                        return None;
257                    }
258                    let Some(query) = search_bar.query_suggestion(cx) else {
259                        vim.clear_operator(cx);
260                        let _ = search_bar.search("", None, cx);
261                        return None;
262                    };
263                    let mut query = regex::escape(&query);
264                    if whole_word {
265                        query = format!(r"\<{}\>", query);
266                    }
267                    Some(search_bar.search(&query, Some(options), cx))
268                });
269
270                if let Some(search) = search {
271                    let search_bar = search_bar.downgrade();
272                    cx.spawn(|_, mut cx| async move {
273                        search.await?;
274                        search_bar.update(&mut cx, |search_bar, cx| {
275                            search_bar.select_match(direction, count, cx);
276
277                            let new_selections =
278                                Vim::update(cx, |vim, cx| vim.editor_selections(cx));
279                            search_motion(
280                                Motion::ZedSearchResult {
281                                    prior_selections,
282                                    new_selections,
283                                },
284                                cx,
285                            )
286                        })?;
287                        anyhow::Ok(())
288                    })
289                    .detach_and_log_err(cx);
290                }
291            }
292        });
293
294        if vim.state().mode.is_visual() {
295            vim.switch_mode(Mode::Normal, false, cx)
296        }
297    });
298}
299
300fn find_command(workspace: &mut Workspace, action: &FindCommand, cx: &mut ViewContext<Workspace>) {
301    let pane = workspace.active_pane().clone();
302    pane.update(cx, |pane, cx| {
303        if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
304            let search = search_bar.update(cx, |search_bar, cx| {
305                if !search_bar.show(cx) {
306                    return None;
307                }
308                let mut query = action.query.clone();
309                if query == "" {
310                    query = search_bar.query(cx);
311                };
312
313                Some(search_bar.search(
314                    &query,
315                    Some(SearchOptions::CASE_SENSITIVE | SearchOptions::REGEX),
316                    cx,
317                ))
318            });
319            let Some(search) = search else { return };
320            let search_bar = search_bar.downgrade();
321            let direction = if action.backwards {
322                Direction::Prev
323            } else {
324                Direction::Next
325            };
326            cx.spawn(|_, mut cx| async move {
327                search.await?;
328                search_bar.update(&mut cx, |search_bar, cx| {
329                    search_bar.select_match(direction, 1, cx)
330                })?;
331                anyhow::Ok(())
332            })
333            .detach_and_log_err(cx);
334        }
335    })
336}
337
338fn replace_command(
339    workspace: &mut Workspace,
340    action: &ReplaceCommand,
341    cx: &mut ViewContext<Workspace>,
342) {
343    let replacement = parse_replace_all(&action.query);
344    let pane = workspace.active_pane().clone();
345    let mut editor = Vim::read(cx)
346        .active_editor
347        .as_ref()
348        .and_then(|editor| editor.upgrade());
349    if let Some(range) = &replacement.range {
350        if let Some(editor) = editor.as_mut() {
351            editor.update(cx, |editor, cx| {
352                let snapshot = &editor.snapshot(cx).buffer_snapshot;
353                let range = snapshot
354                    .anchor_before(Point::new(range.start.saturating_sub(1) as u32, 0))
355                    ..snapshot.anchor_before(Point::new(range.end as u32, 0));
356
357                editor.set_search_within_ranges(&[range], cx)
358            })
359        }
360    }
361    pane.update(cx, |pane, cx| {
362        let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
363            return;
364        };
365        let search = search_bar.update(cx, |search_bar, cx| {
366            if !search_bar.show(cx) {
367                return None;
368            }
369
370            let mut options = SearchOptions::REGEX;
371            if replacement.is_case_sensitive {
372                options.set(SearchOptions::CASE_SENSITIVE, true)
373            }
374            let search = if replacement.search == "" {
375                search_bar.query(cx)
376            } else {
377                replacement.search
378            };
379
380            search_bar.set_replacement(Some(&replacement.replacement), cx);
381            Some(search_bar.search(&search, Some(options), cx))
382        });
383        let Some(search) = search else { return };
384        let search_bar = search_bar.downgrade();
385        cx.spawn(|_, mut cx| async move {
386            search.await?;
387            search_bar.update(&mut cx, |search_bar, cx| {
388                if replacement.should_replace_all {
389                    search_bar.select_last_match(cx);
390                    search_bar.replace_all(&Default::default(), cx);
391                    if let Some(editor) = editor {
392                        cx.spawn(|_, mut cx| async move {
393                            cx.background_executor()
394                                .timer(Duration::from_millis(200))
395                                .await;
396                            editor
397                                .update(&mut cx, |editor, cx| {
398                                    editor.set_search_within_ranges(&[], cx)
399                                })
400                                .ok();
401                        })
402                        .detach();
403                    }
404                    Vim::update(cx, |vim, cx| {
405                        move_cursor(
406                            vim,
407                            Motion::StartOfLine {
408                                display_lines: false,
409                            },
410                            None,
411                            cx,
412                        )
413                    })
414                }
415            })?;
416            anyhow::Ok(())
417        })
418        .detach_and_log_err(cx);
419    })
420}
421
422// convert a vim query into something more usable by zed.
423// we don't attempt to fully convert between the two regex syntaxes,
424// but we do flip \( and \) to ( and ) (and vice-versa) in the pattern,
425// and convert \0..\9 to $0..$9 in the replacement so that common idioms work.
426fn parse_replace_all(query: &str) -> Replacement {
427    let mut chars = query.chars();
428    let mut range = None;
429    let maybe_line_range_and_rest: Option<(Range<usize>, &str)> =
430        range_regex().captures(query).map(|captures| {
431            (
432                captures.get(1).unwrap().as_str().parse().unwrap()
433                    ..captures.get(2).unwrap().as_str().parse().unwrap(),
434                captures.get(3).unwrap().as_str(),
435            )
436        });
437    if maybe_line_range_and_rest.is_some() {
438        let (line_range, rest) = maybe_line_range_and_rest.unwrap();
439        range = Some(line_range);
440        chars = rest.chars();
441    } else if Some('%') != chars.next() || Some('s') != chars.next() {
442        return Replacement::default();
443    }
444
445    let Some(delimiter) = chars.next() else {
446        return Replacement::default();
447    };
448
449    let mut search = String::new();
450    let mut replacement = String::new();
451    let mut flags = String::new();
452
453    let mut buffer = &mut search;
454
455    let mut escaped = false;
456    // 0 - parsing search
457    // 1 - parsing replacement
458    // 2 - parsing flags
459    let mut phase = 0;
460
461    for c in chars {
462        if escaped {
463            escaped = false;
464            if phase == 1 && c.is_digit(10) {
465                buffer.push('$')
466            // unescape escaped parens
467            } else if phase == 0 && c == '(' || c == ')' {
468            } else if c != delimiter {
469                buffer.push('\\')
470            }
471            buffer.push(c)
472        } else if c == '\\' {
473            escaped = true;
474        } else if c == delimiter {
475            if phase == 0 {
476                buffer = &mut replacement;
477                phase = 1;
478            } else if phase == 1 {
479                buffer = &mut flags;
480                phase = 2;
481            } else {
482                break;
483            }
484        } else {
485            // escape unescaped parens
486            if phase == 0 && c == '(' || c == ')' {
487                buffer.push('\\')
488            }
489            buffer.push(c)
490        }
491    }
492
493    let mut replacement = Replacement {
494        search,
495        replacement,
496        should_replace_all: true,
497        is_case_sensitive: true,
498        range,
499    };
500
501    for c in flags.chars() {
502        match c {
503            'g' | 'I' => {}
504            'c' | 'n' => replacement.should_replace_all = false,
505            'i' => replacement.is_case_sensitive = false,
506            _ => {}
507        }
508    }
509
510    replacement
511}
512
513#[cfg(test)]
514mod test {
515    use editor::{display_map::DisplayRow, DisplayPoint};
516    use indoc::indoc;
517    use search::BufferSearchBar;
518
519    use crate::{
520        state::Mode,
521        test::{NeovimBackedTestContext, VimTestContext},
522    };
523
524    #[gpui::test]
525    async fn test_move_to_next(cx: &mut gpui::TestAppContext) {
526        let mut cx = VimTestContext::new(cx, true).await;
527        cx.set_state("ˇhi\nhigh\nhi\n", Mode::Normal);
528
529        cx.simulate_keystrokes("*");
530        cx.run_until_parked();
531        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
532
533        cx.simulate_keystrokes("*");
534        cx.run_until_parked();
535        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
536
537        cx.simulate_keystrokes("#");
538        cx.run_until_parked();
539        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
540
541        cx.simulate_keystrokes("#");
542        cx.run_until_parked();
543        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
544
545        cx.simulate_keystrokes("2 *");
546        cx.run_until_parked();
547        cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
548
549        cx.simulate_keystrokes("g *");
550        cx.run_until_parked();
551        cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
552
553        cx.simulate_keystrokes("n");
554        cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
555
556        cx.simulate_keystrokes("g #");
557        cx.run_until_parked();
558        cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
559    }
560
561    #[gpui::test]
562    async fn test_search(cx: &mut gpui::TestAppContext) {
563        let mut cx = VimTestContext::new(cx, true).await;
564
565        cx.set_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
566        cx.simulate_keystrokes("/ c c");
567
568        let search_bar = cx.workspace(|workspace, cx| {
569            workspace
570                .active_pane()
571                .read(cx)
572                .toolbar()
573                .read(cx)
574                .item_of_type::<BufferSearchBar>()
575                .expect("Buffer search bar should be deployed")
576        });
577
578        cx.update_view(search_bar, |bar, cx| {
579            assert_eq!(bar.query(cx), "cc");
580        });
581
582        cx.run_until_parked();
583
584        cx.update_editor(|editor, cx| {
585            let highlights = editor.all_text_background_highlights(cx);
586            assert_eq!(3, highlights.len());
587            assert_eq!(
588                DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
589                highlights[0].0
590            )
591        });
592
593        cx.simulate_keystrokes("enter");
594        cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
595
596        // n to go to next/N to go to previous
597        cx.simulate_keystrokes("n");
598        cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
599        cx.simulate_keystrokes("shift-n");
600        cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
601
602        // ?<enter> to go to previous
603        cx.simulate_keystrokes("? enter");
604        cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
605        cx.simulate_keystrokes("? enter");
606        cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
607
608        // /<enter> to go to next
609        cx.simulate_keystrokes("/ enter");
610        cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
611
612        // ?{search}<enter> to search backwards
613        cx.simulate_keystrokes("? b enter");
614        cx.assert_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
615
616        // works with counts
617        cx.simulate_keystrokes("4 / c");
618        cx.simulate_keystrokes("enter");
619        cx.assert_state("aa\nbb\ncc\ncˇc\ncc\n", Mode::Normal);
620
621        // check that searching resumes from cursor, not previous match
622        cx.set_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
623        cx.simulate_keystrokes("/ d");
624        cx.simulate_keystrokes("enter");
625        cx.assert_state("aa\nbb\nˇdd\ncc\nbb\n", Mode::Normal);
626        cx.update_editor(|editor, cx| editor.move_to_beginning(&Default::default(), cx));
627        cx.assert_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
628        cx.simulate_keystrokes("/ b");
629        cx.simulate_keystrokes("enter");
630        cx.assert_state("aa\nˇbb\ndd\ncc\nbb\n", Mode::Normal);
631
632        // check that searching switches to normal mode if in visual mode
633        cx.set_state("ˇone two one", Mode::Normal);
634        cx.simulate_keystrokes("v l l");
635        cx.assert_editor_state("«oneˇ» two one");
636        cx.simulate_keystrokes("*");
637        cx.assert_state("one two ˇone", Mode::Normal);
638    }
639
640    #[gpui::test]
641    async fn test_non_vim_search(cx: &mut gpui::TestAppContext) {
642        let mut cx = VimTestContext::new(cx, false).await;
643        cx.set_state("ˇone one one one", Mode::Normal);
644        cx.simulate_keystrokes("cmd-f");
645        cx.run_until_parked();
646
647        cx.assert_editor_state("«oneˇ» one one one");
648        cx.simulate_keystrokes("enter");
649        cx.assert_editor_state("one «oneˇ» one one");
650        cx.simulate_keystrokes("shift-enter");
651        cx.assert_editor_state("«oneˇ» one one one");
652    }
653
654    #[gpui::test]
655    async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
656        let mut cx = NeovimBackedTestContext::new(cx).await;
657
658        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
659        cx.simulate_shared_keystrokes("v 3 l *").await;
660        cx.shared_state().await.assert_eq("a.c. abcd ˇa.c. abcd");
661    }
662
663    #[gpui::test]
664    async fn test_d_search(cx: &mut gpui::TestAppContext) {
665        let mut cx = NeovimBackedTestContext::new(cx).await;
666
667        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
668        cx.simulate_shared_keystrokes("d / c d").await;
669        cx.simulate_shared_keystrokes("enter").await;
670        cx.shared_state().await.assert_eq("ˇcd a.c. abcd");
671    }
672
673    #[gpui::test]
674    async fn test_v_search(cx: &mut gpui::TestAppContext) {
675        let mut cx = NeovimBackedTestContext::new(cx).await;
676
677        cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
678        cx.simulate_shared_keystrokes("v / c d").await;
679        cx.simulate_shared_keystrokes("enter").await;
680        cx.shared_state().await.assert_eq("«a.c. abcˇ»d a.c. abcd");
681
682        cx.set_shared_state("a a aˇ a a a").await;
683        cx.simulate_shared_keystrokes("v / a").await;
684        cx.simulate_shared_keystrokes("enter").await;
685        cx.shared_state().await.assert_eq("a a a« aˇ» a a");
686        cx.simulate_shared_keystrokes("/ enter").await;
687        cx.shared_state().await.assert_eq("a a a« a aˇ» a");
688        cx.simulate_shared_keystrokes("? enter").await;
689        cx.shared_state().await.assert_eq("a a a« aˇ» a a");
690        cx.simulate_shared_keystrokes("? enter").await;
691        cx.shared_state().await.assert_eq("a a «ˇa »a a a");
692        cx.simulate_shared_keystrokes("/ enter").await;
693        cx.shared_state().await.assert_eq("a a a« aˇ» a a");
694        cx.simulate_shared_keystrokes("/ enter").await;
695        cx.shared_state().await.assert_eq("a a a« a aˇ» a");
696    }
697
698    #[gpui::test]
699    async fn test_visual_block_search(cx: &mut gpui::TestAppContext) {
700        let mut cx = NeovimBackedTestContext::new(cx).await;
701
702        cx.set_shared_state(indoc! {
703            "ˇone two
704             three four
705             five six
706             "
707        })
708        .await;
709        cx.simulate_shared_keystrokes("ctrl-v j / f").await;
710        cx.simulate_shared_keystrokes("enter").await;
711        cx.shared_state().await.assert_eq(indoc! {
712            "«one twoˇ»
713             «three fˇ»our
714             five six
715             "
716        });
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").await;
736        cx.simulate_shared_keystrokes("enter").await;
737        cx.shared_state().await.assert_eq(indoc! {
738            "a
739            b
740            b
741            b
742            ˇb
743            a
744            a
745             "
746        });
747    }
748}