1use std::{cell::RefCell, rc::Rc};
2
3use crate::{
4 insert::NormalBefore,
5 motion::Motion,
6 state::{Mode, Operator, RecordedSelection, ReplayableAction, VimGlobals},
7 Vim,
8};
9use editor::Editor;
10use gpui::{actions, Action, ViewContext, WindowContext};
11use util::ResultExt;
12use workspace::Workspace;
13
14actions!(vim, [Repeat, EndRepeat, ToggleRecord, ReplayLastRecording]);
15
16fn should_replay(action: &dyn Action) -> bool {
17 // skip so that we don't leave the character palette open
18 if editor::actions::ShowCharacterPalette.partial_eq(action) {
19 return false;
20 }
21 true
22}
23
24fn repeatable_insert(action: &ReplayableAction) -> Option<Box<dyn Action>> {
25 match action {
26 ReplayableAction::Action(action) => {
27 if super::InsertBefore.partial_eq(&**action)
28 || super::InsertAfter.partial_eq(&**action)
29 || super::InsertFirstNonWhitespace.partial_eq(&**action)
30 || super::InsertEndOfLine.partial_eq(&**action)
31 {
32 Some(super::InsertBefore.boxed_clone())
33 } else if super::InsertLineAbove.partial_eq(&**action)
34 || super::InsertLineBelow.partial_eq(&**action)
35 {
36 Some(super::InsertLineBelow.boxed_clone())
37 } else if crate::replace::ToggleReplace.partial_eq(&**action) {
38 Some(crate::replace::ToggleReplace.boxed_clone())
39 } else {
40 None
41 }
42 }
43 ReplayableAction::Insertion { .. } => None,
44 }
45}
46
47pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
48 Vim::action(editor, cx, |vim, _: &EndRepeat, cx| {
49 Vim::globals(cx).dot_replaying = false;
50 vim.switch_mode(Mode::Normal, false, cx)
51 });
52
53 Vim::action(editor, cx, |vim, _: &Repeat, cx| vim.repeat(false, cx));
54
55 Vim::action(editor, cx, |vim, _: &ToggleRecord, cx| {
56 let globals = Vim::globals(cx);
57 if let Some(char) = globals.recording_register.take() {
58 globals.last_recorded_register = Some(char)
59 } else {
60 vim.push_operator(Operator::RecordRegister, cx);
61 }
62 });
63
64 Vim::action(editor, cx, |vim, _: &ReplayLastRecording, cx| {
65 let Some(register) = Vim::globals(cx).last_recorded_register else {
66 return;
67 };
68 vim.replay_register(register, cx)
69 });
70}
71
72pub struct ReplayerState {
73 actions: Vec<ReplayableAction>,
74 running: bool,
75 ix: usize,
76}
77
78#[derive(Clone)]
79pub struct Replayer(Rc<RefCell<ReplayerState>>);
80
81impl Replayer {
82 pub fn new() -> Self {
83 Self(Rc::new(RefCell::new(ReplayerState {
84 actions: vec![],
85 running: false,
86 ix: 0,
87 })))
88 }
89
90 pub fn replay(&mut self, actions: Vec<ReplayableAction>, cx: &mut WindowContext) {
91 let mut lock = self.0.borrow_mut();
92 let range = lock.ix..lock.ix;
93 lock.actions.splice(range, actions);
94 if lock.running {
95 return;
96 }
97 lock.running = true;
98 let this = self.clone();
99 cx.defer(move |cx| this.next(cx))
100 }
101
102 pub fn stop(self) {
103 self.0.borrow_mut().actions.clear()
104 }
105
106 pub fn next(self, cx: &mut WindowContext) {
107 let mut lock = self.0.borrow_mut();
108 let action = if lock.ix < 10000 {
109 lock.actions.get(lock.ix).cloned()
110 } else {
111 log::error!("Aborting replay after 10000 actions");
112 None
113 };
114 lock.ix += 1;
115 drop(lock);
116 let Some(action) = action else {
117 Vim::globals(cx).replayer.take();
118 return;
119 };
120 match action {
121 ReplayableAction::Action(action) => {
122 if should_replay(&*action) {
123 cx.dispatch_action(action.boxed_clone());
124 cx.defer(move |cx| Vim::globals(cx).observe_action(action.boxed_clone()));
125 }
126 }
127 ReplayableAction::Insertion {
128 text,
129 utf16_range_to_replace,
130 } => {
131 cx.window_handle()
132 .update(cx, |handle, cx| {
133 let Ok(workspace) = handle.downcast::<Workspace>() else {
134 return;
135 };
136 let Some(editor) = workspace.read(cx).active_item_as::<Editor>(cx) else {
137 return;
138 };
139 editor.update(cx, |editor, cx| {
140 editor.replay_insert_event(&text, utf16_range_to_replace.clone(), cx)
141 })
142 })
143 .log_err();
144 }
145 }
146 cx.defer(move |cx| self.next(cx));
147 }
148}
149
150impl Vim {
151 pub(crate) fn record_register(&mut self, register: char, cx: &mut ViewContext<Self>) {
152 let globals = Vim::globals(cx);
153 globals.recording_register = Some(register);
154 globals.recordings.remove(®ister);
155 globals.ignore_current_insertion = true;
156 self.clear_operator(cx)
157 }
158
159 pub(crate) fn replay_register(&mut self, mut register: char, cx: &mut ViewContext<Self>) {
160 let mut count = self.take_count(cx).unwrap_or(1);
161 self.clear_operator(cx);
162
163 let globals = Vim::globals(cx);
164 if register == '@' {
165 let Some(last) = globals.last_replayed_register else {
166 return;
167 };
168 register = last;
169 }
170 let Some(actions) = globals.recordings.get(®ister) else {
171 return;
172 };
173
174 let mut repeated_actions = vec![];
175 while count > 0 {
176 repeated_actions.extend(actions.iter().cloned());
177 count -= 1
178 }
179
180 globals.last_replayed_register = Some(register);
181 let mut replayer = globals
182 .replayer
183 .get_or_insert_with(|| Replayer::new())
184 .clone();
185 replayer.replay(repeated_actions, cx);
186 }
187
188 pub(crate) fn repeat(&mut self, from_insert_mode: bool, cx: &mut ViewContext<Self>) {
189 let count = self.take_count(cx);
190 let Some((mut actions, selection, mode)) = Vim::update_globals(cx, |globals, _| {
191 let actions = globals.recorded_actions.clone();
192 if actions.is_empty() {
193 return None;
194 }
195 if globals.replayer.is_none() {
196 if let Some(recording_register) = globals.recording_register {
197 globals
198 .recordings
199 .entry(recording_register)
200 .or_default()
201 .push(ReplayableAction::Action(Repeat.boxed_clone()));
202 }
203 }
204
205 let mut mode = None;
206 let selection = globals.recorded_selection.clone();
207 match selection {
208 RecordedSelection::SingleLine { .. } | RecordedSelection::Visual { .. } => {
209 globals.recorded_count = None;
210 mode = Some(Mode::Visual);
211 }
212 RecordedSelection::VisualLine { .. } => {
213 globals.recorded_count = None;
214 mode = Some(Mode::VisualLine)
215 }
216 RecordedSelection::VisualBlock { .. } => {
217 globals.recorded_count = None;
218 mode = Some(Mode::VisualBlock)
219 }
220 RecordedSelection::None => {
221 if let Some(count) = count {
222 globals.recorded_count = Some(count);
223 }
224 }
225 }
226
227 Some((actions, selection, mode))
228 }) else {
229 return;
230 };
231 if let Some(mode) = mode {
232 self.switch_mode(mode, false, cx)
233 }
234
235 match selection {
236 RecordedSelection::SingleLine { cols } => {
237 if cols > 1 {
238 self.visual_motion(Motion::Right, Some(cols as usize - 1), cx)
239 }
240 }
241 RecordedSelection::Visual { rows, cols } => {
242 self.visual_motion(
243 Motion::Down {
244 display_lines: false,
245 },
246 Some(rows as usize),
247 cx,
248 );
249 self.visual_motion(
250 Motion::StartOfLine {
251 display_lines: false,
252 },
253 None,
254 cx,
255 );
256 if cols > 1 {
257 self.visual_motion(Motion::Right, Some(cols as usize - 1), cx)
258 }
259 }
260 RecordedSelection::VisualBlock { rows, cols } => {
261 self.visual_motion(
262 Motion::Down {
263 display_lines: false,
264 },
265 Some(rows as usize),
266 cx,
267 );
268 if cols > 1 {
269 self.visual_motion(Motion::Right, Some(cols as usize - 1), cx);
270 }
271 }
272 RecordedSelection::VisualLine { rows } => {
273 self.visual_motion(
274 Motion::Down {
275 display_lines: false,
276 },
277 Some(rows as usize),
278 cx,
279 );
280 }
281 RecordedSelection::None => {}
282 }
283
284 // insert internally uses repeat to handle counts
285 // vim doesn't treat 3a1 as though you literally repeated a1
286 // 3 times, instead it inserts the content thrice at the insert position.
287 if let Some(to_repeat) = repeatable_insert(&actions[0]) {
288 if let Some(ReplayableAction::Action(action)) = actions.last() {
289 if NormalBefore.partial_eq(&**action) {
290 actions.pop();
291 }
292 }
293
294 let mut new_actions = actions.clone();
295 actions[0] = ReplayableAction::Action(to_repeat.boxed_clone());
296
297 let mut count = cx.global::<VimGlobals>().recorded_count.unwrap_or(1);
298
299 // if we came from insert mode we're just doing repetitions 2 onwards.
300 if from_insert_mode {
301 count -= 1;
302 new_actions[0] = actions[0].clone();
303 }
304
305 for _ in 1..count {
306 new_actions.append(actions.clone().as_mut());
307 }
308 new_actions.push(ReplayableAction::Action(NormalBefore.boxed_clone()));
309 actions = new_actions;
310 }
311
312 actions.push(ReplayableAction::Action(EndRepeat.boxed_clone()));
313
314 let globals = Vim::globals(cx);
315 globals.dot_replaying = true;
316 let mut replayer = globals
317 .replayer
318 .get_or_insert_with(|| Replayer::new())
319 .clone();
320 replayer.replay(actions, cx);
321 }
322}
323
324#[cfg(test)]
325mod test {
326 use editor::test::editor_lsp_test_context::EditorLspTestContext;
327 use futures::StreamExt;
328 use indoc::indoc;
329
330 use gpui::ViewInputHandler;
331
332 use crate::{
333 state::Mode,
334 test::{NeovimBackedTestContext, VimTestContext},
335 };
336
337 #[gpui::test]
338 async fn test_dot_repeat(cx: &mut gpui::TestAppContext) {
339 let mut cx = NeovimBackedTestContext::new(cx).await;
340
341 // "o"
342 cx.set_shared_state("ˇhello").await;
343 cx.simulate_shared_keystrokes("o w o r l d escape").await;
344 cx.shared_state().await.assert_eq("hello\nworlˇd");
345 cx.simulate_shared_keystrokes(".").await;
346 cx.shared_state().await.assert_eq("hello\nworld\nworlˇd");
347
348 // "d"
349 cx.simulate_shared_keystrokes("^ d f o").await;
350 cx.simulate_shared_keystrokes("g g .").await;
351 cx.shared_state().await.assert_eq("ˇ\nworld\nrld");
352
353 // "p" (note that it pastes the current clipboard)
354 cx.simulate_shared_keystrokes("j y y p").await;
355 cx.simulate_shared_keystrokes("shift-g y y .").await;
356 cx.shared_state()
357 .await
358 .assert_eq("\nworld\nworld\nrld\nˇrld");
359
360 // "~" (note that counts apply to the action taken, not . itself)
361 cx.set_shared_state("ˇthe quick brown fox").await;
362 cx.simulate_shared_keystrokes("2 ~ .").await;
363 cx.set_shared_state("THE ˇquick brown fox").await;
364 cx.simulate_shared_keystrokes("3 .").await;
365 cx.set_shared_state("THE QUIˇck brown fox").await;
366 cx.run_until_parked();
367 cx.simulate_shared_keystrokes(".").await;
368 cx.shared_state().await.assert_eq("THE QUICK ˇbrown fox");
369 }
370
371 #[gpui::test]
372 async fn test_repeat_ime(cx: &mut gpui::TestAppContext) {
373 let mut cx = VimTestContext::new(cx, true).await;
374
375 cx.set_state("hˇllo", Mode::Normal);
376 cx.simulate_keystrokes("i");
377
378 // simulate brazilian input for ä.
379 cx.update_editor(|editor, cx| {
380 editor.replace_and_mark_text_in_range(None, "\"", Some(1..1), cx);
381 editor.replace_text_in_range(None, "ä", cx);
382 });
383 cx.simulate_keystrokes("escape");
384 cx.assert_state("hˇällo", Mode::Normal);
385 cx.simulate_keystrokes(".");
386 cx.assert_state("hˇäällo", Mode::Normal);
387 }
388
389 #[gpui::test]
390 async fn test_repeat_completion(cx: &mut gpui::TestAppContext) {
391 VimTestContext::init(cx);
392 let cx = EditorLspTestContext::new_rust(
393 lsp::ServerCapabilities {
394 completion_provider: Some(lsp::CompletionOptions {
395 trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
396 resolve_provider: Some(true),
397 ..Default::default()
398 }),
399 ..Default::default()
400 },
401 cx,
402 )
403 .await;
404 let mut cx = VimTestContext::new_with_lsp(cx, true);
405
406 cx.set_state(
407 indoc! {"
408 onˇe
409 two
410 three
411 "},
412 Mode::Normal,
413 );
414
415 let mut request =
416 cx.handle_request::<lsp::request::Completion, _, _>(move |_, params, _| async move {
417 let position = params.text_document_position.position;
418 Ok(Some(lsp::CompletionResponse::Array(vec![
419 lsp::CompletionItem {
420 label: "first".to_string(),
421 text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
422 range: lsp::Range::new(position, position),
423 new_text: "first".to_string(),
424 })),
425 ..Default::default()
426 },
427 lsp::CompletionItem {
428 label: "second".to_string(),
429 text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
430 range: lsp::Range::new(position, position),
431 new_text: "second".to_string(),
432 })),
433 ..Default::default()
434 },
435 ])))
436 });
437 cx.simulate_keystrokes("a .");
438 request.next().await;
439 cx.condition(|editor, _| editor.context_menu_visible())
440 .await;
441 cx.simulate_keystrokes("down enter ! escape");
442
443 cx.assert_state(
444 indoc! {"
445 one.secondˇ!
446 two
447 three
448 "},
449 Mode::Normal,
450 );
451 cx.simulate_keystrokes("j .");
452 cx.assert_state(
453 indoc! {"
454 one.second!
455 two.secondˇ!
456 three
457 "},
458 Mode::Normal,
459 );
460 }
461
462 #[gpui::test]
463 async fn test_repeat_visual(cx: &mut gpui::TestAppContext) {
464 let mut cx = NeovimBackedTestContext::new(cx).await;
465
466 // single-line (3 columns)
467 cx.set_shared_state(indoc! {
468 "ˇthe quick brown
469 fox jumps over
470 the lazy dog"
471 })
472 .await;
473 cx.simulate_shared_keystrokes("v i w s o escape").await;
474 cx.shared_state().await.assert_eq(indoc! {
475 "ˇo quick brown
476 fox jumps over
477 the lazy dog"
478 });
479 cx.simulate_shared_keystrokes("j w .").await;
480 cx.shared_state().await.assert_eq(indoc! {
481 "o quick brown
482 fox ˇops over
483 the lazy dog"
484 });
485 cx.simulate_shared_keystrokes("f r .").await;
486 cx.shared_state().await.assert_eq(indoc! {
487 "o quick brown
488 fox ops oveˇothe lazy dog"
489 });
490
491 // visual
492 cx.set_shared_state(indoc! {
493 "the ˇquick brown
494 fox jumps over
495 fox jumps over
496 fox jumps over
497 the lazy dog"
498 })
499 .await;
500 cx.simulate_shared_keystrokes("v j x").await;
501 cx.shared_state().await.assert_eq(indoc! {
502 "the ˇumps over
503 fox jumps over
504 fox jumps over
505 the lazy dog"
506 });
507 cx.simulate_shared_keystrokes(".").await;
508 cx.shared_state().await.assert_eq(indoc! {
509 "the ˇumps over
510 fox jumps over
511 the lazy dog"
512 });
513 cx.simulate_shared_keystrokes("w .").await;
514 cx.shared_state().await.assert_eq(indoc! {
515 "the umps ˇumps over
516 the lazy dog"
517 });
518 cx.simulate_shared_keystrokes("j .").await;
519 cx.shared_state().await.assert_eq(indoc! {
520 "the umps umps over
521 the ˇog"
522 });
523
524 // block mode (3 rows)
525 cx.set_shared_state(indoc! {
526 "ˇthe quick brown
527 fox jumps over
528 the lazy dog"
529 })
530 .await;
531 cx.simulate_shared_keystrokes("ctrl-v j j shift-i o escape")
532 .await;
533 cx.shared_state().await.assert_eq(indoc! {
534 "ˇothe quick brown
535 ofox jumps over
536 othe lazy dog"
537 });
538 cx.simulate_shared_keystrokes("j 4 l .").await;
539 cx.shared_state().await.assert_eq(indoc! {
540 "othe quick brown
541 ofoxˇo jumps over
542 otheo lazy dog"
543 });
544
545 // line mode
546 cx.set_shared_state(indoc! {
547 "ˇthe quick brown
548 fox jumps over
549 the lazy dog"
550 })
551 .await;
552 cx.simulate_shared_keystrokes("shift-v shift-r o escape")
553 .await;
554 cx.shared_state().await.assert_eq(indoc! {
555 "ˇo
556 fox jumps over
557 the lazy dog"
558 });
559 cx.simulate_shared_keystrokes("j .").await;
560 cx.shared_state().await.assert_eq(indoc! {
561 "o
562 ˇo
563 the lazy dog"
564 });
565 }
566
567 #[gpui::test]
568 async fn test_repeat_motion_counts(cx: &mut gpui::TestAppContext) {
569 let mut cx = NeovimBackedTestContext::new(cx).await;
570
571 cx.set_shared_state(indoc! {
572 "ˇthe quick brown
573 fox jumps over
574 the lazy dog"
575 })
576 .await;
577 cx.simulate_shared_keystrokes("3 d 3 l").await;
578 cx.shared_state().await.assert_eq(indoc! {
579 "ˇ brown
580 fox jumps over
581 the lazy dog"
582 });
583 cx.simulate_shared_keystrokes("j .").await;
584 cx.shared_state().await.assert_eq(indoc! {
585 " brown
586 ˇ over
587 the lazy dog"
588 });
589 cx.simulate_shared_keystrokes("j 2 .").await;
590 cx.shared_state().await.assert_eq(indoc! {
591 " brown
592 over
593 ˇe lazy dog"
594 });
595 }
596
597 #[gpui::test]
598 async fn test_record_interrupted(cx: &mut gpui::TestAppContext) {
599 let mut cx = VimTestContext::new(cx, true).await;
600
601 cx.set_state("ˇhello\n", Mode::Normal);
602 cx.simulate_keystrokes("4 i j cmd-shift-p escape");
603 cx.simulate_keystrokes("escape");
604 cx.assert_state("ˇjhello\n", Mode::Normal);
605 }
606
607 #[gpui::test]
608 async fn test_repeat_over_blur(cx: &mut gpui::TestAppContext) {
609 let mut cx = NeovimBackedTestContext::new(cx).await;
610
611 cx.set_shared_state("ˇhello hello hello\n").await;
612 cx.simulate_shared_keystrokes("c f o x escape").await;
613 cx.shared_state().await.assert_eq("ˇx hello hello\n");
614 cx.simulate_shared_keystrokes(": escape").await;
615 cx.simulate_shared_keystrokes(".").await;
616 cx.shared_state().await.assert_eq("ˇx hello\n");
617 }
618
619 #[gpui::test]
620 async fn test_undo_repeated_insert(cx: &mut gpui::TestAppContext) {
621 let mut cx = NeovimBackedTestContext::new(cx).await;
622
623 cx.set_shared_state("hellˇo").await;
624 cx.simulate_shared_keystrokes("3 a . escape").await;
625 cx.shared_state().await.assert_eq("hello..ˇ.");
626 cx.simulate_shared_keystrokes("u").await;
627 cx.shared_state().await.assert_eq("hellˇo");
628 }
629
630 #[gpui::test]
631 async fn test_record_replay(cx: &mut gpui::TestAppContext) {
632 let mut cx = NeovimBackedTestContext::new(cx).await;
633
634 cx.set_shared_state("ˇhello world").await;
635 cx.simulate_shared_keystrokes("q w c w j escape q").await;
636 cx.shared_state().await.assert_eq("ˇj world");
637 cx.simulate_shared_keystrokes("2 l @ w").await;
638 cx.shared_state().await.assert_eq("j ˇj");
639 }
640
641 #[gpui::test]
642 async fn test_record_replay_count(cx: &mut gpui::TestAppContext) {
643 let mut cx = NeovimBackedTestContext::new(cx).await;
644
645 cx.set_shared_state("ˇhello world!!").await;
646 cx.simulate_shared_keystrokes("q a v 3 l s 0 escape l q")
647 .await;
648 cx.shared_state().await.assert_eq("0ˇo world!!");
649 cx.simulate_shared_keystrokes("2 @ a").await;
650 cx.shared_state().await.assert_eq("000ˇ!");
651 }
652
653 #[gpui::test]
654 async fn test_record_replay_dot(cx: &mut gpui::TestAppContext) {
655 let mut cx = NeovimBackedTestContext::new(cx).await;
656
657 cx.set_shared_state("ˇhello world").await;
658 cx.simulate_shared_keystrokes("q a r a l r b l q").await;
659 cx.shared_state().await.assert_eq("abˇllo world");
660 cx.simulate_shared_keystrokes(".").await;
661 cx.shared_state().await.assert_eq("abˇblo world");
662 cx.simulate_shared_keystrokes("shift-q").await;
663 cx.shared_state().await.assert_eq("ababˇo world");
664 cx.simulate_shared_keystrokes(".").await;
665 cx.shared_state().await.assert_eq("ababˇb world");
666 }
667
668 #[gpui::test]
669 async fn test_record_replay_of_dot(cx: &mut gpui::TestAppContext) {
670 let mut cx = NeovimBackedTestContext::new(cx).await;
671
672 cx.set_shared_state("ˇhello world").await;
673 cx.simulate_shared_keystrokes("r o q w . q").await;
674 cx.shared_state().await.assert_eq("ˇoello world");
675 cx.simulate_shared_keystrokes("d l").await;
676 cx.shared_state().await.assert_eq("ˇello world");
677 cx.simulate_shared_keystrokes("@ w").await;
678 cx.shared_state().await.assert_eq("ˇllo world");
679 }
680
681 #[gpui::test]
682 async fn test_record_replay_interleaved(cx: &mut gpui::TestAppContext) {
683 let mut cx = NeovimBackedTestContext::new(cx).await;
684
685 cx.set_shared_state("ˇhello world").await;
686 cx.simulate_shared_keystrokes("q z r a l q").await;
687 cx.shared_state().await.assert_eq("aˇello world");
688 cx.simulate_shared_keystrokes("q b @ z @ z q").await;
689 cx.shared_state().await.assert_eq("aaaˇlo world");
690 cx.simulate_shared_keystrokes("@ @").await;
691 cx.shared_state().await.assert_eq("aaaaˇo world");
692 cx.simulate_shared_keystrokes("@ b").await;
693 cx.shared_state().await.assert_eq("aaaaaaˇworld");
694 cx.simulate_shared_keystrokes("@ @").await;
695 cx.shared_state().await.assert_eq("aaaaaaaˇorld");
696 cx.simulate_shared_keystrokes("q z r b l q").await;
697 cx.shared_state().await.assert_eq("aaaaaaabˇrld");
698 cx.simulate_shared_keystrokes("@ b").await;
699 cx.shared_state().await.assert_eq("aaaaaaabbbˇd");
700 }
701}