1use command_palette_hooks::CommandInterceptResult;
2use editor::actions::{SortLinesCaseInsensitive, SortLinesCaseSensitive};
3use gpui::{impl_actions, Action, AppContext, ViewContext};
4use serde_derive::Deserialize;
5use workspace::{SaveIntent, Workspace};
6
7use crate::{
8 motion::{EndOfDocument, Motion, StartOfDocument},
9 normal::{
10 move_cursor,
11 search::{FindCommand, ReplaceCommand},
12 JoinLines,
13 },
14 state::Mode,
15 Vim,
16};
17
18#[derive(Debug, Clone, PartialEq, Deserialize)]
19pub struct GoToLine {
20 pub line: u32,
21}
22
23impl_actions!(vim, [GoToLine]);
24
25pub fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
26 workspace.register_action(|_: &mut Workspace, action: &GoToLine, cx| {
27 Vim::update(cx, |vim, cx| {
28 vim.switch_mode(Mode::Normal, false, cx);
29 move_cursor(vim, Motion::StartOfDocument, Some(action.line as usize), cx);
30 });
31 });
32}
33
34pub fn command_interceptor(mut query: &str, cx: &AppContext) -> Option<CommandInterceptResult> {
35 // Note: this is a very poor simulation of vim's command palette.
36 // In the future we should adjust it to handle parsing range syntax,
37 // and then calling the appropriate commands with/without ranges.
38 //
39 // We also need to support passing arguments to commands like :w
40 // (ideally with filename autocompletion).
41 //
42 // For now, you can only do a replace on the % range, and you can
43 // only use a specific line number range to "go to line"
44 while query.starts_with(':') {
45 query = &query[1..];
46 }
47
48 let (name, action) = match query {
49 // save and quit
50 "w" | "wr" | "wri" | "writ" | "write" => (
51 "write",
52 workspace::Save {
53 save_intent: Some(SaveIntent::Save),
54 }
55 .boxed_clone(),
56 ),
57 "w!" | "wr!" | "wri!" | "writ!" | "write!" => (
58 "write!",
59 workspace::Save {
60 save_intent: Some(SaveIntent::Overwrite),
61 }
62 .boxed_clone(),
63 ),
64 "q" | "qu" | "qui" | "quit" => (
65 "quit",
66 workspace::CloseActiveItem {
67 save_intent: Some(SaveIntent::Close),
68 }
69 .boxed_clone(),
70 ),
71 "q!" | "qu!" | "qui!" | "quit!" => (
72 "quit!",
73 workspace::CloseActiveItem {
74 save_intent: Some(SaveIntent::Skip),
75 }
76 .boxed_clone(),
77 ),
78 "wq" => (
79 "wq",
80 workspace::CloseActiveItem {
81 save_intent: Some(SaveIntent::Save),
82 }
83 .boxed_clone(),
84 ),
85 "wq!" => (
86 "wq!",
87 workspace::CloseActiveItem {
88 save_intent: Some(SaveIntent::Overwrite),
89 }
90 .boxed_clone(),
91 ),
92 "x" | "xi" | "xit" | "exi" | "exit" => (
93 "exit",
94 workspace::CloseActiveItem {
95 save_intent: Some(SaveIntent::SaveAll),
96 }
97 .boxed_clone(),
98 ),
99 "x!" | "xi!" | "xit!" | "exi!" | "exit!" => (
100 "exit!",
101 workspace::CloseActiveItem {
102 save_intent: Some(SaveIntent::Overwrite),
103 }
104 .boxed_clone(),
105 ),
106 "up" | "upd" | "upda" | "updat" | "update" => (
107 "update",
108 workspace::Save {
109 save_intent: Some(SaveIntent::SaveAll),
110 }
111 .boxed_clone(),
112 ),
113 "wa" | "wal" | "wall" => (
114 "wall",
115 workspace::SaveAll {
116 save_intent: Some(SaveIntent::SaveAll),
117 }
118 .boxed_clone(),
119 ),
120 "wa!" | "wal!" | "wall!" => (
121 "wall!",
122 workspace::SaveAll {
123 save_intent: Some(SaveIntent::Overwrite),
124 }
125 .boxed_clone(),
126 ),
127 "qa" | "qal" | "qall" | "quita" | "quital" | "quitall" => (
128 "quitall",
129 workspace::CloseAllItemsAndPanes {
130 save_intent: Some(SaveIntent::Close),
131 }
132 .boxed_clone(),
133 ),
134 "qa!" | "qal!" | "qall!" | "quita!" | "quital!" | "quitall!" => (
135 "quitall!",
136 workspace::CloseAllItemsAndPanes {
137 save_intent: Some(SaveIntent::Skip),
138 }
139 .boxed_clone(),
140 ),
141 "xa" | "xal" | "xall" => (
142 "xall",
143 workspace::CloseAllItemsAndPanes {
144 save_intent: Some(SaveIntent::SaveAll),
145 }
146 .boxed_clone(),
147 ),
148 "xa!" | "xal!" | "xall!" => (
149 "xall!",
150 workspace::CloseAllItemsAndPanes {
151 save_intent: Some(SaveIntent::Overwrite),
152 }
153 .boxed_clone(),
154 ),
155 "wqa" | "wqal" | "wqall" => (
156 "wqall",
157 workspace::CloseAllItemsAndPanes {
158 save_intent: Some(SaveIntent::SaveAll),
159 }
160 .boxed_clone(),
161 ),
162 "wqa!" | "wqal!" | "wqall!" => (
163 "wqall!",
164 workspace::CloseAllItemsAndPanes {
165 save_intent: Some(SaveIntent::Overwrite),
166 }
167 .boxed_clone(),
168 ),
169 "cq" | "cqu" | "cqui" | "cquit" | "cq!" | "cqu!" | "cqui!" | "cquit!" => {
170 ("cquit!", zed_actions::Quit.boxed_clone())
171 }
172
173 // pane management
174 "sp" | "spl" | "spli" | "split" => ("split", workspace::SplitUp.boxed_clone()),
175 "vs" | "vsp" | "vspl" | "vspli" | "vsplit" => {
176 ("vsplit", workspace::SplitLeft.boxed_clone())
177 }
178 "new" => (
179 "new",
180 workspace::NewFileInDirection(workspace::SplitDirection::Up).boxed_clone(),
181 ),
182 "vne" | "vnew" => (
183 "vnew",
184 workspace::NewFileInDirection(workspace::SplitDirection::Left).boxed_clone(),
185 ),
186 "tabe" | "tabed" | "tabedi" | "tabedit" => ("tabedit", workspace::NewFile.boxed_clone()),
187 "tabnew" => ("tabnew", workspace::NewFile.boxed_clone()),
188
189 "tabn" | "tabne" | "tabnex" | "tabnext" => {
190 ("tabnext", workspace::ActivateNextItem.boxed_clone())
191 }
192 "tabp" | "tabpr" | "tabpre" | "tabprev" | "tabprevi" | "tabprevio" | "tabpreviou"
193 | "tabprevious" => ("tabprevious", workspace::ActivatePrevItem.boxed_clone()),
194 "tabN" | "tabNe" | "tabNex" | "tabNext" => {
195 ("tabNext", workspace::ActivatePrevItem.boxed_clone())
196 }
197 "tabc" | "tabcl" | "tabclo" | "tabclos" | "tabclose" => (
198 "tabclose",
199 workspace::CloseActiveItem {
200 save_intent: Some(SaveIntent::Close),
201 }
202 .boxed_clone(),
203 ),
204 "tabo" | "tabon" | "tabonl" | "tabonly" => (
205 "tabonly",
206 workspace::CloseInactiveItems {
207 save_intent: Some(SaveIntent::Close),
208 }
209 .boxed_clone(),
210 ),
211 "tabo!" | "tabon!" | "tabonl!" | "tabonly!" => (
212 "tabonly!",
213 workspace::CloseInactiveItems {
214 save_intent: Some(SaveIntent::Skip),
215 }
216 .boxed_clone(),
217 ),
218 "on" | "onl" | "only" => (
219 "only",
220 workspace::CloseInactiveTabsAndPanes {
221 save_intent: Some(SaveIntent::Close),
222 }
223 .boxed_clone(),
224 ),
225 "on!" | "onl!" | "only!" => (
226 "only!",
227 workspace::CloseInactiveTabsAndPanes {
228 save_intent: Some(SaveIntent::Skip),
229 }
230 .boxed_clone(),
231 ),
232
233 // quickfix / loclist (merged together for now)
234 "cl" | "cli" | "clis" | "clist" => (
235 "clist",
236 cx.build_action("diagnostics::Deploy", None).unwrap(),
237 ),
238 "cc" => ("cc", editor::actions::Hover.boxed_clone()),
239 "ll" => ("ll", editor::actions::Hover.boxed_clone()),
240 "cn" | "cne" | "cnex" | "cnext" => ("cnext", editor::actions::GoToDiagnostic.boxed_clone()),
241 "lne" | "lnex" | "lnext" => ("cnext", editor::actions::GoToDiagnostic.boxed_clone()),
242
243 "cpr" | "cpre" | "cprev" | "cprevi" | "cprevio" | "cpreviou" | "cprevious" => (
244 "cprevious",
245 editor::actions::GoToPrevDiagnostic.boxed_clone(),
246 ),
247 "cN" | "cNe" | "cNex" | "cNext" => {
248 ("cNext", editor::actions::GoToPrevDiagnostic.boxed_clone())
249 }
250 "lp" | "lpr" | "lpre" | "lprev" | "lprevi" | "lprevio" | "lpreviou" | "lprevious" => (
251 "lprevious",
252 editor::actions::GoToPrevDiagnostic.boxed_clone(),
253 ),
254 "lN" | "lNe" | "lNex" | "lNext" => {
255 ("lNext", editor::actions::GoToPrevDiagnostic.boxed_clone())
256 }
257
258 // modify the buffer (should accept [range])
259 "j" | "jo" | "joi" | "join" => ("join", JoinLines.boxed_clone()),
260 "d" | "de" | "del" | "dele" | "delet" | "delete" | "dl" | "dell" | "delel" | "deletl"
261 | "deletel" | "dp" | "dep" | "delp" | "delep" | "deletp" | "deletep" => {
262 ("delete", editor::actions::DeleteLine.boxed_clone())
263 }
264 "sor" | "sor " | "sort" | "sort " => ("sort", SortLinesCaseSensitive.boxed_clone()),
265 "sor i" | "sort i" => ("sort i", SortLinesCaseInsensitive.boxed_clone()),
266
267 // Explore, etc.
268 "E" | "Ex" | "Exp" | "Expl" | "Explo" | "Explor" | "Explore" => (
269 "Explore",
270 cx.build_action("project_panel::ToggleFocus", None).unwrap(),
271 ),
272 "H" | "He" | "Hex" | "Hexp" | "Hexpl" | "Hexplo" | "Hexplor" | "Hexplore" => (
273 "Hexplore",
274 cx.build_action("project_panel::ToggleFocus", None).unwrap(),
275 ),
276 "L" | "Le" | "Lex" | "Lexp" | "Lexpl" | "Lexplo" | "Lexplor" | "Lexplore" => (
277 "Lexplore",
278 cx.build_action("project_panel::ToggleFocus", None).unwrap(),
279 ),
280 "S" | "Se" | "Sex" | "Sexp" | "Sexpl" | "Sexplo" | "Sexplor" | "Sexplore" => (
281 "Sexplore",
282 cx.build_action("project_panel::ToggleFocus", None).unwrap(),
283 ),
284 "Ve" | "Vex" | "Vexp" | "Vexpl" | "Vexplo" | "Vexplor" | "Vexplore" => (
285 "Vexplore",
286 cx.build_action("project_panel::ToggleFocus", None).unwrap(),
287 ),
288 "te" | "ter" | "term" => (
289 "term",
290 cx.build_action("terminal_panel::ToggleFocus", None)
291 .unwrap(),
292 ),
293 // Zed panes
294 "T" | "Te" | "Ter" | "Term" => (
295 "Term",
296 cx.build_action("terminal_panel::ToggleFocus", None)
297 .unwrap(),
298 ),
299 "C" | "Co" | "Col" | "Coll" | "Colla" | "Collab" => (
300 "Collab",
301 cx.build_action("collab_panel::ToggleFocus", None).unwrap(),
302 ),
303 "Ch" | "Cha" | "Chat" => (
304 "Chat",
305 cx.build_action("chat_panel::ToggleFocus", None).unwrap(),
306 ),
307 "No" | "Not" | "Noti" | "Notif" | "Notifi" | "Notific" | "Notifica" | "Notificat"
308 | "Notificati" | "Notificatio" | "Notification" => (
309 "Notifications",
310 cx.build_action("notification_panel::ToggleFocus", None)
311 .unwrap(),
312 ),
313 "A" | "AI" | "Ai" => (
314 "AI",
315 cx.build_action("assistant::ToggleFocus", None).unwrap(),
316 ),
317
318 // goto (other ranges handled under _ => )
319 "$" => ("$", EndOfDocument.boxed_clone()),
320 "%" => ("%", EndOfDocument.boxed_clone()),
321 "0" => ("0", StartOfDocument.boxed_clone()),
322
323 _ => {
324 if query.starts_with('/') || query.starts_with('?') {
325 (
326 query,
327 FindCommand {
328 query: query[1..].to_string(),
329 backwards: query.starts_with('?'),
330 }
331 .boxed_clone(),
332 )
333 } else if query.starts_with('%') {
334 (
335 query,
336 ReplaceCommand {
337 query: query.to_string(),
338 }
339 .boxed_clone(),
340 )
341 } else if let Ok(line) = query.parse::<u32>() {
342 (query, GoToLine { line }.boxed_clone())
343 } else {
344 return None;
345 }
346 }
347 };
348
349 let string = ":".to_owned() + name;
350 let positions = generate_positions(&string, query);
351
352 Some(CommandInterceptResult {
353 action,
354 string,
355 positions,
356 })
357}
358
359fn generate_positions(string: &str, query: &str) -> Vec<usize> {
360 let mut positions = Vec::new();
361 let mut chars = query.chars();
362
363 let Some(mut current) = chars.next() else {
364 return positions;
365 };
366
367 for (i, c) in string.char_indices() {
368 if c == current {
369 positions.push(i);
370 if let Some(c) = chars.next() {
371 current = c;
372 } else {
373 break;
374 }
375 }
376 }
377
378 positions
379}
380
381#[cfg(test)]
382mod test {
383 use std::path::Path;
384
385 use crate::test::{NeovimBackedTestContext, VimTestContext};
386 use gpui::TestAppContext;
387 use indoc::indoc;
388
389 #[gpui::test]
390 async fn test_command_basics(cx: &mut TestAppContext) {
391 let mut cx = NeovimBackedTestContext::new(cx).await;
392
393 cx.set_shared_state(indoc! {"
394 ˇa
395 b
396 c"})
397 .await;
398
399 cx.simulate_shared_keystrokes([":", "j", "enter"]).await;
400
401 // hack: our cursor positionining after a join command is wrong
402 cx.simulate_shared_keystrokes(["^"]).await;
403 cx.assert_shared_state(indoc! {
404 "ˇa b
405 c"
406 })
407 .await;
408 }
409
410 #[gpui::test]
411 async fn test_command_goto(cx: &mut TestAppContext) {
412 let mut cx = NeovimBackedTestContext::new(cx).await;
413
414 cx.set_shared_state(indoc! {"
415 ˇa
416 b
417 c"})
418 .await;
419 cx.simulate_shared_keystrokes([":", "3", "enter"]).await;
420 cx.assert_shared_state(indoc! {"
421 a
422 b
423 ˇc"})
424 .await;
425 }
426
427 #[gpui::test]
428 async fn test_command_replace(cx: &mut TestAppContext) {
429 let mut cx = NeovimBackedTestContext::new(cx).await;
430
431 cx.set_shared_state(indoc! {"
432 ˇa
433 b
434 c"})
435 .await;
436 cx.simulate_shared_keystrokes([":", "%", "s", "/", "b", "/", "d", "enter"])
437 .await;
438 cx.assert_shared_state(indoc! {"
439 a
440 ˇd
441 c"})
442 .await;
443 cx.simulate_shared_keystrokes([
444 ":", "%", "s", ":", ".", ":", "\\", "0", "\\", "0", "enter",
445 ])
446 .await;
447 cx.assert_shared_state(indoc! {"
448 aa
449 dd
450 ˇcc"})
451 .await;
452 }
453
454 #[gpui::test]
455 async fn test_command_search(cx: &mut TestAppContext) {
456 let mut cx = NeovimBackedTestContext::new(cx).await;
457
458 cx.set_shared_state(indoc! {"
459 ˇa
460 b
461 a
462 c"})
463 .await;
464 cx.simulate_shared_keystrokes([":", "/", "b", "enter"])
465 .await;
466 cx.assert_shared_state(indoc! {"
467 a
468 ˇb
469 a
470 c"})
471 .await;
472 cx.simulate_shared_keystrokes([":", "?", "a", "enter"])
473 .await;
474 cx.assert_shared_state(indoc! {"
475 ˇa
476 b
477 a
478 c"})
479 .await;
480 }
481
482 #[gpui::test]
483 async fn test_command_write(cx: &mut TestAppContext) {
484 let mut cx = VimTestContext::new(cx, true).await;
485 let path = Path::new("/root/dir/file.rs");
486 let fs = cx.workspace(|workspace, cx| workspace.project().read(cx).fs().clone());
487
488 cx.simulate_keystrokes(["i", "@", "escape"]);
489 cx.simulate_keystrokes([":", "w", "enter"]);
490
491 assert_eq!(fs.load(&path).await.unwrap(), "@\n");
492
493 fs.as_fake().insert_file(path, b"oops\n".to_vec()).await;
494
495 // conflict!
496 cx.simulate_keystrokes(["i", "@", "escape"]);
497 cx.simulate_keystrokes([":", "w", "enter"]);
498 assert!(cx.has_pending_prompt());
499 // "Cancel"
500 cx.simulate_prompt_answer(0);
501 assert_eq!(fs.load(&path).await.unwrap(), "oops\n");
502 assert!(!cx.has_pending_prompt());
503 // force overwrite
504 cx.simulate_keystrokes([":", "w", "!", "enter"]);
505 assert!(!cx.has_pending_prompt());
506 assert_eq!(fs.load(&path).await.unwrap(), "@@\n");
507 }
508
509 #[gpui::test]
510 async fn test_command_quit(cx: &mut TestAppContext) {
511 let mut cx = VimTestContext::new(cx, true).await;
512
513 cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
514 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
515 cx.simulate_keystrokes([":", "q", "enter"]);
516 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 1));
517 cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
518 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
519 cx.simulate_keystrokes([":", "q", "a", "enter"]);
520 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 0));
521 }
522}