ratasm 0.1.1

A terminal IDE and debugger for x86_64 assembly
Documentation
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! Key bindings, and validating that they do not conflict.
//!
//! A binding is written the way people say it out loud: `ctrl+s`, `F5`,
//! `shift+tab`. Parsing is deliberately forgiving about case and about
//! `control`/`ctrl`, because a configuration file that rejects `Ctrl+S` for
//! being capitalised is annoying for no benefit.
//!
//! # Conflicts are reported, not resolved
//!
//! Two commands bound to the same chord is a mistake the user wants to know
//! about. Silently letting one shadow the other produces a key that
//! mysteriously does the wrong thing, so [`Keymap::conflicts`] finds them and
//! start-up reports them.
//!
//! The one intentional exception is a binding that *replaces* a default: that
//! is a user overriding, not a conflict, and it is how customisation works.

use std::collections::{BTreeMap, HashMap};
use std::fmt;

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

use crate::app::page::Page;
use crate::command::Command;

/// A key plus its modifiers.
///
/// Not ordered: crossterm's `KeyCode` has no `Ord`, so keymaps are hashed and
/// sorted by their rendered text when an order is needed for display.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyBinding {
    /// The key itself.
    pub code: KeyCode,
    /// The modifiers held with it.
    pub modifiers: KeyModifiers,
}

impl KeyBinding {
    /// Creates a binding.
    pub const fn new(code: KeyCode, modifiers: KeyModifiers) -> Self {
        Self { code, modifiers }
    }

    /// A binding with no modifiers.
    pub const fn plain(code: KeyCode) -> Self {
        Self::new(code, KeyModifiers::NONE)
    }

    /// A binding with control held.
    pub const fn ctrl(code: KeyCode) -> Self {
        Self::new(code, KeyModifiers::CONTROL)
    }

    /// Builds a binding from a terminal key event.
    ///
    /// Runs the same normalisation as [`parse_binding`], so a chord written in
    /// a configuration file matches the event the terminal actually sends.
    pub fn from_event(event: KeyEvent) -> Self {
        let (code, modifiers) = normalise(event.code, event.modifiers);
        Self::new(code, modifiers)
    }
}

/// Puts a key and its modifiers into the one canonical form.
///
/// Terminals encode shift into the character itself — `shift+p` arrives as
/// `P` *and* the shift modifier — so a binding written either way has to end
/// up identical or it can never match. Two rules do that:
///
/// - shift is dropped for character keys, because the character already
///   carries it;
/// - a character combined with control or alt is lowercased, so `ctrl+shift+p`
///   and the `Ctrl+P` event the terminal sends are the same binding.
///
/// A plain character keeps its case, because in the editor `A` and `a` are
/// genuinely different input.
fn normalise(code: KeyCode, modifiers: KeyModifiers) -> (KeyCode, KeyModifiers) {
    let mut modifiers = modifiers;

    // Terminals send shift+tab as BackTab with no modifier, so both spellings
    // canonicalise to that. Without this, a configuration file saying
    // "shift+tab" would never match the key the terminal actually reports.
    if code == KeyCode::BackTab || (code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT))
    {
        modifiers.remove(KeyModifiers::SHIFT);
        return (KeyCode::BackTab, modifiers);
    }

    let KeyCode::Char(ch) = code else {
        return (code, modifiers);
    };

    let shifted = modifiers.contains(KeyModifiers::SHIFT);
    modifiers.remove(KeyModifiers::SHIFT);

    let is_shortcut = modifiers.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT);
    let ch = if is_shortcut || shifted && is_shortcut {
        ch.to_ascii_lowercase()
    } else {
        ch
    };

    (KeyCode::Char(ch), modifiers)
}

impl fmt::Display for KeyBinding {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.modifiers.contains(KeyModifiers::CONTROL) {
            f.write_str("ctrl+")?;
        }
        if self.modifiers.contains(KeyModifiers::ALT) {
            f.write_str("alt+")?;
        }
        if self.modifiers.contains(KeyModifiers::SHIFT) {
            f.write_str("shift+")?;
        }
        f.write_str(&code_name(self.code))
    }
}

