ratasm 0.3.0

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
815
816
817
818
//! Every action the application can perform.

pub mod palette;

use std::fmt;

use crate::app::page::Page;
use crate::app::panel::Panel;

pub use palette::{fuzzy_match, FuzzyMatch};

/// A group of related commands, used to organise the palette.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Category {
    /// Opening, saving and closing files.
    File,
    /// Modifying text.
    Edit,
    /// Moving around.
    Navigate,
    /// Finding things.
    Search,
    /// Assembling, linking and running.
    Build,
    /// Controlling a debug session.
    Debug,
    /// Changing what is shown.
    View,
    /// Everything else.
    Application,
}

impl Category {
    /// Every category, in palette order.
    pub const ALL: [Category; 8] = [
        Category::File,
        Category::Edit,
        Category::Navigate,
        Category::Search,
        Category::Build,
        Category::Debug,
        Category::View,
        Category::Application,
    ];

    /// The name shown in the palette.
    pub const fn title(self) -> &'static str {
        match self {
            Category::File => "File",
            Category::Edit => "Edit",
            Category::Navigate => "Navigate",
            Category::Search => "Search",
            Category::Build => "Build",
            Category::Debug => "Debug",
            Category::View => "View",
            Category::Application => "Application",
        }
    }
}

impl fmt::Display for Category {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.title())
    }
}

/// An action the application can perform.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Command {
    /// Create a new empty buffer.
    NewFile,
    /// Prompt for a file to open.
    OpenFile,
    /// Save the active buffer.
    SaveFile,
    /// Save every modified buffer.
    SaveAll,
    /// Prompt for a name and save the active buffer under it.
    SaveFileAs,
    /// Close the active buffer.
    CloseFile,
    /// Rename file.
    RenameFile,
    /// Delete file.
    DeleteFile,
    /// Add the active buffer's file to the project's sources.
    AddToProject,
    /// Leave the application.
    Quit,

    /// Undo the last change.
    Undo,
    /// Redo the last undone change.
    Redo,
    /// Select the whole buffer.
    SelectAll,
    /// Copy the selection.
    Copy,
    /// Cut the selection.
    Cut,
    /// Paste the clipboard.
    Paste,
    /// Add one indent level.
    Indent,
    /// Remove one indent level.
    Dedent,

    /// Open a named page.
    GoToPage(Page),
    /// Open the next page.
    NextPage,
    /// Open the previous page.
    PreviousPage,
    /// Focus the next panel.
    NextPanel,
    /// Focus the previous panel.
    PreviousPanel,
    /// Focus a named panel.
    FocusPanel(Panel),
    /// Show the next open document.
    NextDocument,
    /// Show the previous open document.
    PreviousDocument,
    /// Scroll the focused panel up a few rows.
    ScrollUp,
    /// Scroll the focused panel down a few rows.
    ScrollDown,
    /// Scroll the focused panel up one screenful.
    ScrollPageUp,
    /// Scroll the focused panel down one screenful.
    ScrollPageDown,
    /// Scroll the focused panel to its first row.
    ScrollToTop,
    /// Scroll the focused panel to its last screenful.
    ScrollToEnd,
    /// Prompt for a line number and go to it.
    GoToLine,
    /// Prompt for an address and show it in the memory panel.
    GoToAddress,
    /// Jump to the definition of the symbol under the cursor.
    GoToDefinition,
    /// Jump to the first error from the last build.
    GoToFirstError,
    /// Go to symbol.
    GoToSymbol,
    /// Search in files.
    SearchFiles,

    /// Open the search prompt.
    Search,
    /// Go to the next match.
    SearchNext,
    /// Go to the previous match.
    SearchPrevious,
    /// Open the replace prompt.
    Replace,

    /// Assemble and link.
    Build,
    /// Assemble and link with debug information.
    BuildWithDebugInfo,
    /// Build if needed, then run.
    Run,
    /// Stop the running program.
    Stop,
    /// Run one target of the project's Makefile.
    MakeTarget,

    /// Start a debug session.
    DebugStart,
    /// Resume a paused program.
    DebugContinue,
    /// Interrupt a running program.
    DebugInterrupt,
    /// Execute one machine instruction.
    StepInstruction,
    /// Execute one instruction, stepping over calls.
    StepOver,
    /// Execute one source line.
    StepLine,
    /// Run until the current function returns.
    StepOut,
    /// Undo one machine instruction.
    StepBack,
    /// Undo one instruction, over calls.
    StepBackOver,
    /// Run backwards to the previous breakpoint.
    ReverseContinue,
    /// End the debug session.
    DebugStop,
    /// Add or remove a breakpoint on the current line.
    ToggleBreakpoint,
    /// Remove every breakpoint.
    ClearBreakpoints,
    /// Watch an expression.
    AddWatchpoint,
    /// Set breakpoint condition.
    SetBreakpointCondition,
    /// Set a register.
    SetRegister,

    /// Change how register values are displayed.
    CycleRegisterFormat,
    /// Switch between Intel and AT&T disassembly.
    ToggleDisassemblySyntax,
    /// Switch to the next theme.
    CycleTheme,
    /// Show or hide the learning-mode panel.
    ToggleLearningMode,

    /// Open the command palette.
    OpenPalette,
    /// Open the system call reference.
    OpenSyscallFinder,
    /// Open the scratchpad.
    OpenScratchpad,
    /// Show the keyboard shortcuts.
    ShowKeybindings,
    /// Copy the complete status message to the output panel.
    ShowStatusDetails,
}

