1use anyhow::Result;
2use collections::{HashMap, HashSet};
3use command_palette_hooks::CommandInterceptResult;
4use editor::{
5 Bias, Editor, SelectionEffects, ToPoint,
6 actions::{SortLinesCaseInsensitive, SortLinesCaseSensitive},
7 display_map::ToDisplayPoint,
8};
9use gpui::{Action, App, AppContext as _, Context, Global, Window, actions};
10use itertools::Itertools;
11use language::Point;
12use multi_buffer::MultiBufferRow;
13use project::ProjectPath;
14use regex::Regex;
15use schemars::JsonSchema;
16use search::{BufferSearchBar, SearchOptions};
17use serde::Deserialize;
18use std::{
19 io::Write,
20 iter::Peekable,
21 ops::{Deref, Range},
22 path::Path,
23 process::Stdio,
24 str::Chars,
25 sync::{Arc, OnceLock},
26 time::Instant,
27};
28use task::{HideStrategy, RevealStrategy, SpawnInTerminal, TaskId};
29use ui::ActiveTheme;
30use util::ResultExt;
31use workspace::notifications::DetachAndPromptErr;
32use workspace::{Item, SaveIntent, notifications::NotifyResultExt};
33use zed_actions::{OpenDocs, RevealTarget};
34
35use crate::{
36 ToggleMarksView, ToggleRegistersView, Vim,
37 motion::{EndOfDocument, Motion, MotionKind, StartOfDocument},
38 normal::{
39 JoinLines,
40 search::{FindCommand, ReplaceCommand, Replacement},
41 },
42 object::Object,
43 state::{Mark, Mode},
44 visual::VisualDeleteLine,
45};
46
47#[derive(Clone, Debug, PartialEq, Action)]
48#[action(namespace = vim, no_json, no_register)]
49pub struct GoToLine {
50 range: CommandRange,
51}
52
53#[derive(Clone, Debug, PartialEq, Action)]
54#[action(namespace = vim, no_json, no_register)]
55pub struct YankCommand {
56 range: CommandRange,
57}
58
59#[derive(Clone, Debug, PartialEq, Action)]
60#[action(namespace = vim, no_json, no_register)]
61pub struct WithRange {
62 restore_selection: bool,
63 range: CommandRange,
64 action: WrappedAction,
65}
66
67#[derive(Clone, Debug, PartialEq, Action)]
68#[action(namespace = vim, no_json, no_register)]
69pub struct WithCount {
70 count: u32,
71 action: WrappedAction,
72}
73
74#[derive(Clone, Deserialize, JsonSchema, PartialEq)]
75pub enum VimOption {
76 Wrap(bool),
77 Number(bool),
78 RelativeNumber(bool),
79}
80
81impl VimOption {
82 fn possible_commands(query: &str) -> Vec<CommandInterceptResult> {
83 let mut prefix_of_options = Vec::new();
84 let mut options = query.split(" ").collect::<Vec<_>>();
85 let prefix = options.pop().unwrap_or_default();
86 for option in options {
87 if let Some(opt) = Self::from(option) {
88 prefix_of_options.push(opt)
89 } else {
90 return vec![];
91 }
92 }
93
94 Self::possibilities(&prefix)
95 .map(|possible| {
96 let mut options = prefix_of_options.clone();
97 options.push(possible);
98
99 CommandInterceptResult {
100 string: format!(
101 ":set {}",
102 options.iter().map(|opt| opt.to_string()).join(" ")
103 ),
104 action: VimSet { options }.boxed_clone(),
105 positions: vec![],
106 }
107 })
108 .collect()
109 }
110
111 fn possibilities(query: &str) -> impl Iterator<Item = Self> + '_ {
112 [
113 (None, VimOption::Wrap(true)),
114 (None, VimOption::Wrap(false)),
115 (None, VimOption::Number(true)),
116 (None, VimOption::Number(false)),
117 (None, VimOption::RelativeNumber(true)),
118 (None, VimOption::RelativeNumber(false)),
119 (Some("rnu"), VimOption::RelativeNumber(true)),
120 (Some("nornu"), VimOption::RelativeNumber(false)),
121 ]
122 .into_iter()
123 .filter(move |(prefix, option)| prefix.unwrap_or(option.to_string()).starts_with(query))
124 .map(|(_, option)| option)
125 }
126
127 fn from(option: &str) -> Option<Self> {
128 match option {
129 "wrap" => Some(Self::Wrap(true)),
130 "nowrap" => Some(Self::Wrap(false)),
131
132 "number" => Some(Self::Number(true)),
133 "nu" => Some(Self::Number(true)),
134 "nonumber" => Some(Self::Number(false)),
135 "nonu" => Some(Self::Number(false)),
136
137 "relativenumber" => Some(Self::RelativeNumber(true)),
138 "rnu" => Some(Self::RelativeNumber(true)),
139 "norelativenumber" => Some(Self::RelativeNumber(false)),
140 "nornu" => Some(Self::RelativeNumber(false)),
141
142 _ => None,
143 }
144 }
145
146 fn to_string(&self) -> &'static str {
147 match self {
148 VimOption::Wrap(true) => "wrap",
149 VimOption::Wrap(false) => "nowrap",
150 VimOption::Number(true) => "number",
151 VimOption::Number(false) => "nonumber",
152 VimOption::RelativeNumber(true) => "relativenumber",
153 VimOption::RelativeNumber(false) => "norelativenumber",
154 }
155 }
156}
157
158#[derive(Clone, PartialEq, Action)]
159#[action(namespace = vim, no_json, no_register)]
160pub struct VimSet {
161 options: Vec<VimOption>,
162}
163
164#[derive(Clone, PartialEq, Action)]
165#[action(namespace = vim, no_json, no_register)]
166struct VimSave {
167 pub save_intent: Option<SaveIntent>,
168 pub filename: String,
169}
170
171#[derive(Clone, PartialEq, Action)]
172#[action(namespace = vim, no_json, no_register)]
173enum DeleteMarks {
174 Marks(String),
175 AllLocal,
176}
177
178actions!(
179 vim,
180 [VisualCommand, CountCommand, ShellCommand, ArgumentRequired]
181);
182#[derive(Clone, PartialEq, Action)]
183#[action(namespace = vim, no_json, no_register)]
184struct VimEdit {
185 pub filename: String,
186}
187
188#[derive(Debug)]
189struct WrappedAction(Box<dyn Action>);
190
191impl PartialEq for WrappedAction {
192 fn eq(&self, other: &Self) -> bool {
193 self.0.partial_eq(&*other.0)
194 }
195}
196
197impl Clone for WrappedAction {
198 fn clone(&self) -> Self {
199 Self(self.0.boxed_clone())
200 }
201}
202
203impl Deref for WrappedAction {
204 type Target = dyn Action;
205 fn deref(&self) -> &dyn Action {
206 &*self.0
207 }
208}
209
210pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
211 // Vim::action(editor, cx, |vim, action: &StartOfLine, window, cx| {
212 Vim::action(editor, cx, |vim, action: &VimSet, window, cx| {
213 for option in action.options.iter() {
214 vim.update_editor(window, cx, |_, editor, _, cx| match option {
215 VimOption::Wrap(true) => {
216 editor
217 .set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
218 }
219 VimOption::Wrap(false) => {
220 editor.set_soft_wrap_mode(language::language_settings::SoftWrap::None, cx);
221 }
222 VimOption::Number(enabled) => {
223 editor.set_show_line_numbers(*enabled, cx);
224 }
225 VimOption::RelativeNumber(enabled) => {
226 editor.set_relative_line_number(Some(*enabled), cx);
227 }
228 });
229 }
230 });
231 Vim::action(editor, cx, |vim, _: &VisualCommand, window, cx| {
232 let Some(workspace) = vim.workspace(window) else {
233 return;
234 };
235 workspace.update(cx, |workspace, cx| {
236 command_palette::CommandPalette::toggle(workspace, "'<,'>", window, cx);
237 })
238 });
239
240 Vim::action(editor, cx, |vim, _: &ShellCommand, window, cx| {
241 let Some(workspace) = vim.workspace(window) else {
242 return;
243 };
244 workspace.update(cx, |workspace, cx| {
245 command_palette::CommandPalette::toggle(workspace, "'<,'>!", window, cx);
246 })
247 });
248
249 Vim::action(editor, cx, |_, _: &ArgumentRequired, window, cx| {
250 let _ = window.prompt(
251 gpui::PromptLevel::Critical,
252 "Argument required",
253 None,
254 &["Cancel"],
255 cx,
256 );
257 });
258
259 Vim::action(editor, cx, |vim, _: &ShellCommand, window, cx| {
260 let Some(workspace) = vim.workspace(window) else {
261 return;
262 };
263 workspace.update(cx, |workspace, cx| {
264 command_palette::CommandPalette::toggle(workspace, "'<,'>!", window, cx);
265 })
266 });
267
268 Vim::action(editor, cx, |vim, action: &VimSave, window, cx| {
269 vim.update_editor(window, cx, |_, editor, window, cx| {
270 let Some(project) = editor.project.clone() else {
271 return;
272 };
273 let Some(worktree) = project.read(cx).visible_worktrees(cx).next() else {
274 return;
275 };
276 let project_path = ProjectPath {
277 worktree_id: worktree.read(cx).id(),
278 path: Arc::from(Path::new(&action.filename)),
279 };
280
281 if project.read(cx).entry_for_path(&project_path, cx).is_some() && action.save_intent != Some(SaveIntent::Overwrite) {
282 let answer = window.prompt(
283 gpui::PromptLevel::Critical,
284 &format!("{} already exists. Do you want to replace it?", project_path.path.to_string_lossy()),
285 Some(
286 "A file or folder with the same name already exists. Replacing it will overwrite its current contents.",
287 ),
288 &["Replace", "Cancel"],
289 cx);
290 cx.spawn_in(window, async move |editor, cx| {
291 if answer.await.ok() != Some(0) {
292 return;
293 }
294
295 let _ = editor.update_in(cx, |editor, window, cx|{
296 editor
297 .save_as(project, project_path, window, cx)
298 .detach_and_prompt_err("Failed to :w", window, cx, |_, _, _| None);
299 });
300 }).detach();
301 } else {
302 editor
303 .save_as(project, project_path, window, cx)
304 .detach_and_prompt_err("Failed to :w", window, cx, |_, _, _| None);
305 }
306 });
307 });
308
309 Vim::action(editor, cx, |vim, action: &DeleteMarks, window, cx| {
310 fn err(s: String, window: &mut Window, cx: &mut Context<Editor>) {
311 let _ = window.prompt(
312 gpui::PromptLevel::Critical,
313 &format!("Invalid argument: {}", s),
314 None,
315 &["Cancel"],
316 cx,
317 );
318 }
319 vim.update_editor(window, cx, |vim, editor, window, cx| match action {
320 DeleteMarks::Marks(s) => {
321 if s.starts_with('-') || s.ends_with('-') || s.contains(['\'', '`']) {
322 err(s.clone(), window, cx);
323 return;
324 }
325
326 let to_delete = if s.len() < 3 {
327 Some(s.clone())
328 } else {
329 s.chars()
330 .tuple_windows::<(_, _, _)>()
331 .map(|(a, b, c)| {
332 if b == '-' {
333 if match a {
334 'a'..='z' => a <= c && c <= 'z',
335 'A'..='Z' => a <= c && c <= 'Z',
336 '0'..='9' => a <= c && c <= '9',
337 _ => false,
338 } {
339 Some((a..=c).collect_vec())
340 } else {
341 None
342 }
343 } else if a == '-' {
344 if c == '-' { None } else { Some(vec![c]) }
345 } else if c == '-' {
346 if a == '-' { None } else { Some(vec![a]) }
347 } else {
348 Some(vec![a, b, c])
349 }
350 })
351 .fold_options(HashSet::<char>::default(), |mut set, chars| {
352 set.extend(chars.iter().copied());
353 set
354 })
355 .map(|set| set.iter().collect::<String>())
356 };
357
358 let Some(to_delete) = to_delete else {
359 err(s.clone(), window, cx);
360 return;
361 };
362
363 for c in to_delete.chars().filter(|c| !c.is_whitespace()) {
364 vim.delete_mark(c.to_string(), editor, window, cx);
365 }
366 }
367 DeleteMarks::AllLocal => {
368 for s in 'a'..='z' {
369 vim.delete_mark(s.to_string(), editor, window, cx);
370 }
371 }
372 });
373 });
374
375 Vim::action(editor, cx, |vim, action: &VimEdit, window, cx| {
376 vim.update_editor(window, cx, |vim, editor, window, cx| {
377 let Some(workspace) = vim.workspace(window) else {
378 return;
379 };
380 let Some(project) = editor.project.clone() else {
381 return;
382 };
383 let Some(worktree) = project.read(cx).visible_worktrees(cx).next() else {
384 return;
385 };
386 let project_path = ProjectPath {
387 worktree_id: worktree.read(cx).id(),
388 path: Arc::from(Path::new(&action.filename)),
389 };
390
391 let _ = workspace.update(cx, |workspace, cx| {
392 workspace
393 .open_path(project_path, None, true, window, cx)
394 .detach_and_log_err(cx);
395 });
396 });
397 });
398
399 Vim::action(editor, cx, |vim, _: &CountCommand, window, cx| {
400 let Some(workspace) = vim.workspace(window) else {
401 return;
402 };
403 let count = Vim::take_count(cx).unwrap_or(1);
404 Vim::take_forced_motion(cx);
405 let n = if count > 1 {
406 format!(".,.+{}", count.saturating_sub(1))
407 } else {
408 ".".to_string()
409 };
410 workspace.update(cx, |workspace, cx| {
411 command_palette::CommandPalette::toggle(workspace, &n, window, cx);
412 })
413 });
414
415 Vim::action(editor, cx, |vim, action: &GoToLine, window, cx| {
416 vim.switch_mode(Mode::Normal, false, window, cx);
417 let result = vim.update_editor(window, cx, |vim, editor, window, cx| {
418 let snapshot = editor.snapshot(window, cx);
419 let buffer_row = action.range.head().buffer_row(vim, editor, window, cx)?;
420 let current = editor.selections.newest::<Point>(cx);
421 let target = snapshot
422 .buffer_snapshot
423 .clip_point(Point::new(buffer_row.0, current.head().column), Bias::Left);
424 editor.change_selections(Default::default(), window, cx, |s| {
425 s.select_ranges([target..target]);
426 });
427
428 anyhow::Ok(())
429 });
430 if let Some(e @ Err(_)) = result {
431 let Some(workspace) = vim.workspace(window) else {
432 return;
433 };
434 workspace.update(cx, |workspace, cx| {
435 e.notify_err(workspace, cx);
436 });
437 return;
438 }
439 });
440
441 Vim::action(editor, cx, |vim, action: &YankCommand, window, cx| {
442 vim.update_editor(window, cx, |vim, editor, window, cx| {
443 let snapshot = editor.snapshot(window, cx);
444 if let Ok(range) = action.range.buffer_range(vim, editor, window, cx) {
445 let end = if range.end < snapshot.buffer_snapshot.max_row() {
446 Point::new(range.end.0 + 1, 0)
447 } else {
448 snapshot.buffer_snapshot.max_point()
449 };
450 vim.copy_ranges(
451 editor,
452 MotionKind::Linewise,
453 true,
454 vec![Point::new(range.start.0, 0)..end],
455 window,
456 cx,
457 )
458 }
459 });
460 });
461
462 Vim::action(editor, cx, |_, action: &WithCount, window, cx| {
463 for _ in 0..action.count {
464 window.dispatch_action(action.action.boxed_clone(), cx)
465 }
466 });
467
468 Vim::action(editor, cx, |vim, action: &WithRange, window, cx| {
469 let result = vim.update_editor(window, cx, |vim, editor, window, cx| {
470 action.range.buffer_range(vim, editor, window, cx)
471 });
472
473 let range = match result {
474 None => return,
475 Some(e @ Err(_)) => {
476 let Some(workspace) = vim.workspace(window) else {
477 return;
478 };
479 workspace.update(cx, |workspace, cx| {
480 e.notify_err(workspace, cx);
481 });
482 return;
483 }
484 Some(Ok(result)) => result,
485 };
486
487 let previous_selections = vim
488 .update_editor(window, cx, |_, editor, window, cx| {
489 let selections = action.restore_selection.then(|| {
490 editor
491 .selections
492 .disjoint_anchor_ranges()
493 .collect::<Vec<_>>()
494 });
495 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
496 let end = Point::new(range.end.0, s.buffer().line_len(range.end));
497 s.select_ranges([end..Point::new(range.start.0, 0)]);
498 });
499 selections
500 })
501 .flatten();
502 window.dispatch_action(action.action.boxed_clone(), cx);
503 cx.defer_in(window, move |vim, window, cx| {
504 vim.update_editor(window, cx, |_, editor, window, cx| {
505 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
506 if let Some(previous_selections) = previous_selections {
507 s.select_ranges(previous_selections);
508 } else {
509 s.select_ranges([
510 Point::new(range.start.0, 0)..Point::new(range.start.0, 0)
511 ]);
512 }
513 })
514 });
515 });
516 });
517
518 Vim::action(editor, cx, |vim, action: &OnMatchingLines, window, cx| {
519 action.run(vim, window, cx)
520 });
521
522 Vim::action(editor, cx, |vim, action: &ShellExec, window, cx| {
523 action.run(vim, window, cx)
524 })
525}
526
527#[derive(Default)]
528struct VimCommand {
529 prefix: &'static str,
530 suffix: &'static str,
531 action: Option<Box<dyn Action>>,
532 action_name: Option<&'static str>,
533 bang_action: Option<Box<dyn Action>>,
534 args: Option<
535 Box<dyn Fn(Box<dyn Action>, String) -> Option<Box<dyn Action>> + Send + Sync + 'static>,
536 >,
537 range: Option<
538 Box<
539 dyn Fn(Box<dyn Action>, &CommandRange) -> Option<Box<dyn Action>>
540 + Send
541 + Sync
542 + 'static,
543 >,
544 >,
545 has_count: bool,
546}
547
548impl VimCommand {
549 fn new(pattern: (&'static str, &'static str), action: impl Action) -> Self {
550 Self {
551 prefix: pattern.0,
552 suffix: pattern.1,
553 action: Some(action.boxed_clone()),
554 ..Default::default()
555 }
556 }
557
558 // from_str is used for actions in other crates.
559 fn str(pattern: (&'static str, &'static str), action_name: &'static str) -> Self {
560 Self {
561 prefix: pattern.0,
562 suffix: pattern.1,
563 action_name: Some(action_name),
564 ..Default::default()
565 }
566 }
567
568 fn bang(mut self, bang_action: impl Action) -> Self {
569 self.bang_action = Some(bang_action.boxed_clone());
570 self
571 }
572
573 fn args(
574 mut self,
575 f: impl Fn(Box<dyn Action>, String) -> Option<Box<dyn Action>> + Send + Sync + 'static,
576 ) -> Self {
577 self.args = Some(Box::new(f));
578 self
579 }
580
581 fn range(
582 mut self,
583 f: impl Fn(Box<dyn Action>, &CommandRange) -> Option<Box<dyn Action>> + Send + Sync + 'static,
584 ) -> Self {
585 self.range = Some(Box::new(f));
586 self
587 }
588
589 fn count(mut self) -> Self {
590 self.has_count = true;
591 self
592 }
593
594 fn parse(
595 &self,
596 query: &str,
597 range: &Option<CommandRange>,
598 cx: &App,
599 ) -> Option<Box<dyn Action>> {
600 let rest = query
601 .to_string()
602 .strip_prefix(self.prefix)?
603 .to_string()
604 .chars()
605 .zip_longest(self.suffix.to_string().chars())
606 .skip_while(|e| e.clone().both().map(|(s, q)| s == q).unwrap_or(false))
607 .filter_map(|e| e.left())
608 .collect::<String>();
609 let has_bang = rest.starts_with('!');
610 let args = if has_bang {
611 rest.strip_prefix('!')?.trim().to_string()
612 } else if rest.is_empty() {
613 "".into()
614 } else {
615 rest.strip_prefix(' ')?.trim().to_string()
616 };
617
618 let action = if has_bang && self.bang_action.is_some() {
619 self.bang_action.as_ref().unwrap().boxed_clone()
620 } else if let Some(action) = self.action.as_ref() {
621 action.boxed_clone()
622 } else if let Some(action_name) = self.action_name {
623 cx.build_action(action_name, None).log_err()?
624 } else {
625 return None;
626 };
627 if !args.is_empty() {
628 // if command does not accept args and we have args then we should do no action
629 if let Some(args_fn) = &self.args {
630 args_fn.deref()(action, args)
631 } else {
632 None
633 }
634 } else if let Some(range) = range {
635 self.range.as_ref().and_then(|f| f(action, range))
636 } else {
637 Some(action)
638 }
639 }
640
641 // TODO: ranges with search queries
642 fn parse_range(query: &str) -> (Option<CommandRange>, String) {
643 let mut chars = query.chars().peekable();
644
645 match chars.peek() {
646 Some('%') => {
647 chars.next();
648 return (
649 Some(CommandRange {
650 start: Position::Line { row: 1, offset: 0 },
651 end: Some(Position::LastLine { offset: 0 }),
652 }),
653 chars.collect(),
654 );
655 }
656 Some('*') => {
657 chars.next();
658 return (
659 Some(CommandRange {
660 start: Position::Mark {
661 name: '<',
662 offset: 0,
663 },
664 end: Some(Position::Mark {
665 name: '>',
666 offset: 0,
667 }),
668 }),
669 chars.collect(),
670 );
671 }
672 _ => {}
673 }
674
675 let start = Self::parse_position(&mut chars);
676
677 match chars.peek() {
678 Some(',' | ';') => {
679 chars.next();
680 (
681 Some(CommandRange {
682 start: start.unwrap_or(Position::CurrentLine { offset: 0 }),
683 end: Self::parse_position(&mut chars),
684 }),
685 chars.collect(),
686 )
687 }
688 _ => (
689 start.map(|start| CommandRange { start, end: None }),
690 chars.collect(),
691 ),
692 }
693 }
694
695 fn parse_position(chars: &mut Peekable<Chars>) -> Option<Position> {
696 match chars.peek()? {
697 '0'..='9' => {
698 let row = Self::parse_u32(chars);
699 Some(Position::Line {
700 row,
701 offset: Self::parse_offset(chars),
702 })
703 }
704 '\'' => {
705 chars.next();
706 let name = chars.next()?;
707 Some(Position::Mark {
708 name,
709 offset: Self::parse_offset(chars),
710 })
711 }
712 '.' => {
713 chars.next();
714 Some(Position::CurrentLine {
715 offset: Self::parse_offset(chars),
716 })
717 }
718 '+' | '-' => Some(Position::CurrentLine {
719 offset: Self::parse_offset(chars),
720 }),
721 '$' => {
722 chars.next();
723 Some(Position::LastLine {
724 offset: Self::parse_offset(chars),
725 })
726 }
727 _ => None,
728 }
729 }
730
731 fn parse_offset(chars: &mut Peekable<Chars>) -> i32 {
732 let mut res: i32 = 0;
733 while matches!(chars.peek(), Some('+' | '-')) {
734 let sign = if chars.next().unwrap() == '+' { 1 } else { -1 };
735 let amount = if matches!(chars.peek(), Some('0'..='9')) {
736 (Self::parse_u32(chars) as i32).saturating_mul(sign)
737 } else {
738 sign
739 };
740 res = res.saturating_add(amount)
741 }
742 res
743 }
744
745 fn parse_u32(chars: &mut Peekable<Chars>) -> u32 {
746 let mut res: u32 = 0;
747 while matches!(chars.peek(), Some('0'..='9')) {
748 res = res
749 .saturating_mul(10)
750 .saturating_add(chars.next().unwrap() as u32 - '0' as u32);
751 }
752 res
753 }
754}
755
756#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq)]
757enum Position {
758 Line { row: u32, offset: i32 },
759 Mark { name: char, offset: i32 },
760 LastLine { offset: i32 },
761 CurrentLine { offset: i32 },
762}
763
764impl Position {
765 fn buffer_row(
766 &self,
767 vim: &Vim,
768 editor: &mut Editor,
769 window: &mut Window,
770 cx: &mut App,
771 ) -> Result<MultiBufferRow> {
772 let snapshot = editor.snapshot(window, cx);
773 let target = match self {
774 Position::Line { row, offset } => {
775 if let Some(anchor) = editor.active_excerpt(cx).and_then(|(_, buffer, _)| {
776 editor.buffer().read(cx).buffer_point_to_anchor(
777 &buffer,
778 Point::new(row.saturating_sub(1), 0),
779 cx,
780 )
781 }) {
782 anchor
783 .to_point(&snapshot.buffer_snapshot)
784 .row
785 .saturating_add_signed(*offset)
786 } else {
787 row.saturating_add_signed(offset.saturating_sub(1))
788 }
789 }
790 Position::Mark { name, offset } => {
791 let Some(Mark::Local(anchors)) =
792 vim.get_mark(&name.to_string(), editor, window, cx)
793 else {
794 anyhow::bail!("mark {name} not set");
795 };
796 let Some(mark) = anchors.last() else {
797 anyhow::bail!("mark {name} contains empty anchors");
798 };
799 mark.to_point(&snapshot.buffer_snapshot)
800 .row
801 .saturating_add_signed(*offset)
802 }
803 Position::LastLine { offset } => snapshot
804 .buffer_snapshot
805 .max_row()
806 .0
807 .saturating_add_signed(*offset),
808 Position::CurrentLine { offset } => editor
809 .selections
810 .newest_anchor()
811 .head()
812 .to_point(&snapshot.buffer_snapshot)
813 .row
814 .saturating_add_signed(*offset),
815 };
816
817 Ok(MultiBufferRow(target).min(snapshot.buffer_snapshot.max_row()))
818 }
819}
820
821#[derive(Clone, Debug, PartialEq)]
822pub(crate) struct CommandRange {
823 start: Position,
824 end: Option<Position>,
825}
826
827impl CommandRange {
828 fn head(&self) -> &Position {
829 self.end.as_ref().unwrap_or(&self.start)
830 }
831
832 pub(crate) fn buffer_range(
833 &self,
834 vim: &Vim,
835 editor: &mut Editor,
836 window: &mut Window,
837 cx: &mut App,
838 ) -> Result<Range<MultiBufferRow>> {
839 let start = self.start.buffer_row(vim, editor, window, cx)?;
840 let end = if let Some(end) = self.end.as_ref() {
841 end.buffer_row(vim, editor, window, cx)?
842 } else {
843 start
844 };
845 if end < start {
846 anyhow::Ok(end..start)
847 } else {
848 anyhow::Ok(start..end)
849 }
850 }
851
852 pub fn as_count(&self) -> Option<u32> {
853 if let CommandRange {
854 start: Position::Line { row, offset: 0 },
855 end: None,
856 } = &self
857 {
858 Some(*row)
859 } else {
860 None
861 }
862 }
863}
864
865fn generate_commands(_: &App) -> Vec<VimCommand> {
866 vec![
867 VimCommand::new(
868 ("w", "rite"),
869 workspace::Save {
870 save_intent: Some(SaveIntent::Save),
871 },
872 )
873 .bang(workspace::Save {
874 save_intent: Some(SaveIntent::Overwrite),
875 })
876 .args(|action, args| {
877 Some(
878 VimSave {
879 save_intent: action
880 .as_any()
881 .downcast_ref::<workspace::Save>()
882 .and_then(|action| action.save_intent),
883 filename: args,
884 }
885 .boxed_clone(),
886 )
887 }),
888 VimCommand::new(
889 ("q", "uit"),
890 workspace::CloseActiveItem {
891 save_intent: Some(SaveIntent::Close),
892 close_pinned: false,
893 },
894 )
895 .bang(workspace::CloseActiveItem {
896 save_intent: Some(SaveIntent::Skip),
897 close_pinned: true,
898 }),
899 VimCommand::new(
900 ("wq", ""),
901 workspace::CloseActiveItem {
902 save_intent: Some(SaveIntent::Save),
903 close_pinned: false,
904 },
905 )
906 .bang(workspace::CloseActiveItem {
907 save_intent: Some(SaveIntent::Overwrite),
908 close_pinned: true,
909 }),
910 VimCommand::new(
911 ("x", "it"),
912 workspace::CloseActiveItem {
913 save_intent: Some(SaveIntent::SaveAll),
914 close_pinned: false,
915 },
916 )
917 .bang(workspace::CloseActiveItem {
918 save_intent: Some(SaveIntent::Overwrite),
919 close_pinned: true,
920 }),
921 VimCommand::new(
922 ("exi", "t"),
923 workspace::CloseActiveItem {
924 save_intent: Some(SaveIntent::SaveAll),
925 close_pinned: false,
926 },
927 )
928 .bang(workspace::CloseActiveItem {
929 save_intent: Some(SaveIntent::Overwrite),
930 close_pinned: true,
931 }),
932 VimCommand::new(
933 ("up", "date"),
934 workspace::Save {
935 save_intent: Some(SaveIntent::SaveAll),
936 },
937 ),
938 VimCommand::new(
939 ("wa", "ll"),
940 workspace::SaveAll {
941 save_intent: Some(SaveIntent::SaveAll),
942 },
943 )
944 .bang(workspace::SaveAll {
945 save_intent: Some(SaveIntent::Overwrite),
946 }),
947 VimCommand::new(
948 ("qa", "ll"),
949 workspace::CloseAllItemsAndPanes {
950 save_intent: Some(SaveIntent::Close),
951 },
952 )
953 .bang(workspace::CloseAllItemsAndPanes {
954 save_intent: Some(SaveIntent::Skip),
955 }),
956 VimCommand::new(
957 ("quita", "ll"),
958 workspace::CloseAllItemsAndPanes {
959 save_intent: Some(SaveIntent::Close),
960 },
961 )
962 .bang(workspace::CloseAllItemsAndPanes {
963 save_intent: Some(SaveIntent::Skip),
964 }),
965 VimCommand::new(
966 ("xa", "ll"),
967 workspace::CloseAllItemsAndPanes {
968 save_intent: Some(SaveIntent::SaveAll),
969 },
970 )
971 .bang(workspace::CloseAllItemsAndPanes {
972 save_intent: Some(SaveIntent::Overwrite),
973 }),
974 VimCommand::new(
975 ("wqa", "ll"),
976 workspace::CloseAllItemsAndPanes {
977 save_intent: Some(SaveIntent::SaveAll),
978 },
979 )
980 .bang(workspace::CloseAllItemsAndPanes {
981 save_intent: Some(SaveIntent::Overwrite),
982 }),
983 VimCommand::new(("cq", "uit"), zed_actions::Quit),
984 VimCommand::new(("sp", "lit"), workspace::SplitHorizontal),
985 VimCommand::new(("vs", "plit"), workspace::SplitVertical),
986 VimCommand::new(
987 ("bd", "elete"),
988 workspace::CloseActiveItem {
989 save_intent: Some(SaveIntent::Close),
990 close_pinned: false,
991 },
992 )
993 .bang(workspace::CloseActiveItem {
994 save_intent: Some(SaveIntent::Skip),
995 close_pinned: true,
996 }),
997 VimCommand::new(("bn", "ext"), workspace::ActivateNextItem).count(),
998 VimCommand::new(("bN", "ext"), workspace::ActivatePreviousItem).count(),
999 VimCommand::new(("bp", "revious"), workspace::ActivatePreviousItem).count(),
1000 VimCommand::new(("bf", "irst"), workspace::ActivateItem(0)),
1001 VimCommand::new(("br", "ewind"), workspace::ActivateItem(0)),
1002 VimCommand::new(("bl", "ast"), workspace::ActivateLastItem),
1003 VimCommand::str(("buffers", ""), "tab_switcher::ToggleAll"),
1004 VimCommand::str(("ls", ""), "tab_switcher::ToggleAll"),
1005 VimCommand::new(("new", ""), workspace::NewFileSplitHorizontal),
1006 VimCommand::new(("vne", "w"), workspace::NewFileSplitVertical),
1007 VimCommand::new(("tabe", "dit"), workspace::NewFile),
1008 VimCommand::new(("tabnew", ""), workspace::NewFile),
1009 VimCommand::new(("tabn", "ext"), workspace::ActivateNextItem).count(),
1010 VimCommand::new(("tabp", "revious"), workspace::ActivatePreviousItem).count(),
1011 VimCommand::new(("tabN", "ext"), workspace::ActivatePreviousItem).count(),
1012 VimCommand::new(
1013 ("tabc", "lose"),
1014 workspace::CloseActiveItem {
1015 save_intent: Some(SaveIntent::Close),
1016 close_pinned: false,
1017 },
1018 ),
1019 VimCommand::new(
1020 ("tabo", "nly"),
1021 workspace::CloseInactiveItems {
1022 save_intent: Some(SaveIntent::Close),
1023 close_pinned: false,
1024 },
1025 )
1026 .bang(workspace::CloseInactiveItems {
1027 save_intent: Some(SaveIntent::Skip),
1028 close_pinned: false,
1029 }),
1030 VimCommand::new(
1031 ("on", "ly"),
1032 workspace::CloseInactiveTabsAndPanes {
1033 save_intent: Some(SaveIntent::Close),
1034 },
1035 )
1036 .bang(workspace::CloseInactiveTabsAndPanes {
1037 save_intent: Some(SaveIntent::Skip),
1038 }),
1039 VimCommand::str(("cl", "ist"), "diagnostics::Deploy"),
1040 VimCommand::new(("cc", ""), editor::actions::Hover),
1041 VimCommand::new(("ll", ""), editor::actions::Hover),
1042 VimCommand::new(("cn", "ext"), editor::actions::GoToDiagnostic).range(wrap_count),
1043 VimCommand::new(("cp", "revious"), editor::actions::GoToPreviousDiagnostic)
1044 .range(wrap_count),
1045 VimCommand::new(("cN", "ext"), editor::actions::GoToPreviousDiagnostic).range(wrap_count),
1046 VimCommand::new(("lp", "revious"), editor::actions::GoToPreviousDiagnostic)
1047 .range(wrap_count),
1048 VimCommand::new(("lN", "ext"), editor::actions::GoToPreviousDiagnostic).range(wrap_count),
1049 VimCommand::new(("j", "oin"), JoinLines).range(select_range),
1050 VimCommand::new(("fo", "ld"), editor::actions::FoldSelectedRanges).range(act_on_range),
1051 VimCommand::new(("foldo", "pen"), editor::actions::UnfoldLines)
1052 .bang(editor::actions::UnfoldRecursive)
1053 .range(act_on_range),
1054 VimCommand::new(("foldc", "lose"), editor::actions::Fold)
1055 .bang(editor::actions::FoldRecursive)
1056 .range(act_on_range),
1057 VimCommand::new(("dif", "fupdate"), editor::actions::ToggleSelectedDiffHunks)
1058 .range(act_on_range),
1059 VimCommand::str(("rev", "ert"), "git::Restore").range(act_on_range),
1060 VimCommand::new(("d", "elete"), VisualDeleteLine).range(select_range),
1061 VimCommand::new(("y", "ank"), gpui::NoAction).range(|_, range| {
1062 Some(
1063 YankCommand {
1064 range: range.clone(),
1065 }
1066 .boxed_clone(),
1067 )
1068 }),
1069 VimCommand::new(("reg", "isters"), ToggleRegistersView).bang(ToggleRegistersView),
1070 VimCommand::new(("di", "splay"), ToggleRegistersView).bang(ToggleRegistersView),
1071 VimCommand::new(("marks", ""), ToggleMarksView).bang(ToggleMarksView),
1072 VimCommand::new(("delm", "arks"), ArgumentRequired)
1073 .bang(DeleteMarks::AllLocal)
1074 .args(|_, args| Some(DeleteMarks::Marks(args).boxed_clone())),
1075 VimCommand::new(("sor", "t"), SortLinesCaseSensitive).range(select_range),
1076 VimCommand::new(("sort i", ""), SortLinesCaseInsensitive).range(select_range),
1077 VimCommand::str(("E", "xplore"), "project_panel::ToggleFocus"),
1078 VimCommand::str(("H", "explore"), "project_panel::ToggleFocus"),
1079 VimCommand::str(("L", "explore"), "project_panel::ToggleFocus"),
1080 VimCommand::str(("S", "explore"), "project_panel::ToggleFocus"),
1081 VimCommand::str(("Ve", "xplore"), "project_panel::ToggleFocus"),
1082 VimCommand::str(("te", "rm"), "terminal_panel::ToggleFocus"),
1083 VimCommand::str(("T", "erm"), "terminal_panel::ToggleFocus"),
1084 VimCommand::str(("C", "ollab"), "collab_panel::ToggleFocus"),
1085 VimCommand::str(("Ch", "at"), "chat_panel::ToggleFocus"),
1086 VimCommand::str(("No", "tifications"), "notification_panel::ToggleFocus"),
1087 VimCommand::str(("A", "I"), "agent::ToggleFocus"),
1088 VimCommand::str(("G", "it"), "git_panel::ToggleFocus"),
1089 VimCommand::str(("D", "ebug"), "debug_panel::ToggleFocus"),
1090 VimCommand::new(("noh", "lsearch"), search::buffer_search::Dismiss),
1091 VimCommand::new(("$", ""), EndOfDocument),
1092 VimCommand::new(("%", ""), EndOfDocument),
1093 VimCommand::new(("0", ""), StartOfDocument),
1094 VimCommand::new(("e", "dit"), editor::actions::ReloadFile)
1095 .bang(editor::actions::ReloadFile)
1096 .args(|_, args| Some(VimEdit { filename: args }.boxed_clone())),
1097 VimCommand::new(("ex", ""), editor::actions::ReloadFile).bang(editor::actions::ReloadFile),
1098 VimCommand::new(("cpp", "link"), editor::actions::CopyPermalinkToLine).range(act_on_range),
1099 VimCommand::str(("opt", "ions"), "zed::OpenDefaultSettings"),
1100 VimCommand::str(("map", ""), "vim::OpenDefaultKeymap"),
1101 VimCommand::new(("h", "elp"), OpenDocs),
1102 ]
1103}
1104
1105struct VimCommands(Vec<VimCommand>);
1106// safety: we only ever access this from the main thread (as ensured by the cx argument)
1107// actions are not Sync so we can't otherwise use a OnceLock.
1108unsafe impl Sync for VimCommands {}
1109impl Global for VimCommands {}
1110
1111fn commands(cx: &App) -> &Vec<VimCommand> {
1112 static COMMANDS: OnceLock<VimCommands> = OnceLock::new();
1113 &COMMANDS
1114 .get_or_init(|| VimCommands(generate_commands(cx)))
1115 .0
1116}
1117
1118fn act_on_range(action: Box<dyn Action>, range: &CommandRange) -> Option<Box<dyn Action>> {
1119 Some(
1120 WithRange {
1121 restore_selection: true,
1122 range: range.clone(),
1123 action: WrappedAction(action),
1124 }
1125 .boxed_clone(),
1126 )
1127}
1128
1129fn select_range(action: Box<dyn Action>, range: &CommandRange) -> Option<Box<dyn Action>> {
1130 Some(
1131 WithRange {
1132 restore_selection: false,
1133 range: range.clone(),
1134 action: WrappedAction(action),
1135 }
1136 .boxed_clone(),
1137 )
1138}
1139
1140fn wrap_count(action: Box<dyn Action>, range: &CommandRange) -> Option<Box<dyn Action>> {
1141 range.as_count().map(|count| {
1142 WithCount {
1143 count,
1144 action: WrappedAction(action),
1145 }
1146 .boxed_clone()
1147 })
1148}
1149
1150pub fn command_interceptor(mut input: &str, cx: &App) -> Vec<CommandInterceptResult> {
1151 // NOTE: We also need to support passing arguments to commands like :w
1152 // (ideally with filename autocompletion).
1153 while input.starts_with(':') {
1154 input = &input[1..];
1155 }
1156
1157 let (range, query) = VimCommand::parse_range(input);
1158 let range_prefix = input[0..(input.len() - query.len())].to_string();
1159 let query = query.as_str().trim();
1160
1161 let action = if range.is_some() && query.is_empty() {
1162 Some(
1163 GoToLine {
1164 range: range.clone().unwrap(),
1165 }
1166 .boxed_clone(),
1167 )
1168 } else if query.starts_with('/') || query.starts_with('?') {
1169 Some(
1170 FindCommand {
1171 query: query[1..].to_string(),
1172 backwards: query.starts_with('?'),
1173 }
1174 .boxed_clone(),
1175 )
1176 } else if query.starts_with("se ") || query.starts_with("set ") {
1177 let (prefix, option) = query.split_once(' ').unwrap();
1178 let mut commands = VimOption::possible_commands(option);
1179 if !commands.is_empty() {
1180 let query = prefix.to_string() + " " + option;
1181 for command in &mut commands {
1182 command.positions = generate_positions(&command.string, &query);
1183 }
1184 }
1185 return commands;
1186 } else if query.starts_with('s') {
1187 let mut substitute = "substitute".chars().peekable();
1188 let mut query = query.chars().peekable();
1189 while substitute
1190 .peek()
1191 .is_some_and(|char| Some(char) == query.peek())
1192 {
1193 substitute.next();
1194 query.next();
1195 }
1196 if let Some(replacement) = Replacement::parse(query) {
1197 let range = range.clone().unwrap_or(CommandRange {
1198 start: Position::CurrentLine { offset: 0 },
1199 end: None,
1200 });
1201 Some(ReplaceCommand { replacement, range }.boxed_clone())
1202 } else {
1203 None
1204 }
1205 } else if query.starts_with('g') || query.starts_with('v') {
1206 let mut global = "global".chars().peekable();
1207 let mut query = query.chars().peekable();
1208 let mut invert = false;
1209 if query.peek() == Some(&'v') {
1210 invert = true;
1211 query.next();
1212 }
1213 while global.peek().is_some_and(|char| Some(char) == query.peek()) {
1214 global.next();
1215 query.next();
1216 }
1217 if !invert && query.peek() == Some(&'!') {
1218 invert = true;
1219 query.next();
1220 }
1221 let range = range.clone().unwrap_or(CommandRange {
1222 start: Position::Line { row: 0, offset: 0 },
1223 end: Some(Position::LastLine { offset: 0 }),
1224 });
1225 if let Some(action) = OnMatchingLines::parse(query, invert, range, cx) {
1226 Some(action.boxed_clone())
1227 } else {
1228 None
1229 }
1230 } else if query.contains('!') {
1231 ShellExec::parse(query, range.clone())
1232 } else {
1233 None
1234 };
1235 if let Some(action) = action {
1236 let string = input.to_string();
1237 let positions = generate_positions(&string, &(range_prefix + query));
1238 return vec![CommandInterceptResult {
1239 action,
1240 string,
1241 positions,
1242 }];
1243 }
1244
1245 for command in commands(cx).iter() {
1246 if let Some(action) = command.parse(query, &range, cx) {
1247 let mut string = ":".to_owned() + &range_prefix + command.prefix + command.suffix;
1248 if query.contains('!') {
1249 string.push('!');
1250 }
1251 let positions = generate_positions(&string, &(range_prefix + query));
1252
1253 return vec![CommandInterceptResult {
1254 action,
1255 string,
1256 positions,
1257 }];
1258 }
1259 }
1260 return Vec::default();
1261}
1262
1263fn generate_positions(string: &str, query: &str) -> Vec<usize> {
1264 let mut positions = Vec::new();
1265 let mut chars = query.chars();
1266
1267 let Some(mut current) = chars.next() else {
1268 return positions;
1269 };
1270
1271 for (i, c) in string.char_indices() {
1272 if c == current {
1273 positions.push(i);
1274 if let Some(c) = chars.next() {
1275 current = c;
1276 } else {
1277 break;
1278 }
1279 }
1280 }
1281
1282 positions
1283}
1284
1285#[derive(Debug, PartialEq, Clone, Action)]
1286#[action(namespace = vim, no_json, no_register)]
1287pub(crate) struct OnMatchingLines {
1288 range: CommandRange,
1289 search: String,
1290 action: WrappedAction,
1291 invert: bool,
1292}
1293
1294impl OnMatchingLines {
1295 // convert a vim query into something more usable by zed.
1296 // we don't attempt to fully convert between the two regex syntaxes,
1297 // but we do flip \( and \) to ( and ) (and vice-versa) in the pattern,
1298 // and convert \0..\9 to $0..$9 in the replacement so that common idioms work.
1299 pub(crate) fn parse(
1300 mut chars: Peekable<Chars>,
1301 invert: bool,
1302 range: CommandRange,
1303 cx: &App,
1304 ) -> Option<Self> {
1305 let delimiter = chars.next().filter(|c| {
1306 !c.is_alphanumeric() && *c != '"' && *c != '|' && *c != '\'' && *c != '!'
1307 })?;
1308
1309 let mut search = String::new();
1310 let mut escaped = false;
1311
1312 while let Some(c) = chars.next() {
1313 if escaped {
1314 escaped = false;
1315 // unescape escaped parens
1316 if c != '(' && c != ')' && c != delimiter {
1317 search.push('\\')
1318 }
1319 search.push(c)
1320 } else if c == '\\' {
1321 escaped = true;
1322 } else if c == delimiter {
1323 break;
1324 } else {
1325 // escape unescaped parens
1326 if c == '(' || c == ')' {
1327 search.push('\\')
1328 }
1329 search.push(c)
1330 }
1331 }
1332
1333 let command: String = chars.collect();
1334
1335 let action = WrappedAction(
1336 command_interceptor(&command, cx)
1337 .first()?
1338 .action
1339 .boxed_clone(),
1340 );
1341
1342 Some(Self {
1343 range,
1344 search,
1345 invert,
1346 action,
1347 })
1348 }
1349
1350 pub fn run(&self, vim: &mut Vim, window: &mut Window, cx: &mut Context<Vim>) {
1351 let result = vim.update_editor(window, cx, |vim, editor, window, cx| {
1352 self.range.buffer_range(vim, editor, window, cx)
1353 });
1354
1355 let range = match result {
1356 None => return,
1357 Some(e @ Err(_)) => {
1358 let Some(workspace) = vim.workspace(window) else {
1359 return;
1360 };
1361 workspace.update(cx, |workspace, cx| {
1362 e.notify_err(workspace, cx);
1363 });
1364 return;
1365 }
1366 Some(Ok(result)) => result,
1367 };
1368
1369 let mut action = self.action.boxed_clone();
1370 let mut last_pattern = self.search.clone();
1371
1372 let mut regexes = match Regex::new(&self.search) {
1373 Ok(regex) => vec![(regex, !self.invert)],
1374 e @ Err(_) => {
1375 let Some(workspace) = vim.workspace(window) else {
1376 return;
1377 };
1378 workspace.update(cx, |workspace, cx| {
1379 e.notify_err(workspace, cx);
1380 });
1381 return;
1382 }
1383 };
1384 while let Some(inner) = action
1385 .boxed_clone()
1386 .as_any()
1387 .downcast_ref::<OnMatchingLines>()
1388 {
1389 let Some(regex) = Regex::new(&inner.search).ok() else {
1390 break;
1391 };
1392 last_pattern = inner.search.clone();
1393 action = inner.action.boxed_clone();
1394 regexes.push((regex, !inner.invert))
1395 }
1396
1397 if let Some(pane) = vim.pane(window, cx) {
1398 pane.update(cx, |pane, cx| {
1399 if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>()
1400 {
1401 search_bar.update(cx, |search_bar, cx| {
1402 if search_bar.show(window, cx) {
1403 let _ = search_bar.search(
1404 &last_pattern,
1405 Some(SearchOptions::REGEX | SearchOptions::CASE_SENSITIVE),
1406 window,
1407 cx,
1408 );
1409 }
1410 });
1411 }
1412 });
1413 };
1414
1415 vim.update_editor(window, cx, |_, editor, window, cx| {
1416 let snapshot = editor.snapshot(window, cx);
1417 let mut row = range.start.0;
1418
1419 let point_range = Point::new(range.start.0, 0)
1420 ..snapshot
1421 .buffer_snapshot
1422 .clip_point(Point::new(range.end.0 + 1, 0), Bias::Left);
1423 cx.spawn_in(window, async move |editor, cx| {
1424 let new_selections = cx
1425 .background_spawn(async move {
1426 let mut line = String::new();
1427 let mut new_selections = Vec::new();
1428 let chunks = snapshot
1429 .buffer_snapshot
1430 .text_for_range(point_range)
1431 .chain(["\n"]);
1432
1433 for chunk in chunks {
1434 for (newline_ix, text) in chunk.split('\n').enumerate() {
1435 if newline_ix > 0 {
1436 if regexes.iter().all(|(regex, should_match)| {
1437 regex.is_match(&line) == *should_match
1438 }) {
1439 new_selections
1440 .push(Point::new(row, 0).to_display_point(&snapshot))
1441 }
1442 row += 1;
1443 line.clear();
1444 }
1445 line.push_str(text)
1446 }
1447 }
1448
1449 new_selections
1450 })
1451 .await;
1452
1453 if new_selections.is_empty() {
1454 return;
1455 }
1456 editor
1457 .update_in(cx, |editor, window, cx| {
1458 editor.start_transaction_at(Instant::now(), window, cx);
1459 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1460 s.replace_cursors_with(|_| new_selections);
1461 });
1462 window.dispatch_action(action, cx);
1463 cx.defer_in(window, move |editor, window, cx| {
1464 let newest = editor.selections.newest::<Point>(cx).clone();
1465 editor.change_selections(
1466 SelectionEffects::no_scroll(),
1467 window,
1468 cx,
1469 |s| {
1470 s.select(vec![newest]);
1471 },
1472 );
1473 editor.end_transaction_at(Instant::now(), cx);
1474 })
1475 })
1476 .ok();
1477 })
1478 .detach();
1479 });
1480 }
1481}
1482
1483#[derive(Clone, Debug, PartialEq, Action)]
1484#[action(namespace = vim, no_json, no_register)]
1485pub struct ShellExec {
1486 command: String,
1487 range: Option<CommandRange>,
1488 is_read: bool,
1489}
1490
1491impl Vim {
1492 pub fn cancel_running_command(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1493 if self.running_command.take().is_some() {
1494 self.update_editor(window, cx, |_, editor, window, cx| {
1495 editor.transact(window, cx, |editor, _window, _cx| {
1496 editor.clear_row_highlights::<ShellExec>();
1497 })
1498 });
1499 }
1500 }
1501
1502 fn prepare_shell_command(
1503 &mut self,
1504 command: &str,
1505 window: &mut Window,
1506 cx: &mut Context<Self>,
1507 ) -> String {
1508 let mut ret = String::new();
1509 // N.B. non-standard escaping rules:
1510 // * !echo % => "echo README.md"
1511 // * !echo \% => "echo %"
1512 // * !echo \\% => echo \%
1513 // * !echo \\\% => echo \\%
1514 for c in command.chars() {
1515 if c != '%' && c != '!' {
1516 ret.push(c);
1517 continue;
1518 } else if ret.chars().last() == Some('\\') {
1519 ret.pop();
1520 ret.push(c);
1521 continue;
1522 }
1523 match c {
1524 '%' => {
1525 self.update_editor(window, cx, |_, editor, _window, cx| {
1526 if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
1527 if let Some(file) = buffer.read(cx).file() {
1528 if let Some(local) = file.as_local() {
1529 if let Some(str) = local.path().to_str() {
1530 ret.push_str(str)
1531 }
1532 }
1533 }
1534 }
1535 });
1536 }
1537 '!' => {
1538 if let Some(command) = &self.last_command {
1539 ret.push_str(command)
1540 }
1541 }
1542 _ => {}
1543 }
1544 }
1545 self.last_command = Some(ret.clone());
1546 ret
1547 }
1548
1549 pub fn shell_command_motion(
1550 &mut self,
1551 motion: Motion,
1552 times: Option<usize>,
1553 forced_motion: bool,
1554 window: &mut Window,
1555 cx: &mut Context<Vim>,
1556 ) {
1557 self.stop_recording(cx);
1558 let Some(workspace) = self.workspace(window) else {
1559 return;
1560 };
1561 let command = self.update_editor(window, cx, |_, editor, window, cx| {
1562 let snapshot = editor.snapshot(window, cx);
1563 let start = editor.selections.newest_display(cx);
1564 let text_layout_details = editor.text_layout_details(window);
1565 let (mut range, _) = motion
1566 .range(
1567 &snapshot,
1568 start.clone(),
1569 times,
1570 &text_layout_details,
1571 forced_motion,
1572 )
1573 .unwrap_or((start.range(), MotionKind::Exclusive));
1574 if range.start != start.start {
1575 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1576 s.select_ranges([
1577 range.start.to_point(&snapshot)..range.start.to_point(&snapshot)
1578 ]);
1579 })
1580 }
1581 if range.end.row() > range.start.row() && range.end.column() != 0 {
1582 *range.end.row_mut() -= 1
1583 }
1584 if range.end.row() == range.start.row() {
1585 ".!".to_string()
1586 } else {
1587 format!(".,.+{}!", (range.end.row() - range.start.row()).0)
1588 }
1589 });
1590 if let Some(command) = command {
1591 workspace.update(cx, |workspace, cx| {
1592 command_palette::CommandPalette::toggle(workspace, &command, window, cx);
1593 });
1594 }
1595 }
1596
1597 pub fn shell_command_object(
1598 &mut self,
1599 object: Object,
1600 around: bool,
1601 window: &mut Window,
1602 cx: &mut Context<Vim>,
1603 ) {
1604 self.stop_recording(cx);
1605 let Some(workspace) = self.workspace(window) else {
1606 return;
1607 };
1608 let command = self.update_editor(window, cx, |_, editor, window, cx| {
1609 let snapshot = editor.snapshot(window, cx);
1610 let start = editor.selections.newest_display(cx);
1611 let range = object
1612 .range(&snapshot, start.clone(), around)
1613 .unwrap_or(start.range());
1614 if range.start != start.start {
1615 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
1616 s.select_ranges([
1617 range.start.to_point(&snapshot)..range.start.to_point(&snapshot)
1618 ]);
1619 })
1620 }
1621 if range.end.row() == range.start.row() {
1622 ".!".to_string()
1623 } else {
1624 format!(".,.+{}!", (range.end.row() - range.start.row()).0)
1625 }
1626 });
1627 if let Some(command) = command {
1628 workspace.update(cx, |workspace, cx| {
1629 command_palette::CommandPalette::toggle(workspace, &command, window, cx);
1630 });
1631 }
1632 }
1633}
1634
1635impl ShellExec {
1636 pub fn parse(query: &str, range: Option<CommandRange>) -> Option<Box<dyn Action>> {
1637 let (before, after) = query.split_once('!')?;
1638 let before = before.trim();
1639
1640 if !"read".starts_with(before) {
1641 return None;
1642 }
1643
1644 Some(
1645 ShellExec {
1646 command: after.trim().to_string(),
1647 range,
1648 is_read: !before.is_empty(),
1649 }
1650 .boxed_clone(),
1651 )
1652 }
1653
1654 pub fn run(&self, vim: &mut Vim, window: &mut Window, cx: &mut Context<Vim>) {
1655 let Some(workspace) = vim.workspace(window) else {
1656 return;
1657 };
1658
1659 let project = workspace.read(cx).project().clone();
1660 let command = vim.prepare_shell_command(&self.command, window, cx);
1661
1662 if self.range.is_none() && !self.is_read {
1663 workspace.update(cx, |workspace, cx| {
1664 let project = workspace.project().read(cx);
1665 let cwd = project.first_project_directory(cx);
1666 let shell = project.terminal_settings(&cwd, cx).shell.clone();
1667
1668 let spawn_in_terminal = SpawnInTerminal {
1669 id: TaskId("vim".to_string()),
1670 full_label: command.clone(),
1671 label: command.clone(),
1672 command: command.clone(),
1673 args: Vec::new(),
1674 command_label: command.clone(),
1675 cwd,
1676 env: HashMap::default(),
1677 use_new_terminal: true,
1678 allow_concurrent_runs: true,
1679 reveal: RevealStrategy::NoFocus,
1680 reveal_target: RevealTarget::Dock,
1681 hide: HideStrategy::Never,
1682 shell,
1683 show_summary: false,
1684 show_command: false,
1685 show_rerun: false,
1686 };
1687
1688 let task_status = workspace.spawn_in_terminal(spawn_in_terminal, window, cx);
1689 cx.background_spawn(async move {
1690 match task_status.await {
1691 Some(Ok(status)) => {
1692 if status.success() {
1693 log::debug!("Vim shell exec succeeded");
1694 } else {
1695 log::debug!("Vim shell exec failed, code: {:?}", status.code());
1696 }
1697 }
1698 Some(Err(e)) => log::error!("Vim shell exec failed: {e}"),
1699 None => log::debug!("Vim shell exec got cancelled"),
1700 }
1701 })
1702 .detach();
1703 });
1704 return;
1705 };
1706
1707 let mut input_snapshot = None;
1708 let mut input_range = None;
1709 let mut needs_newline_prefix = false;
1710 vim.update_editor(window, cx, |vim, editor, window, cx| {
1711 let snapshot = editor.buffer().read(cx).snapshot(cx);
1712 let range = if let Some(range) = self.range.clone() {
1713 let Some(range) = range.buffer_range(vim, editor, window, cx).log_err() else {
1714 return;
1715 };
1716 Point::new(range.start.0, 0)
1717 ..snapshot.clip_point(Point::new(range.end.0 + 1, 0), Bias::Right)
1718 } else {
1719 let mut end = editor.selections.newest::<Point>(cx).range().end;
1720 end = snapshot.clip_point(Point::new(end.row + 1, 0), Bias::Right);
1721 needs_newline_prefix = end == snapshot.max_point();
1722 end..end
1723 };
1724 if self.is_read {
1725 input_range =
1726 Some(snapshot.anchor_after(range.end)..snapshot.anchor_after(range.end));
1727 } else {
1728 input_range =
1729 Some(snapshot.anchor_before(range.start)..snapshot.anchor_after(range.end));
1730 }
1731 editor.highlight_rows::<ShellExec>(
1732 input_range.clone().unwrap(),
1733 cx.theme().status().unreachable_background,
1734 Default::default(),
1735 cx,
1736 );
1737
1738 if !self.is_read {
1739 input_snapshot = Some(snapshot)
1740 }
1741 });
1742
1743 let Some(range) = input_range else { return };
1744
1745 let mut process = project.read(cx).exec_in_shell(command, cx);
1746 process.stdout(Stdio::piped());
1747 process.stderr(Stdio::piped());
1748
1749 if input_snapshot.is_some() {
1750 process.stdin(Stdio::piped());
1751 } else {
1752 process.stdin(Stdio::null());
1753 };
1754
1755 util::set_pre_exec_to_start_new_session(&mut process);
1756 let is_read = self.is_read;
1757
1758 let task = cx.spawn_in(window, async move |vim, cx| {
1759 let Some(mut running) = process.spawn().log_err() else {
1760 vim.update_in(cx, |vim, window, cx| {
1761 vim.cancel_running_command(window, cx);
1762 })
1763 .log_err();
1764 return;
1765 };
1766
1767 if let Some(mut stdin) = running.stdin.take() {
1768 if let Some(snapshot) = input_snapshot {
1769 let range = range.clone();
1770 cx.background_spawn(async move {
1771 for chunk in snapshot.text_for_range(range) {
1772 if stdin.write_all(chunk.as_bytes()).log_err().is_none() {
1773 return;
1774 }
1775 }
1776 stdin.flush().log_err();
1777 })
1778 .detach();
1779 }
1780 };
1781
1782 let output = cx
1783 .background_spawn(async move { running.wait_with_output() })
1784 .await;
1785
1786 let Some(output) = output.log_err() else {
1787 vim.update_in(cx, |vim, window, cx| {
1788 vim.cancel_running_command(window, cx);
1789 })
1790 .log_err();
1791 return;
1792 };
1793 let mut text = String::new();
1794 if needs_newline_prefix {
1795 text.push('\n');
1796 }
1797 text.push_str(&String::from_utf8_lossy(&output.stdout));
1798 text.push_str(&String::from_utf8_lossy(&output.stderr));
1799 if !text.is_empty() && text.chars().last() != Some('\n') {
1800 text.push('\n');
1801 }
1802
1803 vim.update_in(cx, |vim, window, cx| {
1804 vim.update_editor(window, cx, |_, editor, window, cx| {
1805 editor.transact(window, cx, |editor, window, cx| {
1806 editor.edit([(range.clone(), text)], cx);
1807 let snapshot = editor.buffer().read(cx).snapshot(cx);
1808 editor.change_selections(Default::default(), window, cx, |s| {
1809 let point = if is_read {
1810 let point = range.end.to_point(&snapshot);
1811 Point::new(point.row.saturating_sub(1), 0)
1812 } else {
1813 let point = range.start.to_point(&snapshot);
1814 Point::new(point.row, 0)
1815 };
1816 s.select_ranges([point..point]);
1817 })
1818 })
1819 });
1820 vim.cancel_running_command(window, cx);
1821 })
1822 .log_err();
1823 });
1824 vim.running_command.replace(task);
1825 }
1826}
1827
1828#[cfg(test)]
1829mod test {
1830 use std::path::Path;
1831
1832 use crate::{
1833 VimAddon,
1834 state::Mode,
1835 test::{NeovimBackedTestContext, VimTestContext},
1836 };
1837 use editor::Editor;
1838 use gpui::{Context, TestAppContext};
1839 use indoc::indoc;
1840 use util::path;
1841 use workspace::Workspace;
1842
1843 #[gpui::test]
1844 async fn test_command_basics(cx: &mut TestAppContext) {
1845 let mut cx = NeovimBackedTestContext::new(cx).await;
1846
1847 cx.set_shared_state(indoc! {"
1848 ˇa
1849 b
1850 c"})
1851 .await;
1852
1853 cx.simulate_shared_keystrokes(": j enter").await;
1854
1855 // hack: our cursor positioning after a join command is wrong
1856 cx.simulate_shared_keystrokes("^").await;
1857 cx.shared_state().await.assert_eq(indoc! {
1858 "ˇa b
1859 c"
1860 });
1861 }
1862
1863 #[gpui::test]
1864 async fn test_command_goto(cx: &mut TestAppContext) {
1865 let mut cx = NeovimBackedTestContext::new(cx).await;
1866
1867 cx.set_shared_state(indoc! {"
1868 ˇa
1869 b
1870 c"})
1871 .await;
1872 cx.simulate_shared_keystrokes(": 3 enter").await;
1873 cx.shared_state().await.assert_eq(indoc! {"
1874 a
1875 b
1876 ˇc"});
1877 }
1878
1879 #[gpui::test]
1880 async fn test_command_replace(cx: &mut TestAppContext) {
1881 let mut cx = NeovimBackedTestContext::new(cx).await;
1882
1883 cx.set_shared_state(indoc! {"
1884 ˇa
1885 b
1886 b
1887 c"})
1888 .await;
1889 cx.simulate_shared_keystrokes(": % s / b / d enter").await;
1890 cx.shared_state().await.assert_eq(indoc! {"
1891 a
1892 d
1893 ˇd
1894 c"});
1895 cx.simulate_shared_keystrokes(": % s : . : \\ 0 \\ 0 enter")
1896 .await;
1897 cx.shared_state().await.assert_eq(indoc! {"
1898 aa
1899 dd
1900 dd
1901 ˇcc"});
1902 cx.simulate_shared_keystrokes("k : s / d d / e e enter")
1903 .await;
1904 cx.shared_state().await.assert_eq(indoc! {"
1905 aa
1906 dd
1907 ˇee
1908 cc"});
1909 }
1910
1911 #[gpui::test]
1912 async fn test_command_search(cx: &mut TestAppContext) {
1913 let mut cx = NeovimBackedTestContext::new(cx).await;
1914
1915 cx.set_shared_state(indoc! {"
1916 ˇa
1917 b
1918 a
1919 c"})
1920 .await;
1921 cx.simulate_shared_keystrokes(": / b enter").await;
1922 cx.shared_state().await.assert_eq(indoc! {"
1923 a
1924 ˇb
1925 a
1926 c"});
1927 cx.simulate_shared_keystrokes(": ? a enter").await;
1928 cx.shared_state().await.assert_eq(indoc! {"
1929 ˇa
1930 b
1931 a
1932 c"});
1933 }
1934
1935 #[gpui::test]
1936 async fn test_command_write(cx: &mut TestAppContext) {
1937 let mut cx = VimTestContext::new(cx, true).await;
1938 let path = Path::new(path!("/root/dir/file.rs"));
1939 let fs = cx.workspace(|workspace, _, cx| workspace.project().read(cx).fs().clone());
1940
1941 cx.simulate_keystrokes("i @ escape");
1942 cx.simulate_keystrokes(": w enter");
1943
1944 assert_eq!(fs.load(path).await.unwrap().replace("\r\n", "\n"), "@\n");
1945
1946 fs.as_fake().insert_file(path, b"oops\n".to_vec()).await;
1947
1948 // conflict!
1949 cx.simulate_keystrokes("i @ escape");
1950 cx.simulate_keystrokes(": w enter");
1951 cx.simulate_prompt_answer("Cancel");
1952
1953 assert_eq!(fs.load(path).await.unwrap().replace("\r\n", "\n"), "oops\n");
1954 assert!(!cx.has_pending_prompt());
1955 cx.simulate_keystrokes(": w ! enter");
1956 assert!(!cx.has_pending_prompt());
1957 assert_eq!(fs.load(path).await.unwrap().replace("\r\n", "\n"), "@@\n");
1958 }
1959
1960 #[gpui::test]
1961 async fn test_command_quit(cx: &mut TestAppContext) {
1962 let mut cx = VimTestContext::new(cx, true).await;
1963
1964 cx.simulate_keystrokes(": n e w enter");
1965 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 2));
1966 cx.simulate_keystrokes(": q enter");
1967 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 1));
1968 cx.simulate_keystrokes(": n e w enter");
1969 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 2));
1970 cx.simulate_keystrokes(": q a enter");
1971 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 0));
1972 }
1973
1974 #[gpui::test]
1975 async fn test_offsets(cx: &mut TestAppContext) {
1976 let mut cx = NeovimBackedTestContext::new(cx).await;
1977
1978 cx.set_shared_state("ˇ1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n")
1979 .await;
1980
1981 cx.simulate_shared_keystrokes(": + enter").await;
1982 cx.shared_state()
1983 .await
1984 .assert_eq("1\nˇ2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n");
1985
1986 cx.simulate_shared_keystrokes(": 1 0 - enter").await;
1987 cx.shared_state()
1988 .await
1989 .assert_eq("1\n2\n3\n4\n5\n6\n7\n8\nˇ9\n10\n11\n");
1990
1991 cx.simulate_shared_keystrokes(": . - 2 enter").await;
1992 cx.shared_state()
1993 .await
1994 .assert_eq("1\n2\n3\n4\n5\n6\nˇ7\n8\n9\n10\n11\n");
1995
1996 cx.simulate_shared_keystrokes(": % enter").await;
1997 cx.shared_state()
1998 .await
1999 .assert_eq("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\nˇ");
2000 }
2001
2002 #[gpui::test]
2003 async fn test_command_ranges(cx: &mut TestAppContext) {
2004 let mut cx = NeovimBackedTestContext::new(cx).await;
2005
2006 cx.set_shared_state("ˇ1\n2\n3\n4\n4\n3\n2\n1").await;
2007
2008 cx.simulate_shared_keystrokes(": 2 , 4 d enter").await;
2009 cx.shared_state().await.assert_eq("1\nˇ4\n3\n2\n1");
2010
2011 cx.simulate_shared_keystrokes(": 2 , 4 s o r t enter").await;
2012 cx.shared_state().await.assert_eq("1\nˇ2\n3\n4\n1");
2013
2014 cx.simulate_shared_keystrokes(": 2 , 4 j o i n enter").await;
2015 cx.shared_state().await.assert_eq("1\nˇ2 3 4\n1");
2016 }
2017
2018 #[gpui::test]
2019 async fn test_command_visual_replace(cx: &mut TestAppContext) {
2020 let mut cx = NeovimBackedTestContext::new(cx).await;
2021
2022 cx.set_shared_state("ˇ1\n2\n3\n4\n4\n3\n2\n1").await;
2023
2024 cx.simulate_shared_keystrokes("v 2 j : s / . / k enter")
2025 .await;
2026 cx.shared_state().await.assert_eq("k\nk\nˇk\n4\n4\n3\n2\n1");
2027 }
2028
2029 #[track_caller]
2030 fn assert_active_item(
2031 workspace: &mut Workspace,
2032 expected_path: &str,
2033 expected_text: &str,
2034 cx: &mut Context<Workspace>,
2035 ) {
2036 let active_editor = workspace.active_item_as::<Editor>(cx).unwrap();
2037
2038 let buffer = active_editor
2039 .read(cx)
2040 .buffer()
2041 .read(cx)
2042 .as_singleton()
2043 .unwrap();
2044
2045 let text = buffer.read(cx).text();
2046 let file = buffer.read(cx).file().unwrap();
2047 let file_path = file.as_local().unwrap().abs_path(cx);
2048
2049 assert_eq!(text, expected_text);
2050 assert_eq!(file_path, Path::new(expected_path));
2051 }
2052
2053 #[gpui::test]
2054 async fn test_command_gf(cx: &mut TestAppContext) {
2055 let mut cx = VimTestContext::new(cx, true).await;
2056
2057 // Assert base state, that we're in /root/dir/file.rs
2058 cx.workspace(|workspace, _, cx| {
2059 assert_active_item(workspace, path!("/root/dir/file.rs"), "", cx);
2060 });
2061
2062 // Insert a new file
2063 let fs = cx.workspace(|workspace, _, cx| workspace.project().read(cx).fs().clone());
2064 fs.as_fake()
2065 .insert_file(
2066 path!("/root/dir/file2.rs"),
2067 "This is file2.rs".as_bytes().to_vec(),
2068 )
2069 .await;
2070 fs.as_fake()
2071 .insert_file(
2072 path!("/root/dir/file3.rs"),
2073 "go to file3".as_bytes().to_vec(),
2074 )
2075 .await;
2076
2077 // Put the path to the second file into the currently open buffer
2078 cx.set_state(indoc! {"go to fiˇle2.rs"}, Mode::Normal);
2079
2080 // Go to file2.rs
2081 cx.simulate_keystrokes("g f");
2082
2083 // We now have two items
2084 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 2));
2085 cx.workspace(|workspace, _, cx| {
2086 assert_active_item(
2087 workspace,
2088 path!("/root/dir/file2.rs"),
2089 "This is file2.rs",
2090 cx,
2091 );
2092 });
2093
2094 // Update editor to point to `file2.rs`
2095 cx.editor =
2096 cx.workspace(|workspace, _, cx| workspace.active_item_as::<Editor>(cx).unwrap());
2097
2098 // Put the path to the third file into the currently open buffer,
2099 // but remove its suffix, because we want that lookup to happen automatically.
2100 cx.set_state(indoc! {"go to fiˇle3"}, Mode::Normal);
2101
2102 // Go to file3.rs
2103 cx.simulate_keystrokes("g f");
2104
2105 // We now have three items
2106 cx.workspace(|workspace, _, cx| assert_eq!(workspace.items(cx).count(), 3));
2107 cx.workspace(|workspace, _, cx| {
2108 assert_active_item(workspace, path!("/root/dir/file3.rs"), "go to file3", cx);
2109 });
2110 }
2111
2112 #[gpui::test]
2113 async fn test_w_command(cx: &mut TestAppContext) {
2114 let mut cx = VimTestContext::new(cx, true).await;
2115
2116 cx.workspace(|workspace, _, cx| {
2117 assert_active_item(workspace, path!("/root/dir/file.rs"), "", cx);
2118 });
2119
2120 cx.simulate_keystrokes(": w space other.rs");
2121 cx.simulate_keystrokes("enter");
2122
2123 cx.workspace(|workspace, _, cx| {
2124 assert_active_item(workspace, path!("/root/other.rs"), "", cx);
2125 });
2126
2127 cx.simulate_keystrokes(": w space dir/file.rs");
2128 cx.simulate_keystrokes("enter");
2129
2130 cx.simulate_prompt_answer("Replace");
2131 cx.run_until_parked();
2132
2133 cx.workspace(|workspace, _, cx| {
2134 assert_active_item(workspace, path!("/root/dir/file.rs"), "", cx);
2135 });
2136
2137 cx.simulate_keystrokes(": w ! space other.rs");
2138 cx.simulate_keystrokes("enter");
2139
2140 cx.workspace(|workspace, _, cx| {
2141 assert_active_item(workspace, path!("/root/other.rs"), "", cx);
2142 });
2143 }
2144
2145 #[gpui::test]
2146 async fn test_command_matching_lines(cx: &mut TestAppContext) {
2147 let mut cx = NeovimBackedTestContext::new(cx).await;
2148
2149 cx.set_shared_state(indoc! {"
2150 ˇa
2151 b
2152 a
2153 b
2154 a
2155 "})
2156 .await;
2157
2158 cx.simulate_shared_keystrokes(":").await;
2159 cx.simulate_shared_keystrokes("g / a / d").await;
2160 cx.simulate_shared_keystrokes("enter").await;
2161
2162 cx.shared_state().await.assert_eq(indoc! {"
2163 b
2164 b
2165 ˇ"});
2166
2167 cx.simulate_shared_keystrokes("u").await;
2168
2169 cx.shared_state().await.assert_eq(indoc! {"
2170 ˇa
2171 b
2172 a
2173 b
2174 a
2175 "});
2176
2177 cx.simulate_shared_keystrokes(":").await;
2178 cx.simulate_shared_keystrokes("v / a / d").await;
2179 cx.simulate_shared_keystrokes("enter").await;
2180
2181 cx.shared_state().await.assert_eq(indoc! {"
2182 a
2183 a
2184 ˇa"});
2185 }
2186
2187 #[gpui::test]
2188 async fn test_del_marks(cx: &mut TestAppContext) {
2189 let mut cx = NeovimBackedTestContext::new(cx).await;
2190
2191 cx.set_shared_state(indoc! {"
2192 ˇa
2193 b
2194 a
2195 b
2196 a
2197 "})
2198 .await;
2199
2200 cx.simulate_shared_keystrokes("m a").await;
2201
2202 let mark = cx.update_editor(|editor, window, cx| {
2203 let vim = editor.addon::<VimAddon>().unwrap().entity.clone();
2204 vim.update(cx, |vim, cx| vim.get_mark("a", editor, window, cx))
2205 });
2206 assert!(mark.is_some());
2207
2208 cx.simulate_shared_keystrokes(": d e l m space a").await;
2209 cx.simulate_shared_keystrokes("enter").await;
2210
2211 let mark = cx.update_editor(|editor, window, cx| {
2212 let vim = editor.addon::<VimAddon>().unwrap().entity.clone();
2213 vim.update(cx, |vim, cx| vim.get_mark("a", editor, window, cx))
2214 });
2215 assert!(mark.is_none())
2216 }
2217}