aboutsummaryrefslogtreecommitdiff
path: root/src/buffer/command.rs
blob: c5a2aef0597bc210fe912c2238b4ba52e68361ff (plain) (blame)
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
//! Editor commands — the values key bindings map to.
//!
//! Kept distinct from [`crate::action::Action`], which is application-level
//! (tick, render, resize). A binding names a `Command`; the buffer executes it.
//! `Quit` and `Suspend` appear here because the `Global` keymap is expressed in
//! the same table and must be able to name them.

use serde::{Deserialize, Serialize};
use strum::Display;

/// MJB-LLR-157: one unit variant per bound editor command, deserialized from
/// the variant name so a TOML value like `"MoveCharLeft"` resolves directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, Serialize, Deserialize)]
pub enum Command {
    // --- Application (reachable from the `global` keymap) ---
    Quit,
    Suspend,

    // --- Mode switching ---
    NormalMode,
    InsertMode,
    SelectMode,
    CommandMode,

    // --- Character and line motion (MJB-HLR-006) ---
    MoveCharLeft,
    MoveCharRight,
    MoveLineUp,
    MoveLineDown,

    // --- Word motion; these produce selections (MJB-HLR-007) ---
    MoveNextWordStart,
    MovePrevWordStart,
    MoveNextWordEnd,
    MoveNextLongWordStart,
    MovePrevLongWordStart,
    MoveNextLongWordEnd,

    // --- Extending variants, used by select mode ---
    ExtendCharLeft,
    ExtendCharRight,
    ExtendLineUp,
    ExtendLineDown,
    ExtendNextWordStart,
    ExtendPrevWordStart,
    ExtendNextWordEnd,

    // --- Goto (MJB-HLR-008) ---
    GotoFileStart,
    GotoLastLine,
    GotoLineStart,
    GotoLineEnd,
    GotoFirstNonWhitespace,

    // --- Selection manipulation ---
    ExtendLineBelow,
    CollapseSelection,
    FlipSelections,
    SelectAll,

    // --- Entering insert mode (MJB-HLR-009) ---
    AppendMode,
    InsertAtLineStart,
    InsertAtLineEnd,
    OpenBelow,
    OpenAbove,

    // --- Modification (MJB-HLR-010) ---
    DeleteSelection,
    ChangeSelection,
    InsertNewline,
    InsertTab,
    DeleteCharBackward,
    DeleteCharForward,
    DeleteWordBackward,
    KillToLineStart,

    // --- Undo / redo (MJB-HLR-011) ---
    Undo,
    Redo,

    // --- Scrolling and paging (MJB-HLR-013) ---
    PageCursorHalfUp,
    PageCursorHalfDown,
    PageUp,
    PageDown,

    // --- Command line (MJB-HLR-016) ---
    CommandSubmit,
    CommandBackspace,
}

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

    /// MJB-LLR-157: a TOML value naming a variant deserializes to it, which is
    /// what makes the keymap config-driven.
    #[test]
    fn mjb_llr_157_deserializes_from_the_variant_name() {
        let cmd: Command = serde_json_free_parse("MoveCharLeft");
        assert_eq!(cmd, Command::MoveCharLeft);
        assert_eq!(serde_json_free_parse("Undo"), Command::Undo);
        assert_eq!(
            serde_json_free_parse("PageCursorHalfDown"),
            Command::PageCursorHalfDown
        );
    }

    #[test]
    fn mjb_llr_157_unknown_command_name_is_an_error_not_a_panic() {
        let err = toml::from_str::<Wrapper>("cmd = \"NoSuchCommand\"").unwrap_err();
        assert!(
            err.to_string().contains("NoSuchCommand"),
            "error must name the offending value, got: {err}"
        );
    }

    #[test]
    fn mjb_llr_157_display_round_trips_through_deserialization() {
        for cmd in [
            Command::Quit,
            Command::GotoFileStart,
            Command::DeleteSelection,
            Command::CommandSubmit,
        ] {
            assert_eq!(
                serde_json_free_parse(&cmd.to_string()),
                cmd,
                "{cmd} must round-trip"
            );
        }
    }

    #[derive(Debug, serde::Deserialize)]
    struct Wrapper {
        cmd: Command,
    }

    /// Parse a bare command name the way the keymap table does.
    fn serde_json_free_parse(name: &str) -> Command {
        let doc = format!("cmd = \"{name}\"");
        toml::from_str::<Wrapper>(&doc)
            .unwrap_or_else(|e| panic!("{name} must parse: {e}"))
            .cmd
    }
}