command.rs

  1use command_palette::CommandInterceptResult;
  2use editor::{SortLinesCaseInsensitive, SortLinesCaseSensitive};
  3use gpui::{impl_actions, Action, AppContext};
  4use serde_derive::Deserialize;
  5use workspace::{SaveBehavior, 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(Debug, Clone, PartialEq, Deserialize)]
 19pub struct GoToLine {
 20    pub line: u32,
 21}
 22
 23impl_actions!(vim, [GoToLine]);
 24
 25pub fn init(cx: &mut AppContext) {
 26    cx.add_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, _: &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_behavior: Some(SaveBehavior::PromptOnConflict),
 54            }
 55            .boxed_clone(),
 56        ),
 57        "w!" | "wr!" | "wri!" | "writ!" | "write!" => (
 58            "write!",
 59            workspace::Save {
 60                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
 61            }
 62            .boxed_clone(),
 63        ),
 64        "q" | "qu" | "qui" | "quit" => (
 65            "quit",
 66            workspace::CloseActiveItem {
 67                save_behavior: Some(SaveBehavior::PromptOnWrite),
 68            }
 69            .boxed_clone(),
 70        ),
 71        "q!" | "qu!" | "qui!" | "quit!" => (
 72            "quit!",
 73            workspace::CloseActiveItem {
 74                save_behavior: Some(SaveBehavior::DontSave),
 75            }
 76            .boxed_clone(),
 77        ),
 78        "wq" => (
 79            "wq",
 80            workspace::CloseActiveItem {
 81                save_behavior: Some(SaveBehavior::PromptOnConflict),
 82            }
 83            .boxed_clone(),
 84        ),
 85        "wq!" => (
 86            "wq!",
 87            workspace::CloseActiveItem {
 88                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
 89            }
 90            .boxed_clone(),
 91        ),
 92        "x" | "xi" | "xit" | "exi" | "exit" => (
 93            "exit",
 94            workspace::CloseActiveItem {
 95                save_behavior: Some(SaveBehavior::PromptOnConflict),
 96            }
 97            .boxed_clone(),
 98        ),
 99        "x!" | "xi!" | "xit!" | "exi!" | "exit!" => (
100            "exit!",
101            workspace::CloseActiveItem {
102                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
103            }
104            .boxed_clone(),
105        ),
106        "wa" | "wal" | "wall" => (
107            "wall",
108            workspace::SaveAll {
109                save_behavior: Some(SaveBehavior::PromptOnConflict),
110            }
111            .boxed_clone(),
112        ),
113        "wa!" | "wal!" | "wall!" => (
114            "wall!",
115            workspace::SaveAll {
116                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
117            }
118            .boxed_clone(),
119        ),
120        "qa" | "qal" | "qall" | "quita" | "quital" | "quitall" => (
121            "quitall",
122            workspace::CloseAllItemsAndPanes {
123                save_behavior: Some(SaveBehavior::PromptOnWrite),
124            }
125            .boxed_clone(),
126        ),
127        "qa!" | "qal!" | "qall!" | "quita!" | "quital!" | "quitall!" => (
128            "quitall!",
129            workspace::CloseAllItemsAndPanes {
130                save_behavior: Some(SaveBehavior::DontSave),
131            }
132            .boxed_clone(),
133        ),
134        "xa" | "xal" | "xall" => (
135            "xall",
136            workspace::CloseAllItemsAndPanes {
137                save_behavior: Some(SaveBehavior::PromptOnConflict),
138            }
139            .boxed_clone(),
140        ),
141        "xa!" | "xal!" | "xall!" => (
142            "xall!",
143            workspace::CloseAllItemsAndPanes {
144                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
145            }
146            .boxed_clone(),
147        ),
148        "wqa" | "wqal" | "wqall" => (
149            "wqall",
150            workspace::CloseAllItemsAndPanes {
151                save_behavior: Some(SaveBehavior::PromptOnConflict),
152            }
153            .boxed_clone(),
154        ),
155        "wqa!" | "wqal!" | "wqall!" => (
156            "wqall!",
157            workspace::CloseAllItemsAndPanes {
158                save_behavior: Some(SaveBehavior::SilentlyOverwrite),
159            }
160            .boxed_clone(),
161        ),
162        "cq" | "cqu" | "cqui" | "cquit" | "cq!" | "cqu!" | "cqui!" | "cquit!" => {
163            ("cquit!", zed_actions::Quit.boxed_clone())
164        }
165
166        // pane management
167        "sp" | "spl" | "spli" | "split" => ("split", workspace::SplitUp.boxed_clone()),
168        "vs" | "vsp" | "vspl" | "vspli" | "vsplit" => {
169            ("vsplit", workspace::SplitLeft.boxed_clone())
170        }
171        "new" => (
172            "new",
173            workspace::NewFileInDirection(workspace::SplitDirection::Up).boxed_clone(),
174        ),
175        "vne" | "vnew" => (
176            "vnew",
177            workspace::NewFileInDirection(workspace::SplitDirection::Left).boxed_clone(),
178        ),
179        "tabe" | "tabed" | "tabedi" | "tabedit" => ("tabedit", workspace::NewFile.boxed_clone()),
180        "tabnew" => ("tabnew", workspace::NewFile.boxed_clone()),
181
182        "tabn" | "tabne" | "tabnex" | "tabnext" => {
183            ("tabnext", workspace::ActivateNextItem.boxed_clone())
184        }
185        "tabp" | "tabpr" | "tabpre" | "tabprev" | "tabprevi" | "tabprevio" | "tabpreviou"
186        | "tabprevious" => ("tabprevious", workspace::ActivatePrevItem.boxed_clone()),
187        "tabN" | "tabNe" | "tabNex" | "tabNext" => {
188            ("tabNext", workspace::ActivatePrevItem.boxed_clone())
189        }
190        "tabc" | "tabcl" | "tabclo" | "tabclos" | "tabclose" => (
191            "tabclose",
192            workspace::CloseActiveItem {
193                save_behavior: Some(SaveBehavior::PromptOnWrite),
194            }
195            .boxed_clone(),
196        ),
197
198        // quickfix / loclist (merged together for now)
199        "cl" | "cli" | "clis" | "clist" => ("clist", diagnostics::Deploy.boxed_clone()),
200        "cc" => ("cc", editor::Hover.boxed_clone()),
201        "ll" => ("ll", editor::Hover.boxed_clone()),
202        "cn" | "cne" | "cnex" | "cnext" => ("cnext", editor::GoToDiagnostic.boxed_clone()),
203        "lne" | "lnex" | "lnext" => ("cnext", editor::GoToDiagnostic.boxed_clone()),
204
205        "cpr" | "cpre" | "cprev" | "cprevi" | "cprevio" | "cpreviou" | "cprevious" => {
206            ("cprevious", editor::GoToPrevDiagnostic.boxed_clone())
207        }
208        "cN" | "cNe" | "cNex" | "cNext" => ("cNext", editor::GoToPrevDiagnostic.boxed_clone()),
209        "lp" | "lpr" | "lpre" | "lprev" | "lprevi" | "lprevio" | "lpreviou" | "lprevious" => {
210            ("lprevious", editor::GoToPrevDiagnostic.boxed_clone())
211        }
212        "lN" | "lNe" | "lNex" | "lNext" => ("lNext", editor::GoToPrevDiagnostic.boxed_clone()),
213
214        // modify the buffer (should accept [range])
215        "j" | "jo" | "joi" | "join" => ("join", JoinLines.boxed_clone()),
216        "d" | "de" | "del" | "dele" | "delet" | "delete" | "dl" | "dell" | "delel" | "deletl"
217        | "deletel" | "dp" | "dep" | "delp" | "delep" | "deletp" | "deletep" => {
218            ("delete", editor::DeleteLine.boxed_clone())
219        }
220        "sor" | "sor " | "sort" | "sort " => ("sort", SortLinesCaseSensitive.boxed_clone()),
221        "sor i" | "sort i" => ("sort i", SortLinesCaseInsensitive.boxed_clone()),
222
223        // goto (other ranges handled under _ => )
224        "$" => ("$", EndOfDocument.boxed_clone()),
225
226        _ => {
227            if query.starts_with("/") || query.starts_with("?") {
228                (
229                    query,
230                    FindCommand {
231                        query: query[1..].to_string(),
232                        backwards: query.starts_with("?"),
233                    }
234                    .boxed_clone(),
235                )
236            } else if query.starts_with("%") {
237                (
238                    query,
239                    ReplaceCommand {
240                        query: query.to_string(),
241                    }
242                    .boxed_clone(),
243                )
244            } else if let Ok(line) = query.parse::<u32>() {
245                (query, GoToLine { line }.boxed_clone())
246            } else {
247                return None;
248            }
249        }
250    };
251
252    let string = ":".to_owned() + name;
253    let positions = generate_positions(&string, query);
254
255    Some(CommandInterceptResult {
256        action,
257        string,
258        positions,
259    })
260}
261
262fn generate_positions(string: &str, query: &str) -> Vec<usize> {
263    let mut positions = Vec::new();
264    let mut chars = query.chars().into_iter();
265
266    let Some(mut current) = chars.next() else {
267        return positions;
268    };
269
270    for (i, c) in string.chars().enumerate() {
271        if c == current {
272            positions.push(i);
273            if let Some(c) = chars.next() {
274                current = c;
275            } else {
276                break;
277            }
278        }
279    }
280
281    positions
282}
283
284#[cfg(test)]
285mod test {
286    use std::path::Path;
287
288    use crate::test::{NeovimBackedTestContext, VimTestContext};
289    use gpui::{executor::Foreground, TestAppContext};
290    use indoc::indoc;
291
292    #[gpui::test]
293    async fn test_command_basics(cx: &mut TestAppContext) {
294        if let Foreground::Deterministic { cx_id: _, executor } = cx.foreground().as_ref() {
295            executor.run_until_parked();
296        }
297        let mut cx = NeovimBackedTestContext::new(cx).await;
298
299        cx.set_shared_state(indoc! {"
300            ˇa
301            b
302            c"})
303            .await;
304
305        cx.simulate_shared_keystrokes([":", "j", "enter"]).await;
306
307        // hack: our cursor positionining after a join command is wrong
308        cx.simulate_shared_keystrokes(["^"]).await;
309        cx.assert_shared_state(indoc! {
310            "ˇa b
311            c"
312        })
313        .await;
314    }
315
316    #[gpui::test]
317    async fn test_command_goto(cx: &mut TestAppContext) {
318        let mut cx = NeovimBackedTestContext::new(cx).await;
319
320        cx.set_shared_state(indoc! {"
321            ˇa
322            b
323            c"})
324            .await;
325        cx.simulate_shared_keystrokes([":", "3", "enter"]).await;
326        cx.assert_shared_state(indoc! {"
327            a
328            b
329            ˇc"})
330            .await;
331    }
332
333    #[gpui::test]
334    async fn test_command_replace(cx: &mut TestAppContext) {
335        let mut cx = NeovimBackedTestContext::new(cx).await;
336
337        cx.set_shared_state(indoc! {"
338            ˇa
339            b
340            c"})
341            .await;
342        cx.simulate_shared_keystrokes([":", "%", "s", "/", "b", "/", "d", "enter"])
343            .await;
344        cx.assert_shared_state(indoc! {"
345            a
346            ˇd
347            c"})
348            .await;
349        cx.simulate_shared_keystrokes([
350            ":", "%", "s", ":", ".", ":", "\\", "0", "\\", "0", "enter",
351        ])
352        .await;
353        cx.assert_shared_state(indoc! {"
354            aa
355            dd
356            ˇcc"})
357            .await;
358
359        cx.simulate_shared_keystrokes([":", "%", "s", "/", "/", "/", "enter"])
360            .await;
361    }
362
363    #[gpui::test]
364    async fn test_command_write(cx: &mut TestAppContext) {
365        let mut cx = VimTestContext::new(cx, true).await;
366        let path = Path::new("/root/dir/file.rs");
367        let fs = cx.workspace(|workspace, cx| workspace.project().read(cx).fs().clone());
368
369        cx.simulate_keystrokes(["i", "@", "escape"]);
370        cx.simulate_keystrokes([":", "w", "enter"]);
371
372        assert_eq!(fs.load(&path).await.unwrap(), "@\n");
373
374        fs.as_fake()
375            .write_file_internal(path, "oops\n".to_string())
376            .unwrap();
377
378        // conflict!
379        cx.simulate_keystrokes(["i", "@", "escape"]);
380        cx.simulate_keystrokes([":", "w", "enter"]);
381        let window = cx.window;
382        assert!(window.has_pending_prompt(cx.cx));
383        // "Cancel"
384        window.simulate_prompt_answer(0, cx.cx);
385        assert_eq!(fs.load(&path).await.unwrap(), "oops\n");
386        assert!(!window.has_pending_prompt(cx.cx));
387        // force overwrite
388        cx.simulate_keystrokes([":", "w", "!", "enter"]);
389        assert!(!window.has_pending_prompt(cx.cx));
390        assert_eq!(fs.load(&path).await.unwrap(), "@@\n");
391    }
392
393    #[gpui::test]
394    async fn test_command_quit(cx: &mut TestAppContext) {
395        let mut cx = VimTestContext::new(cx, true).await;
396
397        cx.simulate_keystrokes([":", "n", "e", "w", "enter"]);
398        cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 2));
399        cx.simulate_keystrokes([":", "q", "enter"]);
400        cx.workspace(|workspace, cx| assert_eq!(workspace.items(cx).count(), 1));
401    }
402}