impl Command {
    /// Every command, in a stable order.
    pub fn all() -> Vec<Command> {
        let mut commands = vec![
            Command::NewFile,
            Command::OpenFile,
            Command::SaveFile,
            Command::SaveFileAs,
            Command::SaveAll,
            Command::AddToProject,
            Command::CloseFile,
            Command::RenameFile,
            Command::DeleteFile,
            Command::Quit,
            Command::Undo,
            Command::Redo,
            Command::SelectAll,
            Command::Copy,
            Command::Cut,
            Command::Paste,
            Command::Indent,
            Command::Dedent,
            Command::NextPanel,
            Command::PreviousPanel,
            Command::NextDocument,
            Command::PreviousDocument,
            Command::ScrollUp,
            Command::ScrollDown,
            Command::ScrollPageUp,
            Command::ScrollPageDown,
            Command::ScrollToTop,
            Command::ScrollToEnd,
            Command::GoToLine,
            Command::GoToAddress,
            Command::GoToDefinition,
            Command::GoToFirstError,
            Command::GoToSymbol,
            Command::SearchFiles,
            Command::Search,
            Command::SearchNext,
            Command::SearchPrevious,
            Command::Replace,
            Command::Build,
            Command::BuildWithDebugInfo,
            Command::Run,
            Command::Stop,
            Command::MakeTarget,
            Command::DebugStart,
            Command::DebugContinue,
            Command::DebugInterrupt,
            Command::StepInstruction,
            Command::StepOver,
            Command::StepLine,
            Command::StepOut,
            Command::StepBack,
            Command::StepBackOver,
            Command::ReverseContinue,
            Command::DebugStop,
            Command::ToggleBreakpoint,
            Command::ClearBreakpoints,
            Command::AddWatchpoint,
            Command::SetBreakpointCondition,
            Command::SetRegister,
            Command::CycleRegisterFormat,
            Command::ToggleDisassemblySyntax,
            Command::CycleTheme,
            Command::ToggleLearningMode,
            Command::OpenPalette,
            Command::OpenSyscallFinder,
            Command::OpenScratchpad,
            Command::ShowKeybindings,
            Command::ShowStatusDetails,
        ];
        commands.extend(Page::ALL.map(Command::GoToPage));
        commands.extend(Panel::ALL.map(Command::FocusPanel));
        commands
    }

