strop-engine 0.36.0

strop editor engine: documents, grammar, services and sessions — no frontend UI dependencies
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
//! One owner for modal input (R7). A text prompt — `:ex`, `/search`,
//! `?search`, `|pipe` — owns its `LineEdit` plus everything about the
//! moment it opened: the pane (full selections and viewport), the
//! document and its revision. Structural operator composition stays in
//! the `Walker`; prompts never fabricate grammar text.

use strop_core::{id::BufferRevision, Range};
use strop_picker::LineEdit;

use super::{input::ParserState, panes::Pane, Key};

/// Where a prompt opened: the whole originating pane (document,
/// complete selections, viewport) and the buffer revision. Cancellation
/// restores this verbatim; acceptance validates it before executing.
#[derive(Debug, Clone)]
pub(crate) struct SearchOrigin {
    pub pane_index: usize,
    pub pane: Pane,
    pub revision: BufferRevision,
}

/// What the prompt is for. The sigil is derivable; the context is not.
#[derive(Debug, Clone)]
pub(crate) enum PromptContext {
    Ex(SearchOrigin),
    Search {
        origin: SearchOrigin,
        /// The typed entry state (counts/register/operator) that
        /// survived crossing into the prompt (`2d/foo` keeps 2 and d).
        state: ParserState,
        backward: bool,
    },
    Pipe {
        origin: SearchOrigin,
        range: Range,
        visual: bool,
    },
}

/// One open modal line: the editable text plus its origin context.
#[derive(Debug, Clone)]
pub(crate) struct TextPrompt {
    line: LineEdit,
    context: PromptContext,
}

impl TextPrompt {
    pub(crate) fn new(context: PromptContext) -> Self {
        let sigil = match &context {
            PromptContext::Ex(_) => ':',
            PromptContext::Search {
                backward: false, ..
            } => '/',
            PromptContext::Search { backward: true, .. } => '?',
            PromptContext::Pipe { .. } => '|',
        };
        Self {
            line: LineEdit::new(sigil.to_string()),
            context,
        }
    }

    pub(crate) fn sigil(&self) -> char {
        match &self.context {
            PromptContext::Ex(_) => ':',
            PromptContext::Search {
                backward: false, ..
            } => '/',
            PromptContext::Search { backward: true, .. } => '?',
            PromptContext::Pipe { .. } => '|',
        }
    }

    /// The text after the sigil — the command/pattern body.
    pub(crate) fn body(&self) -> &str {
        &self.line.text()[1..]
    }

    /// The full line, sigil included (empty string semantics live on
    /// `PendingInput`, which knows whether a prompt is open at all).
    pub(crate) fn text(&self) -> &str {
        self.line.text()
    }

    /// Caret byte offset into `text()` (sigil-inclusive).
    pub(crate) fn cursor(&self) -> usize {
        self.line.cursor()
    }

    /// True when Esc put the line's own caret into normal mode.
    pub(crate) fn normal(&self) -> bool {
        self.line.normal
    }

    pub(crate) fn context(&self) -> &PromptContext {
        &self.context
    }

    pub(crate) fn origin(&self) -> &SearchOrigin {
        match &self.context {
            PromptContext::Ex(origin)
            | PromptContext::Search { origin, .. }
            | PromptContext::Pipe { origin, .. } => origin,
        }
    }

    /// The saved search entry, when this prompt is a `/` or `?` line.
    pub(crate) fn search(&self) -> Option<(&SearchOrigin, &ParserState)> {
        match &self.context {
            PromptContext::Search { origin, state, .. } => Some((origin, state)),
            _ => None,
        }
    }

    pub(crate) fn backward(&self) -> Option<bool> {
        match &self.context {
            PromptContext::Search { backward, .. } => Some(*backward),
            _ => None,
        }
    }
}

/// The editor's one prompt slot: open or closed, nothing in between.
/// The field machine rides alongside: key-sequence state (a pending
/// operator) survives between the prompt's normal-mode key events.
#[derive(Debug, Default)]
pub struct PendingInput {
    active: Option<TextPrompt>,
    machine: super::field::FieldMachine,
}

/// One input event for the open prompt.
pub(crate) enum PendingEvent {
    Key(Key),
    /// Bracketed paste routed away from the document (literal text).
    Paste(String),
    /// Ex completion offered the given body (Tab cycling).
    CompleteEx(String),
    Cancel,
}

/// What the reducer did — the caller owns every side effect.
pub(crate) enum PendingEffect {
    None,
    /// Text changed: re-resolve incsearch.
    Edited,
    /// The line's modal mode flipped.
    ModeChanged,
    /// Tab on the ex line: cycle completion.
    CompleteEx,
    /// Ctrl-L: terminal desync recovery.
    Repaint,
    /// The event was refused (a pasted newline, a key the field's
    /// grammar does not admit) — the message is the feedback.
    Rejected(String),
    /// Enter: consume and execute (Pipe/Search/Ex by context).
    Accepted(TextPrompt),
    /// Esc-Esc / sigil deletion / Cancel: consume and restore.
    Aborted(TextPrompt),
}