/// Renders a key code as the name used in configuration files.
fn code_name(code: KeyCode) -> String {
    match code {
        KeyCode::Char(' ') => "space".to_owned(),
        KeyCode::Char(ch) => ch.to_string(),
        KeyCode::F(number) => format!("F{number}"),
        KeyCode::Enter => "enter".to_owned(),
        KeyCode::Tab => "tab".to_owned(),
        KeyCode::BackTab => "shift+tab".to_owned(),
        KeyCode::Backspace => "backspace".to_owned(),
        KeyCode::Delete => "delete".to_owned(),
        KeyCode::Insert => "insert".to_owned(),
        KeyCode::Home => "home".to_owned(),
        KeyCode::End => "end".to_owned(),
        KeyCode::PageUp => "pageup".to_owned(),
        KeyCode::PageDown => "pagedown".to_owned(),
        KeyCode::Up => "up".to_owned(),
        KeyCode::Down => "down".to_owned(),
        KeyCode::Left => "left".to_owned(),
        KeyCode::Right => "right".to_owned(),
        KeyCode::Esc => "esc".to_owned(),
        other => format!("{other:?}").to_lowercase(),
    }
}

/// Why a key binding could not be parsed.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum KeyParseError {
    /// The text was empty.
    #[error("a key binding cannot be empty")]
    Empty,
    /// A modifier was not recognised.
    #[error("'{modifier}' is not a modifier (expected ctrl, alt or shift)")]
    UnknownModifier {
        /// The offending word.
        modifier: String,
    },
    /// The key itself was not recognised.
    #[error("'{key}' is not a key name")]
    UnknownKey {
        /// The offending word.
        key: String,
    },
}

/// Parses a binding such as `ctrl+shift+p`.
///
/// # Errors
///
/// Returns [`KeyParseError`] naming the part that was not understood, so a
/// typo in a configuration file points at itself.
pub fn parse_binding(text: &str) -> Result<KeyBinding, KeyParseError> {
    let text = text.trim();
    if text.is_empty() {
        return Err(KeyParseError::Empty);
    }

    let parts: Vec<&str> = text.split('+').map(str::trim).collect();
    // A lone "+" is the plus key, not an empty separator.
    let (key_part, modifier_parts) = match parts.split_last() {
        Some((last, rest)) if !last.is_empty() => (*last, rest),
        _ => ("+", &parts[..parts.len().saturating_sub(2)]),
    };

    let mut modifiers = KeyModifiers::NONE;
    for part in modifier_parts {
        match part.to_ascii_lowercase().as_str() {
            "ctrl" | "control" | "c" => modifiers |= KeyModifiers::CONTROL,
            "alt" | "meta" | "m" => modifiers |= KeyModifiers::ALT,
            "shift" | "s" => modifiers |= KeyModifiers::SHIFT,
            other => {
                return Err(KeyParseError::UnknownModifier {
                    modifier: other.to_owned(),
                })
            }
        }
    }

    let code = parse_code(key_part)?;
    let (code, modifiers) = normalise(code, modifiers);
    Ok(KeyBinding::new(code, modifiers))
}

/// Parses the key part of a binding.
fn parse_code(text: &str) -> Result<KeyCode, KeyParseError> {
    let lowered = text.to_ascii_lowercase();

    if let Some(number) = lowered.strip_prefix('f') {
        if let Ok(number) = number.parse::<u8>() {
            if (1..=12).contains(&number) {
                return Ok(KeyCode::F(number));
            }
        }
    }

    Ok(match lowered.as_str() {
        "enter" | "return" => KeyCode::Enter,
        "tab" => KeyCode::Tab,
        "backtab" => KeyCode::BackTab,
        "backspace" | "bs" => KeyCode::Backspace,
        "delete" | "del" => KeyCode::Delete,
        "insert" | "ins" => KeyCode::Insert,
        "home" => KeyCode::Home,
        "end" => KeyCode::End,
        "pageup" | "pgup" => KeyCode::PageUp,
        "pagedown" | "pgdn" => KeyCode::PageDown,
        "up" => KeyCode::Up,
        "down" => KeyCode::Down,
        "left" => KeyCode::Left,
        "right" => KeyCode::Right,
        "esc" | "escape" => KeyCode::Esc,
        "space" => KeyCode::Char(' '),
        _ => {
            let mut chars = text.chars();
            match (chars.next(), chars.next()) {
                // The character as written; normalise decides whether case
                // is significant.
                (Some(ch), None) => KeyCode::Char(ch),
                _ => {
                    return Err(KeyParseError::UnknownKey {
                        key: text.to_owned(),
                    })
                }
            }
        }
    })
}