    /// The stable identifier used in configuration files.
    pub fn id(&self) -> String {
        match self {
            Command::NewFile => "file.new".into(),
            Command::OpenFile => "file.open".into(),
            Command::SaveFile => "file.save".into(),
            Command::SaveAll => "file.save-all".into(),
            Command::AddToProject => "file.add-to-project".into(),
            Command::SaveFileAs => "file.save-as".into(),
            Command::CloseFile => "file.close".into(),
            Command::RenameFile => "file.rename".into(),
            Command::DeleteFile => "file.delete".into(),
            Command::Quit => "app.quit".into(),

            Command::Undo => "edit.undo".into(),
            Command::Redo => "edit.redo".into(),
            Command::SelectAll => "edit.select-all".into(),
            Command::Copy => "edit.copy".into(),
            Command::Cut => "edit.cut".into(),
            Command::Paste => "edit.paste".into(),
            Command::Indent => "edit.indent".into(),
            Command::Dedent => "edit.dedent".into(),

            Command::GoToPage(page) => format!("navigate.page.{}", page.id()),
            Command::NextPage => "navigate.next-page".into(),
            Command::PreviousPage => "navigate.previous-page".into(),
            Command::NextPanel => "navigate.next-panel".into(),
            Command::PreviousPanel => "navigate.previous-panel".into(),
            Command::FocusPanel(panel) => format!("navigate.focus.{}", panel.id()),
            Command::NextDocument => "navigate.next-document".into(),
            Command::PreviousDocument => "navigate.previous-document".into(),
            Command::ScrollUp => "navigate.scroll-up".into(),
            Command::ScrollDown => "navigate.scroll-down".into(),
            Command::ScrollPageUp => "navigate.scroll-page-up".into(),
            Command::ScrollPageDown => "navigate.scroll-page-down".into(),
            Command::ScrollToTop => "navigate.scroll-to-top".into(),
            Command::ScrollToEnd => "navigate.scroll-to-end".into(),
            Command::GoToLine => "navigate.go-to-line".into(),
            Command::GoToAddress => "navigate.go-to-address".into(),
            Command::GoToDefinition => "navigate.go-to-definition".into(),
            Command::GoToFirstError => "navigate.go-to-first-error".into(),
            Command::GoToSymbol => "navigate.go-to-symbol".into(),
            Command::SearchFiles => "navigate.search".into(),

            Command::Search => "search.find".into(),
            Command::SearchNext => "search.next".into(),
            Command::SearchPrevious => "search.previous".into(),
            Command::Replace => "search.replace".into(),

            Command::Build => "build.build".into(),
            Command::BuildWithDebugInfo => "build.build-debug".into(),
            Command::Run => "build.run".into(),
            Command::Stop => "build.stop".into(),
            Command::MakeTarget => "build.make-target".into(),

            Command::DebugStart => "debug.start".into(),
            Command::DebugContinue => "debug.continue".into(),
            Command::DebugInterrupt => "debug.interrupt".into(),
            Command::StepInstruction => "debug.step-instruction".into(),
            Command::StepOver => "debug.step-over".into(),
            Command::StepLine => "debug.step-line".into(),
            Command::StepOut => "debug.step-out".into(),
            Command::StepBack => "debug.step-back".into(),
            Command::StepBackOver => "debug.step-back-over".into(),
            Command::ReverseContinue => "debug.reverse-continue".into(),
            Command::DebugStop => "debug.stop".into(),
            Command::ToggleBreakpoint => "debug.toggle-breakpoint".into(),
            Command::ClearBreakpoints => "debug.clear-breakpoints".into(),
            Command::AddWatchpoint => "debug.watch".into(),
            Command::SetBreakpointCondition => "debug.condition".into(),
            Command::SetRegister => "debug.set-register".into(),

            Command::CycleRegisterFormat => "view.cycle-register-format".into(),
            Command::ToggleDisassemblySyntax => "view.toggle-disassembly-syntax".into(),
            Command::CycleTheme => "view.cycle-theme".into(),
            Command::ToggleLearningMode => "view.toggle-learning-mode".into(),

            Command::OpenPalette => "app.palette".into(),
            Command::OpenSyscallFinder => "app.syscalls".into(),
            Command::OpenScratchpad => "app.scratchpad".into(),
            Command::ShowKeybindings => "app.keybindings".into(),
            Command::ShowStatusDetails => "app.status-details".into(),
        }
    }

