vorto 0.15.0

A Vim-flavored modal terminal editor with batteries included: tree-sitter, LSP, fuzzy pickers, vim-surround, multi-cursor, and optional Copilot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! `:` command table — the single source of truth for every `:`-prompt
//! command: its name/aliases, description, what it completes after the
//! head (nothing / a path / subcommands), and how it dispatches.
//!
//! Three layers read this table and none of them duplicate the list:
//! - [`crate::app`] evaluates a command by matching on [`Command::kind`];
//! - [`crate::prompt`] builds Tab-completion candidates from the names
//!   and [`Args`];
//! - [`crate::ui`] builds the `:`-hint / which-key panel from the same.
//!
//! Adding a command is one [`COMMANDS`] entry (plus, for an
//! [`Inline`] command, one arm in the evaluator's dispatch `match`, which
//! the compiler enforces). Kept out of `app` so the lower layers can read
//! it without depending on the editor state machine — hence [`Inline`] is
//! a plain enum rather than a `fn(&mut App)`.

use crate::action::DirectKind;

/// One `:` command.
pub struct Command {
    /// Canonical (primary) name — the documentation handle and what the
    /// hint panel shows.
    pub name: &'static str,
    /// Extra spellings that resolve to the same command.
    pub aliases: &'static [&'static str],
    pub description: &'static str,
    /// What follows the head: nothing, a path, or a fixed subcommand set.
    /// Drives both Tab-completion and the hint panel.
    pub args: Args,
    /// How the evaluator runs it.
    pub kind: Kind,
}

/// Shape of a command's argument.
pub enum Args {
    /// No argument completion.
    None,
    /// Completes a filesystem path (`:e `, `:w `).
    Path,
    /// Completes one of a fixed subcommand set (`:grammar `, `:copilot `).
    Sub(&'static [Subcommand]),
}

/// A subcommand of an [`Args::Sub`] command. Metadata only — the actual
/// dispatch lives in the owning command's `App` handler, which resolves a
/// token with [`resolve_subcommand`].
pub struct Subcommand {
    pub name: &'static str,
    pub aliases: &'static [&'static str],
    pub description: &'static str,
}

/// How [`crate::app::App::execute_command`] runs a command.
pub enum Kind {
    /// Maps to a single [`DirectKind`], dispatched via `Expr::Direct`.
    Direct(DirectKind),
    /// Routed to a dedicated `App` handler — these open modals, spawn
    /// processes, or take subcommands that don't fit the single-`DirectKind`
    /// shape. The [`Inline`] variant names which handler.
    Inline(Inline),
}

/// Inline-dispatched commands. A plain enum (no `fn(&mut App)`) so this
/// table stays free of any `app` dependency; the evaluator matches on it,
/// and adding a variant forces the dispatch site to handle it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Inline {
    Copilot,
    Grammar,
    Agent,
    Theme,
    Bookmarks,
    Conflict,
}

/// `:copilot` subcommands. Source of truth for completion + the hint
/// panel; `App::run_copilot_command` resolves a token against this list.
pub const COPILOT_SUBCOMMANDS: &[Subcommand] = &[
    Subcommand {
        name: "status",
        aliases: &[],
        description: "show Copilot auth state",
    },
    Subcommand {
        name: "signin",
        aliases: &["login"],
        description: "start device-flow sign-in",
    },
    Subcommand {
        name: "signout",
        aliases: &["logout"],
        description: "sign out of Copilot",
    },
    Subcommand {
        name: "code",
        aliases: &[],
        description: "re-show signin modal + re-copy code",
    },
];

/// `:agent` subcommands — intents that build a prompt from editor context
/// and forward it to the agent. Bare `:agent` (no subcommand) still just
/// launches the default agent. `App::run_agent_command` resolves a token
/// against this list.
pub const AGENT_SUBCOMMANDS: &[Subcommand] = &[
    Subcommand {
        name: "explain",
        aliases: &[],
        description: "explain @file / @selection with the agent",
    },
    Subcommand {
        name: "chat",
        aliases: &[],
        description: "discuss @file / @selection with the agent",
    },
];

/// `:grammar` subcommands. Mirrors the `vorto grammar` CLI so muscle
/// memory carries over.
pub const GRAMMAR_SUBCOMMANDS: &[Subcommand] = &[
    Subcommand {
        name: "list",
        aliases: &["ls"],
        description: "browse, install & remove (modal)",
    },
    Subcommand {
        name: "install",
        aliases: &["add"],
        description: "install <name>... | --all",
    },
    Subcommand {
        name: "remove",
        aliases: &["rm", "uninstall"],
        description: "remove <name>...",
    },
];