/// A binding bound to two different commands.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Conflict {
    /// The chord that is bound twice.
    pub binding: KeyBinding,
    /// The commands competing for it.
    pub commands: Vec<Command>,
}

impl fmt::Display for Conflict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let names: Vec<String> = self.commands.iter().map(Command::id).collect();
        write!(f, "{} is bound to {}", self.binding, names.join(" and "))
    }
}

/// The mapping from key chords to commands.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Keymap {
    bindings: HashMap<KeyBinding, Command>,
}

impl Keymap {
    /// The built-in bindings.
    pub fn defaults() -> Self {
        let mut bindings = HashMap::new();
        let mut bind = |binding: KeyBinding, command: Command| {
            bindings.insert(binding, command);
        };

        // Running and debugging, on the function keys.
        bind(KeyBinding::plain(KeyCode::F(5)), Command::Run);
        bind(KeyBinding::plain(KeyCode::F(6)), Command::Build);
        bind(KeyBinding::plain(KeyCode::F(7)), Command::StepInstruction);
        bind(KeyBinding::plain(KeyCode::F(8)), Command::StepOver);
        bind(KeyBinding::plain(KeyCode::F(9)), Command::ToggleBreakpoint);
        bind(KeyBinding::plain(KeyCode::F(10)), Command::StepLine);
        bind(KeyBinding::plain(KeyCode::F(11)), Command::StepOut);
        // Shift with a step key reverses it, which is the mapping people
        // already expect from step-into and step-over.
        bind(
            KeyBinding::new(KeyCode::F(7), KeyModifiers::SHIFT),
            Command::StepBack,
        );
        bind(
            KeyBinding::new(KeyCode::F(8), KeyModifiers::SHIFT),
            Command::StepBackOver,
        );
        bind(
            KeyBinding::new(KeyCode::F(5), KeyModifiers::SHIFT),
            Command::ReverseContinue,
        );
        bind(KeyBinding::plain(KeyCode::F(12)), Command::DebugStart);
        // A program that loops forever needs one key to end it.
        bind(KeyBinding::ctrl(KeyCode::F(5)), Command::Stop);

        // Files.
        bind(KeyBinding::ctrl(KeyCode::Char('s')), Command::SaveFile);
        bind(KeyBinding::ctrl(KeyCode::Char('o')), Command::OpenFile);
        bind(KeyBinding::ctrl(KeyCode::Char('n')), Command::NewFile);
        bind(KeyBinding::ctrl(KeyCode::Char('w')), Command::CloseFile);
        bind(KeyBinding::ctrl(KeyCode::Char('q')), Command::Quit);

        // Editing.
        bind(KeyBinding::ctrl(KeyCode::Char('z')), Command::Undo);
        bind(KeyBinding::ctrl(KeyCode::Char('y')), Command::Redo);
        bind(KeyBinding::ctrl(KeyCode::Char('a')), Command::SelectAll);
        bind(KeyBinding::ctrl(KeyCode::Char('c')), Command::Copy);
        bind(KeyBinding::ctrl(KeyCode::Char('x')), Command::Cut);
        bind(KeyBinding::ctrl(KeyCode::Char('v')), Command::Paste);

        // Navigation and search.
        bind(KeyBinding::ctrl(KeyCode::Char('p')), Command::OpenPalette);
        bind(KeyBinding::ctrl(KeyCode::Char('f')), Command::Search);
        bind(KeyBinding::ctrl(KeyCode::Char('g')), Command::GoToLine);
        bind(
            KeyBinding::ctrl(KeyCode::Char('k')),
            Command::OpenSyscallFinder,
        );
        bind(
            KeyBinding::ctrl(KeyCode::Char('d')),
            Command::GoToDefinition,
        );
        // Learning and the scratchpad. F1 is where a reader looks for help,
        // and the material is what ratasm has instead of a help screen.
        bind(
            KeyBinding::plain(KeyCode::F(1)),
            Command::ToggleLearningMode,
        );
        bind(KeyBinding::plain(KeyCode::F(2)), Command::OpenScratchpad);
        bind(KeyBinding::plain(KeyCode::Tab), Command::NextPanel);
        bind(KeyBinding::plain(KeyCode::BackTab), Command::PreviousPanel);
        bind(KeyBinding::ctrl(KeyCode::PageDown), Command::NextDocument);
        bind(KeyBinding::ctrl(KeyCode::PageUp), Command::PreviousDocument);

        // Alt plus a digit opens the page with that number, which is the
        // number the page bar prints beside its name.
        for (index, page) in Page::ALL.iter().enumerate() {
            let digit = char::from_digit(index as u32 + 1, 10).unwrap_or('1');
            bindings.insert(
                KeyBinding::new(KeyCode::Char(digit), KeyModifiers::ALT),
                Command::GoToPage(*page),
            );
        }

        Self { bindings }
    }