    /// The title shown in the palette.
    pub fn title(&self) -> String {
        match self {
            Command::NewFile => "New file".into(),
            Command::OpenFile => "Open file".into(),
            Command::SaveFile => "Save".into(),
            Command::SaveAll => "Save all".into(),
            Command::AddToProject => "Add to project".into(),
            Command::SaveFileAs => "Save as".into(),
            Command::CloseFile => "Close file".into(),
            Command::RenameFile => "Rename file".into(),
            Command::DeleteFile => "Delete file".into(),
            Command::Quit => "Quit".into(),

            Command::Undo => "Undo".into(),
            Command::Redo => "Redo".into(),
            Command::SelectAll => "Select all".into(),
            Command::Copy => "Copy".into(),
            Command::Cut => "Cut".into(),
            Command::Paste => "Paste".into(),
            Command::Indent => "Indent".into(),
            Command::Dedent => "Dedent".into(),

            Command::GoToPage(page) => format!("{} page", page.title()),
            Command::NextPage => "Next page".into(),
            Command::PreviousPage => "Previous page".into(),
            Command::NextPanel => "Next panel".into(),
            Command::PreviousPanel => "Previous panel".into(),
            Command::FocusPanel(panel) => format!("Focus {}", panel.title().to_lowercase()),
            Command::ScrollUp => "Scroll up".into(),
            Command::ScrollDown => "Scroll down".into(),
            Command::ScrollPageUp => "Scroll up a page".into(),
            Command::ScrollPageDown => "Scroll down a page".into(),
            Command::ScrollToTop => "Scroll to the top".into(),
            Command::ScrollToEnd => "Scroll to the end".into(),
            Command::NextDocument => "Next document".into(),
            Command::PreviousDocument => "Previous document".into(),
            Command::GoToLine => "Go to line".into(),
            Command::GoToAddress => "Go to address".into(),
            Command::GoToDefinition => "Go to definition".into(),
            Command::GoToFirstError => "Go to first error".into(),
            Command::GoToSymbol => "Go to symbol".into(),
            Command::SearchFiles => "Search in files".into(),

            Command::Search => "Find".into(),
            Command::SearchNext => "Find next".into(),
            Command::SearchPrevious => "Find previous".into(),
            Command::Replace => "Replace".into(),

            Command::Build => "Build".into(),
            Command::BuildWithDebugInfo => "Build with debug info".into(),
            Command::Run => "Run".into(),
            Command::Stop => "Stop the program".into(),
            Command::MakeTarget => "Run a make target".into(),

            Command::DebugStart => "Start debugging".into(),
            Command::DebugContinue => "Continue".into(),
            Command::DebugInterrupt => "Interrupt".into(),
            Command::StepInstruction => "Step instruction".into(),
            Command::StepOver => "Step over".into(),
            Command::StepLine => "Step line".into(),
            Command::StepOut => "Step out".into(),
            Command::StepBack => "Step back".into(),
            Command::StepBackOver => "Step back over".into(),
            Command::ReverseContinue => "Run backwards".into(),
            Command::DebugStop => "Stop debugging".into(),
            Command::ToggleBreakpoint => "Toggle breakpoint".into(),
            Command::ClearBreakpoints => "Clear all breakpoints".into(),
            Command::AddWatchpoint => "Watch an expression".into(),
            Command::SetBreakpointCondition => "Set breakpoint condition".into(),
            Command::SetRegister => "Set a register".into(),

            Command::CycleRegisterFormat => "Change register format".into(),
            Command::ToggleDisassemblySyntax => "Toggle Intel/AT&T syntax".into(),
            Command::CycleTheme => "Change theme".into(),
            Command::ToggleLearningMode => "Toggle learning mode".into(),

            Command::OpenPalette => "Command palette".into(),
            Command::OpenSyscallFinder => "Find a system call".into(),
            Command::OpenScratchpad => "Open scratchpad".into(),
            Command::ShowKeybindings => "Show keyboard shortcuts".into(),
            Command::ShowStatusDetails => "Show status details".into(),
        }
    }

