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