    /// An empty keymap.
    pub fn empty() -> Self {
        Self {
            bindings: HashMap::new(),
        }
    }

    /// The command bound to a chord, if any.
    pub fn command_for(&self, binding: KeyBinding) -> Option<&Command> {
        self.bindings.get(&binding)
    }

    /// The command bound to a terminal key event, if any.
    pub fn command_for_event(&self, event: KeyEvent) -> Option<&Command> {
        self.command_for(KeyBinding::from_event(event))
    }

    /// The chord bound to a command, if any.
    ///
    /// Used to show the shortcut beside a command in the palette.
    pub fn binding_for(&self, command: &Command) -> Option<KeyBinding> {
        self.sorted_bindings()
            .into_iter()
            .find(|(_, bound)| bound == command)
            .map(|(binding, _)| binding)
    }

    /// Binds a chord, replacing whatever was there.
    pub fn bind(&mut self, binding: KeyBinding, command: Command) {
        self.bindings.insert(binding, command);
    }

    /// Removes a binding.
    pub fn unbind(&mut self, binding: KeyBinding) -> Option<Command> {
        self.bindings.remove(&binding)
    }

    /// Every binding, in no particular order.
    pub fn bindings(&self) -> impl Iterator<Item = (&KeyBinding, &Command)> {
        self.bindings.iter()
    }

    /// Every binding, sorted by its rendered chord.
    ///
    /// Hash order is not stable between runs, so anything the user sees — the
    /// shortcut list, the palette — goes through this instead.
    pub fn sorted_bindings(&self) -> Vec<(KeyBinding, Command)> {
        let mut all: Vec<(KeyBinding, Command)> = self
            .bindings
            .iter()
            .map(|(binding, command)| (*binding, command.clone()))
            .collect();
        all.sort_by_key(|(binding, _)| binding.to_string());
        all
    }

    /// The number of bindings.
    pub fn len(&self) -> usize {
        self.bindings.len()
    }

    /// Whether nothing is bound.
    pub fn is_empty(&self) -> bool {
        self.bindings.is_empty()
    }

    /// Applies user overrides on top of these bindings.
    ///
    /// Returns the parse errors encountered. A bad entry is skipped rather
    /// than aborting the whole file, so one typo does not cost the user every
    /// other binding they configured — but it is reported.
    pub fn apply_overrides(&mut self, overrides: &BTreeMap<String, String>) -> Vec<KeymapError> {
        let mut errors = Vec::new();

        for (key, command_id) in overrides {
            let binding = match parse_binding(key) {
                Ok(binding) => binding,
                Err(error) => {
                    errors.push(KeymapError::BadKey {
                        key: key.clone(),
                        source: error,
                    });
                    continue;
                }
            };

            // An empty command name unbinds the chord.
            if command_id.trim().is_empty() {
                self.unbind(binding);
                continue;
            }

            match Command::from_id(command_id) {
                Some(command) => self.bind(binding, command),
                None => errors.push(KeymapError::UnknownCommand {
                    key: key.clone(),
                    command: command_id.clone(),
                }),
            }
        }

        errors
    }