    /// A one-line description shown beside the title.
    pub fn description(&self) -> String {
        match self {
            Command::SaveFile => "Write the active buffer to its file".into(),
            Command::SaveAll => "Write every modified buffer to its file".into(),
            Command::AddToProject => "Assemble this file with the rest of the project".into(),
            Command::Quit => "Leave ratasm, confirming any unsaved changes".into(),
            Command::Build => "Assemble and link the project".into(),
            Command::BuildWithDebugInfo => "Assemble with -g so source-level stepping works".into(),
            Command::Run => "Build if needed, then run the program".into(),
            Command::DebugStart => "Load the program under GDB and stop at the entry".into(),
            Command::DebugContinue => "Resume until the next breakpoint".into(),
            Command::DebugInterrupt => "Stop a program that is still running".into(),
            Command::StepInstruction => "Execute exactly one machine instruction".into(),
            Command::StepOver => "Execute one instruction, running calls to completion".into(),
            Command::StepLine => "Execute one line of source, which may be several \
                                  instructions"
                .into(),
            Command::StepOut => "Run until the current function returns".into(),
            Command::StepBack => "Undo the last instruction and put the registers back".into(),
            Command::StepBackOver => "Undo the last instruction, skipping calls".into(),
            Command::ReverseContinue => "Run backwards to the previous breakpoint".into(),
            Command::ToggleBreakpoint => "Add or remove a breakpoint on the current line".into(),
            Command::GoToAddress => "Show an address in the memory panel; accepts rsp-0x20".into(),
            Command::GoToDefinition => {
                "Jump to where the symbol under the cursor is defined".into()
            }
            Command::GoToFirstError => "Jump to the first error from the last build".into(),
            Command::GoToSymbol => {
                "Jump to a label, constant or macro anywhere in the project".into()
            }
            Command::SearchFiles => "Find text in every source file of the project".into(),
            Command::CycleRegisterFormat => "Hexadecimal, decimal, signed, binary or ASCII".into(),
            Command::ToggleDisassemblySyntax => {
                "Intel puts the destination first; AT&T puts the source first".into()
            }
            Command::GoToPage(page) => page.description().into(),
            Command::OpenSyscallFinder => "Search Linux system calls by name or number".into(),
            Command::OpenScratchpad => "Try an instruction without making a project".into(),
            Command::ToggleLearningMode => "Show explanations and exercises alongside".into(),
            Command::ShowStatusDetails => {
                "Show the complete status message in the output panel".into()
            }
            other => other.title(),
        }
    }