impl PendingInput {
    pub(crate) fn prompt(&self) -> Option<&TextPrompt> {
        self.active.as_ref()
    }

    pub fn is_active(&self) -> bool {
        self.active.is_some()
    }

    /// The full line, sigil included; empty when no prompt is open.
    pub fn text(&self) -> &str {
        self.prompt().map_or("", TextPrompt::text)
    }

    /// Caret byte offset into `text()`; 0 when no prompt is open.
    pub fn cursor(&self) -> usize {
        self.prompt().map_or(0, TextPrompt::cursor)
    }

    pub(crate) fn normal(&self) -> bool {
        self.prompt().is_some_and(TextPrompt::normal)
    }

    /// The open prompt's sigil (`: / ? |`); None when closed.
    pub(crate) fn sigil(&self) -> Option<char> {
        self.prompt().map(TextPrompt::sigil)
    }

    /// Open a prompt. Opening while another is active is a caller bug:
    /// the previous origin would be silently discarded. The field
    /// machine grounds here, so a dead prompt's pending operator never
    /// leaks into the next line.
    pub(crate) fn open(&mut self, prompt: TextPrompt) {
        assert!(
            self.active.is_none(),
            "cancel the previous prompt before opening another"
        );
        self.machine.clear();
        self.active = Some(prompt);
    }