/// `:conflict` subcommands — resolve the conflict under the cursor.
/// Navigation lives on the `]c` / `[c` keys, not here. `App::run_conflict_command`
/// resolves a token against this list.
pub const CONFLICT_SUBCOMMANDS: &[Subcommand] = &[
    Subcommand {
        name: "ours",
        aliases: &["local", "top"],
        description: "keep the top side (ours / local)",
    },
    Subcommand {
        name: "theirs",
        aliases: &["disk", "bottom"],
        description: "keep the bottom side (theirs / disk)",
    },
    Subcommand {
        name: "both",
        aliases: &["all"],
        description: "keep both sides, drop the markers",
    },
    Subcommand {
        name: "none",
        aliases: &["remove"],
        description: "delete the whole conflict",
    },
];

/// `:bookmarks` subcommands. Bare `:bookmarks` opens the picker (same as
/// `list`); `add`/`delete` act on the current buffer, mirroring the
/// `<space>ma` / `<space>md` keys. `App::run_bookmarks_command` resolves
/// a token against this list.
pub const BOOKMARK_SUBCOMMANDS: &[Subcommand] = &[
    Subcommand {
        name: "list",
        aliases: &["ls"],
        description: "open the bookmark picker (modal)",
    },
    Subcommand {
        name: "add",
        aliases: &[],
        description: "bookmark the current buffer at the cursor",
    },
    Subcommand {
        name: "delete",
        aliases: &["del", "rm"],
        description: "remove the current buffer's bookmark",
    },
];

impl Command {
    /// Resolve a command head (canonical name or alias) to its entry.
    pub fn find(name: &str) -> Option<&'static Command> {
        COMMANDS
            .iter()
            .find(|c| c.name == name || c.aliases.contains(&name))
    }

    /// The primary name followed by each alias — every typeable form.
    pub fn all_names(&self) -> impl Iterator<Item = &'static str> {
        std::iter::once(self.name).chain(self.aliases.iter().copied())
    }
}

