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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Modeless, VSCode-style keymap. Typing inserts; arrows move (Shift extends a
//! selection); Ctrl+C/X/V/Z/Y/A do the usual; Ctrl+←/→ are word motions;
//! Ctrl+Backspace/Del delete words; Ctrl+/ toggles a line comment; Alt+↑/↓ move
//! the line; Ctrl+S saves; Esc clears a selection (then falls through so the
//! tree gets focus).
//!
//! `[keys.standard]` config overlays: any entry there is checked FIRST, so a
//! user can rebind chords or unbind them entirely without touching the code.
//! Context-sensitive behaviors (smart Ctrl+C, Shift+arrow selection extend,
//! Tab-with-selection = Indent, Esc-with-selection = Clear) stay hardcoded —
//! they can't be expressed as a static chord → op mapping. Overrides for those
//! chords still work; the hardcoded smart behavior is only the fallback.
use std::collections::HashMap;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crate::config::Config;
use crate::edit_op::EditOp;
use crate::input::keymap::{Chord, parse_key_spec};
use crate::input::{AppCommand, EditCtx, EditingMode, InputHandler, InputResult};
/// What a `[keys.standard]` entry can bind a chord to. Constructed by parsing
/// the config value string via `StandardAction::parse`.
#[derive(Debug, Clone)]
enum StandardAction {
/// Fire one EditOp.
Op(EditOp),
/// Fire an ordered sequence of EditOps.
Ops(Vec<EditOp>),
/// Escalate to an App-level command (`Save`).
App(AppCommand),
/// Explicit unbind — the chord is silently ignored, no fallback to
/// the hardcoded logic. Config value `"none"` / `"unbound"` / `""`.
Unbound,
}
impl StandardAction {
/// Vocabulary the config accepts. Case-insensitive. Composite / smart
/// actions (Ctrl+C context-sensitive yank, Tab-with-selection Indent)
/// aren't in the vocabulary — they can only be OVERRIDDEN, not
/// invoked by name.
fn parse(s: &str) -> Option<StandardAction> {
use EditOp::*;
let t = s.trim().to_ascii_lowercase();
// Composite: `"move_line_end; insert_newline"` produces an ordered
// sequence. Lets users configure `ctrl+enter` = open-new-line-below
// (VS Code's Ctrl+Enter behavior) without a special action name.
if t.contains(';') {
let mut ops: Vec<EditOp> = Vec::new();
for part in t.split(';') {
match StandardAction::parse(part)? {
StandardAction::Op(op) => ops.push(op),
_ => return None,
}
}
return Some(if ops.len() == 1 {
StandardAction::Op(ops.remove(0))
} else {
StandardAction::Ops(ops)
});
}
Some(match t.as_str() {
"" | "none" | "unbound" => StandardAction::Unbound,
"app.save" | "save" => StandardAction::App(AppCommand::Save),
"select_all" => StandardAction::Op(SelectAll),
"select_word" => StandardAction::Op(SelectWord),
"select_line_to_end" => StandardAction::Op(SelectLineToEnd),
"select_clear" => StandardAction::Op(SelectClear),
"yank_selection" => StandardAction::Op(YankSelection),
"yank_line" => StandardAction::Op(YankLine),
"cut_selection" => StandardAction::Op(CutSelection),
"paste" => StandardAction::Op(Paste),
"undo" => StandardAction::Op(Undo),
"redo" => StandardAction::Op(Redo),
"delete_line" => StandardAction::Op(DeleteLine),
"duplicate_line" => StandardAction::Op(DuplicateLine),
"toggle_line_comment" => StandardAction::Op(ToggleLineComment),
"indent" => StandardAction::Op(Indent),
"outdent" => StandardAction::Op(Outdent),
"backspace" => StandardAction::Op(Backspace),
"delete_forward" => StandardAction::Op(DeleteForward),
"delete_word_left" => StandardAction::Op(DeleteWordLeft),
"delete_word_right" => StandardAction::Op(DeleteWordRight),
"move_left" => StandardAction::Op(MoveLeft),
"move_right" => StandardAction::Op(MoveRight),
"move_up" => StandardAction::Op(MoveUp),
"move_down" => StandardAction::Op(MoveDown),
"move_word_left" => StandardAction::Op(MoveWordLeft),
"move_word_right" => StandardAction::Op(MoveWordRight),
"move_line_up" => StandardAction::Op(MoveLineUp),
"move_line_down" => StandardAction::Op(MoveLineDown),
"move_line_start" => StandardAction::Op(MoveLineStart),
"move_line_end" => StandardAction::Op(MoveLineEnd),
"move_buffer_start" => StandardAction::Op(MoveBufferStart),
"move_buffer_end" => StandardAction::Op(MoveBufferEnd),
"page_up" => StandardAction::Op(PageUp),
"page_down" => StandardAction::Op(PageDown),
"insert_newline" => StandardAction::Op(InsertNewline),
_ => return None,
})
}
}
#[derive(Debug)]
pub struct StandardInputHandler {
tab_width: usize,
/// `[keys.standard]` overrides — user rebindings. Consulted BEFORE
/// the hardcoded logic so users can remap or unbind any chord.
/// Parsed once at construction. Empty when no config entries exist.
overrides: HashMap<Chord, StandardAction>,
}
impl StandardInputHandler {
pub fn new(cfg: &Config) -> Self {
// Merge `[keys.global]` + `[keys.standard]` in that order so a
// standard-specific override wins. Same precedence as the
// app-level `Keymap::build`.
let mut overrides: HashMap<Chord, StandardAction> = HashMap::new();
for section in ["global", "standard"] {
let Some(entries) = cfg.keys.get(section) else {
continue;
};
for (spec, action) in entries {
let Some(ev) = parse_key_spec(spec) else {
eprintln!("mnml: [keys.{section}] `{spec}` doesn't parse as a chord — ignored",);
continue;
};
let Some(parsed) = StandardAction::parse(action) else {
// Unknown action names go to the app-level Keymap,
// not here. Skip silently so app.* commands don't
// trigger a warning.
continue;
};
overrides.insert(Chord::of(&ev), parsed);
}
}
StandardInputHandler {
tab_width: cfg.editor.tab_width.max(1),
overrides,
}
}
/// Look up a chord in the config overrides. Returns:
/// - `Some(InputResult::…)` — fire the override (may be Ignored if unbound).
/// - `None` — no override registered; caller falls through to hardcoded logic.
fn override_lookup(&self, key: &KeyEvent) -> Option<InputResult> {
let chord = Chord::of(key);
Some(match self.overrides.get(&chord)? {
StandardAction::Op(op) => InputResult::Ops(vec![op.clone()]),
StandardAction::Ops(ops) => InputResult::Ops(ops.clone()),
StandardAction::App(cmd) => InputResult::App(cmd.clone()),
StandardAction::Unbound => InputResult::Ignored,
})
}
}
impl InputHandler for StandardInputHandler {
fn handle_key(&mut self, key: KeyEvent, ctx: &EditCtx) -> InputResult {
use EditOp::*;
// Config-driven overrides win over the hardcoded logic. Any chord
// registered in `[keys.global]` or `[keys.standard]` fires the
// configured action (or `Ignored` when unbound), so users can
// rebind chords WITHOUT touching source. Plain typed characters
// (no modifiers) bypass the override table so a stray
// `"a" = "cut_selection"` in the config doesn't turn a-key into
// a cut — chords the override table handles are the ones with
// at least one modifier or a named key.
let is_plain_typed = matches!(key.code, KeyCode::Char(_))
&& !key.modifiers.contains(KeyModifiers::CONTROL)
&& !key.modifiers.contains(KeyModifiers::ALT)
&& !key.modifiers.contains(KeyModifiers::SUPER);
if !is_plain_typed && let Some(result) = self.override_lookup(&key) {
return result;
}
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let alt = key.modifiers.contains(KeyModifiers::ALT);
let shift = key.modifiers.contains(KeyModifiers::SHIFT);
// A plain typed character (no Ctrl/Alt/Super). vscode-user SEV-2 —
// SUPER guard prevents Cmd+letter from inserting on Kitty /
// WezTerm / any macOS terminal that forwards SUPER (the user
// means to hit Cmd+P, gets a literal `p` in their file).
if let KeyCode::Char(c) = key.code
&& !ctrl
&& !alt
&& !key.modifiers.contains(KeyModifiers::SUPER)
{
return InputResult::Ops(vec![InsertChar(c)]);
}
// A motion that should extend the selection when Shift is held, else replace it.
let mv = |op: EditOp| -> InputResult {
if shift {
if ctx.has_selection {
InputResult::Ops(vec![op])
} else {
InputResult::Ops(vec![SelectStart, op])
}
} else {
InputResult::Ops(vec![SelectClear, op])
}
};
match key.code {
KeyCode::Char(c) if ctrl && !alt => match c.to_ascii_lowercase() {
'a' => InputResult::Ops(vec![SelectAll]),
'c' => InputResult::Ops(vec![if ctx.has_selection {
YankSelection
} else {
YankLine
}]),
'x' => InputResult::Ops(if ctx.has_selection {
vec![CutSelection]
} else {
vec![YankLine, DeleteLine]
}),
'v' => InputResult::Ops(vec![Paste]),
'z' if shift => InputResult::Ops(vec![Redo]),
'z' => InputResult::Ops(vec![Undo]),
'y' => InputResult::Ops(vec![Redo]),
'/' => InputResult::Ops(vec![ToggleLineComment]),
's' => InputResult::App(AppCommand::Save),
// vscode-user-keyboard SEV-2 2026-07-11: Ctrl+Shift+D
// used to silently duplicate the current line. VS Code
// binds Ctrl+Shift+D to "Show Run and Debug view" —
// a reflexive user got a stealth line-duplicate on
// their file. Shift+Alt+↓ already covers DuplicateLine
// (line 272 below), so the Ctrl+Shift+D alias is
// redundant. Ignore it and let the palette own the
// "run and debug" action for now.
'd' if shift => InputResult::Ignored,
// keyboard-round-9 SEV-2 2026-07-12 — was
// `SelectWord` unconditionally, which shadowed the
// registered `editor.add_cursor_at_next_word`
// command's `ctrl+d` binding. VS Code's canonical
// behavior: first Ctrl+D selects the word; every
// subsequent Ctrl+D adds a cursor at the next
// occurrence. Route to the palette command so both
// presses do the right thing — command dispatch
// handles the "first vs subsequent" state via
// Editor.multi_cursor state.
'd' => InputResult::App(AppCommand::RunCommand(
"editor.add_cursor_at_next_word".into(),
)),
// qa-7th vscode SEV-2 2026-06-30 — was SelectLine
// (vim V semantics — cursor stays put, only
// line_start..cursor highlights). Standard mode
// matches VS Code: cursor jumps to line end so
// the WHOLE line shows selected regardless of
// cursor column.
'l' => InputResult::Ops(vec![SelectLineToEnd]),
'g' => InputResult::Ignored, // "go to line" → palette/prompt later
_ => InputResult::Ignored,
},
// `Ctrl+Enter` = open new line below (cursor lands at start of
// the new line, indented to match the current line). VS Code
// muscle memory. `Ctrl+Shift+Enter` opens above. Plain Enter is
// the standard newline-at-cursor.
KeyCode::Enter if ctrl && !alt && key.modifiers.contains(KeyModifiers::SHIFT) => {
InputResult::Ops(vec![MoveLineStart, InsertNewline, MoveUp])
}
KeyCode::Enter if ctrl && !alt => InputResult::Ops(vec![MoveLineEnd, InsertNewline]),
KeyCode::Enter => InputResult::Ops(vec![InsertNewline]),
KeyCode::Tab => {
if ctx.has_selection {
InputResult::Ops(vec![Indent])
} else {
InputResult::Ops(vec![InsertStr(" ".repeat(self.tab_width))])
}
}
KeyCode::BackTab => InputResult::Ops(vec![Outdent]),
KeyCode::Backspace if ctrl && !alt => InputResult::Ops(vec![DeleteWordLeft]),
KeyCode::Backspace => InputResult::Ops(vec![Backspace]),
KeyCode::Delete if ctrl && !alt => InputResult::Ops(vec![DeleteWordRight]),
KeyCode::Delete => InputResult::Ops(vec![DeleteForward]),
KeyCode::Left if ctrl && !alt => mv(MoveWordLeft),
KeyCode::Right if ctrl && !alt => mv(MoveWordRight),
KeyCode::Left => mv(MoveLeft),
KeyCode::Right => mv(MoveRight),
// VS Code parity (bug-hunt seed #274 from 2026-06-07):
// Shift+Alt+↓ / ↑ duplicates the current line down / up.
// Plain Alt+↓ / ↑ stays "move line" — distinct from
// Ctrl+Shift+D (which also duplicates but doesn't choose
// direction).
KeyCode::Up if alt && shift => InputResult::Ops(vec![DuplicateLine, MoveUp]),
KeyCode::Down if alt && shift => InputResult::Ops(vec![DuplicateLine]),
KeyCode::Up if alt => InputResult::Ops(vec![MoveLineUp]),
KeyCode::Down if alt => InputResult::Ops(vec![MoveLineDown]),
// Vim-aligned aliases for line shift in standard mode too.
KeyCode::Char('k' | 'K') if alt => InputResult::Ops(vec![MoveLineUp]),
KeyCode::Char('j' | 'J') if alt => InputResult::Ops(vec![MoveLineDown]),
KeyCode::Up => mv(MoveUp),
KeyCode::Down => mv(MoveDown),
KeyCode::Home if ctrl && !alt => mv(MoveBufferStart),
// vscode-user 2026-06-28 SEV-2: Ctrl+End in VS Code
// lands at the END of the last line, not the start.
// MoveBufferEnd is vim G semantics (start of last
// line, intentional). Compose with MoveLineEnd for the
// standard-mode meaning.
//
// vscode-user 2026-07-10 SEV-2 follow-up: this branch
// used to bypass the `mv` helper entirely, so
// Ctrl+Shift+End didn't extend the selection (missing
// SelectStart) and plain Ctrl+End didn't clear a
// pre-existing selection (missing SelectClear). Mirror
// `mv`'s shift/has_selection logic but keep the two-op
// motion composition.
KeyCode::End if ctrl && !alt => {
let motion = vec![MoveBufferEnd, MoveLineEnd];
if shift {
if ctx.has_selection {
InputResult::Ops(motion)
} else {
let mut ops = vec![SelectStart];
ops.extend(motion);
InputResult::Ops(ops)
}
} else {
let mut ops = vec![SelectClear];
ops.extend(motion);
InputResult::Ops(ops)
}
}
// keyboard-round-14 SEV-3 #7 2026-07-17 — VS Code
// Smart Home. First Home from anywhere past leading-ws
// jumps to first-non-ws; second Home (cursor already
// AT first-non-ws) jumps to col 0. Only fires the
// smart toggle when the line has leading whitespace;
// otherwise straight to col 0 (no wasted keypress).
KeyCode::Home => {
if ctx.line_first_nonws_col > 0 && ctx.cursor_col != ctx.line_first_nonws_col {
mv(MoveLineFirstNonWs)
} else {
mv(MoveLineStart)
}
}
KeyCode::End => mv(MoveLineEnd),
KeyCode::PageUp => mv(PageUp),
KeyCode::PageDown => mv(PageDown),
KeyCode::Esc => {
// keyboard-round-14 SEV-2 2026-07-16 — Esc after
// `Ctrl+Shift+L` (multi-cursor select-all-occurrences)
// used to fire only `SelectClear` because the primary
// cursor DID have a selection. The extras stayed
// armed → typing after Esc still fanned out. Match
// VS Code: Esc exits multi-cursor mode too, so emit
// both. The `ClearExtraCursors` case reached from the
// Ignored branch elsewhere in the app is redundant
// now that both ops are emitted here.
if ctx.has_selection {
InputResult::Ops(vec![SelectClear, ClearExtraCursors])
} else {
InputResult::Ops(vec![ClearExtraCursors])
}
}
_ => InputResult::Ignored,
}
}
fn mode(&self) -> EditingMode {
EditingMode::None
}
fn name(&self) -> &'static str {
"standard"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use ratatui::crossterm::event::KeyEvent;
fn h() -> StandardInputHandler {
StandardInputHandler::new(&Config::default())
}
fn ctx(has_sel: bool) -> EditCtx {
EditCtx {
cursor: 0,
line_len: 0,
line_idx: 0,
line_count: 1,
at_line_start: true,
at_line_end: true,
has_selection: has_sel,
line_first_nonws_col: 0,
cursor_col: 0,
next_find_match: None,
prev_find_match: None,
wrap_width: None,
}
}
fn key(code: KeyCode, mods: KeyModifiers) -> KeyEvent {
KeyEvent::new(code, mods)
}
fn ops(r: InputResult) -> Vec<EditOp> {
match r {
InputResult::Ops(v) => v,
_ => panic!("expected Ops"),
}
}
#[test]
fn typing_inserts() {
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('a'), KeyModifiers::NONE), &ctx(false))),
vec![EditOp::InsertChar('a')]
);
// Shift'd capital still inserts.
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('A'), KeyModifiers::SHIFT), &ctx(false))),
vec![EditOp::InsertChar('A')]
);
}
#[test]
fn enter_backspace_tab() {
assert_eq!(
ops(h().handle_key(key(KeyCode::Enter, KeyModifiers::NONE), &ctx(false))),
vec![EditOp::InsertNewline]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Backspace, KeyModifiers::NONE), &ctx(false))),
vec![EditOp::Backspace]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Tab, KeyModifiers::NONE), &ctx(false))),
vec![EditOp::InsertStr(" ".to_string())]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Tab, KeyModifiers::NONE), &ctx(true))),
vec![EditOp::Indent]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::BackTab, KeyModifiers::NONE), &ctx(false))),
vec![EditOp::Outdent]
);
}
#[test]
fn arrows_and_selection() {
// plain arrow clears any selection then moves
assert_eq!(
ops(h().handle_key(key(KeyCode::Left, KeyModifiers::NONE), &ctx(true))),
vec![EditOp::SelectClear, EditOp::MoveLeft]
);
// shift arrow with no selection: start one, then move
assert_eq!(
ops(h().handle_key(key(KeyCode::Right, KeyModifiers::SHIFT), &ctx(false))),
vec![EditOp::SelectStart, EditOp::MoveRight]
);
// shift arrow with an active selection: just extend
assert_eq!(
ops(h().handle_key(key(KeyCode::Right, KeyModifiers::SHIFT), &ctx(true))),
vec![EditOp::MoveRight]
);
// ctrl arrow → word motion
assert_eq!(
ops(h().handle_key(key(KeyCode::Left, KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::SelectClear, EditOp::MoveWordLeft]
);
// alt up/down → move line
assert_eq!(
ops(h().handle_key(key(KeyCode::Up, KeyModifiers::ALT), &ctx(false))),
vec![EditOp::MoveLineUp]
);
}
#[test]
fn clipboard_and_history() {
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('a'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::SelectAll]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('c'), KeyModifiers::CONTROL), &ctx(true))),
vec![EditOp::YankSelection]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('c'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::YankLine]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('x'), KeyModifiers::CONTROL), &ctx(true))),
vec![EditOp::CutSelection]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('x'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::YankLine, EditOp::DeleteLine]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::Paste]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('z'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::Undo]
);
assert_eq!(
ops(h().handle_key(
key(
KeyCode::Char('z'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
)),
vec![EditOp::Redo]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('y'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::Redo]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Char('/'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::ToggleLineComment]
);
}
#[test]
fn ctrl_s_is_save_app_command() {
match h().handle_key(key(KeyCode::Char('s'), KeyModifiers::CONTROL), &ctx(false)) {
InputResult::App(AppCommand::Save) => {}
_ => panic!("Ctrl+S should be App(Save)"),
}
}
#[test]
fn esc_clears_selection_and_extra_cursors() {
// keyboard-round-14 SEV-2 2026-07-16 — Esc now also clears
// extra cursors so the dormant-extras-fan-out bug after
// Ctrl+Shift+L is fixed. Both branches always emit
// ClearExtraCursors.
assert_eq!(
ops(h().handle_key(key(KeyCode::Esc, KeyModifiers::NONE), &ctx(true))),
vec![EditOp::SelectClear, EditOp::ClearExtraCursors]
);
assert_eq!(
ops(h().handle_key(key(KeyCode::Esc, KeyModifiers::NONE), &ctx(false))),
vec![EditOp::ClearExtraCursors]
);
}
#[test]
fn mode_is_none() {
assert_eq!(h().mode(), EditingMode::None);
}
/// Regression for the 2026-06-07 bug-hunt SEV-2 finding: any
/// extra modifier (Alt, Super) on top of Ctrl+<char> used to
/// silently fall into the Ctrl+<char> arm — e.g. Ctrl+Alt+X cut
/// the current line, Ctrl+Alt+A selected all, Ctrl+Alt+S saved.
/// macOS keyboards emit Ctrl+Alt+* for OS-level shortcuts; the
/// leak was easy to hit accidentally.
#[test]
fn ctrl_plus_alt_is_not_treated_as_plain_ctrl() {
let cases = ['a', 'c', 'x', 'v', 'z', 'y', 's', 'd', 'l', '/'];
for c in cases {
let r = h().handle_key(
key(KeyCode::Char(c), KeyModifiers::CONTROL | KeyModifiers::ALT),
&ctx(false),
);
assert!(
matches!(r, InputResult::Ignored),
"Ctrl+Alt+{c} should be Ignored, was not"
);
}
// Named keys (Enter, Backspace, Delete, arrows, Home, End)
// also have ctrl arms — those now require `!alt` too, so
// Ctrl+Alt+<key> doesn't execute the ctrl version. The named
// keys fall through to their no-modifier handlers (e.g.
// Ctrl+Alt+Enter ⇒ plain InsertNewline) instead of doing the
// word-jump / line-end action. Tested separately:
let r = h().handle_key(
key(KeyCode::Left, KeyModifiers::CONTROL | KeyModifiers::ALT),
&ctx(false),
);
// Ctrl+Left = MoveWordLeft; Ctrl+Alt+Left should NOT word-jump.
// It falls through to plain MoveLeft (cursor-left-one).
let want_one_cell_left = vec![EditOp::SelectClear, EditOp::MoveLeft];
assert_eq!(
ops(r),
want_one_cell_left,
"Ctrl+Alt+Left should be a plain MoveLeft, not MoveWordLeft"
);
}
/// Shift+Alt+↓ / ↑ duplicate the current line (VS Code parity,
/// bug-hunt seed #274). Plain Alt+↓ / ↑ still moves the line —
/// the Shift modifier is what distinguishes "duplicate" from
/// "move."
#[test]
fn shift_alt_down_duplicates_line() {
let mods = KeyModifiers::SHIFT | KeyModifiers::ALT;
assert_eq!(
ops(h().handle_key(key(KeyCode::Down, mods), &ctx(false))),
vec![EditOp::DuplicateLine]
);
// Shift+Alt+Up: duplicate, then move up to land cursor on
// the new copy above (matches VS Code's "duplicate up").
assert_eq!(
ops(h().handle_key(key(KeyCode::Up, mods), &ctx(false))),
vec![EditOp::DuplicateLine, EditOp::MoveUp]
);
// Plain Alt+Down still moves the line (didn't regress).
assert_eq!(
ops(h().handle_key(key(KeyCode::Down, KeyModifiers::ALT), &ctx(false))),
vec![EditOp::MoveLineDown]
);
}
/// Ctrl+Shift+<char> still works for the arms that explicitly
/// opt into shift (Ctrl+Shift+Z = Redo, Ctrl+Shift+D =
/// DuplicateLine) — the modifier-leak fix only blocks Alt.
#[test]
fn ctrl_plus_shift_still_dispatches() {
// Ctrl+Shift+Z → Redo
assert_eq!(
ops(h().handle_key(
key(
KeyCode::Char('z'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
)),
vec![EditOp::Redo]
);
// Ctrl+Shift+D → Ignored (vscode-user-keyboard SEV-2 2026-07-11
// — used to duplicate the line, but VS Code binds it to
// "Show Run and Debug view." Shift+Alt+↓ keeps DuplicateLine).
assert!(matches!(
h().handle_key(
key(
KeyCode::Char('d'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
),
InputResult::Ignored
));
}
// ── [keys.standard] config overrides ──────────────────────────
fn cfg_with_binding(spec: &str, action: &str) -> Config {
let mut cfg = Config::default();
let mut section = std::collections::BTreeMap::new();
section.insert(spec.to_string(), action.to_string());
cfg.keys.insert("standard".to_string(), section);
cfg
}
#[test]
fn config_override_replaces_a_default_chord() {
// Rebind Ctrl+A from SelectAll to Undo — deliberately weird so
// the test proves the override wins.
let cfg = cfg_with_binding("ctrl+a", "undo");
let mut h = StandardInputHandler::new(&cfg);
assert_eq!(
ops(h.handle_key(key(KeyCode::Char('a'), KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::Undo]
);
}
#[test]
fn config_override_none_unbinds_a_chord() {
let cfg = cfg_with_binding("ctrl+z", "none");
let mut h = StandardInputHandler::new(&cfg);
assert!(matches!(
h.handle_key(key(KeyCode::Char('z'), KeyModifiers::CONTROL), &ctx(false)),
InputResult::Ignored
));
}
#[test]
fn config_override_supports_composite_sequences() {
// VS Code's `ctrl+enter` = "move to line end, then insert newline".
let cfg = cfg_with_binding("ctrl+enter", "move_line_end; insert_newline");
let mut h = StandardInputHandler::new(&cfg);
assert_eq!(
ops(h.handle_key(key(KeyCode::Enter, KeyModifiers::CONTROL), &ctx(false))),
vec![EditOp::MoveLineEnd, EditOp::InsertNewline]
);
}
#[test]
fn config_override_leaves_plain_typing_untouched() {
// Even if the user tries to bind `a` to something, plain
// character keys should still insert their literal char.
let cfg = cfg_with_binding("a", "undo");
let mut h = StandardInputHandler::new(&cfg);
assert_eq!(
ops(h.handle_key(key(KeyCode::Char('a'), KeyModifiers::NONE), &ctx(false))),
vec![EditOp::InsertChar('a')]
);
}
#[test]
fn config_override_can_bind_a_new_chord() {
// Chord that has no default binding in the standard handler:
// Ctrl+Shift+K. Should be Ignored without config, and the
// configured action with config.
let mut without = StandardInputHandler::new(&Config::default());
assert!(matches!(
without.handle_key(
key(
KeyCode::Char('k'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
),
InputResult::Ignored
));
let cfg = cfg_with_binding("ctrl+shift+k", "delete_line");
let mut with = StandardInputHandler::new(&cfg);
assert_eq!(
ops(with.handle_key(
key(
KeyCode::Char('k'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
)),
vec![EditOp::DeleteLine]
);
}
#[test]
fn global_section_bindings_apply_to_standard_handler() {
// `[keys.global]` also overlays — a global entry should
// reach the standard handler.
let mut cfg = Config::default();
let mut section = std::collections::BTreeMap::new();
section.insert("ctrl+shift+p".to_string(), "duplicate_line".to_string());
cfg.keys.insert("global".to_string(), section);
let mut h = StandardInputHandler::new(&cfg);
assert_eq!(
ops(h.handle_key(
key(
KeyCode::Char('p'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT
),
&ctx(false)
)),
vec![EditOp::DuplicateLine]
);
}
}