    /// Finds commands bound to more than one chord, and chords bound twice.
    ///
    /// A chord can only map to one command by construction, so what this
    /// actually detects is the reverse: a command reachable from several
    /// chords. That is usually deliberate, so only genuine duplicates — the
    /// same command bound twice with no other command displaced — are
    /// reported.
    pub fn conflicts(&self) -> Vec<Conflict> {
        let mut by_command: BTreeMap<String, Vec<KeyBinding>> = BTreeMap::new();
        for (binding, command) in &self.bindings {
            by_command.entry(command.id()).or_default().push(*binding);
        }

        by_command
            .into_iter()
            .filter(|(_, bindings)| bindings.len() > 1)
            .filter_map(|(id, bindings)| {
                Command::from_id(&id).map(|command| Conflict {
                    binding: bindings[0],
                    commands: vec![command],
                })
            })
            .collect()
    }
}

impl Default for Keymap {
    fn default() -> Self {
        Self::defaults()
    }
}

/// A problem found while applying user key bindings.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum KeymapError {
    /// The key could not be parsed.
    #[error("key binding '{key}': {source}")]
    BadKey {
        /// The key as written.
        key: String,
        /// Why it could not be parsed.
        #[source]
        source: KeyParseError,
    },
    /// The command name is not recognised.
    #[error("key binding '{key}': there is no command called '{command}'")]
    UnknownCommand {
        /// The key as written.
        key: String,
        /// The command name as written.
        command: String,
    },
}

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

    #[test]
    fn simple_keys_parse() {
        assert_eq!(parse_binding("F5"), Ok(KeyBinding::plain(KeyCode::F(5))));
        assert_eq!(parse_binding("tab"), Ok(KeyBinding::plain(KeyCode::Tab)));
        assert_eq!(
            parse_binding("a"),
            Ok(KeyBinding::plain(KeyCode::Char('a')))
        );
    }

    #[test]
    fn modifiers_parse_in_any_case_and_spelling() {
        let expected = KeyBinding::ctrl(KeyCode::Char('s'));
        for text in ["ctrl+s", "Ctrl+S", "CONTROL+s", "  ctrl + s  ", "c+s"] {
            assert_eq!(parse_binding(text), Ok(expected), "failed for {text:?}");
        }
    }

    #[test]
    fn several_modifiers_combine() {
        let binding = parse_binding("ctrl+alt+delete").expect("parses");
        assert_eq!(binding.code, KeyCode::Delete);
        assert!(binding.modifiers.contains(KeyModifiers::CONTROL));
        assert!(binding.modifiers.contains(KeyModifiers::ALT));
    }

    #[test]
    fn function_keys_parse_within_range() {
        assert_eq!(parse_binding("F12"), Ok(KeyBinding::plain(KeyCode::F(12))));
        // F13 is not a key we bind; it falls through to the unknown-key error.
        assert!(parse_binding("F13").is_err());
        assert!(parse_binding("F0").is_err());
    }

    #[test]
    fn shift_is_dropped_for_characters() {
        // The terminal reports shift+a as 'A', so keeping the modifier would
        // make the binding unmatchable.
        let binding = parse_binding("ctrl+shift+p").expect("parses");
        assert!(!binding.modifiers.contains(KeyModifiers::SHIFT));
        assert!(binding.modifiers.contains(KeyModifiers::CONTROL));
    }

    #[test]
    fn shift_tab_is_normalised_to_backtab() {
        // Terminals report shift+tab as BackTab with no modifier, so both
        // spellings must land on that or the binding could never fire.
        let expected = KeyBinding::plain(KeyCode::BackTab);
        assert_eq!(parse_binding("shift+tab"), Ok(expected));
        assert_eq!(parse_binding("backtab"), Ok(expected));
        assert_eq!(parse_binding("Shift+Tab"), Ok(expected));

        let event = KeyEvent::new(KeyCode::BackTab, KeyModifiers::NONE);
        assert_eq!(KeyBinding::from_event(event), expected);
    }

    #[test]
    fn bad_bindings_report_which_part_was_wrong() {
        assert_eq!(parse_binding(""), Err(KeyParseError::Empty));
        assert!(matches!(
            parse_binding("hyper+s"),
            Err(KeyParseError::UnknownModifier { .. })
        ));
        assert!(matches!(
            parse_binding("ctrl+notakey"),
            Err(KeyParseError::UnknownKey { .. })
        ));
    }

    #[test]
    fn malformed_bindings_never_panic() {
        for text in ["+", "++", "ctrl+", "+s", "ctrl++", "   ", "\u{1F600}"] {
            let _ = parse_binding(text);
        }
    }

    #[test]
    fn bindings_render_back_to_readable_text() {
        assert_eq!(KeyBinding::plain(KeyCode::F(5)).to_string(), "F5");
        assert_eq!(KeyBinding::ctrl(KeyCode::Char('s')).to_string(), "ctrl+s");
        assert_eq!(KeyBinding::plain(KeyCode::BackTab).to_string(), "shift+tab");
    }

    #[test]
    fn rendered_bindings_parse_back() {
        for (binding, _) in Keymap::defaults().bindings() {
            let text = binding.to_string();
            assert_eq!(
                parse_binding(&text).as_ref(),
                Ok(binding),
                "{text} did not round trip"
            );
        }
    }

    #[test]
    fn the_documented_defaults_are_bound() {
        // These are the shortcuts the README promises.
        let keymap = Keymap::defaults();
        for (text, expected) in [
            ("F5", Command::Run),
            ("F6", Command::Build),
            ("F7", Command::StepInstruction),
            ("F8", Command::StepOver),
            ("F9", Command::ToggleBreakpoint),
            ("F10", Command::StepLine),
            ("F1", Command::ToggleLearningMode),
            ("F2", Command::OpenScratchpad),
            ("alt+1", Command::GoToPage(Page::Code)),
            ("alt+2", Command::GoToPage(Page::Debug)),
            ("alt+3", Command::GoToPage(Page::Learn)),
            ("alt+4", Command::GoToPage(Page::Reference)),
            ("shift+F5", Command::ReverseContinue),
            ("ctrl+F5", Command::Stop),
            ("shift+F7", Command::StepBack),
            ("shift+F8", Command::StepBackOver),
            ("ctrl+s", Command::SaveFile),
            ("ctrl+o", Command::OpenFile),
            ("ctrl+p", Command::OpenPalette),
            ("ctrl+f", Command::Search),
            ("ctrl+g", Command::GoToLine),
            ("ctrl+k", Command::OpenSyscallFinder),
            ("ctrl+q", Command::Quit),
            ("tab", Command::NextPanel),
            ("shift+tab", Command::PreviousPanel),
        ] {
            let binding = parse_binding(text).expect("parses");
            assert_eq!(
                keymap.command_for(binding),
                Some(&expected),
                "{text} is not bound to {expected}"
            );
        }
    }

    #[test]
    fn no_default_binding_is_used_twice() {
        // A chord bound twice would silently do the wrong thing.
        assert!(
            Keymap::defaults().conflicts().is_empty(),
            "the defaults conflict: {:?}",
            Keymap::defaults().conflicts()
        );
    }

    #[test]
    fn a_key_event_finds_its_command() {
        let keymap = Keymap::defaults();
        let event = KeyEvent::new(KeyCode::F(5), KeyModifiers::NONE);
        assert_eq!(keymap.command_for_event(event), Some(&Command::Run));

        let event = KeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL);
        assert_eq!(keymap.command_for_event(event), Some(&Command::SaveFile));
    }

    #[test]
    fn an_unbound_key_finds_nothing() {
        let keymap = Keymap::defaults();
        let event = KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE);
        assert_eq!(keymap.command_for_event(event), None);
    }

    #[test]
    fn a_shifted_character_event_matches_the_binding_written_for_it() {
        // The terminal sends 'P' with SHIFT for what a user writes as
        // ctrl+shift+p. Both must normalise to the same binding or the chord
        // could never fire.
        let mut keymap = Keymap::empty();
        keymap.bind(
            parse_binding("ctrl+shift+p").expect("parses"),
            Command::OpenPalette,
        );

        let event = KeyEvent::new(
            KeyCode::Char('P'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        );
        assert_eq!(keymap.command_for_event(event), Some(&Command::OpenPalette));
    }

    #[test]
    fn control_shortcuts_ignore_the_case_of_the_character() {
        let keymap = Keymap::defaults();
        for ch in ['s', 'S'] {
            let event = KeyEvent::new(KeyCode::Char(ch), KeyModifiers::CONTROL);
            assert_eq!(
                keymap.command_for_event(event),
                Some(&Command::SaveFile),
                "ctrl+{ch} should save"
            );
        }
    }

    #[test]
    fn a_plain_character_keeps_its_case() {
        // In the editor, A and a are different input; only shortcuts fold.
        let upper = parse_binding("A").expect("parses");
        let lower = parse_binding("a").expect("parses");
        assert_ne!(upper, lower);
    }

    #[test]
    fn a_command_reports_the_chord_that_runs_it() {
        let keymap = Keymap::defaults();
        assert_eq!(
            keymap
                .binding_for(&Command::SaveFile)
                .map(|b| b.to_string()),
            Some("ctrl+s".to_owned())
        );
        assert_eq!(keymap.binding_for(&Command::ClearBreakpoints), None);
    }

    #[test]
    fn user_overrides_replace_defaults() {
        let mut keymap = Keymap::defaults();
        let overrides = BTreeMap::from([("ctrl+b".to_owned(), "build.build".to_owned())]);

        let errors = keymap.apply_overrides(&overrides);
        assert!(errors.is_empty(), "{errors:?}");
        assert_eq!(
            keymap.command_for(parse_binding("ctrl+b").expect("parses")),
            Some(&Command::Build)
        );
    }

    #[test]
    fn an_empty_command_name_unbinds_a_chord() {
        let mut keymap = Keymap::defaults();
        let overrides = BTreeMap::from([("ctrl+q".to_owned(), String::new())]);

        keymap.apply_overrides(&overrides);
        assert_eq!(
            keymap.command_for(parse_binding("ctrl+q").expect("parses")),
            None
        );
    }

    #[test]
    fn a_bad_override_is_reported_without_discarding_the_good_ones() {
        // One typo must not cost the user every other binding.
        let mut keymap = Keymap::empty();
        let overrides = BTreeMap::from([
            ("ctrl+b".to_owned(), "build.build".to_owned()),
            ("nonsense+z".to_owned(), "build.run".to_owned()),
            ("ctrl+j".to_owned(), "no.such.command".to_owned()),
        ]);

        let errors = keymap.apply_overrides(&overrides);
        assert_eq!(errors.len(), 2);
        assert!(errors
            .iter()
            .any(|e| matches!(e, KeymapError::BadKey { .. })));
        assert!(errors
            .iter()
            .any(|e| matches!(e, KeymapError::UnknownCommand { .. })));

        assert_eq!(
            keymap.command_for(parse_binding("ctrl+b").expect("parses")),
            Some(&Command::Build),
            "the valid binding survived"
        );
    }

    #[test]
    fn error_messages_name_the_offending_key() {
        let mut keymap = Keymap::empty();
        let overrides = BTreeMap::from([("ctrl+j".to_owned(), "no.such.command".to_owned())]);
        let errors = keymap.apply_overrides(&overrides);
        let text = errors[0].to_string();
        assert!(text.contains("ctrl+j"), "{text}");
        assert!(text.contains("no.such.command"), "{text}");
    }

    #[test]
    fn binding_one_command_to_two_chords_is_reported() {
        let mut keymap = Keymap::empty();
        keymap.bind(KeyBinding::plain(KeyCode::F(5)), Command::Run);
        keymap.bind(KeyBinding::ctrl(KeyCode::Char('r')), Command::Run);

        let conflicts = keymap.conflicts();
        assert_eq!(conflicts.len(), 1);
        assert!(conflicts[0].to_string().contains("build.run"));
    }

    #[test]
    fn every_default_binding_names_a_real_command() {
        for (_, command) in Keymap::defaults().bindings() {
            assert!(
                Command::from_id(&command.id()).is_some(),
                "{} is bound but does not exist",
                command.id()
            );
        }
    }
}