/// Resolve a subcommand token (canonical name or alias) to its canonical
/// name. Used by inline handlers to dispatch.
pub fn resolve_subcommand(subs: &'static [Subcommand], token: &str) -> Option<&'static str> {
    subs.iter()
        .find(|s| s.name == token || s.aliases.contains(&token))
        .map(|s| s.name)
}

/// Every `:` command. See the module docs for the contract.
pub const COMMANDS: &[Command] = &[
    Command {
        name: "q",
        aliases: &["quit"],
        description: "quit",
        args: Args::None,
        kind: Kind::Direct(DirectKind::Quit),
    },
    Command {
        name: "q!",
        aliases: &["quit!"],
        description: "force quit",
        args: Args::None,
        kind: Kind::Direct(DirectKind::QuitForce),
    },
    Command {
        name: "w",
        aliases: &["write"],
        description: "save (or :w <path>)",
        args: Args::Path,
        kind: Kind::Direct(DirectKind::Save),
    },
    Command {
        name: "w!",
        aliases: &["write!"],
        description: "save, creating dirs",
        args: Args::Path,
        kind: Kind::Direct(DirectKind::SaveForce),
    },
    Command {
        name: "wq",
        aliases: &["x"],
        description: "save & quit",
        args: Args::Path,
        kind: Kind::Direct(DirectKind::SaveAndQuit),
    },
    Command {
        name: "e",
        aliases: &["edit"],
        description: "open <path>",
        args: Args::Path,
        kind: Kind::Direct(DirectKind::Open),
    },
    Command {
        name: "bn",
        aliases: &["bnext"],
        description: "next buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferNext),
    },
    Command {
        name: "bp",
        aliases: &["bprev"],
        description: "previous buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferPrev),
    },
    Command {
        name: "bd",
        aliases: &["bdelete"],
        description: "delete buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferDelete),
    },
    Command {
        name: "bd!",
        aliases: &["bdelete!", "bc", "bc!"],
        description: "force delete buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferDeleteForce),
    },
    Command {
        name: "bca",
        aliases: &["bca!"],
        description: "force delete all buffers",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferDeleteAll),
    },
    Command {
        name: "bls",
        aliases: &["buffers"],
        description: "buffer picker",
        args: Args::None,
        kind: Kind::Direct(DirectKind::BufferList),
    },
    Command {
        name: "new",
        aliases: &["enew"],
        description: "new scratch buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::NewScratchBuffer),
    },
    Command {
        name: "goto",
        aliases: &[],
        description: "go to line <n>",
        args: Args::None,
        kind: Kind::Direct(DirectKind::GotoLine),
    },
    Command {
        name: "jumps",
        aliases: &[],
        description: "jump history picker",
        args: Args::None,
        kind: Kind::Direct(DirectKind::JumpList),
    },
    Command {
        name: "bookmarks",
        aliases: &["bm"],
        description: "list / add / delete bookmarks",
        args: Args::Sub(BOOKMARK_SUBCOMMANDS),
        kind: Kind::Inline(Inline::Bookmarks),
    },
    Command {
        name: "conflict",
        aliases: &[],
        description: "resolve the conflict at the cursor (]c / [c to navigate)",
        args: Args::Sub(CONFLICT_SUBCOMMANDS),
        kind: Kind::Inline(Inline::Conflict),
    },
    Command {
        name: "log",
        aliases: &[],
        description: "open debug log file",
        args: Args::None,
        kind: Kind::Direct(DirectKind::OpenLog),
    },
    Command {
        name: "lsp",
        aliases: &[],
        description: "show LSP for current buffer (:lsp all for every language)",
        args: Args::None,
        kind: Kind::Direct(DirectKind::LspStatus),
    },
    Command {
        name: "reload",
        aliases: &["e!"],
        description: "reload buffer from disk (undo restores)",
        args: Args::None,
        kind: Kind::Direct(DirectKind::Reload),
    },
    Command {
        name: "reload-all",
        aliases: &[],
        description: "reload every file-backed buffer",
        args: Args::None,
        kind: Kind::Direct(DirectKind::ReloadAll),
    },
    Command {
        name: "noh",
        aliases: &["nohl", "nohlsearch"],
        description: "clear search highlight",
        args: Args::None,
        kind: Kind::Direct(DirectKind::ClearSearch),
    },
    Command {
        name: "split",
        aliases: &["sp", "nh"],
        description: "split pane below",
        args: Args::None,
        kind: Kind::Direct(DirectKind::SplitWindowHorizontal),
    },
    Command {
        name: "vsplit",
        aliases: &["vsp", "vs", "nv"],
        description: "split pane right",
        args: Args::None,
        kind: Kind::Direct(DirectKind::SplitWindowVertical),
    },
    Command {
        name: "close",
        aliases: &["clo"],
        description: "close active pane",
        args: Args::None,
        kind: Kind::Direct(DirectKind::CloseWindow),
    },
    Command {
        name: "only",
        aliases: &["on"],
        description: "(future) close all but active pane",
        args: Args::None,
        kind: Kind::Direct(DirectKind::CloseWindow),
    },
    Command {
        name: "copilot",
        aliases: &[],
        description: "Copilot status / signin / signout",
        args: Args::Sub(COPILOT_SUBCOMMANDS),
        kind: Kind::Inline(Inline::Copilot),
    },
    Command {
        name: "grammar",
        aliases: &[],
        description: "install / remove tree-sitter grammars",
        args: Args::Sub(GRAMMAR_SUBCOMMANDS),
        kind: Kind::Inline(Inline::Grammar),
    },
    Command {
        name: "agent",
        aliases: &[],
        description: "launch an AI agent (or :agent explain/chat @file)",
        args: Args::Sub(AGENT_SUBCOMMANDS),
        kind: Kind::Inline(Inline::Agent),
    },
    Command {
        name: "theme",
        aliases: &["colorscheme", "colo"],
        description: "pick a color theme (live preview)",
        args: Args::None,
        kind: Kind::Inline(Inline::Theme),
    },
];

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn find_resolves_name_and_alias() {
        assert!(matches!(
            Command::find("w").map(|c| &c.kind),
            Some(Kind::Direct(DirectKind::Save))
        ));
        // alias
        assert!(matches!(
            Command::find("write").map(|c| &c.kind),
            Some(Kind::Direct(DirectKind::Save))
        ));
        assert!(Command::find("nope").is_none());
    }

    #[test]
    fn inline_commands_are_in_the_table() {
        for (name, inline) in [
            ("copilot", Inline::Copilot),
            ("grammar", Inline::Grammar),
            ("agent", Inline::Agent),
            ("bookmarks", Inline::Bookmarks),
            ("bm", Inline::Bookmarks),
        ] {
            match Command::find(name).map(|c| &c.kind) {
                Some(Kind::Inline(i)) => assert_eq!(*i, inline),
                other => panic!("{name} should be Inline, got {:?}", other.is_some()),
            }
        }
    }

    #[test]
    fn path_commands_take_a_path() {
        assert!(matches!(
            Command::find("e").map(|c| &c.args),
            Some(Args::Path)
        ));
        assert!(matches!(
            Command::find("q").map(|c| &c.args),
            Some(Args::None)
        ));
    }

    #[test]
    fn resolve_subcommand_handles_aliases() {
        assert_eq!(
            resolve_subcommand(COPILOT_SUBCOMMANDS, "login"),
            Some("signin")
        );
        assert_eq!(resolve_subcommand(GRAMMAR_SUBCOMMANDS, "ls"), Some("list"));
        assert_eq!(resolve_subcommand(GRAMMAR_SUBCOMMANDS, "nope"), None);
        assert_eq!(
            resolve_subcommand(BOOKMARK_SUBCOMMANDS, "rm"),
            Some("delete")
        );
        assert_eq!(resolve_subcommand(BOOKMARK_SUBCOMMANDS, "ls"), Some("list"));
    }
}