    /// The one reducer. Every surface funnels here; effects are the
    /// caller's to apply. Enter and abort consume the prompt and hand
    /// it back — exactly once (a closed prompt yields `None`).
    pub(crate) fn reduce(&mut self, event: PendingEvent) -> PendingEffect {
        let Some(prompt) = self.active.as_mut() else {
            return PendingEffect::None;
        };
        if matches!(event, PendingEvent::Cancel)
            || matches!(event, PendingEvent::Key(Key::Esc)) && prompt.line.normal
        {
            return PendingEffect::Aborted(self.active.take().expect("active prompt"));
        }
        if matches!(event, PendingEvent::Key(Key::Enter)) {
            return PendingEffect::Accepted(self.active.take().expect("active prompt"));
        }
        let old_revision = prompt.line.revision();
        let old_normal = prompt.line.normal;
        match event {
            PendingEvent::Paste(text) => {
                if text.contains(['\r', '\n']) {
                    return PendingEffect::Rejected("input line cannot contain a newline".into());
                }
                prompt.line.insert_str(&text);
            }
            PendingEvent::CompleteEx(body) if prompt.sigil() == ':' => {
                prompt.line.set_text(format!(":{body}"));
                return PendingEffect::Edited;
            }
            PendingEvent::CompleteEx(_) | PendingEvent::Cancel => return PendingEffect::None,
            PendingEvent::Key(Key::Esc) => {
                prompt.line.normal = true;
                prompt.line.set_cursor(usize::MAX);
            }
            PendingEvent::Key(Key::Backspace) if prompt.line.normal => {
                // vim: BS in normal mode is h — a pure grammar motion
                let _ = self.machine.feed(&mut prompt.line, Key::Char('h'));
            }
            PendingEvent::Key(Key::Backspace) => {
                prompt.line.backspace();
            }
            PendingEvent::Key(Key::Char(c)) if prompt.line.normal => {
                // the real vim grammar (0003 §2): the field machine
                // resolves every sequence against the line's buffer
                if let super::field::FieldReply::Refused(message) =
                    self.machine.feed(&mut prompt.line, Key::Char(c))
                {
                    return PendingEffect::Rejected(message);
                }
            }
            PendingEvent::Key(Key::Char(c)) => {
                prompt.line.insert_char(c);
            }
            PendingEvent::Key(Key::Left) => prompt.line.move_left(),
            PendingEvent::Key(Key::Right) => prompt.line.move_right(),
            PendingEvent::Key(Key::Tab) if prompt.sigil() == ':' => {
                return PendingEffect::CompleteEx;
            }
            PendingEvent::Key(Key::CtrlL) => return PendingEffect::Repaint,
            PendingEvent::Key(_) => return PendingEffect::None,
        }
        // The sigil is structural: an edit that removes it (backspace
        // at 1, `x` at 0, an operator range covering the sigil) closes
        // the prompt — same gesture as Esc-Esc.
        if !prompt.line.text().starts_with(prompt.sigil()) {
            return PendingEffect::Aborted(self.active.take().expect("active prompt"));
        }
        if old_revision != prompt.line.revision() {
            PendingEffect::Edited
        } else if old_normal != prompt.line.normal {
            PendingEffect::ModeChanged
        } else {
            PendingEffect::None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::editor::Editor;
    use strop_core::Buffer;

    #[test]
    fn accepting_a_visual_pipe_returns_its_original_range_and_literal_command() {
        let e = Editor::new(Buffer::from_text("first\nsecond\n"));
        let origin = SearchOrigin {
            pane_index: e.active_pane,
            pane: e.view().clone(),
            revision: e.buf().revision(),
        };
        let mut pending = PendingInput::default();
        pending.open(TextPrompt::new(PromptContext::Pipe {
            origin,
            range: Range::charwise(0, 13),
            visual: true,
        }));
        for c in "cat".chars() {
            pending.reduce(PendingEvent::Key(Key::Char(c)));
        }
        let PendingEffect::Accepted(prompt) = pending.reduce(PendingEvent::Key(Key::Enter)) else {
            panic!("pipe not accepted");
        };
        assert_eq!(prompt.body(), "cat");
        match prompt.context() {
            PromptContext::Pipe { range, visual, .. } => {
                assert_eq!(*range, Range::charwise(0, 13));
                assert!(*visual);
            }
            _ => panic!("lost pipe effect"),
        }
        assert!(!pending.is_active());
        // exactly-once consumption: a second Enter finds nothing open
        assert!(matches!(
            pending.reduce(PendingEvent::Key(Key::Enter)),
            PendingEffect::None
        ));
    }

    #[test]
    fn esc_once_is_modal_twice_aborts_and_sigil_deletion_aborts() {
        let e = Editor::new(Buffer::from_text("x\n"));
        let origin = SearchOrigin {
            pane_index: e.active_pane,
            pane: e.view().clone(),
            revision: e.buf().revision(),
        };
        let mut pending = PendingInput::default();
        pending.open(TextPrompt::new(PromptContext::Search {
            origin,
            state: ParserState::default(),
            backward: false,
        }));
        for c in "ab".chars() {
            pending.reduce(PendingEvent::Key(Key::Char(c)));
        }
        assert_eq!(pending.text(), "/ab");
        // first Esc: the line's caret goes modal, prompt stays open
        assert!(matches!(
            pending.reduce(PendingEvent::Key(Key::Esc)),
            PendingEffect::ModeChanged
        ));
        assert!(pending.normal());
        // modal x at the sigil deletes it: abort
        let _ = pending.reduce(PendingEvent::Key(Key::Char('0')));
        let PendingEffect::Aborted(prompt) = pending.reduce(PendingEvent::Key(Key::Char('x')))
        else {
            panic!("sigil deletion must abort");
        };
        assert_eq!(prompt.text(), "ab"); // sigil gone, body intact
        assert!(!pending.is_active());
        assert_eq!(pending.text(), "");
    }

    #[test]
    fn paste_is_literal_and_newlines_are_rejected() {
        let e = Editor::new(Buffer::from_text("x\n"));
        let origin = SearchOrigin {
            pane_index: e.active_pane,
            pane: e.view().clone(),
            revision: e.buf().revision(),
        };
        let mut pending = PendingInput::default();
        pending.open(TextPrompt::new(PromptContext::Ex(origin)));
        assert!(matches!(
            pending.reduce(PendingEvent::Paste("w q".into())),
            PendingEffect::Edited
        ));
        assert_eq!(pending.text(), ":w q");
        assert!(matches!(
            pending.reduce(PendingEvent::Paste("\nx".into())),
            PendingEffect::Rejected(message) if message == "input line cannot contain a newline"
        ));
        assert_eq!(pending.text(), ":w q");
    }

    /// Body typed on the `:` line, Esc into the line's normal mode,
    /// then edited with the given keys: the resulting line text.
    fn ex_after(body: &str, keys: &str) -> String {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.feed_text(":");
        e.feed_text(body);
        e.feed(crate::editor::Key::Esc);
        e.feed_text(keys);
        e.pending.text().to_string()
    }

    #[test]
    fn ex_line_normal_mode_is_the_real_grammar() {
        // the same grammar machine as the picker fields (0003 §2)
        assert_eq!(ex_after("w foo", "bdw"), ":w ");
        assert_eq!(ex_after("w foo", "db"), ":w ");
        // de on a one-char word: e sits at the word's end already, so
        // it extends to the next word's end (vim does the same)
        assert_eq!(ex_after("w foo", "0lde"), ":");
        assert_eq!(ex_after("w:x", "0ldf:"), ":x");
        assert_eq!(ex_after("a b c", "0l2dw"), ":c");
        assert_eq!(ex_after("w foo", "bdiw"), ":w ");
        // j/k are motions that cannot leave the one line: honest no-ops
        assert_eq!(ex_after("w", "jk"), ":w");
    }

    #[test]
    fn ex_line_change_reenters_insert_and_sigil_ops_abort() {
        let mut e = Editor::new(Buffer::from_text("x\n"));
        e.feed_text(":w foo");
        e.feed(crate::editor::Key::Esc);
        e.feed_text("bcwbar");
        assert_eq!(e.pending.text(), ":w bar");
        assert!(!e.pending.normal(), "cw re-enters the line's insert mode");
        // an operator range covering the sigil trips the standing
        // sigil rule: the prompt closes, exactly like x at 0
        e.feed(crate::editor::Key::Esc);
        assert!(e.pending.normal());
        e.feed_text("0dw");
        assert!(!e.pending.is_active(), "0dw covers the sigil: abort");
    }
}