1use editor::{display_map::ToDisplayPoint, movement, scroll::Autoscroll, DisplayPoint, RowExt};
2use gpui::{impl_actions, Context, Window};
3use language::{Bias, SelectionGoal};
4use schemars::JsonSchema;
5use serde::Deserialize;
6use std::cmp;
7
8use crate::{
9 motion::Motion,
10 object::Object,
11 state::{Mode, Register},
12 Vim,
13};
14
15#[derive(Clone, Deserialize, JsonSchema, PartialEq)]
16#[serde(deny_unknown_fields)]
17pub struct Paste {
18 #[serde(default)]
19 before: bool,
20 #[serde(default)]
21 preserve_clipboard: bool,
22}
23
24impl_actions!(vim, [Paste]);
25
26impl Vim {
27 pub fn paste(&mut self, action: &Paste, window: &mut Window, cx: &mut Context<Self>) {
28 self.record_current_action(cx);
29 self.store_visual_marks(window, cx);
30 let count = Vim::take_count(cx).unwrap_or(1);
31
32 self.update_editor(window, cx, |vim, editor, window, cx| {
33 let text_layout_details = editor.text_layout_details(window);
34 editor.transact(window, cx, |editor, window, cx| {
35 editor.set_clip_at_line_ends(false, cx);
36
37 let selected_register = vim.selected_register.take();
38
39 let Some(Register {
40 text,
41 clipboard_selections,
42 }) = Vim::update_globals(cx, |globals, cx| {
43 globals.read_register(selected_register, Some(editor), cx)
44 })
45 .filter(|reg| !reg.text.is_empty())
46 else {
47 return;
48 };
49 let clipboard_selections = clipboard_selections
50 .filter(|sel| sel.len() > 1 && vim.mode != Mode::VisualLine);
51
52 if !action.preserve_clipboard && vim.mode.is_visual() {
53 vim.copy_selections_content(editor, vim.mode == Mode::VisualLine, window, cx);
54 }
55
56 let (display_map, current_selections) = editor.selections.all_adjusted_display(cx);
57
58 // unlike zed, if you have a multi-cursor selection from vim block mode,
59 // pasting it will paste it on subsequent lines, even if you don't yet
60 // have a cursor there.
61 let mut selections_to_process = Vec::new();
62 let mut i = 0;
63 while i < current_selections.len() {
64 selections_to_process
65 .push((current_selections[i].start..current_selections[i].end, true));
66 i += 1;
67 }
68 if let Some(clipboard_selections) = clipboard_selections.as_ref() {
69 let left = current_selections
70 .iter()
71 .map(|selection| cmp::min(selection.start.column(), selection.end.column()))
72 .min()
73 .unwrap();
74 let mut row = current_selections.last().unwrap().end.row().next_row();
75 while i < clipboard_selections.len() {
76 let cursor =
77 display_map.clip_point(DisplayPoint::new(row, left), Bias::Left);
78 selections_to_process.push((cursor..cursor, false));
79 i += 1;
80 row.0 += 1;
81 }
82 }
83
84 let first_selection_indent_column =
85 clipboard_selections.as_ref().and_then(|zed_selections| {
86 zed_selections
87 .first()
88 .map(|selection| selection.first_line_indent)
89 });
90 let before = action.before || vim.mode == Mode::VisualLine;
91
92 let mut edits = Vec::new();
93 let mut new_selections = Vec::new();
94 let mut original_indent_columns = Vec::new();
95 let mut start_offset = 0;
96
97 for (ix, (selection, preserve)) in selections_to_process.iter().enumerate() {
98 let (mut to_insert, original_indent_column) =
99 if let Some(clipboard_selections) = &clipboard_selections {
100 if let Some(clipboard_selection) = clipboard_selections.get(ix) {
101 let end_offset = start_offset + clipboard_selection.len;
102 let text = text[start_offset..end_offset].to_string();
103 start_offset = end_offset + 1;
104 (text, Some(clipboard_selection.first_line_indent))
105 } else {
106 ("".to_string(), first_selection_indent_column)
107 }
108 } else {
109 (text.to_string(), first_selection_indent_column)
110 };
111 let line_mode = to_insert.ends_with('\n');
112 let is_multiline = to_insert.contains('\n');
113
114 if line_mode && !before {
115 if selection.is_empty() {
116 to_insert =
117 "\n".to_owned() + &to_insert[..to_insert.len() - "\n".len()];
118 } else {
119 to_insert = "\n".to_owned() + &to_insert;
120 }
121 } else if !line_mode && vim.mode == Mode::VisualLine {
122 to_insert += "\n";
123 }
124
125 let display_range = if !selection.is_empty() {
126 selection.start..selection.end
127 } else if line_mode {
128 let point = if before {
129 movement::line_beginning(&display_map, selection.start, false)
130 } else {
131 movement::line_end(&display_map, selection.start, false)
132 };
133 point..point
134 } else {
135 let point = if before {
136 selection.start
137 } else {
138 movement::saturating_right(&display_map, selection.start)
139 };
140 point..point
141 };
142
143 let point_range = display_range.start.to_point(&display_map)
144 ..display_range.end.to_point(&display_map);
145 let anchor = if is_multiline || vim.mode == Mode::VisualLine {
146 display_map.buffer_snapshot.anchor_before(point_range.start)
147 } else {
148 display_map.buffer_snapshot.anchor_after(point_range.end)
149 };
150
151 if *preserve {
152 new_selections.push((anchor, line_mode, is_multiline));
153 }
154 edits.push((point_range, to_insert.repeat(count)));
155 original_indent_columns.push(original_indent_column);
156 }
157
158 let cursor_offset = editor.selections.last::<usize>(cx).head();
159 if editor
160 .buffer()
161 .read(cx)
162 .snapshot(cx)
163 .language_settings_at(cursor_offset, cx)
164 .auto_indent_on_paste
165 {
166 editor.edit_with_block_indent(edits, original_indent_columns, cx);
167 } else {
168 editor.edit(edits, cx);
169 }
170
171 // in line_mode vim will insert the new text on the next (or previous if before) line
172 // and put the cursor on the first non-blank character of the first inserted line (or at the end if the first line is blank).
173 // otherwise vim will insert the next text at (or before) the current cursor position,
174 // the cursor will go to the last (or first, if is_multiline) inserted character.
175 editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
176 s.replace_cursors_with(|map| {
177 let mut cursors = Vec::new();
178 for (anchor, line_mode, is_multiline) in &new_selections {
179 let mut cursor = anchor.to_display_point(map);
180 if *line_mode {
181 if !before {
182 cursor = movement::down(
183 map,
184 cursor,
185 SelectionGoal::None,
186 false,
187 &text_layout_details,
188 )
189 .0;
190 }
191 cursor = movement::indented_line_beginning(map, cursor, true, true);
192 } else if !is_multiline && !vim.temp_mode {
193 cursor = movement::saturating_left(map, cursor)
194 }
195 cursors.push(cursor);
196 if vim.mode == Mode::VisualBlock {
197 break;
198 }
199 }
200
201 cursors
202 });
203 })
204 });
205 });
206 self.switch_mode(Mode::Normal, true, window, cx);
207 }
208
209 pub fn replace_with_register_object(
210 &mut self,
211 object: Object,
212 around: bool,
213 window: &mut Window,
214 cx: &mut Context<Self>,
215 ) {
216 self.stop_recording(cx);
217 let selected_register = self.selected_register.take();
218 self.update_editor(window, cx, |_, editor, window, cx| {
219 editor.transact(window, cx, |editor, window, cx| {
220 editor.set_clip_at_line_ends(false, cx);
221 editor.change_selections(None, window, cx, |s| {
222 s.move_with(|map, selection| {
223 object.expand_selection(map, selection, around);
224 });
225 });
226
227 let Some(Register { text, .. }) = Vim::update_globals(cx, |globals, cx| {
228 globals.read_register(selected_register, Some(editor), cx)
229 })
230 .filter(|reg| !reg.text.is_empty()) else {
231 return;
232 };
233 editor.insert(&text, window, cx);
234 editor.set_clip_at_line_ends(true, cx);
235 editor.change_selections(None, window, cx, |s| {
236 s.move_with(|map, selection| {
237 selection.start = map.clip_point(selection.start, Bias::Left);
238 selection.end = selection.start
239 })
240 })
241 });
242 });
243 }
244
245 pub fn replace_with_register_motion(
246 &mut self,
247 motion: Motion,
248 times: Option<usize>,
249 window: &mut Window,
250 cx: &mut Context<Self>,
251 ) {
252 self.stop_recording(cx);
253 let selected_register = self.selected_register.take();
254 self.update_editor(window, cx, |_, editor, window, cx| {
255 let text_layout_details = editor.text_layout_details(window);
256 editor.transact(window, cx, |editor, window, cx| {
257 editor.set_clip_at_line_ends(false, cx);
258 editor.change_selections(None, window, cx, |s| {
259 s.move_with(|map, selection| {
260 motion.expand_selection(map, selection, times, false, &text_layout_details);
261 });
262 });
263
264 let Some(Register { text, .. }) = Vim::update_globals(cx, |globals, cx| {
265 globals.read_register(selected_register, Some(editor), cx)
266 })
267 .filter(|reg| !reg.text.is_empty()) else {
268 return;
269 };
270 editor.insert(&text, window, cx);
271 editor.set_clip_at_line_ends(true, cx);
272 editor.change_selections(None, window, cx, |s| {
273 s.move_with(|map, selection| {
274 selection.start = map.clip_point(selection.start, Bias::Left);
275 selection.end = selection.start
276 })
277 })
278 });
279 });
280 }
281}
282
283#[cfg(test)]
284mod test {
285 use crate::{
286 state::{Mode, Register},
287 test::{NeovimBackedTestContext, VimTestContext},
288 UseSystemClipboard, VimSettings,
289 };
290 use gpui::ClipboardItem;
291 use indoc::indoc;
292 use language::{
293 language_settings::{AllLanguageSettings, LanguageSettingsContent},
294 LanguageName,
295 };
296 use settings::SettingsStore;
297
298 #[gpui::test]
299 async fn test_paste(cx: &mut gpui::TestAppContext) {
300 let mut cx = NeovimBackedTestContext::new(cx).await;
301
302 // single line
303 cx.set_shared_state(indoc! {"
304 The quick brown
305 fox ˇjumps over
306 the lazy dog"})
307 .await;
308 cx.simulate_shared_keystrokes("v w y").await;
309 cx.shared_clipboard().await.assert_eq("jumps o");
310 cx.set_shared_state(indoc! {"
311 The quick brown
312 fox jumps oveˇr
313 the lazy dog"})
314 .await;
315 cx.simulate_shared_keystrokes("p").await;
316 cx.shared_state().await.assert_eq(indoc! {"
317 The quick brown
318 fox jumps overjumps ˇo
319 the lazy dog"});
320
321 cx.set_shared_state(indoc! {"
322 The quick brown
323 fox jumps oveˇr
324 the lazy dog"})
325 .await;
326 cx.simulate_shared_keystrokes("shift-p").await;
327 cx.shared_state().await.assert_eq(indoc! {"
328 The quick brown
329 fox jumps ovejumps ˇor
330 the lazy dog"});
331
332 // line mode
333 cx.set_shared_state(indoc! {"
334 The quick brown
335 fox juˇmps over
336 the lazy dog"})
337 .await;
338 cx.simulate_shared_keystrokes("d d").await;
339 cx.shared_clipboard().await.assert_eq("fox jumps over\n");
340 cx.shared_state().await.assert_eq(indoc! {"
341 The quick brown
342 the laˇzy dog"});
343 cx.simulate_shared_keystrokes("p").await;
344 cx.shared_state().await.assert_eq(indoc! {"
345 The quick brown
346 the lazy dog
347 ˇfox jumps over"});
348 cx.simulate_shared_keystrokes("k shift-p").await;
349 cx.shared_state().await.assert_eq(indoc! {"
350 The quick brown
351 ˇfox jumps over
352 the lazy dog
353 fox jumps over"});
354
355 // multiline, cursor to first character of pasted text.
356 cx.set_shared_state(indoc! {"
357 The quick brown
358 fox jumps ˇover
359 the lazy dog"})
360 .await;
361 cx.simulate_shared_keystrokes("v j y").await;
362 cx.shared_clipboard().await.assert_eq("over\nthe lazy do");
363
364 cx.simulate_shared_keystrokes("p").await;
365 cx.shared_state().await.assert_eq(indoc! {"
366 The quick brown
367 fox jumps oˇover
368 the lazy dover
369 the lazy dog"});
370 cx.simulate_shared_keystrokes("u shift-p").await;
371 cx.shared_state().await.assert_eq(indoc! {"
372 The quick brown
373 fox jumps ˇover
374 the lazy doover
375 the lazy dog"});
376 }
377
378 #[gpui::test]
379 async fn test_yank_system_clipboard_never(cx: &mut gpui::TestAppContext) {
380 let mut cx = VimTestContext::new(cx, true).await;
381
382 cx.update_global(|store: &mut SettingsStore, cx| {
383 store.update_user_settings::<VimSettings>(cx, |s| {
384 s.use_system_clipboard = Some(UseSystemClipboard::Never)
385 });
386 });
387
388 cx.set_state(
389 indoc! {"
390 The quick brown
391 fox jˇumps over
392 the lazy dog"},
393 Mode::Normal,
394 );
395 cx.simulate_keystrokes("v i w y");
396 cx.assert_state(
397 indoc! {"
398 The quick brown
399 fox ˇjumps over
400 the lazy dog"},
401 Mode::Normal,
402 );
403 cx.simulate_keystrokes("p");
404 cx.assert_state(
405 indoc! {"
406 The quick brown
407 fox jjumpˇsumps over
408 the lazy dog"},
409 Mode::Normal,
410 );
411 assert_eq!(cx.read_from_clipboard(), None);
412 }
413
414 #[gpui::test]
415 async fn test_yank_system_clipboard_on_yank(cx: &mut gpui::TestAppContext) {
416 let mut cx = VimTestContext::new(cx, true).await;
417
418 cx.update_global(|store: &mut SettingsStore, cx| {
419 store.update_user_settings::<VimSettings>(cx, |s| {
420 s.use_system_clipboard = Some(UseSystemClipboard::OnYank)
421 });
422 });
423
424 // copy in visual mode
425 cx.set_state(
426 indoc! {"
427 The quick brown
428 fox jˇumps over
429 the lazy dog"},
430 Mode::Normal,
431 );
432 cx.simulate_keystrokes("v i w y");
433 cx.assert_state(
434 indoc! {"
435 The quick brown
436 fox ˇjumps over
437 the lazy dog"},
438 Mode::Normal,
439 );
440 cx.simulate_keystrokes("p");
441 cx.assert_state(
442 indoc! {"
443 The quick brown
444 fox jjumpˇsumps over
445 the lazy dog"},
446 Mode::Normal,
447 );
448 assert_eq!(
449 cx.read_from_clipboard()
450 .map(|item| item.text().unwrap().to_string()),
451 Some("jumps".into())
452 );
453 cx.simulate_keystrokes("d d p");
454 cx.assert_state(
455 indoc! {"
456 The quick brown
457 the lazy dog
458 ˇfox jjumpsumps over"},
459 Mode::Normal,
460 );
461 assert_eq!(
462 cx.read_from_clipboard()
463 .map(|item| item.text().unwrap().to_string()),
464 Some("jumps".into())
465 );
466 cx.write_to_clipboard(ClipboardItem::new_string("test-copy".to_string()));
467 cx.simulate_keystrokes("shift-p");
468 cx.assert_state(
469 indoc! {"
470 The quick brown
471 the lazy dog
472 test-copˇyfox jjumpsumps over"},
473 Mode::Normal,
474 );
475 }
476
477 #[gpui::test]
478 async fn test_paste_visual(cx: &mut gpui::TestAppContext) {
479 let mut cx = NeovimBackedTestContext::new(cx).await;
480
481 // copy in visual mode
482 cx.set_shared_state(indoc! {"
483 The quick brown
484 fox jˇumps over
485 the lazy dog"})
486 .await;
487 cx.simulate_shared_keystrokes("v i w y").await;
488 cx.shared_state().await.assert_eq(indoc! {"
489 The quick brown
490 fox ˇjumps over
491 the lazy dog"});
492 // paste in visual mode
493 cx.simulate_shared_keystrokes("w v i w p").await;
494 cx.shared_state().await.assert_eq(indoc! {"
495 The quick brown
496 fox jumps jumpˇs
497 the lazy dog"});
498 cx.shared_clipboard().await.assert_eq("over");
499 // paste in visual line mode
500 cx.simulate_shared_keystrokes("up shift-v shift-p").await;
501 cx.shared_state().await.assert_eq(indoc! {"
502 ˇover
503 fox jumps jumps
504 the lazy dog"});
505 cx.shared_clipboard().await.assert_eq("over");
506 // paste in visual block mode
507 cx.simulate_shared_keystrokes("ctrl-v down down p").await;
508 cx.shared_state().await.assert_eq(indoc! {"
509 oveˇrver
510 overox jumps jumps
511 overhe lazy dog"});
512
513 // copy in visual line mode
514 cx.set_shared_state(indoc! {"
515 The quick brown
516 fox juˇmps over
517 the lazy dog"})
518 .await;
519 cx.simulate_shared_keystrokes("shift-v d").await;
520 cx.shared_state().await.assert_eq(indoc! {"
521 The quick brown
522 the laˇzy dog"});
523 // paste in visual mode
524 cx.simulate_shared_keystrokes("v i w p").await;
525 cx.shared_state().await.assert_eq(indoc! {"
526 The quick brown
527 the•
528 ˇfox jumps over
529 dog"});
530 cx.shared_clipboard().await.assert_eq("lazy");
531 cx.set_shared_state(indoc! {"
532 The quick brown
533 fox juˇmps over
534 the lazy dog"})
535 .await;
536 cx.simulate_shared_keystrokes("shift-v d").await;
537 cx.shared_state().await.assert_eq(indoc! {"
538 The quick brown
539 the laˇzy dog"});
540 // paste in visual line mode
541 cx.simulate_shared_keystrokes("k shift-v p").await;
542 cx.shared_state().await.assert_eq(indoc! {"
543 ˇfox jumps over
544 the lazy dog"});
545 cx.shared_clipboard().await.assert_eq("The quick brown\n");
546 }
547
548 #[gpui::test]
549 async fn test_paste_visual_block(cx: &mut gpui::TestAppContext) {
550 let mut cx = NeovimBackedTestContext::new(cx).await;
551 // copy in visual block mode
552 cx.set_shared_state(indoc! {"
553 The ˇquick brown
554 fox jumps over
555 the lazy dog"})
556 .await;
557 cx.simulate_shared_keystrokes("ctrl-v 2 j y").await;
558 cx.shared_clipboard().await.assert_eq("q\nj\nl");
559 cx.simulate_shared_keystrokes("p").await;
560 cx.shared_state().await.assert_eq(indoc! {"
561 The qˇquick brown
562 fox jjumps over
563 the llazy dog"});
564 cx.simulate_shared_keystrokes("v i w shift-p").await;
565 cx.shared_state().await.assert_eq(indoc! {"
566 The ˇq brown
567 fox jjjumps over
568 the lllazy dog"});
569 cx.simulate_shared_keystrokes("v i w shift-p").await;
570
571 cx.set_shared_state(indoc! {"
572 The ˇquick brown
573 fox jumps over
574 the lazy dog"})
575 .await;
576 cx.simulate_shared_keystrokes("ctrl-v j y").await;
577 cx.shared_clipboard().await.assert_eq("q\nj");
578 cx.simulate_shared_keystrokes("l ctrl-v 2 j shift-p").await;
579 cx.shared_state().await.assert_eq(indoc! {"
580 The qˇqick brown
581 fox jjmps over
582 the lzy dog"});
583
584 cx.simulate_shared_keystrokes("shift-v p").await;
585 cx.shared_state().await.assert_eq(indoc! {"
586 ˇq
587 j
588 fox jjmps over
589 the lzy dog"});
590 }
591
592 #[gpui::test]
593 async fn test_paste_indent(cx: &mut gpui::TestAppContext) {
594 let mut cx = VimTestContext::new_typescript(cx).await;
595
596 cx.set_state(
597 indoc! {"
598 class A {ˇ
599 }
600 "},
601 Mode::Normal,
602 );
603 cx.simulate_keystrokes("o a ( ) { escape");
604 cx.assert_state(
605 indoc! {"
606 class A {
607 a()ˇ{}
608 }
609 "},
610 Mode::Normal,
611 );
612 // cursor goes to the first non-blank character in the line;
613 cx.simulate_keystrokes("y y p");
614 cx.assert_state(
615 indoc! {"
616 class A {
617 a(){}
618 ˇa(){}
619 }
620 "},
621 Mode::Normal,
622 );
623 // indentation is preserved when pasting
624 cx.simulate_keystrokes("u shift-v up y shift-p");
625 cx.assert_state(
626 indoc! {"
627 ˇclass A {
628 a(){}
629 class A {
630 a(){}
631 }
632 "},
633 Mode::Normal,
634 );
635 }
636
637 #[gpui::test]
638 async fn test_paste_auto_indent(cx: &mut gpui::TestAppContext) {
639 let mut cx = VimTestContext::new(cx, true).await;
640
641 cx.set_state(
642 indoc! {"
643 mod some_module {
644 ˇfn main() {
645 }
646 }
647 "},
648 Mode::Normal,
649 );
650 // default auto indentation
651 cx.simulate_keystrokes("y y p");
652 cx.assert_state(
653 indoc! {"
654 mod some_module {
655 fn main() {
656 ˇfn main() {
657 }
658 }
659 "},
660 Mode::Normal,
661 );
662 // back to previous state
663 cx.simulate_keystrokes("u u");
664 cx.assert_state(
665 indoc! {"
666 mod some_module {
667 ˇfn main() {
668 }
669 }
670 "},
671 Mode::Normal,
672 );
673 cx.update_global(|store: &mut SettingsStore, cx| {
674 store.update_user_settings::<AllLanguageSettings>(cx, |settings| {
675 settings.languages.insert(
676 LanguageName::new("Rust"),
677 LanguageSettingsContent {
678 auto_indent_on_paste: Some(false),
679 ..Default::default()
680 },
681 );
682 });
683 });
684 // auto indentation turned off
685 cx.simulate_keystrokes("y y p");
686 cx.assert_state(
687 indoc! {"
688 mod some_module {
689 fn main() {
690 ˇfn main() {
691 }
692 }
693 "},
694 Mode::Normal,
695 );
696 }
697
698 #[gpui::test]
699 async fn test_paste_count(cx: &mut gpui::TestAppContext) {
700 let mut cx = NeovimBackedTestContext::new(cx).await;
701
702 cx.set_shared_state(indoc! {"
703 onˇe
704 two
705 three
706 "})
707 .await;
708 cx.simulate_shared_keystrokes("y y 3 p").await;
709 cx.shared_state().await.assert_eq(indoc! {"
710 one
711 ˇone
712 one
713 one
714 two
715 three
716 "});
717
718 cx.set_shared_state(indoc! {"
719 one
720 ˇtwo
721 three
722 "})
723 .await;
724 cx.simulate_shared_keystrokes("y $ $ 3 p").await;
725 cx.shared_state().await.assert_eq(indoc! {"
726 one
727 twotwotwotwˇo
728 three
729 "});
730 }
731
732 #[gpui::test]
733 async fn test_numbered_registers(cx: &mut gpui::TestAppContext) {
734 let mut cx = NeovimBackedTestContext::new(cx).await;
735
736 cx.update_global(|store: &mut SettingsStore, cx| {
737 store.update_user_settings::<VimSettings>(cx, |s| {
738 s.use_system_clipboard = Some(UseSystemClipboard::Never)
739 });
740 });
741
742 cx.set_shared_state(indoc! {"
743 The quick brown
744 fox jˇumps over
745 the lazy dog"})
746 .await;
747 cx.simulate_shared_keystrokes("y y \" 0 p").await;
748 cx.shared_register('0').await.assert_eq("fox jumps over\n");
749 cx.shared_register('"').await.assert_eq("fox jumps over\n");
750
751 cx.shared_state().await.assert_eq(indoc! {"
752 The quick brown
753 fox jumps over
754 ˇfox jumps over
755 the lazy dog"});
756 cx.simulate_shared_keystrokes("k k d d").await;
757 cx.shared_register('0').await.assert_eq("fox jumps over\n");
758 cx.shared_register('1').await.assert_eq("The quick brown\n");
759 cx.shared_register('"').await.assert_eq("The quick brown\n");
760
761 cx.simulate_shared_keystrokes("d d shift-g d d").await;
762 cx.shared_register('0').await.assert_eq("fox jumps over\n");
763 cx.shared_register('3').await.assert_eq("The quick brown\n");
764 cx.shared_register('2').await.assert_eq("fox jumps over\n");
765 cx.shared_register('1').await.assert_eq("the lazy dog\n");
766
767 cx.shared_state().await.assert_eq(indoc! {"
768 ˇfox jumps over"});
769
770 cx.simulate_shared_keystrokes("d d \" 3 p p \" 1 p").await;
771 cx.set_shared_state(indoc! {"
772 The quick brown
773 fox jumps over
774 ˇthe lazy dog"})
775 .await;
776 }
777
778 #[gpui::test]
779 async fn test_named_registers(cx: &mut gpui::TestAppContext) {
780 let mut cx = NeovimBackedTestContext::new(cx).await;
781
782 cx.update_global(|store: &mut SettingsStore, cx| {
783 store.update_user_settings::<VimSettings>(cx, |s| {
784 s.use_system_clipboard = Some(UseSystemClipboard::Never)
785 });
786 });
787
788 cx.set_shared_state(indoc! {"
789 The quick brown
790 fox jˇumps over
791 the lazy dog"})
792 .await;
793 cx.simulate_shared_keystrokes("\" a d a w").await;
794 cx.shared_register('a').await.assert_eq("jumps ");
795 cx.simulate_shared_keystrokes("\" shift-a d i w").await;
796 cx.shared_register('a').await.assert_eq("jumps over");
797 cx.shared_register('"').await.assert_eq("jumps over");
798 cx.simulate_shared_keystrokes("\" a p").await;
799 cx.shared_state().await.assert_eq(indoc! {"
800 The quick brown
801 fox jumps oveˇr
802 the lazy dog"});
803 cx.simulate_shared_keystrokes("\" a d a w").await;
804 cx.shared_register('a').await.assert_eq(" over");
805 }
806
807 #[gpui::test]
808 async fn test_special_registers(cx: &mut gpui::TestAppContext) {
809 let mut cx = NeovimBackedTestContext::new(cx).await;
810
811 cx.update_global(|store: &mut SettingsStore, cx| {
812 store.update_user_settings::<VimSettings>(cx, |s| {
813 s.use_system_clipboard = Some(UseSystemClipboard::Never)
814 });
815 });
816
817 cx.set_shared_state(indoc! {"
818 The quick brown
819 fox jˇumps over
820 the lazy dog"})
821 .await;
822 cx.simulate_shared_keystrokes("d i w").await;
823 cx.shared_register('-').await.assert_eq("jumps");
824 cx.simulate_shared_keystrokes("\" _ d d").await;
825 cx.shared_register('_').await.assert_eq("");
826
827 cx.simulate_shared_keystrokes("shift-v \" _ y w").await;
828 cx.shared_register('"').await.assert_eq("jumps");
829
830 cx.shared_state().await.assert_eq(indoc! {"
831 The quick brown
832 the ˇlazy dog"});
833 cx.simulate_shared_keystrokes("\" \" d ^").await;
834 cx.shared_register('0').await.assert_eq("the ");
835 cx.shared_register('"').await.assert_eq("the ");
836
837 cx.simulate_shared_keystrokes("^ \" + d $").await;
838 cx.shared_clipboard().await.assert_eq("lazy dog");
839 cx.shared_register('"').await.assert_eq("lazy dog");
840
841 cx.simulate_shared_keystrokes("/ d o g enter").await;
842 cx.shared_register('/').await.assert_eq("dog");
843 cx.simulate_shared_keystrokes("\" / shift-p").await;
844 cx.shared_state().await.assert_eq(indoc! {"
845 The quick brown
846 doˇg"});
847
848 // not testing nvim as it doesn't have a filename
849 cx.simulate_keystrokes("\" % p");
850 #[cfg(not(target_os = "windows"))]
851 cx.assert_state(
852 indoc! {"
853 The quick brown
854 dogdir/file.rˇs"},
855 Mode::Normal,
856 );
857 #[cfg(target_os = "windows")]
858 cx.assert_state(
859 indoc! {"
860 The quick brown
861 dogdir\\file.rˇs"},
862 Mode::Normal,
863 );
864 }
865
866 #[gpui::test]
867 async fn test_multicursor_paste(cx: &mut gpui::TestAppContext) {
868 let mut cx = VimTestContext::new(cx, true).await;
869
870 cx.update_global(|store: &mut SettingsStore, cx| {
871 store.update_user_settings::<VimSettings>(cx, |s| {
872 s.use_system_clipboard = Some(UseSystemClipboard::Never)
873 });
874 });
875
876 cx.set_state(
877 indoc! {"
878 ˇfish one
879 fish two
880 fish red
881 fish blue
882 "},
883 Mode::Normal,
884 );
885 cx.simulate_keystrokes("4 g l w escape d i w 0 shift-p");
886 cx.assert_state(
887 indoc! {"
888 onˇefish•
889 twˇofish•
890 reˇdfish•
891 bluˇefish•
892 "},
893 Mode::Normal,
894 );
895 }
896
897 #[gpui::test]
898 async fn test_replace_with_register(cx: &mut gpui::TestAppContext) {
899 let mut cx = VimTestContext::new(cx, true).await;
900
901 cx.set_state(
902 indoc! {"
903 ˇfish one
904 two three
905 "},
906 Mode::Normal,
907 );
908 cx.simulate_keystrokes("y i w");
909 cx.simulate_keystrokes("w");
910 cx.simulate_keystrokes("g r i w");
911 cx.assert_state(
912 indoc! {"
913 fish fisˇh
914 two three
915 "},
916 Mode::Normal,
917 );
918 cx.simulate_keystrokes("j b g r e");
919 cx.assert_state(
920 indoc! {"
921 fish fish
922 two fisˇh
923 "},
924 Mode::Normal,
925 );
926 let clipboard: Register = cx.read_from_clipboard().unwrap().into();
927 assert_eq!(clipboard.text, "fish");
928
929 cx.set_state(
930 indoc! {"
931 ˇfish one
932 two three
933 "},
934 Mode::Normal,
935 );
936 cx.simulate_keystrokes("y i w");
937 cx.simulate_keystrokes("w");
938 cx.simulate_keystrokes("v i w g r");
939 cx.assert_state(
940 indoc! {"
941 fish fisˇh
942 two three
943 "},
944 Mode::Normal,
945 );
946 cx.simulate_keystrokes("g r r");
947 cx.assert_state(
948 indoc! {"
949 fisˇh
950 two three
951 "},
952 Mode::Normal,
953 );
954 cx.simulate_keystrokes("j w g r $");
955 cx.assert_state(
956 indoc! {"
957 fish
958 two fisˇh
959 "},
960 Mode::Normal,
961 );
962 let clipboard: Register = cx.read_from_clipboard().unwrap().into();
963 assert_eq!(clipboard.text, "fish");
964 }
965
966 #[gpui::test]
967 async fn test_replace_with_register_dot_repeat(cx: &mut gpui::TestAppContext) {
968 let mut cx = VimTestContext::new(cx, true).await;
969
970 cx.set_state(
971 indoc! {"
972 ˇfish one
973 two three
974 "},
975 Mode::Normal,
976 );
977 cx.simulate_keystrokes("y i w");
978 cx.simulate_keystrokes("w");
979 cx.simulate_keystrokes("g r i w");
980 cx.assert_state(
981 indoc! {"
982 fish fisˇh
983 two three
984 "},
985 Mode::Normal,
986 );
987 cx.simulate_keystrokes("j .");
988 cx.assert_state(
989 indoc! {"
990 fish fish
991 two fisˇh
992 "},
993 Mode::Normal,
994 );
995 }
996}