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::{range_regex, 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 if range_regex().is_match(query) {
344 (
345 query,
346 ReplaceCommand {
347 query: query.to_string(),
348 }
349 .boxed_clone(),
350 )
351 } else {
352 return None;
353 }
354 }
355 };
356
357 let string = ":".to_owned() + name;
358 let positions = generate_positions(&string, query);
359
360 Some(CommandInterceptResult {
361 action,
362 string,
363 positions,
364 })
365}
366
367fn generate_positions(string: &str, query: &str) -> Vec<usize> {
368 let mut positions = Vec::new();
369 let mut chars = query.chars();
370
371 let Some(mut current) = chars.next() else {
372 return positions;
373 };
374
375 for (i, c) in string.char_indices() {
376 if c == current {
377 positions.push(i);
378 if let Some(c) = chars.next() {
379 current = c;
380 } else {
381 break;
382 }
383 }
384 }
385
386 positions
387}
388
389#[cfg(test)]
390mod test {
391 use std::path::Path;
392
393 use crate::test::{NeovimBackedTestContext, VimTestContext};
394 use gpui::TestAppContext;
395 use indoc::indoc;
396
397 #[gpui::test]
398 async fn test_command_basics(cx: &mut TestAppContext) {
399 let mut cx = NeovimBackedTestContext::new(cx).await;
400
401 cx.set_shared_state(indoc! {"
402 ˇa
403 b
404 c"})
405 .await;
406
407 cx.simulate_shared_keystrokes(": j enter").await;
408
409 // hack: our cursor positionining after a join command is wrong
410 cx.simulate_shared_keystrokes("^").await;
411 cx.shared_state().await.assert_eq(indoc! {
412 "ˇa b
413 c"
414 });
415 }
416
417 #[gpui::test]
418 async fn test_command_goto(cx: &mut TestAppContext) {
419 let mut cx = NeovimBackedTestContext::new(cx).await;
420
421 cx.set_shared_state(indoc! {"
422 ˇa
423 b
424 c"})
425 .await;
426 cx.simulate_shared_keystrokes(": 3 enter").await;
427 cx.shared_state().await.assert_eq(indoc! {"
428 a
429 b
430 ˇc"});
431 }
432
433 #[gpui::test]
434 async fn test_command_replace(cx: &mut TestAppContext) {
435 let mut cx = NeovimBackedTestContext::new(cx).await;
436
437 cx.set_shared_state(indoc! {"
438 ˇa
439 b
440 c"})
441 .await;
442 cx.simulate_shared_keystrokes(": % s / b / d enter").await;
443 cx.shared_state().await.assert_eq(indoc! {"
444 a
445 ˇd
446 c"});
447 cx.simulate_shared_keystrokes(": % s : . : \\ 0 \\ 0 enter")
448 .await;
449 cx.shared_state().await.assert_eq(indoc! {"
450 aa
451 dd
452 ˇcc"});
453 }
454
455 #[gpui::test]
456 async fn test_command_search(cx: &mut TestAppContext) {
457 let mut cx = NeovimBackedTestContext::new(cx).await;
458
459 cx.set_shared_state(indoc! {"
460 ˇa
461 b
462 a
463 c"})
464 .await;
465 cx.simulate_shared_keystrokes(": / b enter").await;
466 cx.shared_state().await.assert_eq(indoc! {"
467 a
468 ˇb
469 a
470 c"});
471 cx.simulate_shared_keystrokes(": ? a enter").await;
472 cx.shared_state().await.assert_eq(indoc! {"
473 ˇa
474 b
475 a
476 c"});
477 }
478
479 #[gpui::test]
480 async fn test_command_write(cx: &mut TestAppContext) {
481 let mut cx = VimTestContext::new(cx, true).await;
482 let path = Path::new("/root/dir/file.rs");
483 let fs = cx.workspace(|workspace, cx| workspace.project().read(cx).fs().clone());
484
485 cx.simulate_keystrokes("i @ escape");
486 cx.simulate_keystrokes(": w enter");
487
488 assert_eq!(fs.load(&path).await.unwrap(), "@\n");
489
490 fs.as_fake().insert_file(path, b"oops\n".to_vec()).await;
491
492 // conflict!
493 cx.simulate_keystrokes("i @ escape");
494 cx.simulate_keystrokes(": w enter");
495 assert!(cx.has_pending_prompt());
496 // "Cancel"
497 cx.simulate_prompt_answer(0);
498 assert_eq!(fs.load(&path).await.unwrap(), "oops\n");
499 assert!(!cx.has_pending_prompt());
500 // force overwrite
501 cx.simulate_keystrokes(": w ! enter");
502 assert!(!cx.has_pending_prompt());
503 assert_eq!(fs.load(&path).await.unwrap(), "@@\n");
504 }
505
506 #[gpui::test]
507 async fn test_command_quit(cx: &mut TestAppContext) {
508 let mut cx = VimTestContext::new(cx, true).await;
509
510 cx.simulate_keystrokes(": n e w enter");
511 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
512 cx.simulate_keystrokes(": q enter");
513 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 1));
514 cx.simulate_keystrokes(": n e w enter");
515 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
516 cx.simulate_keystrokes(": q a enter");
517 cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 0));
518 }
519}