    /// The category this command belongs to.
    pub fn category(&self) -> Category {
        match self {
            Command::NewFile
            | Command::OpenFile
            | Command::SaveFile
            | Command::SaveFileAs
            | Command::SaveAll
            | Command::AddToProject
            | Command::CloseFile
            | Command::RenameFile
            | Command::DeleteFile => Category::File,

            Command::Undo
            | Command::Redo
            | Command::SelectAll
            | Command::Copy
            | Command::Cut
            | Command::Paste
            | Command::Indent
            | Command::Dedent => Category::Edit,

            Command::GoToPage(_)
            | Command::NextPage
            | Command::PreviousPage
            | Command::NextPanel
            | Command::PreviousPanel
            | Command::FocusPanel(_)
            | Command::NextDocument
            | Command::PreviousDocument
            | Command::GoToLine
            | Command::GoToAddress
            | Command::GoToDefinition
            | Command::GoToFirstError
            | Command::GoToSymbol
            | Command::SearchFiles
            | Command::ScrollUp
            | Command::ScrollDown
            | Command::ScrollPageUp
            | Command::ScrollPageDown
            | Command::ScrollToTop
            | Command::ScrollToEnd => Category::Navigate,

            Command::Search | Command::SearchNext | Command::SearchPrevious | Command::Replace => {
                Category::Search
            }

            Command::Build
            | Command::BuildWithDebugInfo
            | Command::Run
            | Command::Stop
            | Command::MakeTarget => Category::Build,

            Command::DebugStart
            | Command::DebugContinue
            | Command::DebugInterrupt
            | Command::StepInstruction
            | Command::StepOver
            | Command::StepLine
            | Command::StepOut
            | Command::StepBack
            | Command::StepBackOver
            | Command::ReverseContinue
            | Command::DebugStop
            | Command::ToggleBreakpoint
            | Command::ClearBreakpoints
            | Command::AddWatchpoint
            | Command::SetBreakpointCondition
            | Command::SetRegister => Category::Debug,

            Command::CycleRegisterFormat
            | Command::ToggleDisassemblySyntax
            | Command::CycleTheme
            | Command::ToggleLearningMode => Category::View,

            Command::Quit
            | Command::OpenPalette
            | Command::OpenSyscallFinder
            | Command::OpenScratchpad
            | Command::ShowKeybindings
            | Command::ShowStatusDetails => Category::Application,
        }
    }

    /// Whether the command opens a prompt for a value.
    pub fn prompts_for_input(&self) -> bool {
        matches!(
            self,
            Command::OpenFile
                | Command::NewFile
                | Command::SaveFileAs
                | Command::GoToLine
                | Command::GoToAddress
                | Command::Search
                | Command::Replace
                | Command::RenameFile
                | Command::DeleteFile
                | Command::GoToSymbol
                | Command::SearchFiles
                | Command::MakeTarget
                | Command::AddWatchpoint
                | Command::SetBreakpointCondition
                | Command::SetRegister
        )
    }

    /// Whether the command needs a live debug session.
    pub fn needs_debug_session(&self) -> bool {
        matches!(
            self,
            Command::DebugContinue
                | Command::DebugInterrupt
                | Command::StepInstruction
                | Command::StepOver
                | Command::StepLine
                | Command::StepOut
                | Command::StepBack
                | Command::StepBackOver
                | Command::ReverseContinue
                | Command::DebugStop
        )
    }

    /// Resolves a command from its identifier.
    pub fn from_id(id: &str) -> Option<Command> {
        let id = id.trim().to_ascii_lowercase();
        Command::all()
            .into_iter()
            .find(|command| command.id() == id)
    }

