command.rs

  1use command_palette::CommandInterceptResult;
  2use editor::{SortLinesCaseInsensitive, SortLinesCaseSensitive};
  3use gpui::{Action, AppContext};
  4use serde_derive::Deserialize;
  5use workspace::{SaveIntent, Workspace};
  6
  7use crate::{
  8    motion::{EndOfDocument, Motion},
  9    normal::{
 10        move_cursor,
 11        search::{FindCommand, ReplaceCommand},
 12        JoinLines,
 13    },
 14    state::Mode,
 15    Vim,
 16};
 17
 18#[derive(Action, Debug, Clone, PartialEq, Deserialize)]
 19pub struct GoToLine {
 20    pub line: u32,
 21}
 22
 23pub fn init(cx: &mut AppContext) {
 24    // todo!()
 25    // cx.add_action(|_: &mut Workspace, action: &GoToLine, cx| {
 26    //     Vim::update(cx, |vim, cx| {
 27    //         vim.switch_mode(Mode::Normal, false, cx);
 28    //         move_cursor(vim, Motion::StartOfDocument, Some(action.line as usize), cx);
 29    //     });
 30    // });
 31}
 32
 33pub fn command_interceptor(mut query: &str, _: &AppContext) -> Option<CommandInterceptResult> {
 34    // Note: this is a very poor simulation of vim's command palette.
 35    // In the future we should adjust it to handle parsing range syntax,
 36    // and then calling the appropriate commands with/without ranges.
 37    //
 38    // We also need to support passing arguments to commands like :w
 39    // (ideally with filename autocompletion).
 40    //
 41    // For now, you can only do a replace on the % range, and you can
 42    // only use a specific line number range to "go to line"
 43    while query.starts_with(":") {
 44        query = &query[1..];
 45    }
 46
 47    let (name, action) = match query {
 48        // save and quit
 49        "w" | "wr" | "wri" | "writ" | "write" => (
 50            "write",
 51            workspace::Save {
 52                save_intent: Some(SaveIntent::Save),
 53            }
 54            .boxed_clone(),
 55        ),
 56        "w!" | "wr!" | "wri!" | "writ!" | "write!" => (
 57            "write!",
 58            workspace::Save {
 59                save_intent: Some(SaveIntent::Overwrite),
 60            }
 61            .boxed_clone(),
 62        ),
 63        "q" | "qu" | "qui" | "quit" => (
 64            "quit",
 65            workspace::CloseActiveItem {
 66                save_intent: Some(SaveIntent::Close),
 67            }
 68            .boxed_clone(),
 69        ),
 70        "q!" | "qu!" | "qui!" | "quit!" => (
 71            "quit!",
 72            workspace::CloseActiveItem {
 73                save_intent: Some(SaveIntent::Skip),
 74            }
 75            .boxed_clone(),
 76        ),
 77        "wq" => (
 78            "wq",
 79            workspace::CloseActiveItem {
 80                save_intent: Some(SaveIntent::Save),
 81            }
 82            .boxed_clone(),
 83        ),
 84        "wq!" => (
 85            "wq!",
 86            workspace::CloseActiveItem {
 87                save_intent: Some(SaveIntent::Overwrite),
 88            }
 89            .boxed_clone(),
 90        ),
 91        "x" | "xi" | "xit" | "exi" | "exit" => (
 92            "exit",
 93            workspace::CloseActiveItem {
 94                save_intent: Some(SaveIntent::SaveAll),
 95            }
 96            .boxed_clone(),
 97        ),
 98        "x!" | "xi!" | "xit!" | "exi!" | "exit!" => (
 99            "exit!",
100            workspace::CloseActiveItem {
101                save_intent: Some(SaveIntent::Overwrite),
102            }
103            .boxed_clone(),
104        ),
105        "up" | "upd" | "upda" | "updat" | "update" => (
106            "update",
107            workspace::Save {
108                save_intent: Some(SaveIntent::SaveAll),
109            }
110            .boxed_clone(),
111        ),
112        "wa" | "wal" | "wall" => (
113            "wall",
114            workspace::SaveAll {
115                save_intent: Some(SaveIntent::SaveAll),
116            }
117            .boxed_clone(),
118        ),
119        "wa!" | "wal!" | "wall!" => (
120            "wall!",
121            workspace::SaveAll {
122                save_intent: Some(SaveIntent::Overwrite),
123            }
124            .boxed_clone(),
125        ),
126        "qa" | "qal" | "qall" | "quita" | "quital" | "quitall" => (
127            "quitall",
128            workspace::CloseAllItemsAndPanes {
129                save_intent: Some(SaveIntent::Close),
130            }
131            .boxed_clone(),
132        ),
133        "qa!" | "qal!" | "qall!" | "quita!" | "quital!" | "quitall!" => (
134            "quitall!",
135            workspace::CloseAllItemsAndPanes {
136                save_intent: Some(SaveIntent::Skip),
137            }
138            .boxed_clone(),
139        ),
140        "xa" | "xal" | "xall" => (
141            "xall",
142            workspace::CloseAllItemsAndPanes {
143                save_intent: Some(SaveIntent::SaveAll),
144            }
145            .boxed_clone(),
146        ),
147        "xa!" | "xal!" | "xall!" => (
148            "xall!",
149            workspace::CloseAllItemsAndPanes {
150                save_intent: Some(SaveIntent::Overwrite),
151            }
152            .boxed_clone(),
153        ),
154        "wqa" | "wqal" | "wqall" => (
155            "wqall",
156            workspace::CloseAllItemsAndPanes {
157                save_intent: Some(SaveIntent::SaveAll),
158            }
159            .boxed_clone(),
160        ),
161        "wqa!" | "wqal!" | "wqall!" => (
162            "wqall!",
163            workspace::CloseAllItemsAndPanes {
164                save_intent: Some(SaveIntent::Overwrite),
165            }
166            .boxed_clone(),
167        ),
168        "cq" | "cqu" | "cqui" | "cquit" | "cq!" | "cqu!" | "cqui!" | "cquit!" => {
169            // ("cquit!", zed_actions::Quit.boxed_clone())
170            todo!(); // Quit is no longer in zed actions :/
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
205        // quickfix / loclist (merged together for now)
206        "cl" | "cli" | "clis" | "clist" => ("clist", diagnostics::Deploy.boxed_clone()),
207        "cc" => ("cc", editor::Hover.boxed_clone()),
208        "ll" => ("ll", editor::Hover.boxed_clone()),
209        "cn" | "cne" | "cnex" | "cnext" => ("cnext", editor::GoToDiagnostic.boxed_clone()),
210        "lne" | "lnex" | "lnext" => ("cnext", editor::GoToDiagnostic.boxed_clone()),
211
212        "cpr" | "cpre" | "cprev" | "cprevi" | "cprevio" | "cpreviou" | "cprevious" => {
213            ("cprevious", editor::GoToPrevDiagnostic.boxed_clone())
214        }
215        "cN" | "cNe" | "cNex" | "cNext" => ("cNext", editor::GoToPrevDiagnostic.boxed_clone()),
216        "lp" | "lpr" | "lpre" | "lprev" | "lprevi" | "lprevio" | "lpreviou" | "lprevious" => {
217            ("lprevious", editor::GoToPrevDiagnostic.boxed_clone())
218        }
219        "lN" | "lNe" | "lNex" | "lNext" => ("lNext", editor::GoToPrevDiagnostic.boxed_clone()),
220
221        // modify the buffer (should accept [range])
222        "j" | "jo" | "joi" | "join" => ("join", JoinLines.boxed_clone()),
223        "d" | "de" | "del" | "dele" | "delet" | "delete" | "dl" | "dell" | "delel" | "deletl"
224        | "deletel" | "dp" | "dep" | "delp" | "delep" | "deletp" | "deletep" => {
225            ("delete", editor::DeleteLine.boxed_clone())
226        }
227        "sor" | "sor " | "sort" | "sort " => ("sort", SortLinesCaseSensitive.boxed_clone()),
228        "sor i" | "sort i" => ("sort i", SortLinesCaseInsensitive.boxed_clone()),
229
230        // goto (other ranges handled under _ => )
231        "$" => ("$", EndOfDocument.boxed_clone()),
232
233        _ => {
234            if query.starts_with("/") || query.starts_with("?") {
235                (
236                    query,
237                    FindCommand {
238                        query: query[1..].to_string(),
239                        backwards: query.starts_with("?"),
240                    }
241                    .boxed_clone(),
242                )
243            } else if query.starts_with("%") {
244                (
245                    query,
246                    ReplaceCommand {
247                        query: query.to_string(),
248                    }
249                    .boxed_clone(),
250                )
251            } else if let Ok(line) = query.parse::<u32>() {
252                (query, GoToLine { line }.boxed_clone())
253            } else {
254                return None;
255            }
256        }
257    };
258
259    let string = ":".to_owned() + name;
260    let positions = generate_positions(&string, query);
261
262    Some(CommandInterceptResult {
263        action,
264        string,
265        positions,
266    })
267}
268
269fn generate_positions(string: &str, query: &str) -> Vec<usize> {
270    let mut positions = Vec::new();
271    let mut chars = query.chars().into_iter();
272
273    let Some(mut current) = chars.next() else {
274        return positions;
275    };
276
277    for (i, c) in string.chars().enumerate() {
278        if c == current {
279            positions.push(i);
280            if let Some(c) = chars.next() {
281                current = c;
282            } else {
283                break;
284            }
285        }
286    }
287
288    positions
289}
290
291// #[cfg(test)]
292// mod test {
293//     use std::path::Path;
294
295//     use crate::test::{NeovimBackedTestContext, VimTestContext};
296//     use gpui::TestAppContext;
297//     use indoc::indoc;
298
299//     #[gpui::test]
300//     async fn test_command_basics(cx: &mut TestAppContext) {
301//         if let Foreground::Deterministic { cx_id: _, executor } = cx.foreground().as_ref() {
302//             executor.run_until_parked();
303//         }
304//         let mut cx = NeovimBackedTestContext::new(cx).await;
305
306//         cx.set_shared_state(indoc! {"
307//             ˇa
308//             b
309//             c"})
310//             .await;
311
312//         cx.simulate_shared_keystrokes([":", "j", "enter"]).await;
313
314//         // hack: our cursor positionining after a join command is wrong
315//         cx.simulate_shared_keystrokes(["^"]).await;
316//         cx.assert_shared_state(indoc! {
317//             "ˇa b
318//             c"
319//         })
320//         .await;
321//     }
322
323//     #[gpui::test]
324//     async fn test_command_goto(cx: &mut TestAppContext) {
325//         let mut cx = NeovimBackedTestContext::new(cx).await;
326
327//         cx.set_shared_state(indoc! {"
328//             ˇa
329//             b
330//             c"})
331//             .await;
332//         cx.simulate_shared_keystrokes([":", "3", "enter"]).await;
333//         cx.assert_shared_state(indoc! {"
334//             a
335//             b
336//             ˇc"})
337//             .await;
338//     }
339
340//     #[gpui::test]
341//     async fn test_command_replace(cx: &mut TestAppContext) {
342//         let mut cx = NeovimBackedTestContext::new(cx).await;
343
344//         cx.set_shared_state(indoc! {"
345//             ˇa
346//             b
347//             c"})
348//             .await;
349//         cx.simulate_shared_keystrokes([":", "%", "s", "/", "b", "/", "d", "enter"])
350//             .await;
351//         cx.assert_shared_state(indoc! {"
352//             a
353//             ˇd
354//             c"})
355//             .await;
356//         cx.simulate_shared_keystrokes([
357//             ":", "%", "s", ":", ".", ":", "\\", "0", "\\", "0", "enter",
358//         ])
359//         .await;
360//         cx.assert_shared_state(indoc! {"
361//             aa
362//             dd
363//             ˇcc"})
364//             .await;
365//     }
366
367//     #[gpui::test]
368//     async fn test_command_search(cx: &mut TestAppContext) {
369//         let mut cx = NeovimBackedTestContext::new(cx).await;
370
371//         cx.set_shared_state(indoc! {"
372//                 ˇa
373//                 b
374//                 a
375//                 c"})
376//             .await;
377//         cx.simulate_shared_keystrokes([":", "/", "b", "enter"])
378//             .await;
379//         cx.assert_shared_state(indoc! {"
380//                 a
381//                 ˇb
382//                 a
383//                 c"})
384//             .await;
385//         cx.simulate_shared_keystrokes([":", "?", "a", "enter"])
386//             .await;
387//         cx.assert_shared_state(indoc! {"
388//                 ˇa
389//                 b
390//                 a
391//                 c"})
392//             .await;
393//     }
394
395//     #[gpui::test]
396//     async fn test_command_write(cx: &mut TestAppContext) {
397//         let mut cx = VimTestContext::new(cx, true).await;
398//         let path = Path::new("/root/dir/file.rs");
399//         let fs = cx.workspace(|workspace, cx| workspace.project().read(cx).fs().clone());
400
401//         cx.simulate_keystrokes(["i", "@", "escape"]);
402//         cx.simulate_keystrokes([":", "w", "enter"]);
403
404//         assert_eq!(fs.load(&path).await.unwrap(), "@\n");
405
406//         fs.as_fake()
407//             .write_file_internal(path, "oops\n".to_string())
408//             .unwrap();
409
410//         // conflict!
411//         cx.simulate_keystrokes(["i", "@", "escape"]);
412//         cx.simulate_keystrokes([":", "w", "enter"]);
413//         let window = cx.window;
414//         assert!(window.has_pending_prompt(cx.cx));
415//         // "Cancel"
416//         window.simulate_prompt_answer(0, cx.cx);
417//         assert_eq!(fs.load(&path).await.unwrap(), "oops\n");
418//         assert!(!window.has_pending_prompt(cx.cx));
419//         // force overwrite
420//         cx.simulate_keystrokes([":", "w", "!", "enter"]);
421//         assert!(!window.has_pending_prompt(cx.cx));
422//         assert_eq!(fs.load(&path).await.unwrap(), "@@\n");
423//     }
424
425//     #[gpui::test]
426//     async fn test_command_quit(cx: &mut TestAppContext) {
427//         let mut cx = VimTestContext::new(cx, true).await;
428
429//         cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
430//         cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
431//         cx.simulate_keystrokes([":", "q", "enter"]);
432//         cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 1));
433//         cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
434//         cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
435//         cx.simulate_keystrokes([":", "q", "a", "enter"]);
436//         cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 0));
437//     }
438// }