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: 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 let mut options = SearchOptions::REGEX | SearchOptions::CASE_SENSITIVE;
306 if search_bar.should_use_smartcase_search(cx) {
307 options.set(
308 SearchOptions::CASE_SENSITIVE,
309 search_bar.is_contains_uppercase(&query),
310 );
311 }
312
313 Some(search_bar.search(&query, Some(options), cx))
314 });
315 let Some(search) = search else { return };
316 let search_bar = search_bar.downgrade();
317 let direction = if action.backwards {
318 Direction::Prev
319 } else {
320 Direction::Next
321 };
322 cx.spawn(|_, mut cx| async move {
323 search.await?;
324 search_bar.update(&mut cx, |search_bar, cx| {
325 search_bar.select_match(direction, 1, cx)
326 })?;
327 anyhow::Ok(())
328 })
329 .detach_and_log_err(cx);
330 }
331 })
332 }
333
334 fn replace_command(&mut self, action: &ReplaceCommand, cx: &mut ViewContext<Self>) {
335 let replacement = action.replacement.clone();
336 let Some(((pane, workspace), editor)) =
337 self.pane(cx).zip(self.workspace(cx)).zip(self.editor())
338 else {
339 return;
340 };
341 if let Some(result) = self.update_editor(cx, |vim, editor, cx| {
342 let range = action.range.buffer_range(vim, editor, cx)?;
343 let snapshot = &editor.snapshot(cx).buffer_snapshot;
344 let end_point = Point::new(range.end.0, snapshot.line_len(range.end));
345 let range = snapshot.anchor_before(Point::new(range.start.0, 0))
346 ..snapshot.anchor_after(end_point);
347 editor.set_search_within_ranges(&[range], cx);
348 anyhow::Ok(())
349 }) {
350 workspace.update(cx, |workspace, cx| {
351 result.notify_err(workspace, cx);
352 })
353 }
354 let vim = cx.view().clone();
355 pane.update(cx, |pane, cx| {
356 let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
357 return;
358 };
359 let search = search_bar.update(cx, |search_bar, cx| {
360 if !search_bar.show(cx) {
361 return None;
362 }
363
364 let mut options = SearchOptions::REGEX;
365 if replacement.is_case_sensitive {
366 options.set(SearchOptions::CASE_SENSITIVE, true)
367 }
368 let search = if replacement.search == "" {
369 search_bar.query(cx)
370 } else {
371 replacement.search
372 };
373 if search_bar.should_use_smartcase_search(cx) {
374 options.set(
375 SearchOptions::CASE_SENSITIVE,
376 search_bar.is_contains_uppercase(&search),
377 );
378 }
379 search_bar.set_replacement(Some(&replacement.replacement), cx);
380 Some(search_bar.search(&search, Some(options), cx))
381 });
382 let Some(search) = search else { return };
383 let search_bar = search_bar.downgrade();
384 cx.spawn(|_, mut cx| async move {
385 search.await?;
386 search_bar.update(&mut cx, |search_bar, cx| {
387 if replacement.should_replace_all {
388 search_bar.select_last_match(cx);
389 search_bar.replace_all(&Default::default(), cx);
390 cx.spawn(|_, mut cx| async move {
391 cx.background_executor()
392 .timer(Duration::from_millis(200))
393 .await;
394 editor
395 .update(&mut cx, |editor, cx| editor.clear_search_within_ranges(cx))
396 .ok();
397 })
398 .detach();
399 vim.update(cx, |vim, cx| {
400 vim.move_cursor(
401 Motion::StartOfLine {
402 display_lines: false,
403 },
404 None,
405 cx,
406 )
407 });
408 }
409 })?;
410 anyhow::Ok(())
411 })
412 .detach_and_log_err(cx);
413 })
414 }
415}
416
417impl Replacement {
418 // convert a vim query into something more usable by zed.
419 // we don't attempt to fully convert between the two regex syntaxes,
420 // but we do flip \( and \) to ( and ) (and vice-versa) in the pattern,
421 // and convert \0..\9 to $0..$9 in the replacement so that common idioms work.
422 pub(crate) fn parse(mut chars: Peekable<Chars>) -> Option<Replacement> {
423 let Some(delimiter) = chars
424 .next()
425 .filter(|c| !c.is_alphanumeric() && *c != '"' && *c != '|' && *c != '\'')
426 else {
427 return None;
428 };
429
430 let mut search = String::new();
431 let mut replacement = String::new();
432 let mut flags = String::new();
433
434 let mut buffer = &mut search;
435
436 let mut escaped = false;
437 // 0 - parsing search
438 // 1 - parsing replacement
439 // 2 - parsing flags
440 let mut phase = 0;
441
442 for c in chars {
443 if escaped {
444 escaped = false;
445 if phase == 1 && c.is_digit(10) {
446 buffer.push('$')
447 // unescape escaped parens
448 } else if phase == 0 && c == '(' || c == ')' {
449 } else if c != delimiter {
450 buffer.push('\\')
451 }
452 buffer.push(c)
453 } else if c == '\\' {
454 escaped = true;
455 } else if c == delimiter {
456 if phase == 0 {
457 buffer = &mut replacement;
458 phase = 1;
459 } else if phase == 1 {
460 buffer = &mut flags;
461 phase = 2;
462 } else {
463 break;
464 }
465 } else {
466 // escape unescaped parens
467 if phase == 0 && c == '(' || c == ')' {
468 buffer.push('\\')
469 }
470 buffer.push(c)
471 }
472 }
473
474 let mut replacement = Replacement {
475 search,
476 replacement,
477 should_replace_all: true,
478 is_case_sensitive: true,
479 };
480
481 for c in flags.chars() {
482 match c {
483 'g' | 'I' => {}
484 'c' | 'n' => replacement.should_replace_all = false,
485 'i' => replacement.is_case_sensitive = false,
486 _ => {}
487 }
488 }
489
490 Some(replacement)
491 }
492}
493
494#[cfg(test)]
495mod test {
496 use std::time::Duration;
497
498 use crate::{
499 state::Mode,
500 test::{NeovimBackedTestContext, VimTestContext},
501 };
502 use editor::EditorSettings;
503 use editor::{display_map::DisplayRow, DisplayPoint};
504 use indoc::indoc;
505 use search::BufferSearchBar;
506 use settings::SettingsStore;
507
508 #[gpui::test]
509 async fn test_move_to_next(cx: &mut gpui::TestAppContext) {
510 let mut cx = VimTestContext::new(cx, true).await;
511 cx.set_state("ˇhi\nhigh\nhi\n", Mode::Normal);
512
513 cx.simulate_keystrokes("*");
514 cx.run_until_parked();
515 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
516
517 cx.simulate_keystrokes("*");
518 cx.run_until_parked();
519 cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
520
521 cx.simulate_keystrokes("#");
522 cx.run_until_parked();
523 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
524
525 cx.simulate_keystrokes("#");
526 cx.run_until_parked();
527 cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
528
529 cx.simulate_keystrokes("2 *");
530 cx.run_until_parked();
531 cx.assert_state("ˇhi\nhigh\nhi\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 cx.simulate_keystrokes("n");
538 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
539
540 cx.simulate_keystrokes("g #");
541 cx.run_until_parked();
542 cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
543 }
544
545 #[gpui::test]
546 async fn test_move_to_next_with_no_search_wrap(cx: &mut gpui::TestAppContext) {
547 let mut cx = VimTestContext::new(cx, true).await;
548
549 cx.update_global(|store: &mut SettingsStore, cx| {
550 store.update_user_settings::<EditorSettings>(cx, |s| s.search_wrap = Some(false));
551 });
552
553 cx.set_state("ˇhi\nhigh\nhi\n", Mode::Normal);
554
555 cx.simulate_keystrokes("*");
556 cx.run_until_parked();
557 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
558
559 cx.simulate_keystrokes("*");
560 cx.run_until_parked();
561 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
562
563 cx.simulate_keystrokes("#");
564 cx.run_until_parked();
565 cx.assert_state("ˇhi\nhigh\nhi\n", Mode::Normal);
566
567 cx.simulate_keystrokes("3 *");
568 cx.run_until_parked();
569 cx.assert_state("ˇhi\nhigh\nhi\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 cx.simulate_keystrokes("n");
576 cx.assert_state("hi\nhigh\nˇhi\n", Mode::Normal);
577
578 cx.simulate_keystrokes("g #");
579 cx.run_until_parked();
580 cx.assert_state("hi\nˇhigh\nhi\n", Mode::Normal);
581 }
582
583 #[gpui::test]
584 async fn test_search(cx: &mut gpui::TestAppContext) {
585 let mut cx = VimTestContext::new(cx, true).await;
586
587 cx.set_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
588 cx.simulate_keystrokes("/ c c");
589
590 let search_bar = cx.workspace(|workspace, cx| {
591 workspace
592 .active_pane()
593 .read(cx)
594 .toolbar()
595 .read(cx)
596 .item_of_type::<BufferSearchBar>()
597 .expect("Buffer search bar should be deployed")
598 });
599
600 cx.update_view(search_bar, |bar, cx| {
601 assert_eq!(bar.query(cx), "cc");
602 });
603
604 cx.run_until_parked();
605
606 cx.update_editor(|editor, cx| {
607 let highlights = editor.all_text_background_highlights(cx);
608 assert_eq!(3, highlights.len());
609 assert_eq!(
610 DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
611 highlights[0].0
612 )
613 });
614
615 cx.simulate_keystrokes("enter");
616 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
617
618 // n to go to next/N to go to previous
619 cx.simulate_keystrokes("n");
620 cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
621 cx.simulate_keystrokes("shift-n");
622 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
623
624 // ?<enter> to go to previous
625 cx.simulate_keystrokes("? enter");
626 cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
627 cx.simulate_keystrokes("? enter");
628 cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
629
630 // /<enter> to go to next
631 cx.simulate_keystrokes("/ enter");
632 cx.assert_state("aa\nbb\ncc\ncc\nˇcc\n", Mode::Normal);
633
634 // ?{search}<enter> to search backwards
635 cx.simulate_keystrokes("? b enter");
636 cx.assert_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
637
638 // works with counts
639 cx.simulate_keystrokes("4 / c");
640 cx.simulate_keystrokes("enter");
641 cx.assert_state("aa\nbb\ncc\ncˇc\ncc\n", Mode::Normal);
642
643 // check that searching resumes from cursor, not previous match
644 cx.set_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
645 cx.simulate_keystrokes("/ d");
646 cx.simulate_keystrokes("enter");
647 cx.assert_state("aa\nbb\nˇdd\ncc\nbb\n", Mode::Normal);
648 cx.update_editor(|editor, cx| editor.move_to_beginning(&Default::default(), cx));
649 cx.assert_state("ˇaa\nbb\ndd\ncc\nbb\n", Mode::Normal);
650 cx.simulate_keystrokes("/ b");
651 cx.simulate_keystrokes("enter");
652 cx.assert_state("aa\nˇbb\ndd\ncc\nbb\n", Mode::Normal);
653
654 // check that searching switches to normal mode if in visual mode
655 cx.set_state("ˇone two one", Mode::Normal);
656 cx.simulate_keystrokes("v l l");
657 cx.assert_editor_state("«oneˇ» two one");
658 cx.simulate_keystrokes("*");
659 cx.assert_state("one two ˇone", Mode::Normal);
660
661 // check that searching with unable search wrap
662 cx.update_global(|store: &mut SettingsStore, cx| {
663 store.update_user_settings::<EditorSettings>(cx, |s| s.search_wrap = Some(false));
664 });
665 cx.set_state("aa\nbˇb\ncc\ncc\ncc\n", Mode::Normal);
666 cx.simulate_keystrokes("/ c c enter");
667
668 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
669
670 // n to go to next/N to go to previous
671 cx.simulate_keystrokes("n");
672 cx.assert_state("aa\nbb\ncc\nˇcc\ncc\n", Mode::Normal);
673 cx.simulate_keystrokes("shift-n");
674 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
675
676 // ?<enter> to go to previous
677 cx.simulate_keystrokes("? enter");
678 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
679 cx.simulate_keystrokes("? enter");
680 cx.assert_state("aa\nbb\nˇcc\ncc\ncc\n", Mode::Normal);
681 }
682
683 #[gpui::test]
684 async fn test_non_vim_search(cx: &mut gpui::TestAppContext) {
685 let mut cx = VimTestContext::new(cx, false).await;
686 cx.cx.set_state("ˇone one one one");
687 cx.simulate_keystrokes("cmd-f");
688 cx.run_until_parked();
689
690 cx.assert_editor_state("«oneˇ» one one one");
691 cx.simulate_keystrokes("enter");
692 cx.assert_editor_state("one «oneˇ» one one");
693 cx.simulate_keystrokes("shift-enter");
694 cx.assert_editor_state("«oneˇ» one one one");
695 }
696
697 #[gpui::test]
698 async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
699 let mut cx = NeovimBackedTestContext::new(cx).await;
700
701 cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
702 cx.simulate_shared_keystrokes("v 3 l *").await;
703 cx.shared_state().await.assert_eq("a.c. abcd ˇa.c. abcd");
704 }
705
706 #[gpui::test]
707 async fn test_d_search(cx: &mut gpui::TestAppContext) {
708 let mut cx = NeovimBackedTestContext::new(cx).await;
709
710 cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
711 cx.simulate_shared_keystrokes("d / c d").await;
712 cx.simulate_shared_keystrokes("enter").await;
713 cx.shared_state().await.assert_eq("ˇcd a.c. abcd");
714 }
715
716 #[gpui::test]
717 async fn test_backwards_n(cx: &mut gpui::TestAppContext) {
718 let mut cx = NeovimBackedTestContext::new(cx).await;
719
720 cx.set_shared_state("ˇa b a b a b a").await;
721 cx.simulate_shared_keystrokes("*").await;
722 cx.simulate_shared_keystrokes("n").await;
723 cx.shared_state().await.assert_eq("a b a b ˇa b a");
724 cx.simulate_shared_keystrokes("#").await;
725 cx.shared_state().await.assert_eq("a b ˇa b a b a");
726 cx.simulate_shared_keystrokes("n").await;
727 cx.shared_state().await.assert_eq("ˇa b a b a b a");
728 }
729
730 #[gpui::test]
731 async fn test_v_search(cx: &mut gpui::TestAppContext) {
732 let mut cx = NeovimBackedTestContext::new(cx).await;
733
734 cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
735 cx.simulate_shared_keystrokes("v / c d").await;
736 cx.simulate_shared_keystrokes("enter").await;
737 cx.shared_state().await.assert_eq("«a.c. abcˇ»d a.c. abcd");
738
739 cx.set_shared_state("a a aˇ a a a").await;
740 cx.simulate_shared_keystrokes("v / a").await;
741 cx.simulate_shared_keystrokes("enter").await;
742 cx.shared_state().await.assert_eq("a a a« aˇ» a a");
743 cx.simulate_shared_keystrokes("/ enter").await;
744 cx.shared_state().await.assert_eq("a a a« a aˇ» a");
745 cx.simulate_shared_keystrokes("? enter").await;
746 cx.shared_state().await.assert_eq("a a a« aˇ» a a");
747 cx.simulate_shared_keystrokes("? enter").await;
748 cx.shared_state().await.assert_eq("a a «ˇa »a a a");
749 cx.simulate_shared_keystrokes("/ enter").await;
750 cx.shared_state().await.assert_eq("a a a« aˇ» a a");
751 cx.simulate_shared_keystrokes("/ enter").await;
752 cx.shared_state().await.assert_eq("a a a« a aˇ» a");
753 }
754
755 #[gpui::test]
756 async fn test_visual_block_search(cx: &mut gpui::TestAppContext) {
757 let mut cx = NeovimBackedTestContext::new(cx).await;
758
759 cx.set_shared_state(indoc! {
760 "ˇone two
761 three four
762 five six
763 "
764 })
765 .await;
766 cx.simulate_shared_keystrokes("ctrl-v j / f").await;
767 cx.simulate_shared_keystrokes("enter").await;
768 cx.shared_state().await.assert_eq(indoc! {
769 "«one twoˇ»
770 «three fˇ»our
771 five six
772 "
773 });
774 }
775
776 // cargo test -p vim --features neovim test_replace_with_range_at_start
777 #[gpui::test]
778 async fn test_replace_with_range_at_start(cx: &mut gpui::TestAppContext) {
779 let mut cx = NeovimBackedTestContext::new(cx).await;
780
781 cx.set_shared_state(indoc! {
782 "ˇa
783 a
784 a
785 a
786 a
787 a
788 a
789 "
790 })
791 .await;
792 cx.simulate_shared_keystrokes(": 2 , 5 s / ^ / b").await;
793 cx.simulate_shared_keystrokes("enter").await;
794 cx.shared_state().await.assert_eq(indoc! {
795 "a
796 ba
797 ba
798 ba
799 ˇba
800 a
801 a
802 "
803 });
804 cx.executor().advance_clock(Duration::from_millis(250));
805 cx.run_until_parked();
806
807 cx.simulate_shared_keystrokes("/ a enter").await;
808 cx.shared_state().await.assert_eq(indoc! {
809 "a
810 ba
811 ba
812 ba
813 bˇa
814 a
815 a
816 "
817 });
818 }
819
820 // cargo test -p vim --features neovim test_replace_with_range
821 #[gpui::test]
822 async fn test_replace_with_range(cx: &mut gpui::TestAppContext) {
823 let mut cx = NeovimBackedTestContext::new(cx).await;
824
825 cx.set_shared_state(indoc! {
826 "ˇa
827 a
828 a
829 a
830 a
831 a
832 a
833 "
834 })
835 .await;
836 cx.simulate_shared_keystrokes(": 2 , 5 s / a / b").await;
837 cx.simulate_shared_keystrokes("enter").await;
838 cx.shared_state().await.assert_eq(indoc! {
839 "a
840 b
841 b
842 b
843 ˇb
844 a
845 a
846 "
847 });
848 cx.executor().advance_clock(Duration::from_millis(250));
849 cx.run_until_parked();
850
851 cx.simulate_shared_keystrokes("/ a enter").await;
852 cx.shared_state().await.assert_eq(indoc! {
853 "a
854 b
855 b
856 b
857 b
858 ˇa
859 a
860 "
861 });
862 }
863}