    /// The text the palette searches, combining title and identifier.
    pub fn search_text(&self) -> String {
        format!("{} {}", self.title(), self.id())
    }
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.title())
    }
}

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

    #[test]
    fn identifiers_round_trip() {
        for command in Command::all() {
            assert_eq!(
                Command::from_id(&command.id()),
                Some(command.clone()),
                "{} did not round trip",
                command.id()
            );
        }
    }

    #[test]
    fn identifiers_are_unique() {
        let mut ids: Vec<String> = Command::all().iter().map(Command::id).collect();
        let count = ids.len();
        ids.sort();
        ids.dedup();
        assert_eq!(ids.len(), count, "two commands share an identifier");
    }

    #[test]
    fn identifiers_follow_the_group_dot_name_convention() {
        for command in Command::all() {
            let id = command.id();
            assert!(id.contains('.'), "{id} has no group prefix");
            assert_eq!(id, id.to_ascii_lowercase(), "{id} is not lowercase");
            assert!(
                !id.contains(' ') && !id.contains('_'),
                "{id} should use hyphens, not spaces or underscores"
            );
        }
    }

    #[test]
    fn every_command_has_a_title_and_description() {
        for command in Command::all() {
            assert!(!command.title().is_empty(), "{} has no title", command.id());
            assert!(
                !command.description().is_empty(),
                "{} has no description",
                command.id()
            );
        }
    }

    #[test]
    fn titles_are_unique_so_the_palette_is_unambiguous() {
        let mut titles: Vec<String> = Command::all().iter().map(Command::title).collect();
        let count = titles.len();
        titles.sort();
        titles.dedup();
        assert_eq!(titles.len(), count, "two commands share a title");
    }

    #[test]
    fn every_panel_has_a_focus_command() {
        for panel in Panel::ALL {
            let command = Command::FocusPanel(panel);
            assert!(Command::all().contains(&command), "{panel} has no command");
            assert_eq!(command.category(), Category::Navigate);
        }
    }

    #[test]
    fn every_category_has_at_least_one_command() {
        for category in Category::ALL {
            assert!(
                Command::all().iter().any(|c| c.category() == category),
                "{category} is empty"
            );
        }
    }

    #[test]
    fn stepping_commands_require_a_session() {
        for command in [
            Command::StepInstruction,
            Command::StepOver,
            Command::StepLine,
            Command::StepOut,
            Command::DebugContinue,
        ] {
            assert!(command.needs_debug_session(), "{command}");
        }
        assert!(!Command::DebugStart.needs_debug_session());
        assert!(!Command::Build.needs_debug_session());
        assert!(!Command::SaveFile.needs_debug_session());
    }

    #[test]
    fn commands_that_ask_for_a_value_are_marked() {
        assert!(Command::GoToLine.prompts_for_input());
        assert!(!Command::ToggleBreakpoint.prompts_for_input());
        assert!(Command::AddWatchpoint.prompts_for_input());
        assert!(Command::GoToAddress.prompts_for_input());
        assert!(Command::OpenFile.prompts_for_input());
        assert!(!Command::SaveFile.prompts_for_input());
        assert!(!Command::Build.prompts_for_input());
    }

    #[test]
    fn an_unknown_identifier_resolves_to_nothing() {
        assert_eq!(Command::from_id("nonsense.command"), None);
        assert_eq!(Command::from_id(""), None);
    }

    #[test]
    fn identifier_lookup_is_forgiving_about_case_and_space() {
        assert_eq!(Command::from_id("  FILE.SAVE "), Some(Command::SaveFile));
    }

    #[test]
    fn search_text_covers_both_the_title_and_the_identifier() {
        let text = Command::StepOver.search_text();
        assert!(text.contains("Step over"));
        assert!(text.contains("debug.step-over"));
    }

    #[test]
    fn the_documented_shortcuts_all_name_a_real_command() {
        for id in [
            "build.run",
            "build.build",
            "debug.step-instruction",
            "debug.step-over",
            "debug.toggle-breakpoint",
            "debug.step-line",
            "file.save",
            "file.open",
            "app.palette",
            "search.find",
            "navigate.go-to-line",
            "app.syscalls",
            "app.quit",
            "navigate.next-panel",
            "navigate.previous-panel",
        ] {
            assert!(Command::from_id(id).is_some(), "{id} does not exist");
        }
    }

    #[test]
    fn every_command_identifier_is_documented() {
        let docs = include_str!("../../docs/keybindings.md");
        for command in Command::all() {
            let row = format!("| `{}` | {} |", command.id(), command.title());
            assert!(
                docs.contains(&row),
                "docs/keybindings.md is missing the row: {row}"
            );
        }
    }
}