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
//! Self-contained macro record/playback state.
//!
//! `MacroState` owns the four fields that used to live directly on `Editor`:
//! the map of register-key to recorded actions, the in-flight recording
//! buffer, the last register played (for "play last macro"), and the
//! playing flag that suppresses recursive playback.
//!
//! Nothing else in the codebase reaches into `MacroState`'s internals —
//! every cross-subsystem effect (status messages, handle_action replay,
//! buffer creation for `show_macro_in_buffer`) is handled by the
//! orchestrator on `Editor` using the narrow public API below.
use std::collections::HashMap;
use crate::input::keybindings::Action;
/// State for a macro recording in progress.
#[derive(Debug, Clone)]
pub(crate) struct RecordingState {
/// The register key this macro is being recorded to.
pub key: char,
/// Actions recorded so far.
pub actions: Vec<Action>,
}
/// Owner of all macro record/playback state.
#[derive(Debug, Default)]
pub(crate) struct MacroState {
/// Register-key -> recorded actions.
macros: HashMap<char, Vec<Action>>,
/// Current in-flight recording, if any.
recording: Option<RecordingState>,
/// Last register that was recorded/played (for "play last macro").
last_register: Option<char>,
/// True while a macro is being replayed — suppresses recursive recording.
playing: bool,
}
impl MacroState {
// ---- Queries -----------------------------------------------------------
/// The register key of the in-flight recording, if any.
///
/// Callers use `recording_key().is_some()` as the "is recording?" query —
/// we deliberately don't expose a separate `is_recording` boolean since
/// callers almost always want the key too.
pub(crate) fn recording_key(&self) -> Option<char> {
self.recording.as_ref().map(|r| r.key)
}
/// Whether a macro is currently being replayed.
pub(crate) fn is_playing(&self) -> bool {
self.playing
}
/// Register key of the most recently recorded/played macro.
pub(crate) fn last_register(&self) -> Option<char> {
self.last_register
}
/// Recorded actions for `key`, or `None` if no such macro exists.
pub(crate) fn get(&self, key: char) -> Option<&[Action]> {
self.macros.get(&key).map(Vec::as_slice)
}
/// Whether any macros have been recorded.
pub(crate) fn is_empty(&self) -> bool {
self.macros.is_empty()
}
/// Total number of recorded macros.
pub(crate) fn count(&self) -> usize {
self.macros.len()
}
/// All register keys, sorted — suitable for listing in a view buffer.
pub(crate) fn keys_sorted(&self) -> Vec<char> {
let mut keys: Vec<char> = self.macros.keys().copied().collect();
keys.sort();
keys
}
// ---- Lifecycle ---------------------------------------------------------
/// Begin recording into `key`. Any existing recording is discarded —
/// callers should call [`Self::stop_recording`] first if they want the
/// previous recording saved.
pub(crate) fn start_recording(&mut self, key: char) {
self.recording = Some(RecordingState {
key,
actions: Vec::new(),
});
}
/// Stop the in-flight recording and save it.
///
/// Returns `Some((key, action_count))` on success, `None` if no recording
/// was in progress.
pub(crate) fn stop_recording(&mut self) -> Option<(char, usize)> {
let state = self.recording.take()?;
let action_count = state.actions.len();
let key = state.key;
self.macros.insert(key, state.actions);
self.last_register = Some(key);
Some((key, action_count))
}
/// Mark replay as started. Callers must call [`Self::end_play`] exactly
/// once afterwards, even on error.
pub(crate) fn begin_play(&mut self) {
self.playing = true;
}
/// Mark replay as finished.
pub(crate) fn end_play(&mut self) {
self.playing = false;
}
// ---- Recording ---------------------------------------------------------
/// Append `action` to the in-flight recording, if any, unless the action
/// is a macro-control action itself (those don't belong in the recorded
/// stream — replaying them would trigger infinite regress).
///
/// Does nothing if no recording is in progress or if a macro is currently
/// playing back (we never record replay actions).
pub(crate) fn record_if_recording(&mut self, action: &Action) {
if self.playing {
return;
}
let Some(state) = self.recording.as_mut() else {
return;
};
if is_macro_control_action(action) {
return;
}
state.actions.push(action.clone());
}
/// Append an already-transformed action to the recording (e.g. a
/// `PromptConfirmWithText` the orchestrator produced by snapshotting the
/// current prompt text). Skips the `is_macro_control_action` filter so
/// that the transformed variant lands in the recording even though the
/// raw form it replaces was filterable.
pub(crate) fn record_transformed(&mut self, action: Action) {
if self.playing {
return;
}
let Some(state) = self.recording.as_mut() else {
return;
};
state.actions.push(action);
}
}
/// Actions that manage macros themselves — recording a "start recording"
/// action inside the macro would cause recursive recording on replay.
fn is_macro_control_action(action: &Action) -> bool {
matches!(
action,
Action::StartMacroRecording
| Action::StopMacroRecording
| Action::PlayMacro(_)
| Action::ToggleMacroRecording(_)
| Action::ShowMacro(_)
| Action::ListMacros
| Action::PromptRecordMacro
| Action::PromptPlayMacro
| Action::PlayLastMacro
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_empty_and_idle() {
let m = MacroState::default();
assert!(!m.is_playing());
assert_eq!(m.recording_key(), None);
assert_eq!(m.last_register(), None);
assert!(m.is_empty());
assert_eq!(m.count(), 0);
}
#[test]
fn start_then_stop_round_trips_recording() {
let mut m = MacroState::default();
m.start_recording('q');
assert_eq!(m.recording_key(), Some('q'));
// Record a few non-control actions.
m.record_if_recording(&Action::MoveLeft);
m.record_if_recording(&Action::MoveRight);
let saved = m.stop_recording();
assert_eq!(saved, Some(('q', 2)));
assert_eq!(m.recording_key(), None);
assert_eq!(m.last_register(), Some('q'));
assert_eq!(m.get('q').map(|a| a.len()), Some(2));
}
#[test]
fn stop_recording_when_not_recording_returns_none() {
let mut m = MacroState::default();
assert_eq!(m.stop_recording(), None);
}
#[test]
fn recording_filters_control_actions() {
let mut m = MacroState::default();
m.start_recording('q');
m.record_if_recording(&Action::MoveLeft);
m.record_if_recording(&Action::StartMacroRecording); // filtered
m.record_if_recording(&Action::StopMacroRecording); // filtered
m.record_if_recording(&Action::PlayMacro('x')); // filtered
m.record_if_recording(&Action::ListMacros); // filtered
m.record_if_recording(&Action::MoveRight);
let (_key, count) = m.stop_recording().unwrap();
assert_eq!(count, 2); // only Left + Right were recorded
}
#[test]
fn recording_suppressed_while_playing() {
let mut m = MacroState::default();
m.start_recording('q');
m.begin_play();
m.record_if_recording(&Action::MoveLeft);
m.end_play();
let (_key, count) = m.stop_recording().unwrap();
assert_eq!(count, 0);
}
#[test]
fn record_transformed_bypasses_control_filter() {
// PromptConfirm is not a control action, but the orchestrator
// transforms it into PromptConfirmWithText. That transformed form
// should land in the recording even though it's a manually-produced
// variant.
let mut m = MacroState::default();
m.start_recording('q');
m.record_transformed(Action::PromptConfirmWithText("hello".to_string()));
let (_key, count) = m.stop_recording().unwrap();
assert_eq!(count, 1);
}
#[test]
fn starting_second_recording_discards_first() {
// Deliberate behaviour: start_recording clobbers any in-flight
// recording. Callers that want to save the previous one must call
// stop_recording first.
let mut m = MacroState::default();
m.start_recording('a');
m.record_if_recording(&Action::MoveLeft);
m.start_recording('b');
assert_eq!(m.recording_key(), Some('b'));
let (key, count) = m.stop_recording().unwrap();
assert_eq!(key, 'b');
assert_eq!(count, 0);
}
#[test]
fn keys_sorted_returns_stable_order() {
let mut m = MacroState::default();
m.start_recording('c');
m.stop_recording();
m.start_recording('a');
m.stop_recording();
m.start_recording('b');
m.stop_recording();
assert_eq!(m.keys_sorted(), vec!['a', 'b', 'c']);
}
#[test]
fn last_register_tracks_most_recent_recording() {
let mut m = MacroState::default();
m.start_recording('a');
m.stop_recording();
assert_eq!(m.last_register(), Some('a'));
m.start_recording('b');
m.stop_recording();
assert_eq!(m.last_register(), Some('b'));
}
}