vorto 0.4.0

A terminal text editor with tree-sitter syntax highlighting and LSP support
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Static binding tables (the single source of truth for both the
//! parser and the which-key hint renderer) plus the bulk vim-default
//! initializer that copies them — and the inline Initial-context
//! defaults — into a fresh `Keymap`.

use crossterm::event::{KeyCode, KeyModifiers};

use crate::action::{DirectKind, MotionKind, Operator, PromptKind, Token};
use crate::mode::Mode;

use super::{Binding, KeySig, Keymap, LEADER};

/// Keys valid in the OpPending context (right after `d`/`y`/`c`,
/// possibly with an inner count). Operator-repeat (`dd`/`yy`/`cc`) is
/// handled separately — it depends on the active operator.
pub const OP_PENDING_BINDINGS: &[Binding] = {
    use crate::action::Scope;
    use MotionKind::*;
    use Token::Motion as M;
    use Token::Scope as S;
    &[
        Binding {
            key: KeyCode::Char('i'),
            aliases: &[],
            token: S(Scope::Inner),
            label: "inner …",
        },
        // `g` after an operator opens the goto-prefix sub-grammar
        // (`dge`, `dg_`, `dgn`, ...). The follow-up key is then
        // resolved through `GOTO_BINDINGS` in `goto_pending_token`.
        Binding {
            key: KeyCode::Char('g'),
            aliases: &[],
            token: Token::GotoPrefix,
            label: "goto …",
        },
        Binding {
            key: KeyCode::Char('a'),
            aliases: &[],
            token: S(Scope::Around),
            label: "around …",
        },
        Binding {
            key: KeyCode::Char('w'),
            aliases: &[],
            token: M(WordForward),
            label: "word",
        },
        Binding {
            key: KeyCode::Char('b'),
            aliases: &[],
            token: M(WordBack),
            label: "back",
        },
        Binding {
            key: KeyCode::Char('e'),
            aliases: &[],
            token: M(WordEnd),
            label: "word end",
        },
        Binding {
            key: KeyCode::Char('W'),
            aliases: &[],
            token: M(BigWordForward),
            label: "WORD",
        },
        Binding {
            key: KeyCode::Char('B'),
            aliases: &[],
            token: M(BigWordBack),
            label: "WORD back",
        },
        Binding {
            key: KeyCode::Char('E'),
            aliases: &[],
            token: M(BigWordEnd),
            label: "WORD end",
        },
        Binding {
            key: KeyCode::Char('f'),
            aliases: &[],
            token: Token::FindCharPrefix { forward: true, till: false },
            label: "find char →",
        },
        Binding {
            key: KeyCode::Char('F'),
            aliases: &[],
            token: Token::FindCharPrefix { forward: false, till: false },
            label: "find char ←",
        },
        Binding {
            key: KeyCode::Char('t'),
            aliases: &[],
            token: Token::FindCharPrefix { forward: true, till: true },
            label: "till char →",
        },
        Binding {
            key: KeyCode::Char('T'),
            aliases: &[],
            token: Token::FindCharPrefix { forward: false, till: true },
            label: "till char ←",
        },
        Binding {
            key: KeyCode::Char(';'),
            aliases: &[],
            token: M(RepeatFind { reverse: false }),
            label: "repeat find",
        },
        Binding {
            key: KeyCode::Char(','),
            aliases: &[],
            token: M(RepeatFind { reverse: true }),
            label: "repeat find ↺",
        },
        Binding {
            key: KeyCode::Char('}'),
            aliases: &[],
            token: M(ParagraphForward),
            label: "paragraph fwd",
        },
        Binding {
            key: KeyCode::Char('{'),
            aliases: &[],
            token: M(ParagraphBack),
            label: "paragraph back",
        },
        Binding {
            key: KeyCode::Char('$'),
            aliases: &[KeyCode::End],
            token: M(LineEnd),
            label: "line end",
        },
        Binding {
            key: KeyCode::Char('0'),
            aliases: &[KeyCode::Home],
            token: M(LineStart),
            label: "line start",
        },
        Binding {
            key: KeyCode::Char('^'),
            aliases: &[],
            token: M(LineFirstNonBlank),
            label: "first non-blank",
        },
        Binding {
            key: KeyCode::Char('%'),
            aliases: &[],
            token: M(BracketMatch),
            label: "match bracket",
        },
        Binding {
            key: KeyCode::Char('h'),
            aliases: &[KeyCode::Left],
            token: M(Left),
            label: "left",
        },
        Binding {
            key: KeyCode::Char('l'),
            aliases: &[KeyCode::Right],
            token: M(Right),
            label: "right",
        },
        Binding {
            key: KeyCode::Char('j'),
            aliases: &[KeyCode::Down],
            token: M(Down),
            label: "down",
        },
        Binding {
            key: KeyCode::Char('k'),
            aliases: &[KeyCode::Up],
            token: M(Up),
            label: "up",
        },
        Binding {
            key: KeyCode::Char('G'),
            aliases: &[],
            token: M(FileEnd),
            label: "file end",
        },
    ]
};

/// Keys valid in the GotoPending context (right after `g`). Lives
/// here as the single source of truth for both the parser
/// (`goto_pending_token` in `app/eval.rs`) and the which-key hint
/// renderer (`pending_hints` in `ui/hints.rs`).
pub const GOTO_BINDINGS: &[Binding] = {
    use crate::action::DirectKind as D;
    use MotionKind::*;
    use Token::Direct as Dir;
    use Token::Motion as M;
    &[
        // `gg` re-emits the prefix so `[GotoPrefix, GotoPrefix]` closes
        // to a motion in `build_expr`.
        Binding {
            key: KeyCode::Char('g'),
            aliases: &[],
            token: Token::GotoPrefix,
            label: "file start (gg)",
        },
        Binding {
            key: KeyCode::Char('_'),
            aliases: &[],
            token: M(LineLastNonBlank),
            label: "line last non-blank",
        },
        Binding {
            key: KeyCode::Char('e'),
            aliases: &[],
            token: M(WordEndBack),
            label: "word end back",
        },
        Binding {
            key: KeyCode::Char('E'),
            aliases: &[],
            token: M(BigWordEndBack),
            label: "WORD end back",
        },
        Binding {
            key: KeyCode::Char('s'),
            aliases: &[],
            token: M(LineFirstNonBlank),
            label: "line start (= ^)",
        },
        Binding {
            key: KeyCode::Char('l'),
            aliases: &[],
            token: M(LineEnd),
            label: "line end (= $)",
        },
        Binding {
            key: KeyCode::Char('c'),
            aliases: &[],
            token: M(ViewportMiddle),
            label: "viewport mid (= M)",
        },
        Binding {
            key: KeyCode::Char('b'),
            aliases: &[],
            token: M(ViewportBottom),
            label: "viewport bot (= L)",
        },
        Binding {
            key: KeyCode::Char('d'),
            aliases: &[],
            token: Dir(D::GotoDefinition),
            label: "definition (lsp)",
        },
        Binding {
            key: KeyCode::Char('D'),
            aliases: &[],
            token: Dir(D::GotoDeclaration),
            label: "declaration (lsp)",
        },
        Binding {
            key: KeyCode::Char('i'),
            aliases: &[],
            token: Dir(D::GotoImplementation),
            label: "implementation (lsp)",
        },
        Binding {
            key: KeyCode::Char('r'),
            aliases: &[],
            token: Dir(D::FindReferences),
            label: "references (lsp)",
        },
        Binding {
            key: KeyCode::Char('w'),
            aliases: &[],
            token: Dir(D::JumpLabel),
            label: "jump label (2-char)",
        },
        Binding {
            key: KeyCode::Char('n'),
            aliases: &[],
            token: Dir(D::SearchSelectNext { reverse: false }),
            label: "select next match",
        },
        Binding {
            key: KeyCode::Char('N'),
            aliases: &[],
            token: Dir(D::SearchSelectNext { reverse: true }),
            label: "select prev match",
        },
        Binding {
            key: KeyCode::Char('A'),
            aliases: &[],
            token: Dir(D::SelectWholeBuffer),
            label: "select whole buffer (= ggVG)",
        },
    ]
};

/// Keys valid in the ZPending context (right after `z`). Same
/// pattern as [`GOTO_BINDINGS`].
pub const Z_BINDINGS: &[Binding] = {
    use crate::action::DirectKind as D;
    use Token::Direct as Dir;
    &[
        Binding {
            key: KeyCode::Char('z'),
            aliases: &[],
            token: Dir(D::ViewportCenter),
            label: "center cursor",
        },
        Binding {
            key: KeyCode::Char('t'),
            aliases: &[],
            token: Dir(D::ViewportTopAtCursor),
            label: "scroll cursor to top",
        },
        Binding {
            key: KeyCode::Char('b'),
            aliases: &[],
            token: Dir(D::ViewportBottomAtCursor),
            label: "scroll cursor to bottom",
        },
    ]
};

/// Default `<space>` leader bindings. Lives here as the single source
/// of truth for both `Keymap::install_vim_defaults` (which copies
/// them into the runtime-mutable Leader HashMap) and the which-key
/// hint renderer.
pub const LEADER_DEFAULTS: &[Binding] = {
    use crate::action::{DirectKind as D, PromptKind};
    use crate::finder::{FuzzyKind, IgnoreOpts};
    use Token::Direct as Dir;
    &[
        Binding {
            key: KeyCode::Char('f'),
            aliases: &[],
            token: Dir(D::OpenPrompt(PromptKind::Fuzzy(FuzzyKind::Files {
                ignore: IgnoreOpts::DEFAULT,
            }))),
            label: "fuzzy files",
        },
        // `<space>F` — same picker, but dotfile segments are kept so
        // `.env`, `.github/...` etc. become visible. `.gitignore` is
        // still honored.
        Binding {
            key: KeyCode::Char('F'),
            aliases: &[],
            token: Dir(D::OpenPrompt(PromptKind::Fuzzy(FuzzyKind::Files {
                ignore: IgnoreOpts::SHOW_HIDDEN,
            }))),
            label: "fuzzy files (+hidden)",
        },
        Binding {
            key: KeyCode::Char('l'),
            aliases: &[],
            token: Dir(D::OpenPrompt(PromptKind::Fuzzy(FuzzyKind::Lines))),
            label: "fuzzy lines",
        },
        Binding {
            key: KeyCode::Char('b'),
            aliases: &[],
            token: Dir(D::OpenPrompt(PromptKind::Fuzzy(FuzzyKind::Buffers))),
            label: "buffer picker",
        },
        Binding {
            key: KeyCode::Char('r'),
            aliases: &[],
            token: Dir(D::Rename),
            label: "rename (lsp)",
        },
        Binding {
            key: KeyCode::Char('a'),
            aliases: &[],
            token: Dir(D::CodeAction),
            label: "code action (lsp)",
        },
        Binding {
            key: KeyCode::Char('K'),
            aliases: &[KeyCode::Char('k')],
            token: Dir(D::Hover),
            label: "hover (lsp)",
        },
        Binding {
            key: KeyCode::Char('c'),
            aliases: &[],
            token: Dir(D::ToggleComment),
            label: "toggle line comment",
        },
        Binding {
            key: KeyCode::Char('*'),
            aliases: &[],
            token: Dir(D::SearchWordKeep { forward: true }),
            label: "search word (keep cursor)",
        },
        Binding {
            key: KeyCode::Char('#'),
            aliases: &[],
            token: Dir(D::SearchWordKeep { forward: false }),
            label: "search word ← (keep cursor)",
        },
        Binding {
            key: KeyCode::Char(','),
            aliases: &[],
            token: Dir(D::MultiCursorClear),
            label: "clear extra cursors",
        },
    ]
};

/// Keys valid in the ObjectExpected context (right after `i`/`a` as
/// the scope marker).
pub const OBJECT_BINDINGS: &[Binding] = {
    use crate::action::Object::*;
    use Token::Object as O;
    &[
        Binding {
            key: KeyCode::Char('w'),
            aliases: &[],
            token: O(Word),
            label: "word",
        },
        Binding {
            key: KeyCode::Char('p'),
            aliases: &[],
            token: O(Paragraph),
            label: "paragraph",
        },
        Binding {
            key: KeyCode::Char('"'),
            aliases: &[],
            token: O(DoubleQuote),
            label: "double-quotes",
        },
        Binding {
            key: KeyCode::Char('\''),
            aliases: &[],
            token: O(SingleQuote),
            label: "single-quotes",
        },
        Binding {
            key: KeyCode::Char('('),
            aliases: &[KeyCode::Char(')'), KeyCode::Char('b')],
            token: O(Paren),
            label: "parens",
        },
        Binding {
            key: KeyCode::Char('{'),
            aliases: &[KeyCode::Char('}'), KeyCode::Char('B')],
            token: O(Brace),
            label: "braces",
        },
        Binding {
            key: KeyCode::Char('['),
            aliases: &[KeyCode::Char(']')],
            token: O(Bracket),
            label: "brackets",
        },
        Binding {
            key: KeyCode::Char('f'),
            aliases: &[],
            token: O(Function),
            label: "function (ts)",
        },
        Binding {
            key: KeyCode::Char('c'),
            aliases: &[],
            token: O(Class),
            label: "class (ts)",
        },
        Binding {
            key: KeyCode::Char('a'),
            aliases: &[],
            token: O(Parameter),
            label: "argument (ts)",
        },
    ]
};

impl Keymap {
    pub(super) fn install_vim_defaults(&mut self) {
        use DirectKind as D;
        use MotionKind as M;
        use Token::*;

        let none = KeyModifiers::NONE;
        let initial = [
            // ── movement ─────────────────────────────────────────────
            (KeyCode::Char('h'), Motion(M::Left)),
            (KeyCode::Left, Motion(M::Left)),
            (KeyCode::Char('l'), Motion(M::Right)),
            (KeyCode::Right, Motion(M::Right)),
            (KeyCode::Char('j'), Motion(M::Down)),
            (KeyCode::Down, Motion(M::Down)),
            (KeyCode::Char('k'), Motion(M::Up)),
            (KeyCode::Up, Motion(M::Up)),
            (KeyCode::Char('$'), Motion(M::LineEnd)),
            (KeyCode::End, Motion(M::LineEnd)),
            (KeyCode::Home, Motion(M::LineStart)),
            (KeyCode::Char('^'), Motion(M::LineFirstNonBlank)),
            (KeyCode::Char('w'), Motion(M::WordForward)),
            (KeyCode::Char('b'), Motion(M::WordBack)),
            (KeyCode::Char('e'), Motion(M::WordEnd)),
            (KeyCode::Char('W'), Motion(M::BigWordForward)),
            (KeyCode::Char('B'), Motion(M::BigWordBack)),
            (KeyCode::Char('E'), Motion(M::BigWordEnd)),
            (KeyCode::Char('{'), Motion(M::ParagraphBack)),
            (KeyCode::Char('}'), Motion(M::ParagraphForward)),
            (KeyCode::Char('G'), Motion(M::FileEnd)),
            (KeyCode::Char('H'), Motion(M::ViewportTop)),
            (KeyCode::Char('M'), Motion(M::ViewportMiddle)),
            (KeyCode::Char('L'), Motion(M::ViewportBottom)),
            (KeyCode::Char('%'), Motion(M::BracketMatch)),
            (KeyCode::Char('*'), Motion(M::SearchWordForward)),
            (KeyCode::Char('#'), Motion(M::SearchWordBack)),
            (KeyCode::Char('n'), Motion(M::SearchNext)),
            (KeyCode::Char('N'), Motion(M::SearchPrev)),
            // ── char-find prefixes (next keystroke is the literal target) ─
            (
                KeyCode::Char('f'),
                FindCharPrefix { forward: true, till: false },
            ),
            (
                KeyCode::Char('F'),
                FindCharPrefix { forward: false, till: false },
            ),
            (
                KeyCode::Char('t'),
                FindCharPrefix { forward: true, till: true },
            ),
            (
                KeyCode::Char('T'),
                FindCharPrefix { forward: false, till: true },
            ),
            (KeyCode::Char(';'), Motion(M::RepeatFind { reverse: false })),
            (KeyCode::Char(','), Motion(M::RepeatFind { reverse: true })),
            // ── operators ────────────────────────────────────────────
            (KeyCode::Char('d'), Op(Operator::Delete)),
            (KeyCode::Char('y'), Op(Operator::Yank)),
            (KeyCode::Char('c'), Op(Operator::Change)),
            (KeyCode::Char('>'), Op(Operator::Indent)),
            (KeyCode::Char('<'), Op(Operator::Dedent)),
            // ── standalone commands ──────────────────────────────────
            (KeyCode::Char('i'), Direct(D::EnterMode(Mode::Insert))),
            (KeyCode::Char('I'), Direct(D::InsertAtLineStart)),
            (KeyCode::Char('a'), Direct(D::AppendAfterCursor)),
            (KeyCode::Char('A'), Direct(D::AppendAtLineEnd)),
            (KeyCode::Char('v'), Direct(D::EnterMode(Mode::Visual))),
            (KeyCode::Char('V'), Direct(D::EnterMode(Mode::VisualLine))),
            (KeyCode::Char('o'), Direct(D::OpenLineBelow)),
            (KeyCode::Char('O'), Direct(D::OpenLineAbove)),
            (KeyCode::Char('x'), Direct(D::DeleteCharUnderCursor)),
            (KeyCode::Char('p'), Direct(D::Paste)),
            (KeyCode::Char('u'), Direct(D::Undo)),
            (KeyCode::Char('C'), Direct(D::ChangeToEol)),
            (KeyCode::Char('D'), Direct(D::DeleteToEol)),
            (KeyCode::Char('Y'), Direct(D::YankLine)),
            (KeyCode::Char('J'), Direct(D::JoinLines)),
            (KeyCode::Char('~'), Direct(D::ToggleCase)),
            (KeyCode::Char('s'), Direct(D::SubstituteChar)),
            (KeyCode::Char('S'), Direct(D::SubstituteLine)),
            (KeyCode::Char('r'), ReplaceCharPrefix),
            (KeyCode::Char('z'), ZPrefix),
            (
                KeyCode::Char(':'),
                Direct(D::OpenPrompt(PromptKind::Command)),
            ),
            (
                KeyCode::Char('/'),
                Direct(D::OpenPrompt(PromptKind::Search { forward: true })),
            ),
            (
                KeyCode::Char('?'),
                Direct(D::OpenPrompt(PromptKind::Search { forward: false })),
            ),
            (KeyCode::Char('.'), Direct(D::RepeatLast)),
            (KeyCode::Char('g'), GotoPrefix),
            // `K` — LSP hover popup for the symbol under the cursor.
            // Matches vim's `K` (which runs `man`-style lookups by
            // default) and helix's binding.
            (KeyCode::Char('K'), Direct(D::Hover)),
            (KeyCode::Char(LEADER), LeaderPrefix),
        ];
        for (code, token) in initial {
            self.bind_initial(KeySig::new(code, none), token);
        }

        // Ctrl-V → visual-block. (Plain `v` and `V` are bound above; the
        // SHIFT modifier on `V` is stripped by `KeySig::new`.)
        self.bind_initial(
            KeySig::new(KeyCode::Char('v'), KeyModifiers::CONTROL),
            Direct(D::EnterMode(Mode::VisualBlock)),
        );
        // Page motions.
        let ctrl = KeyModifiers::CONTROL;
        for (ch, m) in [
            ('d', M::HalfPageDown),
            ('u', M::HalfPageUp),
            ('f', M::PageDown),
            ('b', M::PageUp),
        ] {
            self.bind_initial(KeySig::new(KeyCode::Char(ch), ctrl), Motion(m));
        }

        // Multi-cursor: `+` adds a cursor at the next word match, `-`
        // removes the most recently added one. Bare keys (no Ctrl) so
        // they're trivially reachable inside any terminal multiplexer.
        // Vim's `+` / `-` (first non-blank of next/prev line) are
        // redundant with `j^` / `k^`, so re-purposing them here costs
        // nothing. Clearing all extras is on the leader (`<space>,`)
        // — see LEADER_DEFAULTS.
        self.bind_initial(
            KeySig::new(KeyCode::Char('+'), none),
            Direct(D::MultiCursorAddNext),
        );
        self.bind_initial(
            KeySig::new(KeyCode::Char('-'), none),
            Direct(D::MultiCursorPop),
        );

        // Leader bindings — single source of truth in LEADER_DEFAULTS.
        for b in LEADER_DEFAULTS {
            self.bind_leader(KeySig::new(b.key, none), b.token);
            for &alias in b.aliases {
                self.bind_leader(KeySig::new(alias, none), b.token);
            }
        }
    }
}