Skip to main content

hjkl_engine/
editor.rs

1//! Editor — the public sqeel-vim type, layered over `hjkl_buffer::Buffer`.
2//!
3//! This file owns the public Editor API — construction, content access,
4//! mouse and goto helpers, the (buffer-level) undo stack, and insert-mode
5//! session bookkeeping. All vim-specific keyboard handling lives in
6//! [`vim`] and communicates with Editor through a small internal API
7//! exposed via `pub(super)` fields and helper methods.
8
9use crate::input::Input;
10#[cfg(feature = "crossterm")]
11use crate::input::Key;
12use crate::vim::{self, VimState};
13use crate::{KeybindingMode, VimMode};
14#[cfg(feature = "crossterm")]
15use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
16use std::sync::atomic::{AtomicU16, Ordering};
17
18/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
19/// [`crate::types::Edit`] (`EditOp`) records.
20///
21/// Most buffer edits map to a single EditOp. Block ops
22/// ([`hjkl_buffer::Edit::InsertBlock`] /
23/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
24/// touched — they edit non-contiguous cells and a single
25/// `range..range` can't represent the rectangle.
26///
27/// Returns an empty vec when the edit isn't representable (no buffer
28/// variant currently fails this check).
29fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
30    use crate::types::{Edit as Op, Pos};
31    use hjkl_buffer::Edit as B;
32    let to_pos = |p: hjkl_buffer::Position| Pos {
33        line: p.row as u32,
34        col: p.col as u32,
35    };
36    match edit {
37        B::InsertChar { at, ch } => vec![Op {
38            range: to_pos(*at)..to_pos(*at),
39            replacement: ch.to_string(),
40        }],
41        B::InsertStr { at, text } => vec![Op {
42            range: to_pos(*at)..to_pos(*at),
43            replacement: text.clone(),
44        }],
45        B::DeleteRange { start, end, .. } => vec![Op {
46            range: to_pos(*start)..to_pos(*end),
47            replacement: String::new(),
48        }],
49        B::Replace { start, end, with } => vec![Op {
50            range: to_pos(*start)..to_pos(*end),
51            replacement: with.clone(),
52        }],
53        B::JoinLines {
54            row,
55            count,
56            with_space,
57        } => {
58            // Joining `count` rows after `row` collapses
59            // [(row+1, 0) .. (row+count, EOL)] into the joined
60            // sentinel. The replacement is either an empty string
61            // (gJ) or " " between segments (J).
62            let start = Pos {
63                line: *row as u32 + 1,
64                col: 0,
65            };
66            let end = Pos {
67                line: (*row + *count) as u32,
68                col: u32::MAX, // covers to EOL of the last source row
69            };
70            vec![Op {
71                range: start..end,
72                replacement: if *with_space {
73                    " ".into()
74                } else {
75                    String::new()
76                },
77            }]
78        }
79        B::SplitLines {
80            row,
81            cols,
82            inserted_space: _,
83        } => {
84            // SplitLines reverses a JoinLines: insert a `\n`
85            // (and optional dropped space) at each col on `row`.
86            cols.iter()
87                .map(|c| {
88                    let p = Pos {
89                        line: *row as u32,
90                        col: *c as u32,
91                    };
92                    Op {
93                        range: p..p,
94                        replacement: "\n".into(),
95                    }
96                })
97                .collect()
98        }
99        B::InsertBlock { at, chunks } => {
100            // One EditOp per row in the block — non-contiguous edits.
101            chunks
102                .iter()
103                .enumerate()
104                .map(|(i, chunk)| {
105                    let p = Pos {
106                        line: at.row as u32 + i as u32,
107                        col: at.col as u32,
108                    };
109                    Op {
110                        range: p..p,
111                        replacement: chunk.clone(),
112                    }
113                })
114                .collect()
115        }
116        B::DeleteBlockChunks { at, widths } => {
117            // One EditOp per row, deleting `widths[i]` chars at
118            // `(at.row + i, at.col)`.
119            widths
120                .iter()
121                .enumerate()
122                .map(|(i, w)| {
123                    let start = Pos {
124                        line: at.row as u32 + i as u32,
125                        col: at.col as u32,
126                    };
127                    let end = Pos {
128                        line: at.row as u32 + i as u32,
129                        col: at.col as u32 + *w as u32,
130                    };
131                    Op {
132                        range: start..end,
133                        replacement: String::new(),
134                    }
135                })
136                .collect()
137        }
138    }
139}
140
141/// Sum of bytes from the start of the buffer to the start of `row`.
142/// Walks lines + their separating `\n` bytes — matches the canonical
143/// `lines().join("\n")` byte rendering used by syntax tooling.
144#[inline]
145fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
146    let n = buf.row_count();
147    let row = row.min(n);
148    let mut acc = 0usize;
149    for r in 0..row {
150        acc += buf.line(r).map(|s| s.len()).unwrap_or(0);
151        if r + 1 < n {
152            acc += 1; // separator '\n'
153        }
154    }
155    acc
156}
157
158/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
159/// coordinates `(byte_within_buffer, (row, col_byte))` against the
160/// **pre-edit** buffer.
161fn position_to_byte_coords(
162    buf: &hjkl_buffer::Buffer,
163    pos: hjkl_buffer::Position,
164) -> (usize, (u32, u32)) {
165    let row = pos.row.min(buf.row_count().saturating_sub(1));
166    let line = buf.line(row).unwrap_or_default();
167    let col_byte = pos.byte_offset(&line);
168    let byte = buffer_byte_of_row(buf, row) + col_byte;
169    (byte, (row as u32, col_byte as u32))
170}
171
172/// Compute the byte position after inserting `text` starting at
173/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
174fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
175    let new_end_byte = start_byte + text.len();
176    let newlines = text.bytes().filter(|&b| b == b'\n').count();
177    let end_pos = if newlines == 0 {
178        (start_pos.0, start_pos.1 + text.len() as u32)
179    } else {
180        // Bytes after the last newline determine the trailing column.
181        let last_nl = text.rfind('\n').unwrap();
182        let tail_bytes = (text.len() - last_nl - 1) as u32;
183        (start_pos.0 + newlines as u32, tail_bytes)
184    };
185    (new_end_byte, end_pos)
186}
187
188/// Translate a single `hjkl_buffer::Edit` into one or more
189/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
190/// state for byte/position lookups. Block ops fan out to one entry per
191/// touched row (matches `edit_to_editops`).
192fn content_edits_from_buffer_edit(
193    buf: &hjkl_buffer::Buffer,
194    edit: &hjkl_buffer::Edit,
195) -> Vec<crate::types::ContentEdit> {
196    use hjkl_buffer::Edit as B;
197    use hjkl_buffer::Position;
198
199    let mut out: Vec<crate::types::ContentEdit> = Vec::new();
200
201    match edit {
202        B::InsertChar { at, ch } => {
203            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
204            let new_end_byte = start_byte + ch.len_utf8();
205            let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
206            out.push(crate::types::ContentEdit {
207                start_byte,
208                old_end_byte: start_byte,
209                new_end_byte,
210                start_position: start_pos,
211                old_end_position: start_pos,
212                new_end_position: new_end_pos,
213            });
214        }
215        B::InsertStr { at, text } => {
216            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
217            let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
218            out.push(crate::types::ContentEdit {
219                start_byte,
220                old_end_byte: start_byte,
221                new_end_byte,
222                start_position: start_pos,
223                old_end_position: start_pos,
224                new_end_position: new_end_pos,
225            });
226        }
227        B::DeleteRange { start, end, kind } => {
228            let (start, end) = if start <= end {
229                (*start, *end)
230            } else {
231                (*end, *start)
232            };
233            match kind {
234                hjkl_buffer::MotionKind::Char => {
235                    let (start_byte, start_pos) = position_to_byte_coords(buf, start);
236                    let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
237                    out.push(crate::types::ContentEdit {
238                        start_byte,
239                        old_end_byte,
240                        new_end_byte: start_byte,
241                        start_position: start_pos,
242                        old_end_position: old_end_pos,
243                        new_end_position: start_pos,
244                    });
245                }
246                hjkl_buffer::MotionKind::Line => {
247                    // Linewise delete drops rows [start.row..=end.row]. Map
248                    // to a span from start of `start.row` through start of
249                    // (end.row + 1). The buffer's own `do_delete_range`
250                    // collapses to row `start.row` after dropping.
251                    let lo = start.row;
252                    let hi = end.row.min(buf.row_count().saturating_sub(1));
253                    let start_byte = buffer_byte_of_row(buf, lo);
254                    let next_row_byte = if hi + 1 < buf.row_count() {
255                        buffer_byte_of_row(buf, hi + 1)
256                    } else {
257                        // No row after; clamp to end-of-buffer byte.
258                        buffer_byte_of_row(buf, buf.row_count())
259                            + buf
260                                .line(buf.row_count().saturating_sub(1))
261                                .map(|s| s.len())
262                                .unwrap_or(0)
263                    };
264                    out.push(crate::types::ContentEdit {
265                        start_byte,
266                        old_end_byte: next_row_byte,
267                        new_end_byte: start_byte,
268                        start_position: (lo as u32, 0),
269                        old_end_position: ((hi + 1) as u32, 0),
270                        new_end_position: (lo as u32, 0),
271                    });
272                }
273                hjkl_buffer::MotionKind::Block => {
274                    // Block delete removes a rectangle of chars per row.
275                    // Fan out to one ContentEdit per row.
276                    let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
277                    for row in start.row..=end.row {
278                        let row_start_pos = Position::new(row, left_col);
279                        let row_end_pos = Position::new(row, right_col + 1);
280                        let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
281                        let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
282                        if eb <= sb {
283                            continue;
284                        }
285                        out.push(crate::types::ContentEdit {
286                            start_byte: sb,
287                            old_end_byte: eb,
288                            new_end_byte: sb,
289                            start_position: sp,
290                            old_end_position: ep,
291                            new_end_position: sp,
292                        });
293                    }
294                }
295            }
296        }
297        B::Replace { start, end, with } => {
298            let (start, end) = if start <= end {
299                (*start, *end)
300            } else {
301                (*end, *start)
302            };
303            let (start_byte, start_pos) = position_to_byte_coords(buf, start);
304            let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
305            let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
306            out.push(crate::types::ContentEdit {
307                start_byte,
308                old_end_byte,
309                new_end_byte,
310                start_position: start_pos,
311                old_end_position: old_end_pos,
312                new_end_position: new_end_pos,
313            });
314        }
315        B::JoinLines {
316            row,
317            count,
318            with_space,
319        } => {
320            // Joining `count` rows after `row` collapses the bytes
321            // between EOL of `row` and EOL of `row + count` into either
322            // an empty string (gJ) or a single space per join (J — but
323            // only when both sides are non-empty; we approximate with
324            // a single space for simplicity).
325            let row = (*row).min(buf.row_count().saturating_sub(1));
326            let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
327            let line = buf.line(row).unwrap_or_default();
328            let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
329            let row_eol_col = line.len() as u32;
330            let next_row_after = last_join_row + 1;
331            let old_end_byte = if next_row_after < buf.row_count() {
332                buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
333            } else {
334                buffer_byte_of_row(buf, buf.row_count())
335                    + buf
336                        .line(buf.row_count().saturating_sub(1))
337                        .map(|s| s.len())
338                        .unwrap_or(0)
339            };
340            let last_line = buf.line(last_join_row).unwrap_or_default();
341            let old_end_pos = (last_join_row as u32, last_line.len() as u32);
342            let replacement_len = if *with_space { 1 } else { 0 };
343            let new_end_byte = row_eol_byte + replacement_len;
344            let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
345            out.push(crate::types::ContentEdit {
346                start_byte: row_eol_byte,
347                old_end_byte,
348                new_end_byte,
349                start_position: (row as u32, row_eol_col),
350                old_end_position: old_end_pos,
351                new_end_position: new_end_pos,
352            });
353        }
354        B::SplitLines {
355            row,
356            cols,
357            inserted_space,
358        } => {
359            // Splits insert "\n" (or "\n " inverse) at each col on `row`.
360            // The buffer applies all splits left-to-right via the
361            // do_split_lines path; we emit one ContentEdit per col,
362            // each treated as an insert at that col on `row`. Note: the
363            // buffer state during emission is *pre-edit*, so all cols
364            // index into the same pre-edit row.
365            let row = (*row).min(buf.row_count().saturating_sub(1));
366            let line = buf.line(row).unwrap_or_default();
367            let row_byte = buffer_byte_of_row(buf, row);
368            let insert = if *inserted_space { "\n " } else { "\n" };
369            for &c in cols {
370                let pos = Position::new(row, c);
371                let col_byte = pos.byte_offset(&line);
372                let start_byte = row_byte + col_byte;
373                let start_pos = (row as u32, col_byte as u32);
374                let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
375                out.push(crate::types::ContentEdit {
376                    start_byte,
377                    old_end_byte: start_byte,
378                    new_end_byte,
379                    start_position: start_pos,
380                    old_end_position: start_pos,
381                    new_end_position: new_end_pos,
382                });
383            }
384        }
385        B::InsertBlock { at, chunks } => {
386            // One ContentEdit per chunk; each lands at `(at.row + i,
387            // at.col)` in the pre-edit buffer.
388            for (i, chunk) in chunks.iter().enumerate() {
389                let pos = Position::new(at.row + i, at.col);
390                let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
391                let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
392                out.push(crate::types::ContentEdit {
393                    start_byte,
394                    old_end_byte: start_byte,
395                    new_end_byte,
396                    start_position: start_pos,
397                    old_end_position: start_pos,
398                    new_end_position: new_end_pos,
399                });
400            }
401        }
402        B::DeleteBlockChunks { at, widths } => {
403            for (i, w) in widths.iter().enumerate() {
404                let row = at.row + i;
405                let start_pos = Position::new(row, at.col);
406                let end_pos = Position::new(row, at.col + *w);
407                let (sb, sp) = position_to_byte_coords(buf, start_pos);
408                let (eb, ep) = position_to_byte_coords(buf, end_pos);
409                if eb <= sb {
410                    continue;
411                }
412                out.push(crate::types::ContentEdit {
413                    start_byte: sb,
414                    old_end_byte: eb,
415                    new_end_byte: sb,
416                    start_position: sp,
417                    old_end_position: ep,
418                    new_end_position: sp,
419                });
420            }
421        }
422    }
423
424    out
425}
426
427/// Where the cursor should land in the viewport after a `z`-family
428/// scroll (`zz` / `zt` / `zb`).
429#[derive(Debug, Clone, Copy, PartialEq, Eq)]
430pub(super) enum CursorScrollTarget {
431    Center,
432    Top,
433    Bottom,
434}
435
436// ── Trait-surface cast helpers ────────────────────────────────────
437//
438// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
439// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
440// their reaches through the same primitives. Re-import via
441// `use` so the editor body keeps its terse call shape.
442
443use crate::buf_helpers::{
444    apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
445    buf_lines_to_vec, buf_row_count, buf_set_cursor_rc,
446};
447
448pub struct Editor<
449    B: crate::types::Buffer = hjkl_buffer::Buffer,
450    H: crate::types::Host = crate::types::DefaultHost,
451> {
452    pub keybinding_mode: KeybindingMode,
453    /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
454    pub last_yank: Option<String>,
455    /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
456    /// Internal — exposed via Editor accessor methods
457    /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
458    /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
459    pub(crate) vim: VimState,
460    /// Undo history: each entry is (lines, cursor) before the edit.
461    /// Internal — managed by [`Editor::push_undo`] / [`Editor::restore`]
462    /// / [`Editor::pop_last_undo`].
463    pub(crate) undo_stack: Vec<(Vec<String>, (usize, usize))>,
464    /// Redo history: entries pushed when undoing.
465    pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
466    /// Set whenever the buffer content changes; cleared by `take_dirty`.
467    pub(super) content_dirty: bool,
468    /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
469    /// so repeated `content_arc()` calls within the same un-mutated
470    /// window are free (ref-count bump instead of a full-buffer join).
471    /// Invalidated by every [`mark_content_dirty`] call.
472    pub(super) cached_content: Option<std::sync::Arc<String>>,
473    /// Last rendered viewport height (text rows only, no chrome). Written
474    /// by the draw path via [`set_viewport_height`] so the scroll helpers
475    /// can clamp the cursor to stay visible without plumbing the height
476    /// through every call.
477    pub(super) viewport_height: AtomicU16,
478    /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
479    /// goto-definition). The host app drains this each step and fires
480    /// the matching request against its own LSP client.
481    pub(super) pending_lsp: Option<LspIntent>,
482    /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
483    /// the `:fold*` Ex commands, or the edit pipeline's
484    /// "edits-inside-a-fold open it" invalidation. Drained by hosts
485    /// via [`Editor::take_fold_ops`]; the engine also applies each op
486    /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
487    /// so the in-tree buffer fold storage stays in sync without host
488    /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
489    pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
490    /// Buffer storage.
491    ///
492    /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
493    /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
494    /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
495    /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
496    pub(super) buffer: B,
497    /// Engine-native style intern table. Opaque `Span::style` ids index
498    /// into this table; the render path resolves ids back to
499    /// [`crate::types::Style`]. Ratatui hosts convert at the boundary via
500    /// `hjkl_engine_tui::style_to_ratatui`. Always present — no cfg-mutex.
501    pub(super) style_table: Vec<crate::types::Style>,
502    /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
503    /// every `p` / `P` via the active selector (default unnamed).
504    /// Internal — read via [`Editor::registers`]; mutated by yank /
505    /// delete / paste FSM paths and by [`Editor::seed_yank`].
506    pub(crate) registers: crate::registers::Registers,
507    /// Per-row syntax styling in engine-native form. Always present —
508    /// populated by [`Editor::install_syntax_spans`]. Ratatui hosts use
509    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`.
510    pub styled_spans: Vec<Vec<(usize, usize, crate::types::Style)>>,
511    /// Per-editor settings tweakable via `:set`. Exposed by reference
512    /// so handlers (indent, search) read the live value rather than a
513    /// snapshot taken at startup. Read via [`Editor::settings`];
514    /// mutate via [`Editor::settings_mut`].
515    pub(crate) settings: Settings,
516    /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
517    /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
518    /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
519    /// "file marks" that survive [`Editor::set_content`] calls so
520    /// they persist across tab swaps within the same Editor.
521    ///
522    /// 0.0.36: consolidated from three former storages:
523    /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
524    /// - `vim::VimState::marks` (lowercase) (deleted).
525    /// - `Editor::file_marks` (uppercase) (replaced by this map).
526    ///
527    /// `BTreeMap` so iteration is deterministic for snapshot tests
528    /// and the `:marks` ex command. Mark-shift on edits is handled
529    /// by [`Editor::shift_marks_after_edit`].
530    pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
531    /// Block ranges (`(start_row, end_row)` inclusive) the host has
532    /// extracted from a syntax tree. `:foldsyntax` reads these to
533    /// populate folds. The host refreshes them on every re-parse via
534    /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
535    /// [`Editor::syntax_fold_ranges`].
536    pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
537    /// Pending edit log drained by [`Editor::take_changes`]. Each entry
538    /// is a SPEC [`crate::types::Edit`] mapped from the underlying
539    /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
540    /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
541    /// best-effort EditOp covering the touched range; hosts wanting
542    /// per-cell deltas should diff their own snapshot of `lines()`.
543    /// Sealed at 0.1.0 trait extraction.
544    /// Drained by [`Editor::take_changes`].
545    pub(crate) change_log: Vec<crate::types::Edit>,
546    /// Vim's "sticky column" (curswant). `None` before the first
547    /// motion — the next vertical motion bootstraps from the live
548    /// cursor column. Horizontal motions refresh this to the new
549    /// column; vertical motions read it back so bouncing through a
550    /// shorter row doesn't drag the cursor to col 0. Hoisted out of
551    /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
552    /// the single owner now. Buffer motion methods that need it
553    /// take a `&mut Option<usize>` parameter.
554    pub(crate) sticky_col: Option<usize>,
555    /// Host adapter for clipboard, cursor-shape, time, viewport, and
556    /// search-prompt / cancellation side-channels.
557    ///
558    /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
559    /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
560    /// dyn-shim is gone — every method now dispatches through `H`'s
561    /// `Host` trait surface directly.
562    pub(crate) host: H,
563    /// Last public mode the cursor-shape emitter saw. Drives
564    /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
565    /// fires exactly once per mode transition without sprinkling the
566    /// call across every `vim.mode = ...` site.
567    pub(crate) last_emitted_mode: crate::VimMode,
568    /// Search FSM state (pattern + per-row match cache + wrapscan).
569    /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
570    /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
571    /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
572    /// gone; `BufferView` now takes the active regex as a `&Regex`
573    /// parameter, sourced from `Editor::search_state().pattern`.
574    pub(crate) search_state: crate::search::SearchState,
575    /// Per-row syntax span overlay. Source of truth for the host's
576    /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
577    /// [`Editor::install_syntax_spans`] (ratatui hosts use
578    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`)
579    /// and, in due course, by `Host::syntax_highlights` once the engine
580    /// drives that path directly.
581    ///
582    /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
583    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
584    /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
585    pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
586    /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
587    /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
588    /// tree (or any other content-change observer that needs byte-level
589    /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
590    pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
591    /// Pending "reset" flag set when the entire buffer is replaced
592    /// (e.g. `set_content` / `restore`). Supersedes any queued
593    /// `pending_content_edits` on the same frame: hosts call
594    /// [`Editor::take_content_reset`] before draining edits.
595    pub(crate) pending_content_reset: bool,
596    /// Row range touched by the most recent `auto_indent_rows` call.
597    /// `(top_row, bot_row)` inclusive. Set by the engine after every
598    /// auto-indent operation; drained (and cleared) by the host via
599    /// [`Editor::take_last_indent_range`] so it can display a brief
600    /// visual flash over the reindented rows.
601    pub(crate) last_indent_range: Option<(usize, usize)>,
602}
603
604/// Vim-style options surfaced by `:set`. New fields land here as
605/// individual ex commands gain `:set` plumbing.
606#[derive(Debug, Clone)]
607pub struct Settings {
608    /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
609    pub shiftwidth: usize,
610    /// Visual width of a `\t` character. Stored for future render
611    /// hookup; not yet consumed by the buffer renderer.
612    pub tabstop: usize,
613    /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
614    /// without an explicit `i` flag.
615    pub ignore_case: bool,
616    /// When true *and* `ignore_case` is true, an uppercase letter in
617    /// the pattern flips that search back to case-sensitive. Matches
618    /// vim's `:set smartcase`. Default `false`.
619    pub smartcase: bool,
620    /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
621    /// Default `true`.
622    pub wrapscan: bool,
623    /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
624    pub textwidth: usize,
625    /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
626    /// instead of a literal `\t`. Matches vim's `:set expandtab`.
627    /// Default `false`.
628    pub expandtab: bool,
629    /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
630    /// next softtabstop boundary (when `expandtab`), and Backspace at the
631    /// end of a softtabstop-aligned space run deletes the entire run as
632    /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
633    pub softtabstop: usize,
634    /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
635    /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
636    /// past the right edge and `top_col` clips the left side.
637    /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
638    /// to word-break wrap; `:set nowrap` resets.
639    pub wrap: hjkl_buffer::Wrap,
640    /// When true, the engine drops every edit before it touches the
641    /// buffer — undo, dirty flag, and change log all stay clean.
642    /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
643    pub readonly: bool,
644    /// When `true`, pressing Enter in insert mode copies the leading
645    /// whitespace of the current line onto the new line. Matches vim's
646    /// `:set autoindent`. Default `true` (vim parity).
647    pub autoindent: bool,
648    /// When `true`, bumps indent by one `shiftwidth` after a line ending
649    /// in `{` / `(` / `[`, and strips one indent unit when the user types
650    /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
651    /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
652    pub smartindent: bool,
653    /// Cap on undo-stack length. Older entries are pruned past this
654    /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
655    /// Default `1000`.
656    pub undo_levels: u32,
657    /// When `true`, cursor motions inside insert mode break the
658    /// current undo group (so a single `u` only reverses the run of
659    /// keystrokes that preceded the motion). Default `true`.
660    /// Currently a no-op — engine doesn't yet break the undo group
661    /// on insert-mode motions; field is wired through `:set
662    /// undobreak` for forward compatibility.
663    pub undo_break_on_motion: bool,
664    /// Vim-flavoured "what counts as a word" character class.
665    /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
666    /// `_`, `48-57` = decimal char range, bare integer = single char
667    /// code, single ASCII punctuation = literal. Default
668    /// `"@,48-57,_,192-255"` matches vim.
669    pub iskeyword: String,
670    /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
671    /// pauses longer than this between keys, any pending prefix is
672    /// abandoned and the next key starts a fresh sequence. Matches
673    /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
674    pub timeout_len: core::time::Duration,
675    /// When true, render absolute line numbers in the gutter. Matches
676    /// vim's `:set number` / `:set nu`. Default `true`.
677    pub number: bool,
678    /// When true, render line numbers as offsets from the cursor row.
679    /// Combined with `number`, the cursor row shows its absolute number
680    /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
681    /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
682    pub relativenumber: bool,
683    /// Minimum gutter width in cells for the line-number column.
684    /// Width grows past this to fit the largest displayed number.
685    /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
686    /// Range 1..=20.
687    pub numberwidth: usize,
688    /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
689    /// Default `false`.
690    pub cursorline: bool,
691    /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
692    /// Default `false`.
693    pub cursorcolumn: bool,
694    /// Sign-column display mode. Matches vim's `:set signcolumn`.
695    /// Default [`crate::types::SignColumnMode::Auto`].
696    pub signcolumn: crate::types::SignColumnMode,
697    /// Number of cells reserved for a fold-marker gutter.
698    /// Matches vim's `:set foldcolumn`. Default `0`.
699    pub foldcolumn: u32,
700    /// Comma-separated 1-based column indices for vertical rulers.
701    /// Matches vim's `:set colorcolumn`. Default `""`.
702    pub colorcolumn: String,
703}
704
705impl Default for Settings {
706    fn default() -> Self {
707        Self {
708            shiftwidth: 4,
709            tabstop: 4,
710            softtabstop: 4,
711            ignore_case: false,
712            smartcase: false,
713            wrapscan: true,
714            textwidth: 79,
715            expandtab: true,
716            wrap: hjkl_buffer::Wrap::None,
717            readonly: false,
718            autoindent: true,
719            smartindent: true,
720            undo_levels: 1000,
721            undo_break_on_motion: true,
722            iskeyword: "@,48-57,_,192-255".to_string(),
723            timeout_len: core::time::Duration::from_millis(1000),
724            number: true,
725            relativenumber: false,
726            numberwidth: 4,
727            cursorline: false,
728            cursorcolumn: false,
729            signcolumn: crate::types::SignColumnMode::Auto,
730            foldcolumn: 0,
731            colorcolumn: String::new(),
732        }
733    }
734}
735
736/// Translate a SPEC [`crate::types::Options`] into the engine's
737/// internal [`Settings`] representation. Field-by-field map; the
738/// shapes are isomorphic except for type widths
739/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
740/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
741/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
742/// is the canonical entry point.
743fn settings_from_options(o: &crate::types::Options) -> Settings {
744    Settings {
745        shiftwidth: o.shiftwidth as usize,
746        tabstop: o.tabstop as usize,
747        softtabstop: o.softtabstop as usize,
748        ignore_case: o.ignorecase,
749        smartcase: o.smartcase,
750        wrapscan: o.wrapscan,
751        textwidth: o.textwidth as usize,
752        expandtab: o.expandtab,
753        wrap: match o.wrap {
754            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
755            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
756            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
757        },
758        readonly: o.readonly,
759        autoindent: o.autoindent,
760        smartindent: o.smartindent,
761        undo_levels: o.undo_levels,
762        undo_break_on_motion: o.undo_break_on_motion,
763        iskeyword: o.iskeyword.clone(),
764        timeout_len: o.timeout_len,
765        number: o.number,
766        relativenumber: o.relativenumber,
767        numberwidth: o.numberwidth,
768        cursorline: o.cursorline,
769        cursorcolumn: o.cursorcolumn,
770        signcolumn: o.signcolumn,
771        foldcolumn: o.foldcolumn,
772        colorcolumn: o.colorcolumn.clone(),
773    }
774}
775
776/// Host-observable LSP requests triggered by editor bindings. The
777/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
778/// intent that the TUI layer picks up and routes to `sqls`.
779#[derive(Debug, Clone, Copy, PartialEq, Eq)]
780pub enum LspIntent {
781    /// `gd` — textDocument/definition at the cursor.
782    GotoDefinition,
783}
784
785impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
786    /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
787    ///
788    /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
789    /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
790    /// `with_host` / `with_options` triad — there is no shim.
791    ///
792    /// Consumers that don't need a custom host pass
793    /// [`crate::types::DefaultHost::new()`]; consumers that don't need
794    /// custom options pass [`crate::types::Options::default()`].
795    pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
796        let settings = settings_from_options(&options);
797        Self {
798            keybinding_mode: KeybindingMode::Vim,
799            last_yank: None,
800            vim: VimState::default(),
801            undo_stack: Vec::new(),
802            redo_stack: Vec::new(),
803            content_dirty: false,
804            cached_content: None,
805            viewport_height: AtomicU16::new(0),
806            pending_lsp: None,
807            pending_fold_ops: Vec::new(),
808            buffer,
809            style_table: Vec::new(),
810            registers: crate::registers::Registers::default(),
811            styled_spans: Vec::new(),
812            settings,
813            marks: std::collections::BTreeMap::new(),
814            syntax_fold_ranges: Vec::new(),
815            change_log: Vec::new(),
816            sticky_col: None,
817            host,
818            last_emitted_mode: crate::VimMode::Normal,
819            search_state: crate::search::SearchState::new(),
820            buffer_spans: Vec::new(),
821            pending_content_edits: Vec::new(),
822            pending_content_reset: false,
823            last_indent_range: None,
824        }
825    }
826}
827
828impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
829    /// Borrow the buffer (typed `&B`). Host renders through this via
830    /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
831    pub fn buffer(&self) -> &B {
832        &self.buffer
833    }
834
835    /// Mutably borrow the buffer (typed `&mut B`).
836    pub fn buffer_mut(&mut self) -> &mut B {
837        &mut self.buffer
838    }
839
840    /// Borrow the host adapter directly (typed `&H`).
841    pub fn host(&self) -> &H {
842        &self.host
843    }
844
845    /// Mutably borrow the host adapter (typed `&mut H`).
846    pub fn host_mut(&mut self) -> &mut H {
847        &mut self.host
848    }
849}
850
851impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
852    /// Update the active `iskeyword` spec for word motions
853    /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
854    /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
855    /// single owner now. Equivalent to assigning
856    /// `settings_mut().iskeyword` directly; the dedicated setter is
857    /// retained for source-compatibility with 0.0.27 callers.
858    pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
859        self.settings.iskeyword = spec.into();
860    }
861
862    /// Emit `Host::emit_cursor_shape` if the public mode has changed
863    /// since the last emit. Engine calls this at the end of every input
864    /// step so mode transitions surface to the host without sprinkling
865    /// the call across every `vim.mode = ...` site.
866    pub fn emit_cursor_shape_if_changed(&mut self) {
867        let mode = self.vim_mode();
868        if mode == self.last_emitted_mode {
869            return;
870        }
871        let shape = match mode {
872            crate::VimMode::Insert => crate::types::CursorShape::Bar,
873            _ => crate::types::CursorShape::Block,
874        };
875        self.host.emit_cursor_shape(shape);
876        self.last_emitted_mode = mode;
877    }
878
879    /// Record a yank/cut payload. Writes both the legacy
880    /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
881    /// hosts) and the new [`crate::types::Host::write_clipboard`]
882    /// side-channel (Patch B). Consumers should migrate to a `Host`
883    /// impl whose `write_clipboard` queues the platform-clipboard
884    /// write; the `last_yank` mirror will be removed at 0.1.0.
885    pub(crate) fn record_yank_to_host(&mut self, text: String) {
886        self.host.write_clipboard(text.clone());
887        self.last_yank = Some(text);
888    }
889
890    /// Vim's sticky column (curswant). `None` before the first motion;
891    /// hosts shouldn't normally need to read this directly — it's
892    /// surfaced for migration off `Buffer::sticky_col` and for
893    /// snapshot tests.
894    pub fn sticky_col(&self) -> Option<usize> {
895        self.sticky_col
896    }
897
898    /// Replace the sticky column. Hosts should rarely touch this —
899    /// motion code maintains it through the standard horizontal /
900    /// vertical motion paths.
901    pub fn set_sticky_col(&mut self, col: Option<usize>) {
902        self.sticky_col = col;
903    }
904
905    /// Host hook: replace the cached syntax-derived block ranges that
906    /// `:foldsyntax` consumes. the host calls this on every re-parse;
907    /// the cost is just a `Vec` swap.
908    /// Look up a named mark by character. Returns `(row, col)` if
909    /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
910    /// uppercase (`'A`–`'Z`) marks live in the same unified
911    /// [`Editor::marks`] map as of 0.0.36.
912    pub fn mark(&self, c: char) -> Option<(usize, usize)> {
913        self.marks.get(&c).copied()
914    }
915
916    /// Set the named mark `c` to `(row, col)`. Used by the FSM's
917    /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
918    pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
919        self.marks.insert(c, pos);
920    }
921
922    /// Remove the named mark `c` (no-op if unset).
923    pub fn clear_mark(&mut self, c: char) {
924        self.marks.remove(&c);
925    }
926
927    /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
928    /// thin wrapper over [`Editor::mark`] for source compatibility
929    /// with pre-0.0.36 callers; new code should call
930    /// [`Editor::mark`] directly.
931    #[deprecated(
932        since = "0.0.36",
933        note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
934    )]
935    pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
936        self.mark(c)
937    }
938
939    /// Discard the most recent undo entry. Used by ex commands that
940    /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
941    /// matching nothing — popping prevents a no-op undo step from
942    /// polluting the user's history.
943    ///
944    /// Returns `true` if an entry was discarded.
945    pub fn pop_last_undo(&mut self) -> bool {
946        self.undo_stack.pop().is_some()
947    }
948
949    /// Read all named marks set this session — both lowercase
950    /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
951    /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
952    /// output is stable.
953    pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
954        self.marks.iter().map(|(c, p)| (*c, *p))
955    }
956
957    /// Read all buffer-local lowercase marks. Kept for source
958    /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
959    /// command); new code should use [`Editor::marks`] which
960    /// iterates the unified map.
961    #[deprecated(
962        since = "0.0.36",
963        note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
964    )]
965    pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
966        self.marks
967            .iter()
968            .filter(|(c, _)| c.is_ascii_lowercase())
969            .map(|(c, p)| (*c, *p))
970    }
971
972    /// Position the cursor was at when the user last jumped via
973    /// `<C-o>` / `g;` / similar. `None` before any jump.
974    pub fn last_jump_back(&self) -> Option<(usize, usize)> {
975        self.vim.jump_back.last().copied()
976    }
977
978    /// Position of the last edit (where `.` would replay). `None` if
979    /// no edit has happened yet in this session.
980    pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
981        self.vim.last_edit_pos
982    }
983
984    /// Read-only view of the file-marks table — uppercase / "file"
985    /// marks (`'A`–`'Z`) the host has set this session. Returns an
986    /// iterator of `(mark_char, (row, col))` pairs.
987    ///
988    /// Mutate via the FSM (`m{A-Z}` keystroke) or via
989    /// [`Editor::restore_snapshot`].
990    ///
991    /// 0.0.36: file marks now live in the unified [`Editor::marks`]
992    /// map; this accessor is kept for source compatibility and
993    /// filters the unified map to uppercase entries.
994    pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
995        self.marks
996            .iter()
997            .filter(|(c, _)| c.is_ascii_uppercase())
998            .map(|(c, p)| (*c, *p))
999    }
1000
1001    /// Read-only view of the cached syntax-derived block ranges that
1002    /// `:foldsyntax` consumes. Returns the slice the host last
1003    /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1004    /// no syntax integration is active.
1005    pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1006        &self.syntax_fold_ranges
1007    }
1008
1009    pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1010        self.syntax_fold_ranges = ranges;
1011    }
1012
1013    /// Live settings (read-only). `:set` mutates these via
1014    /// [`Editor::settings_mut`].
1015    pub fn settings(&self) -> &Settings {
1016        &self.settings
1017    }
1018
1019    /// Live settings (mutable). `:set` flows through here to mutate
1020    /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1021    /// configuring at startup typically construct a [`Settings`]
1022    /// snapshot and overwrite via `*editor.settings_mut() = …`.
1023    pub fn settings_mut(&mut self) -> &mut Settings {
1024        &mut self.settings
1025    }
1026
1027    /// Returns `true` when `:set readonly` is active. Convenience
1028    /// accessor for hosts that cannot import the internal [`Settings`]
1029    /// type. Phase 5 binary uses this to gate `:w` writes.
1030    pub fn is_readonly(&self) -> bool {
1031        self.settings.readonly
1032    }
1033
1034    /// Borrow the engine search state. Hosts inspecting the
1035    /// committed `/` / `?` pattern (e.g. for status-line display) or
1036    /// feeding the active regex into `BufferView::search_pattern`
1037    /// read it from here.
1038    pub fn search_state(&self) -> &crate::search::SearchState {
1039        &self.search_state
1040    }
1041
1042    /// Mutable engine search state. Hosts driving search
1043    /// programmatically (test fixtures, scripted demos) write the
1044    /// pattern through here.
1045    pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1046        &mut self.search_state
1047    }
1048
1049    /// Install `pattern` as the active search regex on the engine
1050    /// state and clear the cached row matches. Pass `None` to clear.
1051    /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1052    /// — `BufferView` now takes the regex through its `search_pattern`
1053    /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1054    pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1055        self.search_state.set_pattern(pattern);
1056    }
1057
1058    /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1059    /// to the next match of `search_state.pattern` from the cursor's
1060    /// current position. Returns `true` when a match was found.
1061    /// `skip_current = true` excludes a match the cursor sits on.
1062    pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1063        crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1064    }
1065
1066    /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1067    pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1068        crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1069    }
1070
1071    /// Snapshot of the unnamed register (the default `p` / `P` source).
1072    pub fn yank(&self) -> &str {
1073        &self.registers.unnamed.text
1074    }
1075
1076    /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1077    pub fn registers(&self) -> &crate::registers::Registers {
1078        &self.registers
1079    }
1080
1081    /// Mutably borrow the full register bank. Hosts that share registers
1082    /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1083    /// the slots here on buffer switch.
1084    pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1085        &mut self.registers
1086    }
1087
1088    /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1089    /// register slot. the host calls this before letting vim consume a
1090    /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1091    /// stale snapshot from the last yank.
1092    pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1093        self.registers.set_clipboard(text, linewise);
1094    }
1095
1096    /// Return the user's pending register selection (set via `"<reg>` chord
1097    /// before an operator). `None` if no register was selected — caller should
1098    /// use the unnamed register `"`.
1099    ///
1100    /// Read-only — does not consume / clear the pending selection. The
1101    /// register is cleared by the engine after the next operator fires.
1102    ///
1103    /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1104    /// honor `"a` + visual op chord sequences.
1105    pub fn pending_register(&self) -> Option<char> {
1106        self.vim.pending_register
1107    }
1108
1109    /// True when the user's pending register selector is `+` or `*`.
1110    /// the host peeks this so it can refresh `sync_clipboard_register`
1111    /// only when a clipboard read is actually about to happen.
1112    pub fn pending_register_is_clipboard(&self) -> bool {
1113        matches!(self.vim.pending_register, Some('+') | Some('*'))
1114    }
1115
1116    /// Register currently being recorded into via `q{reg}`. `None` when
1117    /// no recording is active. Hosts use this to surface a "recording @r"
1118    /// indicator in the status line.
1119    pub fn recording_register(&self) -> Option<char> {
1120        self.vim.recording_macro
1121    }
1122
1123    /// Pending repeat count the user has typed but not yet resolved
1124    /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1125    /// Hosts surface this in a "showcmd" area.
1126    pub fn pending_count(&self) -> Option<u32> {
1127        self.vim.pending_count_val()
1128    }
1129
1130    /// The operator character for any in-flight operator that is waiting
1131    /// for a motion (e.g. `d` after the user types `d` but before a
1132    /// motion). Returns `None` when no operator is pending.
1133    pub fn pending_op(&self) -> Option<char> {
1134        self.vim.pending_op_char()
1135    }
1136
1137    /// `true` when the engine is in any pending chord state — waiting for
1138    /// the next key to complete a command (e.g. `r<char>` replace,
1139    /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1140    /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1141    /// register selection `"<reg>`, macro recording target, etc).
1142    ///
1143    /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1144    /// and forward keys directly to the engine so in-flight commands can
1145    /// complete without the host eating their continuation keys.
1146    pub fn is_chord_pending(&self) -> bool {
1147        self.vim.is_chord_pending()
1148    }
1149
1150    /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1151    /// is waiting for the next typed character to name the register to paste.
1152    /// The dispatcher should call `insert_paste_register(c)` instead of
1153    /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1154    ///
1155    /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1156    /// without having to drive the full FSM.
1157    pub fn is_insert_register_pending(&self) -> bool {
1158        self.vim.insert_pending_register
1159    }
1160
1161    /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1162    /// before `insert_paste_register(c)` in app-level dispatchers so that the
1163    /// flag does not persist into the next key. Call before
1164    /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1165    ///
1166    /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1167    pub fn clear_insert_register_pending(&mut self) {
1168        self.vim.insert_pending_register = false;
1169    }
1170
1171    /// Read-only view of the jump-back list (positions pushed on "big"
1172    /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1173    #[allow(clippy::type_complexity)]
1174    pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1175        (&self.vim.jump_back, &self.vim.jump_fwd)
1176    }
1177
1178    /// Read-only view of the change list (positions of recent edits) plus
1179    /// the current walk cursor. Newest entry is at the back.
1180    pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1181        (&self.vim.change_list, self.vim.change_list_cursor)
1182    }
1183
1184    /// Replace the unnamed register without touching any other slot.
1185    /// For host-driven imports (e.g. system clipboard); operator
1186    /// code uses [`record_yank`] / [`record_delete`].
1187    pub fn set_yank(&mut self, text: impl Into<String>) {
1188        let text = text.into();
1189        let linewise = self.vim.yank_linewise;
1190        self.registers.unnamed = crate::registers::Slot { text, linewise };
1191    }
1192
1193    /// Record a yank into `"` and `"0`, plus the named target if the
1194    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1195    /// paste path.
1196    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1197        self.vim.yank_linewise = linewise;
1198        let target = self.vim.pending_register.take();
1199        self.registers.record_yank(text, linewise, target);
1200    }
1201
1202    /// Direct write to a named register slot — bypasses the unnamed
1203    /// `"` and `"0` updates that `record_yank` does. Used by the
1204    /// macro recorder so finishing a `q{reg}` recording doesn't
1205    /// pollute the user's last yank.
1206    pub(crate) fn set_named_register_text(&mut self, reg: char, text: String) {
1207        if let Some(slot) = match reg {
1208            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1209            'A'..='Z' => {
1210                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1211            }
1212            _ => None,
1213        } {
1214            slot.text = text;
1215            slot.linewise = false;
1216        }
1217    }
1218
1219    /// Record a delete / change into `"` and the `"1`–`"9` ring.
1220    /// Honours the active named-register prefix.
1221    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1222        self.vim.yank_linewise = linewise;
1223        let target = self.vim.pending_register.take();
1224        self.registers.record_delete(text, linewise, target);
1225    }
1226
1227    /// Install styled syntax spans using the engine-native
1228    /// [`crate::types::Style`]. Always available — engine is ratatui-free.
1229    /// Ratatui hosts use
1230    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`
1231    /// which converts at the boundary and delegates here.
1232    ///
1233    /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1234    /// 0.1.0 freeze the unprefixed name is the universally-available
1235    /// engine-native variant.
1236    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1237        let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1238            .map(|r| buf_line(&self.buffer, r).map(|s| s.len()).unwrap_or(0))
1239            .collect();
1240        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1241        let mut engine_spans: Vec<Vec<(usize, usize, crate::types::Style)>> =
1242            Vec::with_capacity(spans.len());
1243        for (row, row_spans) in spans.iter().enumerate() {
1244            let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1245            let mut translated = Vec::with_capacity(row_spans.len());
1246            let mut translated_e = Vec::with_capacity(row_spans.len());
1247            for (start, end, style) in row_spans {
1248                let end_clamped = (*end).min(line_len);
1249                if end_clamped <= *start {
1250                    continue;
1251                }
1252                let id = self.intern_style(*style);
1253                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1254                translated_e.push((*start, end_clamped, *style));
1255            }
1256            by_row.push(translated);
1257            engine_spans.push(translated_e);
1258        }
1259        self.buffer_spans = by_row;
1260        self.styled_spans = engine_spans;
1261    }
1262
1263    /// Read-only view of the style table in engine-native form —
1264    /// id `i` → `style_table[i]`. Always available, no cfg gate.
1265    ///
1266    /// Ratatui hosts that need a `ratatui::style::Style` slice should
1267    /// use `hjkl_engine_tui::EditorRatatuiExt::ratatui_style_table` or
1268    /// convert individual entries via `hjkl_engine_tui::style_to_ratatui`.
1269    pub fn style_table(&self) -> &[crate::types::Style] {
1270        &self.style_table
1271    }
1272
1273    /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1274    /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1275    /// per draw frame.
1276    ///
1277    /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1278    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1279    /// caches spans; they live on the engine and route through the
1280    /// `Host::syntax_highlights` pipeline.
1281    pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1282        &self.buffer_spans
1283    }
1284
1285    /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1286    /// Engine-native — the unified `style_table` is always engine-native.
1287    /// Linear-scan dedup — the table grows only as new tree-sitter token
1288    /// kinds appear, so it stays tiny. Ratatui callers use
1289    /// `hjkl_engine_tui::EditorRatatuiExt::intern_ratatui_style` which
1290    /// converts at the boundary and delegates here.
1291    ///
1292    /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1293    /// the unprefixed name is the universally-available engine-native
1294    /// variant.
1295    pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1296        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1297            return idx as u32;
1298        }
1299        self.style_table.push(style);
1300        (self.style_table.len() - 1) as u32
1301    }
1302
1303    /// Look up an interned style by id and return it as a SPEC
1304    /// [`crate::types::Style`]. Returns `None` for ids past the end
1305    /// of the table.
1306    pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1307        self.style_table.get(id as usize).copied()
1308    }
1309
1310    /// Historical reverse-sync hook from when the textarea mirrored
1311    /// the buffer. Now that Buffer is the cursor authority this is a
1312    /// no-op; call sites can remain in place during the migration.
1313    pub fn push_buffer_cursor_to_textarea(&mut self) {}
1314
1315    /// Force the host viewport's top row without touching the
1316    /// cursor. Used by tests that simulate a scroll without the
1317    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1318    /// apply.
1319    ///
1320    /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1321    /// instead of the (now-deleted) `Buffer::viewport_mut`.
1322    pub fn set_viewport_top(&mut self, row: usize) {
1323        let last = buf_row_count(&self.buffer).saturating_sub(1);
1324        let target = row.min(last);
1325        self.host.viewport_mut().top_row = target;
1326    }
1327
1328    /// Set the cursor to `(row, col)`, clamped to the buffer's
1329    /// content. Hosts use this for goto-line, jump-to-mark, and
1330    /// programmatic cursor placement.
1331    ///
1332    /// Resets `sticky_col` (curswant) to `col` — every explicit jump
1333    /// (goto-line, jump-to-mark, search hit, click, `]d`) follows vim
1334    /// semantics. Only `j`/`k`/`+`/`-` READ `sticky_col`; everything
1335    /// else resets it to the column where the cursor actually landed.
1336    pub fn jump_cursor(&mut self, row: usize, col: usize) {
1337        buf_set_cursor_rc(&mut self.buffer, row, col);
1338        self.sticky_col = Some(col);
1339    }
1340
1341    /// Set the cursor to `(row, col)` without modifying `sticky_col`.
1342    ///
1343    /// Use this for host-side state restores (viewport sync, snapshot
1344    /// replay) where the cursor was already at this position semantically
1345    /// and the host's sticky tracking should remain authoritative.
1346    ///
1347    /// For user-facing jumps (goto-line, search hit, picker `<CR>`, `]d`,
1348    /// click), use [`Editor::jump_cursor`] which DOES reset `sticky_col`
1349    /// per vim curswant semantics.
1350    pub fn set_cursor_quiet(&mut self, row: usize, col: usize) {
1351        buf_set_cursor_rc(&mut self.buffer, row, col);
1352    }
1353
1354    /// `(row, col)` cursor read sourced from the migration buffer.
1355    /// Equivalent to `self.textarea.cursor()` when the two are in
1356    /// sync — which is the steady state during Phase 7f because
1357    /// every step opens with `sync_buffer_content_from_textarea` and
1358    /// every ported motion pushes the result back. Prefer this over
1359    /// `self.textarea.cursor()` so call sites keep working unchanged
1360    /// once the textarea field is ripped.
1361    pub fn cursor(&self) -> (usize, usize) {
1362        buf_cursor_rc(&self.buffer)
1363    }
1364
1365    /// Drain any pending LSP intent raised by the last key. Returns
1366    /// `None` when no intent is armed.
1367    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1368        self.pending_lsp.take()
1369    }
1370
1371    /// Drain every [`crate::types::FoldOp`] raised since the last
1372    /// call. Hosts that mirror the engine's fold storage (or that
1373    /// project folds onto a separate fold tree, LSP folding ranges,
1374    /// …) drain this each step and dispatch as their own
1375    /// [`crate::types::Host::Intent`] requires.
1376    ///
1377    /// The engine has already applied every op locally against the
1378    /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1379    /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1380    /// don't track folds independently can ignore the queue
1381    /// (or simply never call this drain).
1382    ///
1383    /// Introduced in 0.0.38 (Patch C-δ.4).
1384    pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1385        std::mem::take(&mut self.pending_fold_ops)
1386    }
1387
1388    /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1389    /// surface: queue it for host observation (drained by
1390    /// [`Editor::take_fold_ops`]) and apply it locally against the
1391    /// in-tree buffer fold storage via
1392    /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1393    /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1394    /// invalidation) route every fold mutation through this method.
1395    ///
1396    /// Introduced in 0.0.38 (Patch C-δ.4).
1397    pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1398        use crate::types::FoldProvider;
1399        self.pending_fold_ops.push(op);
1400        let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1401        provider.apply(op);
1402    }
1403
1404    /// Refresh the host viewport's height from the cached
1405    /// `viewport_height_value()`. Called from the per-step
1406    /// boilerplate; was the textarea → buffer mirror before Phase 7f
1407    /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1408    /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1409    /// `Host::viewport_mut`.
1410    pub(crate) fn sync_buffer_from_textarea(&mut self) {
1411        let height = self.viewport_height_value();
1412        self.host.viewport_mut().height = height;
1413    }
1414
1415    /// Was the full textarea → buffer content sync. Buffer is the
1416    /// content authority now; this remains as a no-op so the per-step
1417    /// call sites don't have to be ripped in the same patch.
1418    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1419        self.sync_buffer_from_textarea();
1420    }
1421
1422    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1423    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1424    /// the cursor without going through the vim engine's motion
1425    /// machinery, where push_jump fires automatically.
1426    pub fn record_jump(&mut self, pos: (usize, usize)) {
1427        const JUMPLIST_MAX: usize = 100;
1428        self.vim.jump_back.push(pos);
1429        if self.vim.jump_back.len() > JUMPLIST_MAX {
1430            self.vim.jump_back.remove(0);
1431        }
1432        self.vim.jump_fwd.clear();
1433    }
1434
1435    /// Host apps call this each draw with the current text area height so
1436    /// scroll helpers can clamp the cursor without recomputing layout.
1437    pub fn set_viewport_height(&self, height: u16) {
1438        self.viewport_height.store(height, Ordering::Relaxed);
1439    }
1440
1441    /// Last height published by `set_viewport_height` (in rows).
1442    pub fn viewport_height_value(&self) -> u16 {
1443        self.viewport_height.load(Ordering::Relaxed)
1444    }
1445
1446    /// Apply `edit` against the buffer and return the inverse so the
1447    /// host can push it onto an undo stack. Side effects: dirty
1448    /// flag, change-list ring, mark / jump-list shifts, change_log
1449    /// append, fold invalidation around the touched rows.
1450    ///
1451    /// The primary edit funnel — both FSM operators and ex commands
1452    /// route mutations through here so the side effects fire
1453    /// uniformly.
1454    pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1455        // `:set readonly` short-circuits every mutation funnel: no
1456        // buffer change, no dirty flag, no undo entry, no change-log
1457        // emission. We swallow the requested `edit` and hand back a
1458        // self-inverse no-op (`InsertStr` of an empty string at the
1459        // current cursor) so callers that push the return value onto
1460        // an undo stack still get a structurally valid round trip.
1461        if self.settings.readonly {
1462            let _ = edit;
1463            return hjkl_buffer::Edit::InsertStr {
1464                at: buf_cursor_pos(&self.buffer),
1465                text: String::new(),
1466            };
1467        }
1468        let pre_row = buf_cursor_row(&self.buffer);
1469        let pre_rows = buf_row_count(&self.buffer);
1470        // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1471        // Vim's `:h '.` says "the position where the last change was made",
1472        // meaning the change-start, not the post-insert cursor. We snap it
1473        // here before `apply_buffer_edit` moves the cursor.
1474        let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1475        // Map the underlying buffer edit to a SPEC EditOp for
1476        // change-log emission before consuming it. Coarse — see
1477        // change_log field doc on the struct.
1478        self.change_log.extend(edit_to_editops(&edit));
1479        // Compute ContentEdit fan-out from the pre-edit buffer state.
1480        // Done before `apply_buffer_edit` consumes `edit` so we can
1481        // inspect the operation's fields and the buffer's pre-edit row
1482        // bytes (needed for byte_of_row / col_byte conversion). Edits
1483        // are pushed onto `pending_content_edits` for host drain.
1484        let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1485        self.pending_content_edits.extend(content_edits);
1486        // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1487        // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1488        // the 0.0.42 plan — see that fn's doc comment). The free fn
1489        // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1490        // no longer carries a `self.buffer.<inherent>` hop.
1491        let inverse = apply_buffer_edit(&mut self.buffer, edit);
1492        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1493        // Drop any folds the edit's range overlapped — vim opens the
1494        // surrounding fold automatically when you edit inside it. The
1495        // approximation here invalidates folds covering either the
1496        // pre-edit cursor row or the post-edit cursor row, which
1497        // catches the common single-line / multi-line edit shapes.
1498        let lo = pre_row.min(pos_row);
1499        let hi = pre_row.max(pos_row);
1500        self.apply_fold_op(crate::types::FoldOp::Invalidate {
1501            start_row: lo,
1502            end_row: hi,
1503        });
1504        // Dot mark records the PRE-edit position (change start), matching
1505        // vim's `:h '.` semantics. Previously this stored the post-edit
1506        // cursor, which diverged from nvim on `iX<Esc>j`.
1507        self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1508        // Append to the change-list ring (skip when the cursor sits on
1509        // the same cell as the last entry — back-to-back keystrokes on
1510        // one column shouldn't pollute the ring). A new edit while
1511        // walking the ring trims the forward half, vim style.
1512        let entry = (pos_row, pos_col);
1513        if self.vim.change_list.last() != Some(&entry) {
1514            if let Some(idx) = self.vim.change_list_cursor.take() {
1515                self.vim.change_list.truncate(idx + 1);
1516            }
1517            self.vim.change_list.push(entry);
1518            let len = self.vim.change_list.len();
1519            if len > crate::vim::CHANGE_LIST_MAX {
1520                self.vim
1521                    .change_list
1522                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1523            }
1524        }
1525        self.vim.change_list_cursor = None;
1526        // Shift / drop marks + jump-list entries to track the row
1527        // delta the edit produced. Without this, every line-changing
1528        // edit silently invalidates `'a`-style positions.
1529        let post_rows = buf_row_count(&self.buffer);
1530        let delta = post_rows as isize - pre_rows as isize;
1531        if delta != 0 {
1532            self.shift_marks_after_edit(pre_row, delta);
1533        }
1534        self.push_buffer_content_to_textarea();
1535        self.mark_content_dirty();
1536        inverse
1537    }
1538
1539    /// Migrate user marks + jumplist entries when an edit at row
1540    /// `edit_start` changes the buffer's row count by `delta` (positive
1541    /// for inserts, negative for deletes). Marks tied to a deleted row
1542    /// are dropped; marks past the affected band shift by `delta`.
1543    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1544        if delta == 0 {
1545            return;
1546        }
1547        // Deleted-row band (only meaningful for delta < 0). Inclusive
1548        // start, exclusive end.
1549        let drop_end = if delta < 0 {
1550            edit_start.saturating_add((-delta) as usize)
1551        } else {
1552            edit_start
1553        };
1554        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1555
1556        // 0.0.36: lowercase + uppercase marks share the unified
1557        // `marks` map; one pass migrates both.
1558        let mut to_drop: Vec<char> = Vec::new();
1559        for (c, (row, _col)) in self.marks.iter_mut() {
1560            if (edit_start..drop_end).contains(row) {
1561                to_drop.push(*c);
1562            } else if *row >= shift_threshold {
1563                *row = ((*row as isize) + delta).max(0) as usize;
1564            }
1565        }
1566        for c in to_drop {
1567            self.marks.remove(&c);
1568        }
1569
1570        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1571            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1572            for (row, _) in entries.iter_mut() {
1573                if *row >= shift_threshold {
1574                    *row = ((*row as isize) + delta).max(0) as usize;
1575                }
1576            }
1577        };
1578        shift_jumps(&mut self.vim.jump_back);
1579        shift_jumps(&mut self.vim.jump_fwd);
1580    }
1581
1582    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1583    /// the textarea from the buffer's lines + cursor, preserving yank
1584    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1585    /// textarea field disappears at the end of Phase 7f anyway.
1586    /// No-op since Buffer is the content authority. Retained as a
1587    /// shim so call sites in `mutate_edit` and friends don't have to
1588    /// be ripped in lockstep with the field removal.
1589    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1590
1591    /// Single choke-point for "the buffer just changed". Sets the
1592    /// dirty flag and drops the cached `content_arc` snapshot so
1593    /// subsequent reads rebuild from the live textarea. Callers
1594    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1595    /// path) must invoke this to keep the cache honest.
1596    pub fn mark_content_dirty(&mut self) {
1597        self.content_dirty = true;
1598        self.cached_content = None;
1599    }
1600
1601    /// Returns true if content changed since the last call, then clears the flag.
1602    pub fn take_dirty(&mut self) -> bool {
1603        let dirty = self.content_dirty;
1604        self.content_dirty = false;
1605        dirty
1606    }
1607
1608    /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1609    /// the last call. Each entry corresponds to a single buffer
1610    /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1611    /// fan out to one entry per row touched.
1612    ///
1613    /// Hosts call this each frame (after [`Editor::take_content_reset`])
1614    /// to fan edits into a tree-sitter parser via `Tree::edit`.
1615    pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1616        std::mem::take(&mut self.pending_content_edits)
1617    }
1618
1619    /// Returns `true` if a bulk buffer replacement happened since the
1620    /// last call (e.g. `set_content` / `restore` / undo restore), then
1621    /// clears the flag. When this returns `true`, hosts should drop
1622    /// any retained syntax tree before consuming
1623    /// [`Editor::take_content_edits`].
1624    pub fn take_content_reset(&mut self) -> bool {
1625        let r = self.pending_content_reset;
1626        self.pending_content_reset = false;
1627        r
1628    }
1629
1630    /// Pull-model coarse change observation. If content changed since
1631    /// the last call, returns `Some(Arc<String>)` with the new content
1632    /// and clears the dirty flag; otherwise returns `None`.
1633    ///
1634    /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1635    /// the character level) should diff against their own previous
1636    /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1637    /// once every edit path inside the engine is instrumented; this
1638    /// coarse form covers the pull-model use case in the meantime.
1639    pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1640        if !self.content_dirty {
1641            return None;
1642        }
1643        let arc = self.content_arc();
1644        self.content_dirty = false;
1645        Some(arc)
1646    }
1647
1648    /// Width in cells of the line-number gutter for the current buffer
1649    /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
1650    /// in front of the text column. Returns `0` when both `number` and
1651    /// `relativenumber` are off.
1652    pub fn lnum_width(&self) -> u16 {
1653        if self.settings.number || self.settings.relativenumber {
1654            let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1655            needed.max(self.settings.numberwidth) as u16
1656        } else {
1657            0
1658        }
1659    }
1660
1661    /// Returns the cursor's row within the visible textarea (0-based), updating
1662    /// the stored viewport top so subsequent calls remain accurate.
1663    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1664        let cursor = buf_cursor_row(&self.buffer);
1665        let top = self.host.viewport().top_row;
1666        cursor.saturating_sub(top).min(height as usize - 1) as u16
1667    }
1668
1669    /// Returns the cursor's screen position `(x, y)` for the textarea
1670    /// described by `(area_x, area_y, area_width, area_height)`.
1671    /// Accounts for line-number gutter, viewport scroll, and any extra
1672    /// gutter width to the left of the number column (sign column, fold
1673    /// column). Returns `None` if the cursor is outside the visible
1674    /// viewport. Always available (engine-native; no ratatui dependency).
1675    ///
1676    /// `extra_gutter_width` is added to the number-column width before
1677    /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
1678    /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
1679    /// when a dedicated sign or fold column is present.
1680    ///
1681    /// Renamed from `cursor_screen_pos_xywh` in 0.0.32.
1682    pub fn cursor_screen_pos(
1683        &self,
1684        area_x: u16,
1685        area_y: u16,
1686        area_width: u16,
1687        area_height: u16,
1688        extra_gutter_width: u16,
1689    ) -> Option<(u16, u16)> {
1690        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1691        let v = self.host.viewport();
1692        if pos_row < v.top_row || pos_col < v.top_col {
1693            return None;
1694        }
1695        let lnum_width = self.lnum_width();
1696        // Full offset from the left edge of the window to the first text cell.
1697        let gutter_total = lnum_width + extra_gutter_width;
1698        let dy = (pos_row - v.top_row) as u16;
1699        // Convert char column to visual column so cursor lands on the
1700        // correct cell when the line contains tabs (which the renderer
1701        // expands to TAB_WIDTH stops). Tab width must match the renderer.
1702        let line = self.buffer.line(pos_row).unwrap_or_default();
1703        let tab_width = if v.tab_width == 0 {
1704            4
1705        } else {
1706            v.tab_width as usize
1707        };
1708        let visual_pos = visual_col_for_char(&line, pos_col, tab_width);
1709        let visual_top = visual_col_for_char(&line, v.top_col, tab_width);
1710        let dx = (visual_pos - visual_top) as u16;
1711        if dy >= area_height || dx + gutter_total >= area_width {
1712            return None;
1713        }
1714        Some((area_x + gutter_total + dx, area_y + dy))
1715    }
1716
1717    /// Returns the current vim mode. Phase 6.3: reads from the stable
1718    /// `current_mode` field (kept in sync by both the FSM step loop and
1719    /// the Phase 6.3 primitive bridges) rather than deriving from the
1720    /// FSM-internal `mode` field via `public_mode()`.
1721    pub fn vim_mode(&self) -> VimMode {
1722        self.vim.current_mode
1723    }
1724
1725    /// Bounds of the active visual-block rectangle as
1726    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1727    /// `None` when we're not in VisualBlock mode.
1728    /// Read-only view of the live `/` or `?` prompt. `None` outside
1729    /// search-prompt mode.
1730    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1731        self.vim.search_prompt.as_ref()
1732    }
1733
1734    /// Most recent committed search pattern (persists across `n` / `N`
1735    /// and across prompt exits). `None` before the first search.
1736    pub fn last_search(&self) -> Option<&str> {
1737        self.vim.last_search.as_deref()
1738    }
1739
1740    /// Whether the last committed search was a forward `/` (`true`) or
1741    /// a backward `?` (`false`). `n` and `N` consult this to honour the
1742    /// direction the user committed.
1743    pub fn last_search_forward(&self) -> bool {
1744        self.vim.last_search_forward
1745    }
1746
1747    /// Set the most recent committed search text + direction. Used by
1748    /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1749    /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1750    /// most recent commit with the right direction. Pass `None` /
1751    /// `true` to clear.
1752    pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1753        self.vim.last_search = text;
1754        self.vim.last_search_forward = forward;
1755    }
1756
1757    /// Start/end `(row, col)` of the active char-wise Visual selection
1758    /// (inclusive on both ends, positionally ordered). `None` when not
1759    /// in Visual mode.
1760    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1761        if self.vim_mode() != VimMode::Visual {
1762            return None;
1763        }
1764        let anchor = self.vim.visual_anchor;
1765        let cursor = self.cursor();
1766        let (start, end) = if anchor <= cursor {
1767            (anchor, cursor)
1768        } else {
1769            (cursor, anchor)
1770        };
1771        Some((start, end))
1772    }
1773
1774    /// Top/bottom rows of the active VisualLine selection (inclusive).
1775    /// `None` when we're not in VisualLine mode.
1776    pub fn line_highlight(&self) -> Option<(usize, usize)> {
1777        if self.vim_mode() != VimMode::VisualLine {
1778            return None;
1779        }
1780        let anchor = self.vim.visual_line_anchor;
1781        let cursor = buf_cursor_row(&self.buffer);
1782        Some((anchor.min(cursor), anchor.max(cursor)))
1783    }
1784
1785    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
1786        if self.vim_mode() != VimMode::VisualBlock {
1787            return None;
1788        }
1789        let (ar, ac) = self.vim.block_anchor;
1790        let cr = buf_cursor_row(&self.buffer);
1791        let cc = self.vim.block_vcol;
1792        let top = ar.min(cr);
1793        let bot = ar.max(cr);
1794        let left = ac.min(cc);
1795        let right = ac.max(cc);
1796        Some((top, bot, left, right))
1797    }
1798
1799    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
1800    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
1801    /// straight to `BufferView` once render flips off textarea
1802    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
1803    /// switch).
1804    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
1805        use hjkl_buffer::{Position, Selection};
1806        match self.vim_mode() {
1807            VimMode::Visual => {
1808                let (ar, ac) = self.vim.visual_anchor;
1809                let head = buf_cursor_pos(&self.buffer);
1810                Some(Selection::Char {
1811                    anchor: Position::new(ar, ac),
1812                    head,
1813                })
1814            }
1815            VimMode::VisualLine => {
1816                let anchor_row = self.vim.visual_line_anchor;
1817                let head_row = buf_cursor_row(&self.buffer);
1818                Some(Selection::Line {
1819                    anchor_row,
1820                    head_row,
1821                })
1822            }
1823            VimMode::VisualBlock => {
1824                let (ar, ac) = self.vim.block_anchor;
1825                let cr = buf_cursor_row(&self.buffer);
1826                let cc = self.vim.block_vcol;
1827                Some(Selection::Block {
1828                    anchor: Position::new(ar, ac),
1829                    head: Position::new(cr, cc),
1830                })
1831            }
1832            _ => None,
1833        }
1834    }
1835
1836    /// Force back to normal mode (used when dismissing completions etc.)
1837    pub fn force_normal(&mut self) {
1838        self.vim.force_normal();
1839    }
1840
1841    pub fn content(&self) -> String {
1842        let n = buf_row_count(&self.buffer);
1843        let mut s = String::new();
1844        for r in 0..n {
1845            if r > 0 {
1846                s.push('\n');
1847            }
1848            s.push_str(&crate::types::Query::line(&self.buffer, r as u32));
1849        }
1850        s.push('\n');
1851        s
1852    }
1853
1854    /// Same logical output as [`content`], but returns a cached
1855    /// `Arc<String>` so back-to-back reads within an un-mutated window
1856    /// are ref-count bumps instead of multi-MB joins. The cache is
1857    /// invalidated by every [`mark_content_dirty`] call.
1858    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
1859        if let Some(arc) = &self.cached_content {
1860            return std::sync::Arc::clone(arc);
1861        }
1862        let arc = std::sync::Arc::new(self.content());
1863        self.cached_content = Some(std::sync::Arc::clone(&arc));
1864        arc
1865    }
1866
1867    pub fn set_content(&mut self, text: &str) {
1868        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1869        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1870            lines.pop();
1871        }
1872        if lines.is_empty() {
1873            lines.push(String::new());
1874        }
1875        let _ = lines;
1876        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1877        self.undo_stack.clear();
1878        self.redo_stack.clear();
1879        // Whole-buffer replace supersedes any queued ContentEdits.
1880        self.pending_content_edits.clear();
1881        self.pending_content_reset = true;
1882        self.mark_content_dirty();
1883    }
1884
1885    /// Whole-buffer replace that **preserves the undo history**.
1886    ///
1887    /// Equivalent to [`Editor::set_content`] but pushes the current buffer
1888    /// state onto the undo stack first, so a subsequent `u` walks back to
1889    /// the pre-replacement content. Use this for any operation the user
1890    /// expects to undo as a single step — e.g. external formatter output
1891    /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
1892    ///
1893    /// Like `push_undo`, this clears the redo stack (vim semantics: any
1894    /// new edit invalidates redo).
1895    pub fn set_content_undoable(&mut self, text: &str) {
1896        self.push_undo();
1897        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1898        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1899            lines.pop();
1900        }
1901        if lines.is_empty() {
1902            lines.push(String::new());
1903        }
1904        let _ = lines;
1905        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1906        // Whole-buffer replace supersedes any queued ContentEdits.
1907        self.pending_content_edits.clear();
1908        self.pending_content_reset = true;
1909        self.mark_content_dirty();
1910    }
1911
1912    /// Drain the pending change log produced by buffer mutations.
1913    ///
1914    /// Returns a `Vec<EditOp>` covering edits applied since the last
1915    /// call. Empty when no edits ran. Pull-model, complementary to
1916    /// [`Editor::take_content_change`] which gives back the new full
1917    /// content.
1918    ///
1919    /// Mapping coverage:
1920    /// - InsertChar / InsertStr → exact `EditOp` with empty range +
1921    ///   replacement.
1922    /// - DeleteRange (`Char` kind) → exact range + empty replacement.
1923    /// - Replace → exact range + new replacement.
1924    /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
1925    ///   InsertBlock, DeleteBlockChunks → best-effort placeholder
1926    ///   covering the touched range. Hosts wanting per-cell deltas
1927    ///   should diff their own `lines()` snapshot.
1928    pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
1929        std::mem::take(&mut self.change_log)
1930    }
1931
1932    /// Read the engine's current settings as a SPEC
1933    /// [`crate::types::Options`].
1934    ///
1935    /// Bridges between the legacy [`Settings`] (which carries fewer
1936    /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
1937    /// not present in `Settings` fall back to vim defaults (e.g.,
1938    /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
1939    /// Once trait extraction lands, this becomes the canonical config
1940    /// reader and `Settings` retires.
1941    pub fn current_options(&self) -> crate::types::Options {
1942        crate::types::Options {
1943            shiftwidth: self.settings.shiftwidth as u32,
1944            tabstop: self.settings.tabstop as u32,
1945            softtabstop: self.settings.softtabstop as u32,
1946            textwidth: self.settings.textwidth as u32,
1947            expandtab: self.settings.expandtab,
1948            ignorecase: self.settings.ignore_case,
1949            smartcase: self.settings.smartcase,
1950            wrapscan: self.settings.wrapscan,
1951            wrap: match self.settings.wrap {
1952                hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
1953                hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
1954                hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
1955            },
1956            readonly: self.settings.readonly,
1957            autoindent: self.settings.autoindent,
1958            smartindent: self.settings.smartindent,
1959            undo_levels: self.settings.undo_levels,
1960            undo_break_on_motion: self.settings.undo_break_on_motion,
1961            iskeyword: self.settings.iskeyword.clone(),
1962            timeout_len: self.settings.timeout_len,
1963            ..crate::types::Options::default()
1964        }
1965    }
1966
1967    /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
1968    /// Only the fields backed by today's [`Settings`] take effect;
1969    /// remaining options become live once trait extraction wires them
1970    /// through.
1971    pub fn apply_options(&mut self, opts: &crate::types::Options) {
1972        self.settings.shiftwidth = opts.shiftwidth as usize;
1973        self.settings.tabstop = opts.tabstop as usize;
1974        self.settings.softtabstop = opts.softtabstop as usize;
1975        self.settings.textwidth = opts.textwidth as usize;
1976        self.settings.expandtab = opts.expandtab;
1977        self.settings.ignore_case = opts.ignorecase;
1978        self.settings.smartcase = opts.smartcase;
1979        self.settings.wrapscan = opts.wrapscan;
1980        self.settings.wrap = match opts.wrap {
1981            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
1982            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
1983            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
1984        };
1985        self.settings.readonly = opts.readonly;
1986        self.settings.autoindent = opts.autoindent;
1987        self.settings.smartindent = opts.smartindent;
1988        self.settings.undo_levels = opts.undo_levels;
1989        self.settings.undo_break_on_motion = opts.undo_break_on_motion;
1990        self.set_iskeyword(opts.iskeyword.clone());
1991        self.settings.timeout_len = opts.timeout_len;
1992        self.settings.number = opts.number;
1993        self.settings.relativenumber = opts.relativenumber;
1994        self.settings.numberwidth = opts.numberwidth;
1995        self.settings.cursorline = opts.cursorline;
1996        self.settings.cursorcolumn = opts.cursorcolumn;
1997        self.settings.signcolumn = opts.signcolumn;
1998        self.settings.foldcolumn = opts.foldcolumn;
1999        self.settings.colorcolumn = opts.colorcolumn.clone();
2000    }
2001
2002    /// Active visual selection as a SPEC [`crate::types::Highlight`]
2003    /// with [`crate::types::HighlightKind::Selection`].
2004    ///
2005    /// Returns `None` when the editor isn't in a Visual mode.
2006    /// Visual-line and visual-block selections collapse to the
2007    /// bounding char range of the selection — the SPEC `Selection`
2008    /// kind doesn't carry sub-line info today; hosts that need full
2009    /// line / block geometry continue to read [`buffer_selection`]
2010    /// (the legacy [`hjkl_buffer::Selection`] shape).
2011    pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2012        use crate::types::{Highlight, HighlightKind, Pos};
2013        let sel = self.buffer_selection()?;
2014        let (start, end) = match sel {
2015            hjkl_buffer::Selection::Char { anchor, head } => {
2016                let a = (anchor.row, anchor.col);
2017                let h = (head.row, head.col);
2018                if a <= h { (a, h) } else { (h, a) }
2019            }
2020            hjkl_buffer::Selection::Line {
2021                anchor_row,
2022                head_row,
2023            } => {
2024                let (top, bot) = if anchor_row <= head_row {
2025                    (anchor_row, head_row)
2026                } else {
2027                    (head_row, anchor_row)
2028                };
2029                let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2030                ((top, 0), (bot, last_col))
2031            }
2032            hjkl_buffer::Selection::Block { anchor, head } => {
2033                let (top, bot) = if anchor.row <= head.row {
2034                    (anchor.row, head.row)
2035                } else {
2036                    (head.row, anchor.row)
2037                };
2038                let (left, right) = if anchor.col <= head.col {
2039                    (anchor.col, head.col)
2040                } else {
2041                    (head.col, anchor.col)
2042                };
2043                ((top, left), (bot, right))
2044            }
2045        };
2046        Some(Highlight {
2047            range: Pos {
2048                line: start.0 as u32,
2049                col: start.1 as u32,
2050            }..Pos {
2051                line: end.0 as u32,
2052                col: end.1 as u32,
2053            },
2054            kind: HighlightKind::Selection,
2055        })
2056    }
2057
2058    /// SPEC-typed highlights for `line`.
2059    ///
2060    /// Two emission modes:
2061    ///
2062    /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2063    ///   `Editor::search_prompt` is `Some`. Live-preview matches of
2064    ///   the in-flight pattern surface as
2065    ///   [`crate::types::HighlightKind::IncSearch`].
2066    /// - **SearchMatch**: the prompt has been committed (or absent)
2067    ///   and the buffer's armed pattern is non-empty. Matches surface
2068    ///   as [`crate::types::HighlightKind::SearchMatch`].
2069    ///
2070    /// Selection / MatchParen / Syntax(id) variants land once the
2071    /// trait extraction routes the FSM's selection set + the host's
2072    /// syntax pipeline through the [`crate::types::Host`] trait.
2073    ///
2074    /// Returns an empty vec when there is nothing to highlight or
2075    /// `line` is out of bounds.
2076    pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2077        use crate::types::{Highlight, HighlightKind, Pos};
2078        let row = line as usize;
2079        if row >= buf_row_count(&self.buffer) {
2080            return Vec::new();
2081        }
2082
2083        // Live preview while the prompt is open beats the committed
2084        // pattern.
2085        if let Some(prompt) = self.search_prompt() {
2086            if prompt.text.is_empty() {
2087                return Vec::new();
2088            }
2089            let Ok(re) = regex::Regex::new(&prompt.text) else {
2090                return Vec::new();
2091            };
2092            let Some(haystack) = buf_line(&self.buffer, row) else {
2093                return Vec::new();
2094            };
2095            return re
2096                .find_iter(&haystack)
2097                .map(|m| Highlight {
2098                    range: Pos {
2099                        line,
2100                        col: m.start() as u32,
2101                    }..Pos {
2102                        line,
2103                        col: m.end() as u32,
2104                    },
2105                    kind: HighlightKind::IncSearch,
2106                })
2107                .collect();
2108        }
2109
2110        if self.search_state.pattern.is_none() {
2111            return Vec::new();
2112        }
2113        let dgen = crate::types::Query::dirty_gen(&self.buffer);
2114        crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2115            .into_iter()
2116            .map(|(start, end)| Highlight {
2117                range: Pos {
2118                    line,
2119                    col: start as u32,
2120                }..Pos {
2121                    line,
2122                    col: end as u32,
2123                },
2124                kind: HighlightKind::SearchMatch,
2125            })
2126            .collect()
2127    }
2128
2129    /// Build the engine's [`crate::types::RenderFrame`] for the
2130    /// current state. Hosts call this once per redraw and diff
2131    /// across frames.
2132    ///
2133    /// Coarse today — covers mode + cursor + cursor shape + viewport
2134    /// top + line count. SPEC-target fields (selections, highlights,
2135    /// command line, search prompt, status line) land once trait
2136    /// extraction routes them through `SelectionSet` and the
2137    /// `Highlight` pipeline.
2138    pub fn render_frame(&self) -> crate::types::RenderFrame {
2139        use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2140        let (cursor_row, cursor_col) = self.cursor();
2141        let (mode, shape) = match self.vim_mode() {
2142            crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2143            crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2144            crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2145            crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2146            crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2147        };
2148        RenderFrame {
2149            mode,
2150            cursor_row: cursor_row as u32,
2151            cursor_col: cursor_col as u32,
2152            cursor_shape: shape,
2153            viewport_top: self.host.viewport().top_row as u32,
2154            line_count: crate::types::Query::line_count(&self.buffer),
2155        }
2156    }
2157
2158    /// Capture the editor's coarse state into a serde-friendly
2159    /// [`crate::types::EditorSnapshot`].
2160    ///
2161    /// Today's snapshot covers mode, cursor, lines, viewport top.
2162    /// Registers, marks, jump list, undo tree, and full options arrive
2163    /// once phase 5 trait extraction lands the generic
2164    /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2165    /// stays stable; only the snapshot's internal fields grow.
2166    ///
2167    /// Distinct from the internal `snapshot` used by undo (which
2168    /// returns `(Vec<String>, (usize, usize))`); host-facing
2169    /// persistence goes through this one.
2170    pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2171        use crate::types::{EditorSnapshot, SnapshotMode};
2172        let mode = match self.vim_mode() {
2173            crate::VimMode::Normal => SnapshotMode::Normal,
2174            crate::VimMode::Insert => SnapshotMode::Insert,
2175            crate::VimMode::Visual => SnapshotMode::Visual,
2176            crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2177            crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2178        };
2179        let cursor = self.cursor();
2180        let cursor = (cursor.0 as u32, cursor.1 as u32);
2181        let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2182        let viewport_top = self.host.viewport().top_row as u32;
2183        let marks = self
2184            .marks
2185            .iter()
2186            .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2187            .collect();
2188        EditorSnapshot {
2189            version: EditorSnapshot::VERSION,
2190            mode,
2191            cursor,
2192            lines,
2193            viewport_top,
2194            registers: self.registers.clone(),
2195            marks,
2196        }
2197    }
2198
2199    /// Restore editor state from an [`EditorSnapshot`]. Returns
2200    /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2201    /// `version` doesn't match [`EditorSnapshot::VERSION`].
2202    ///
2203    /// Mode is best-effort: `SnapshotMode` only round-trips the
2204    /// status-line summary, not the full FSM state. Visual / Insert
2205    /// mode entry happens through synthetic key dispatch when needed.
2206    pub fn restore_snapshot(
2207        &mut self,
2208        snap: crate::types::EditorSnapshot,
2209    ) -> Result<(), crate::EngineError> {
2210        use crate::types::EditorSnapshot;
2211        if snap.version != EditorSnapshot::VERSION {
2212            return Err(crate::EngineError::SnapshotVersion(
2213                snap.version,
2214                EditorSnapshot::VERSION,
2215            ));
2216        }
2217        let text = snap.lines.join("\n");
2218        self.set_content(&text);
2219        self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2220        self.host.viewport_mut().top_row = snap.viewport_top as usize;
2221        self.registers = snap.registers;
2222        self.marks = snap
2223            .marks
2224            .into_iter()
2225            .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2226            .collect();
2227        Ok(())
2228    }
2229
2230    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2231    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2232    /// shape their payload.
2233    pub fn seed_yank(&mut self, text: String) {
2234        let linewise = text.ends_with('\n');
2235        self.vim.yank_linewise = linewise;
2236        self.registers.unnamed = crate::registers::Slot { text, linewise };
2237    }
2238
2239    /// Scroll the viewport down by `rows`. The cursor stays on its
2240    /// absolute line (vim convention) unless the scroll would take it
2241    /// off-screen — in that case it's clamped to the first row still
2242    /// visible.
2243    pub fn scroll_down(&mut self, rows: i16) {
2244        self.scroll_viewport(rows);
2245    }
2246
2247    /// Scroll the viewport up by `rows`. Cursor stays unless it would
2248    /// fall off the bottom of the new viewport, then clamp to the
2249    /// bottom-most visible row.
2250    pub fn scroll_up(&mut self, rows: i16) {
2251        self.scroll_viewport(-rows);
2252    }
2253
2254    /// Scroll the viewport right by `cols` columns. Only the horizontal
2255    /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2256    /// vim's `zl` behaviour for horizontal scroll without wrap).
2257    pub fn scroll_right(&mut self, cols: i16) {
2258        let vp = self.host.viewport_mut();
2259        let cols_i = cols as isize;
2260        let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2261        vp.top_col = new_top;
2262    }
2263
2264    /// Scroll the viewport left by `cols` columns. Delegates to
2265    /// `scroll_right` with a negated argument so the floor-at-zero
2266    /// clamp is shared.
2267    pub fn scroll_left(&mut self, cols: i16) {
2268        self.scroll_right(-cols);
2269    }
2270
2271    /// Vim's `scrolloff` default — keep the cursor at least this many
2272    /// rows away from the top / bottom edge of the viewport while
2273    /// scrolling. Collapses to `height / 2` for tiny viewports.
2274    const SCROLLOFF: usize = 5;
2275
2276    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2277    /// rows from each edge. Replaces the bare
2278    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2279    /// don't park the cursor on the very last visible row.
2280    pub fn ensure_cursor_in_scrolloff(&mut self) {
2281        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2282        if height == 0 {
2283            // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2284            // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2285            // Disjoint-field borrow split: `self.buffer` (immutable via
2286            // `folds` snapshot + cursor) and `self.host` (mutable
2287            // viewport ref) live on distinct struct fields, so one
2288            // statement satisfies the borrow checker.
2289            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2290            crate::viewport_math::ensure_cursor_visible(
2291                &self.buffer,
2292                &folds,
2293                self.host.viewport_mut(),
2294            );
2295            return;
2296        }
2297        // Cap margin at (height - 1) / 2 so the upper + lower bands
2298        // can't overlap on tiny windows (margin=5 + height=10 would
2299        // otherwise produce contradictory clamp ranges).
2300        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2301        // Soft-wrap path: scrolloff math runs in *screen rows*, not
2302        // doc rows, since a wrapped doc row spans many visual lines.
2303        if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2304            self.ensure_scrolloff_wrap(height, margin);
2305            return;
2306        }
2307        let cursor_row = buf_cursor_row(&self.buffer);
2308        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2309        let v = self.host.viewport_mut();
2310        // Top edge: cursor_row should sit at >= top_row + margin.
2311        if cursor_row < v.top_row + margin {
2312            v.top_row = cursor_row.saturating_sub(margin);
2313        }
2314        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2315        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2316        if cursor_row > v.top_row + max_bottom {
2317            v.top_row = cursor_row.saturating_sub(max_bottom);
2318        }
2319        // Clamp top_row so we never scroll past the buffer's bottom.
2320        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2321        if v.top_row > max_top {
2322            v.top_row = max_top;
2323        }
2324        // Defer to Buffer for column-side scroll (no scrolloff for
2325        // horizontal scrolling — vim default `sidescrolloff = 0`).
2326        let cursor = buf_cursor_pos(&self.buffer);
2327        self.host.viewport_mut().ensure_visible(cursor);
2328    }
2329
2330    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2331    /// at a time so the cursor's *screen* row stays inside
2332    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2333    /// buffer's bottom never leaves blank rows below it.
2334    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2335        let cursor_row = buf_cursor_row(&self.buffer);
2336        // Step 1 — cursor above viewport: snap top to cursor row,
2337        // then we'll fix up the margin below.
2338        if cursor_row < self.host.viewport().top_row {
2339            let v = self.host.viewport_mut();
2340            v.top_row = cursor_row;
2341            v.top_col = 0;
2342        }
2343        // Step 2 — push top forward until cursor's screen row is
2344        // within the bottom margin (`csr <= height - 1 - margin`).
2345        // 0.0.33 (Patch C-γ): fold-iteration goes through the
2346        // [`crate::types::FoldProvider`] surface via
2347        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2348        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2349        // a `&Viewport` parameter; the host owns the viewport, so the
2350        // disjoint `(self.host, self.buffer)` borrows split cleanly.
2351        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2352        loop {
2353            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2354            let csr =
2355                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2356                    .unwrap_or(0);
2357            if csr <= max_csr {
2358                break;
2359            }
2360            let top = self.host.viewport().top_row;
2361            let row_count = buf_row_count(&self.buffer);
2362            let next = {
2363                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2364                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2365            };
2366            let Some(next) = next else {
2367                break;
2368            };
2369            // Don't walk past the cursor's row.
2370            if next > cursor_row {
2371                self.host.viewport_mut().top_row = cursor_row;
2372                break;
2373            }
2374            self.host.viewport_mut().top_row = next;
2375        }
2376        // Step 3 — pull top backward until cursor's screen row is
2377        // past the top margin (`csr >= margin`).
2378        loop {
2379            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2380            let csr =
2381                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2382                    .unwrap_or(0);
2383            if csr >= margin {
2384                break;
2385            }
2386            let top = self.host.viewport().top_row;
2387            let prev = {
2388                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2389                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2390            };
2391            let Some(prev) = prev else {
2392                break;
2393            };
2394            self.host.viewport_mut().top_row = prev;
2395        }
2396        // Step 4 — clamp top so the buffer's bottom doesn't leave
2397        // blank rows below it. `max_top_for_height` walks segments
2398        // backward from the last row until it accumulates `height`
2399        // screen rows.
2400        let max_top = {
2401            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2402            crate::viewport_math::max_top_for_height(
2403                &self.buffer,
2404                &folds,
2405                self.host.viewport(),
2406                height,
2407            )
2408        };
2409        if self.host.viewport().top_row > max_top {
2410            self.host.viewport_mut().top_row = max_top;
2411        }
2412        self.host.viewport_mut().top_col = 0;
2413    }
2414
2415    fn scroll_viewport(&mut self, delta: i16) {
2416        if delta == 0 {
2417            return;
2418        }
2419        // Bump the host viewport's top within bounds.
2420        let total_rows = buf_row_count(&self.buffer) as isize;
2421        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2422        let cur_top = self.host.viewport().top_row as isize;
2423        let new_top = (cur_top + delta as isize)
2424            .max(0)
2425            .min((total_rows - 1).max(0)) as usize;
2426        self.host.viewport_mut().top_row = new_top;
2427        // Mirror to textarea so its viewport reads (still consumed by
2428        // a couple of helpers) stay accurate.
2429        let _ = cur_top;
2430        if height == 0 {
2431            return;
2432        }
2433        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2434        // from the visible viewport edges.
2435        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2436        let margin = Self::SCROLLOFF.min(height / 2);
2437        let min_row = new_top + margin;
2438        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2439        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2440        if target_row != cursor_row {
2441            let line_len = buf_line(&self.buffer, target_row)
2442                .map(|l| l.chars().count())
2443                .unwrap_or(0);
2444            let target_col = cursor_col.min(line_len.saturating_sub(1));
2445            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2446        }
2447    }
2448
2449    pub fn goto_line(&mut self, line: usize) {
2450        let row = line.saturating_sub(1);
2451        let max = buf_row_count(&self.buffer).saturating_sub(1);
2452        let target = row.min(max);
2453        buf_set_cursor_rc(&mut self.buffer, target, 0);
2454        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2455        // the cursor lands off-screen and the user has to scroll
2456        // manually to see it.
2457        self.ensure_cursor_in_scrolloff();
2458    }
2459
2460    /// Scroll so the cursor row lands at the given viewport position:
2461    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2462    /// Cursor stays on its absolute line; only the viewport moves.
2463    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2464        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2465        if height == 0 {
2466            return;
2467        }
2468        let cur_row = buf_cursor_row(&self.buffer);
2469        let cur_top = self.host.viewport().top_row;
2470        // Scrolloff awareness: `zt` lands the cursor at the top edge
2471        // of the viable area (top + margin), `zb` at the bottom edge
2472        // (top + height - 1 - margin). Match the cap used by
2473        // `ensure_cursor_in_scrolloff` so contradictory bounds are
2474        // impossible on tiny viewports.
2475        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2476        let new_top = match pos {
2477            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2478            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2479            CursorScrollTarget::Bottom => {
2480                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2481            }
2482        };
2483        if new_top == cur_top {
2484            return;
2485        }
2486        self.host.viewport_mut().top_row = new_top;
2487    }
2488
2489    /// Jump the cursor to the given 1-based line/column, clamped to the document.
2490    pub fn jump_to(&mut self, line: usize, col: usize) {
2491        let r = line.saturating_sub(1);
2492        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2493        let r = r.min(max_row);
2494        let line_len = buf_line(&self.buffer, r)
2495            .map(|l| l.chars().count())
2496            .unwrap_or(0);
2497        let c = col.saturating_sub(1).min(line_len);
2498        buf_set_cursor_rc(&mut self.buffer, r, c);
2499    }
2500
2501    // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2502    //
2503    // These primitives operate on document (row, col) coordinates that the HOST
2504    // computes from its own layout knowledge (cell geometry for the TUI host,
2505    // pixel geometry for the future GUI host). The engine has no u16 terminal
2506    // assumption here — it just moves the cursor in doc-space.
2507
2508    /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2509    /// document bounds. Hosts use this for programmatic cursor placement and
2510    /// as the building block for the mouse-click path.
2511    ///
2512    /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2513    /// position); values beyond that are clamped to `char_count`.
2514    pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2515        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2516        let r = row.min(max_row);
2517        let line_len = buf_line(&self.buffer, r)
2518            .map(|l| l.chars().count())
2519            .unwrap_or(0);
2520        let c = col.min(line_len);
2521        buf_set_cursor_rc(&mut self.buffer, r, c);
2522    }
2523
2524    /// Handle a left-button click at doc-space `(row, col)`.
2525    ///
2526    /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2527    /// parity for `undo_break_on_motion`), then moves the cursor. The host
2528    /// performs cell→doc or pixel→doc translation before calling this.
2529    ///
2530    /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2531    /// cursor lives on chars and never on the implicit `\n` — `col` is
2532    /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2533    /// allows the one-past-EOL insert position (`col == chars().count()`).
2534    ///
2535    /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2536    /// motion uses the clicked column as the intended visual column
2537    /// (otherwise the cursor would snap back to the keyboard-tracked
2538    /// column on the first vertical motion after a click).
2539    pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2540        if self.vim.is_visual() {
2541            self.vim.force_normal();
2542        }
2543        // Mouse-position click counts as a motion — break the active
2544        // insert-mode undo group when the toggle is on (vim parity).
2545        crate::vim::break_undo_group_in_insert(self);
2546
2547        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2548        let r = row.min(max_row);
2549        let line_len = buf_line(&self.buffer, r)
2550            .map(|l| l.chars().count())
2551            .unwrap_or(0);
2552        let cap = if self.vim.current_mode == crate::VimMode::Insert {
2553            line_len
2554        } else {
2555            line_len.saturating_sub(1)
2556        };
2557        let c = col.min(cap);
2558        buf_set_cursor_rc(&mut self.buffer, r, c);
2559        self.sticky_col = Some(c);
2560    }
2561
2562    /// Begin a mouse-drag selection: anchor at the current cursor and enter
2563    /// Visual-char mode. Idempotent if already in Visual-char mode.
2564    pub fn mouse_begin_drag(&mut self) {
2565        if !self.vim.is_visual_char() {
2566            vim::enter_visual_char_bridge(self);
2567        }
2568    }
2569
2570    /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2571    ///
2572    /// Moves the live cursor; the Visual anchor stays where
2573    /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2574    /// translated the drag position to doc coordinates.
2575    pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2576        self.set_cursor_doc(row, col);
2577    }
2578
2579    pub fn insert_str(&mut self, text: &str) {
2580        let pos = crate::types::Cursor::cursor(&self.buffer);
2581        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2582        self.push_buffer_content_to_textarea();
2583        self.mark_content_dirty();
2584    }
2585
2586    pub fn accept_completion(&mut self, completion: &str) {
2587        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2588        let cursor_pos = CursorTrait::cursor(&self.buffer);
2589        let cursor_row = cursor_pos.line as usize;
2590        let cursor_col = cursor_pos.col as usize;
2591        let line = buf_line(&self.buffer, cursor_row).unwrap_or_default();
2592        let chars: Vec<char> = line.chars().collect();
2593        let prefix_len = chars[..cursor_col.min(chars.len())]
2594            .iter()
2595            .rev()
2596            .take_while(|c| c.is_alphanumeric() || **c == '_')
2597            .count();
2598        if prefix_len > 0 {
2599            let start = Pos {
2600                line: cursor_row as u32,
2601                col: (cursor_col - prefix_len) as u32,
2602            };
2603            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2604        }
2605        let cursor = CursorTrait::cursor(&self.buffer);
2606        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2607        self.push_buffer_content_to_textarea();
2608        self.mark_content_dirty();
2609    }
2610
2611    pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2612        let rc = buf_cursor_rc(&self.buffer);
2613        (buf_lines_to_vec(&self.buffer), rc)
2614    }
2615
2616    /// Walk one step back through the undo history. Equivalent to the
2617    /// user pressing `u` in normal mode. Drains the most recent undo
2618    /// entry and pushes it onto the redo stack.
2619    pub fn undo(&mut self) {
2620        crate::vim::do_undo(self);
2621    }
2622
2623    /// Walk one step forward through the redo history. Equivalent to
2624    /// `<C-r>` in normal mode.
2625    pub fn redo(&mut self) {
2626        crate::vim::do_redo(self);
2627    }
2628
2629    /// Snapshot current buffer state onto the undo stack and clear
2630    /// the redo stack. Bounded by `settings.undo_levels` — older
2631    /// entries pruned. Call before any group of buffer mutations the
2632    /// user might want to undo as a single step.
2633    pub fn push_undo(&mut self) {
2634        let snap = self.snapshot();
2635        self.undo_stack.push(snap);
2636        self.cap_undo();
2637        self.redo_stack.clear();
2638    }
2639
2640    /// Trim the undo stack down to `settings.undo_levels`, dropping
2641    /// the oldest entries. `undo_levels == 0` is treated as
2642    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2643    /// skipped — guarding with `> 0` is one line shorter than gating
2644    /// the cap path with an explicit zero-check above the call site).
2645    pub(crate) fn cap_undo(&mut self) {
2646        let cap = self.settings.undo_levels as usize;
2647        if cap > 0 && self.undo_stack.len() > cap {
2648            let diff = self.undo_stack.len() - cap;
2649            self.undo_stack.drain(..diff);
2650        }
2651    }
2652
2653    /// Test-only accessor for the undo stack length.
2654    #[doc(hidden)]
2655    pub fn undo_stack_len(&self) -> usize {
2656        self.undo_stack.len()
2657    }
2658
2659    /// Replace the buffer with `lines` joined by `\n` and set the
2660    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2661    /// paths. Marks the editor dirty.
2662    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2663        let text = lines.join("\n");
2664        crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2665        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2666        // Bulk replace — supersedes any queued ContentEdits.
2667        self.pending_content_edits.clear();
2668        self.pending_content_reset = true;
2669        self.mark_content_dirty();
2670    }
2671
2672    /// Returns true if the key was consumed by the editor.
2673    /// Replace the char under the cursor with `ch`, `count` times. Matches
2674    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2675    /// snapshot taken once at start. Promoted to public surface in 0.5.5
2676    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2677    /// re-entering the FSM.
2678    pub fn replace_char_at(&mut self, ch: char, count: usize) {
2679        vim::replace_char(self, ch, count);
2680    }
2681
2682    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2683    /// to the `count`-th occurrence of `ch` on the current line, respecting
2684    /// `forward` (direction) and `till` (stop one char before target).
2685    /// Records `last_find` so `;` / `,` repeat work.
2686    ///
2687    /// No-op if the target char isn't on the current line within range.
2688    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2689    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2690        vim::apply_find_char(self, ch, forward, till, count.max(1));
2691    }
2692
2693    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2694    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2695    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2696    ///
2697    /// Promoted to public surface in 0.5.10 so hjkl-vim's
2698    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2699    /// re-entering the engine FSM.
2700    pub fn after_g(&mut self, ch: char, count: usize) {
2701        vim::apply_after_g(self, ch, count);
2702    }
2703
2704    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2705    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2706    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2707    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2708    ///
2709    /// Promoted to public surface in 0.5.11 so hjkl-vim's
2710    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2711    /// re-entering the engine FSM.
2712    pub fn after_z(&mut self, ch: char, count: usize) {
2713        vim::apply_after_z(self, ch, count);
2714    }
2715
2716    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2717    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2718    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2719    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2720    /// then calls `apply_op_with_motion`. `total_count` is already the product of
2721    /// the prefix count and any inner count accumulated by the reducer.
2722    ///
2723    /// No-op when `motion_key` does not map to a known motion (engine silently
2724    /// cancels the operator, matching vim's behaviour on unknown motions).
2725    ///
2726    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2727    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2728    /// re-entering the engine FSM.
2729    pub fn apply_op_motion(
2730        &mut self,
2731        op: crate::vim::Operator,
2732        motion_key: char,
2733        total_count: usize,
2734    ) {
2735        vim::apply_op_motion_key(self, op, motion_key, total_count);
2736    }
2737
2738    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2739    /// `total_count` is the product of prefix count and inner count.
2740    ///
2741    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2742    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2743    /// re-entering the engine FSM.
2744    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2745        vim::apply_op_double(self, op, total_count);
2746    }
2747
2748    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2749    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2750    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2751    /// updates `last_change` when `op` is Change (for dot-repeat).
2752    ///
2753    /// `total_count` is the product of prefix count and any inner count
2754    /// accumulated by the reducer — already folded at transition time.
2755    ///
2756    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2757    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2758    /// re-entering the engine FSM. `handle_op_find_target` (used by the
2759    /// chord-init op path) delegates here to avoid logic duplication.
2760    pub fn apply_op_find(
2761        &mut self,
2762        op: crate::vim::Operator,
2763        ch: char,
2764        forward: bool,
2765        till: bool,
2766        total_count: usize,
2767    ) {
2768        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2769    }
2770
2771    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2772    /// Maps `ch` to a `TextObject` per the standard vim table, calls
2773    /// `apply_op_with_text_object`, and records `last_change` when `op` is
2774    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2775    /// matching the engine FSM's behaviour on unrecognised text-object chars.
2776    ///
2777    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2778    /// `apply_op_find` but is currently unused — text objects don't repeat in
2779    /// vim's current grammar. Kept for future-proofing.
2780    ///
2781    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
2782    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
2783    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
2784    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
2785    /// duplication.
2786    pub fn apply_op_text_obj(
2787        &mut self,
2788        op: crate::vim::Operator,
2789        ch: char,
2790        inner: bool,
2791        total_count: usize,
2792    ) {
2793        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
2794    }
2795
2796    /// Apply an operator over a g-chord motion or case-op linewise form
2797    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
2798    ///
2799    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
2800    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
2801    /// - Otherwise maps `ch` to a motion:
2802    ///   - `'g'` → `Motion::FileTop` (gg)
2803    ///   - `'e'` → `Motion::WordEndBack` (ge)
2804    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
2805    ///   - `'j'` → `Motion::ScreenDown` (gj)
2806    ///   - `'k'` → `Motion::ScreenUp` (gk)
2807    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
2808    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
2809    ///
2810    /// `total_count` is the already-folded product of prefix and inner counts.
2811    ///
2812    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
2813    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
2814    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
2815    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
2816    /// duplication.
2817    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
2818        vim::apply_op_g_inner(self, op, ch, total_count);
2819    }
2820
2821    // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
2822
2823    /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
2824    /// row and the motion's target row. Used by the app layer to compute the
2825    /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
2826    /// format job.
2827    ///
2828    /// Returns `None` when `motion_key` does not map to a known motion (same
2829    /// condition that makes `apply_op_motion` a no-op).
2830    ///
2831    /// The cursor is restored to its original position after the probe —
2832    /// the buffer content is not touched.
2833    pub fn range_for_op_motion(
2834        &mut self,
2835        motion_key: char,
2836        total_count: usize,
2837    ) -> Option<(usize, usize)> {
2838        let start = self.cursor();
2839        // Reuse the same logic as apply_op_motion_key but only read the
2840        // target row — we parse the motion, apply it to move the cursor,
2841        // then immediately restore.
2842        let input = crate::input::Input {
2843            key: crate::input::Key::Char(motion_key),
2844            ctrl: false,
2845            alt: false,
2846            shift: false,
2847        };
2848        let motion = vim::parse_motion(&input)?;
2849        // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
2850        let motion = match motion {
2851            vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
2852                Some((ch, forward, till)) => vim::Motion::Find {
2853                    ch,
2854                    forward: if reverse { !forward } else { forward },
2855                    till,
2856                },
2857                None => return None,
2858            },
2859            m => m,
2860        };
2861        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2862        let end = self.cursor();
2863        // Restore cursor.
2864        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2865        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2866        Some((r0, r1))
2867    }
2868
2869    /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
2870    /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
2871    /// linewise forms that don't map to a row range.
2872    ///
2873    /// The cursor is restored after the probe.
2874    pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
2875        let start = self.cursor();
2876        let motion = match ch {
2877            'g' => vim::Motion::FileTop,
2878            'e' => vim::Motion::WordEndBack,
2879            'E' => vim::Motion::BigWordEndBack,
2880            'j' => vim::Motion::ScreenDown,
2881            'k' => vim::Motion::ScreenUp,
2882            _ => return None,
2883        };
2884        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2885        let end = self.cursor();
2886        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2887        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2888        Some((r0, r1))
2889    }
2890
2891    /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
2892    /// matched region. Returns `None` when `ch` is not a known text-object
2893    /// kind or the text object could not be resolved (e.g. no enclosing bracket).
2894    ///
2895    /// The buffer is not mutated.
2896    pub fn range_for_op_text_obj(
2897        &self,
2898        ch: char,
2899        inner: bool,
2900        _total_count: usize,
2901    ) -> Option<(usize, usize)> {
2902        let obj = match ch {
2903            'w' => vim::TextObject::Word { big: false },
2904            'W' => vim::TextObject::Word { big: true },
2905            '"' | '\'' | '`' => vim::TextObject::Quote(ch),
2906            '(' | ')' | 'b' => vim::TextObject::Bracket('('),
2907            '[' | ']' => vim::TextObject::Bracket('['),
2908            '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
2909            '<' | '>' => vim::TextObject::Bracket('<'),
2910            'p' => vim::TextObject::Paragraph,
2911            't' => vim::TextObject::XmlTag,
2912            's' => vim::TextObject::Sentence,
2913            _ => return None,
2914        };
2915        let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
2916        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2917        Some((r0, r1))
2918    }
2919
2920    // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
2921    //
2922    // These do not consume input — the caller (hjkl-vim's visual-mode operator
2923    // path, chunk 4e) has already resolved the range from the visual selection
2924    // before calling in. Normal-mode op dispatch continues to use
2925    // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
2926
2927    /// Delete the region `[start, end)` and stash the removed text in
2928    /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
2929    /// select named registers.
2930    ///
2931    /// Pure range-mutation primitive — does not consume input. Called by
2932    /// hjkl-vim's visual-mode operator path which has already resolved the range
2933    /// from the visual selection.
2934    ///
2935    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2936    /// grammar migration (kryptic-sh/hjkl#70).
2937    pub fn delete_range(
2938        &mut self,
2939        start: (usize, usize),
2940        end: (usize, usize),
2941        kind: crate::vim::RangeKind,
2942        register: char,
2943    ) {
2944        vim::delete_range_bridge(self, start, end, kind, register);
2945    }
2946
2947    /// Yank (copy) the region `[start, end)` into `register` without mutating
2948    /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
2949    /// register; `'a'`–`'z'` select named registers.
2950    ///
2951    /// Pure range-mutation primitive — does not consume input. Called by
2952    /// hjkl-vim's visual-mode operator path which has already resolved the range
2953    /// from the visual selection.
2954    ///
2955    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2956    /// grammar migration (kryptic-sh/hjkl#70).
2957    pub fn yank_range(
2958        &mut self,
2959        start: (usize, usize),
2960        end: (usize, usize),
2961        kind: crate::vim::RangeKind,
2962        register: char,
2963    ) {
2964        vim::yank_range_bridge(self, start, end, kind, register);
2965    }
2966
2967    /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
2968    /// operator). The deleted text is stashed in `register`. On return the
2969    /// editor is in Insert mode; the caller must not issue further normal-mode
2970    /// ops until the insert session ends.
2971    ///
2972    /// Pure range-mutation primitive — does not consume input. Called by
2973    /// hjkl-vim's visual-mode operator path which has already resolved the range
2974    /// from the visual selection.
2975    ///
2976    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2977    /// grammar migration (kryptic-sh/hjkl#70).
2978    pub fn change_range(
2979        &mut self,
2980        start: (usize, usize),
2981        end: (usize, usize),
2982        kind: crate::vim::RangeKind,
2983        register: char,
2984    ) {
2985        vim::change_range_bridge(self, start, end, kind, register);
2986    }
2987
2988    /// Indent (`count > 0`) or outdent (`count < 0`) the row span
2989    /// `[start.0, end.0]`. Column components are ignored — indent is always
2990    /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
2991    /// this call; pass `0` to use the current editor setting. `count == 0` is a
2992    /// no-op.
2993    ///
2994    /// Pure range-mutation primitive — does not consume input. Called by
2995    /// hjkl-vim's visual-mode operator path which has already resolved the range
2996    /// from the visual selection.
2997    ///
2998    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2999    /// grammar migration (kryptic-sh/hjkl#70).
3000    pub fn indent_range(
3001        &mut self,
3002        start: (usize, usize),
3003        end: (usize, usize),
3004        count: i32,
3005        shiftwidth: u32,
3006    ) {
3007        vim::indent_range_bridge(self, start, end, count, shiftwidth);
3008    }
3009
3010    /// Apply a case transformation (`Operator::Uppercase` /
3011    /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3012    /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3013    /// Yanks registers are left untouched — vim's case operators do not write
3014    /// to registers.
3015    ///
3016    /// Pure range-mutation primitive — does not consume input. Called by
3017    /// hjkl-vim's visual-mode operator path which has already resolved the range
3018    /// from the visual selection.
3019    ///
3020    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3021    /// grammar migration (kryptic-sh/hjkl#70).
3022    pub fn case_range(
3023        &mut self,
3024        start: (usize, usize),
3025        end: (usize, usize),
3026        kind: crate::vim::RangeKind,
3027        op: crate::vim::Operator,
3028    ) {
3029        vim::case_range_bridge(self, start, end, kind, op);
3030    }
3031
3032    // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3033    //
3034    // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3035    // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3036    // Ragged-edge handling (short lines not reaching `right_col`) matches the
3037    // engine FSM's `apply_block_operator` path — short lines lose only the
3038    // chars that exist.
3039    //
3040    // `register` is the target register; `'"'` selects the unnamed register.
3041
3042    /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3043    /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3044    /// bounds at the visual (display) column level. Ragged-edge handling
3045    /// matches engine FSM's VisualBlock op behavior — short lines that don't
3046    /// reach `right_col` lose only the chars that exist.
3047    ///
3048    /// `register` honors the user's pending register selection.
3049    ///
3050    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3051    pub fn delete_block(
3052        &mut self,
3053        top_row: usize,
3054        bot_row: usize,
3055        left_col: usize,
3056        right_col: usize,
3057        register: char,
3058    ) {
3059        vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3060    }
3061
3062    /// Yank a rectangular VisualBlock selection into `register` without
3063    /// mutating the buffer. `'"'` selects the unnamed register.
3064    ///
3065    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3066    pub fn yank_block(
3067        &mut self,
3068        top_row: usize,
3069        bot_row: usize,
3070        left_col: usize,
3071        right_col: usize,
3072        register: char,
3073    ) {
3074        vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3075    }
3076
3077    /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3078    /// operator). The deleted text is stashed in `register`. Mode is Insert
3079    /// on return; the caller must not issue further normal-mode ops until the
3080    /// insert session ends.
3081    ///
3082    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3083    pub fn change_block(
3084        &mut self,
3085        top_row: usize,
3086        bot_row: usize,
3087        left_col: usize,
3088        right_col: usize,
3089        register: char,
3090    ) {
3091        vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3092    }
3093
3094    /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3095    /// Column bounds are ignored — vim's block indent is always linewise.
3096    /// `count == 0` is a no-op.
3097    ///
3098    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3099    pub fn indent_block(
3100        &mut self,
3101        top_row: usize,
3102        bot_row: usize,
3103        _left_col: usize,
3104        _right_col: usize,
3105        count: i32,
3106    ) {
3107        vim::indent_block_bridge(self, top_row, bot_row, count);
3108    }
3109
3110    /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3111    /// Column components are ignored — auto-indent is always linewise.
3112    ///
3113    /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3114    /// row 0 to compute the correct depth at `start.0`, then for each line in
3115    /// the target range strips existing leading whitespace and prepends
3116    /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3117    /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3118    /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3119    /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3120    ///
3121    /// After the operation the cursor lands on the first non-whitespace
3122    /// character of `start_row` (vim parity for `==`).
3123    ///
3124    /// **v1 limitation**: the bracket scan does not detect brackets inside
3125    /// string literals or comments. Code such as `let s = "{";` will increment
3126    /// the depth counter even though the brace is not a structural opener.
3127    /// Tree-sitter / LSP indentation is deferred to a follow-up.
3128    pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3129        vim::auto_indent_range_bridge(self, start, end);
3130    }
3131
3132    /// Drain the row range set by the most recent auto-indent operation.
3133    ///
3134    /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3135    /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3136    /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3137    /// uses this to arm a brief visual flash over the reindented rows.
3138    pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3139        self.last_indent_range.take()
3140    }
3141
3142    // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3143    //
3144    // Pure functions — no cursor mutation, no mode change, no register write.
3145    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3146    // the existing `word_text_object` private resolver in vim.rs.
3147    //
3148    // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3149    // before invoking a range-mutation primitive (`delete_range`, etc.).
3150    //
3151    // Return value: `Some((start, end))` where both positions are `(row, col)`
3152    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3153    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3154    // Returns `None` when the cursor is on an empty line or the resolver cannot
3155    // find a word boundary.
3156
3157    /// Resolve the range of `iw` (inner word) at the current cursor position.
3158    ///
3159    /// An inner word is the contiguous run of keyword characters (or punctuation
3160    /// characters if the cursor is on punctuation) under the cursor, without any
3161    /// surrounding whitespace. Whitespace-only positions return `None`.
3162    ///
3163    /// Pure function — does not move the cursor or change any editor state.
3164    /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3165    /// invoking a range-mutation primitive (`delete_range`, etc.).
3166    ///
3167    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3168    /// migration (kryptic-sh/hjkl#70).
3169    pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3170        vim::text_object_inner_word_bridge(self)
3171    }
3172
3173    /// Resolve the range of `aw` (around word) at the current cursor position.
3174    ///
3175    /// Like `iw` but extends the range to include trailing whitespace after the
3176    /// word. If no trailing whitespace exists, leading whitespace before the word
3177    /// is absorbed instead (vim `:help text-objects` behaviour).
3178    ///
3179    /// Pure function — does not move the cursor or change any editor state.
3180    ///
3181    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3182    /// migration (kryptic-sh/hjkl#70).
3183    pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3184        vim::text_object_around_word_bridge(self)
3185    }
3186
3187    /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3188    ///
3189    /// A WORD is any contiguous run of non-whitespace characters — punctuation
3190    /// is not treated as a word boundary. Returns the span of the WORD under the
3191    /// cursor, without surrounding whitespace.
3192    ///
3193    /// Pure function — does not move the cursor or change any editor state.
3194    ///
3195    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3196    /// migration (kryptic-sh/hjkl#70).
3197    pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3198        vim::text_object_inner_big_word_bridge(self)
3199    }
3200
3201    /// Resolve the range of `aW` (around WORD) at the current cursor position.
3202    ///
3203    /// Like `iW` but extends the range to include trailing whitespace after the
3204    /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3205    /// is absorbed instead.
3206    ///
3207    /// Pure function — does not move the cursor or change any editor state.
3208    ///
3209    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3210    /// migration (kryptic-sh/hjkl#70).
3211    pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3212        vim::text_object_around_big_word_bridge(self)
3213    }
3214
3215    // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3216    //
3217    // Pure functions — no cursor mutation, no mode change, no register write.
3218    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3219    // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3220    // in vim.rs.
3221    //
3222    // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3223    // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3224    // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3225    // hjkl-vim grammar layer normalises close→open before calling these methods.
3226    //
3227    // Return value: `Some((start, end))` where both positions are `(row, col)`
3228    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3229    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3230    // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3231    // ranges for multi-line pairs; that tag is stripped here — callers receive
3232    // the same flat shape as all other text-object resolvers.
3233
3234    /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3235    ///
3236    /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3237    /// cursor's line contains fewer than two occurrences of `quote`, or when no
3238    /// matching pair can be found around or ahead of the cursor.
3239    ///
3240    /// Inner range excludes the quote characters themselves.
3241    ///
3242    /// Pure function — no cursor mutation.
3243    ///
3244    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3245    /// migration (kryptic-sh/hjkl#70).
3246    pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3247        vim::text_object_inner_quote_bridge(self, quote)
3248    }
3249
3250    /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3251    ///
3252    /// Like `i<quote>` but includes the quote characters themselves plus
3253    /// surrounding whitespace on one side: trailing whitespace after the closing
3254    /// quote if any exists; otherwise leading whitespace before the opening
3255    /// quote. This matches vim `:help text-objects` behaviour.
3256    ///
3257    /// Pure function — no cursor mutation.
3258    ///
3259    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3260    /// migration (kryptic-sh/hjkl#70).
3261    pub fn text_object_around_quote(
3262        &self,
3263        quote: char,
3264    ) -> Option<((usize, usize), (usize, usize))> {
3265        vim::text_object_around_quote_bridge(self, quote)
3266    }
3267
3268    /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3269    ///
3270    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3271    /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3272    /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3273    /// calling this method. Returns `None` when no enclosing pair is found.
3274    ///
3275    /// The cursor may be anywhere inside the pair or on a bracket character
3276    /// itself. When not inside any pair the resolver falls back to a forward
3277    /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3278    ///
3279    /// Inner range excludes the bracket characters. Multi-line pairs are
3280    /// supported; the returned range spans the full content between the
3281    /// brackets.
3282    ///
3283    /// Pure function — no cursor mutation.
3284    ///
3285    /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3286    /// handled here.
3287    ///
3288    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3289    /// migration (kryptic-sh/hjkl#70).
3290    pub fn text_object_inner_bracket(
3291        &self,
3292        open: char,
3293    ) -> Option<((usize, usize), (usize, usize))> {
3294        vim::text_object_inner_bracket_bridge(self, open)
3295    }
3296
3297    /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
3298    ///
3299    /// Like `i<bracket>` but includes the bracket characters themselves.
3300    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
3301    ///
3302    /// Pure function — no cursor mutation.
3303    ///
3304    /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
3305    ///
3306    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3307    /// migration (kryptic-sh/hjkl#70).
3308    pub fn text_object_around_bracket(
3309        &self,
3310        open: char,
3311    ) -> Option<((usize, usize), (usize, usize))> {
3312        vim::text_object_around_bracket_bridge(self, open)
3313    }
3314
3315    // ── Sentence text objects (is / as) ───────────────────────────────────
3316
3317    /// Resolve `is` (inner sentence) at the cursor position.
3318    ///
3319    /// Returns the range of the current sentence, excluding trailing
3320    /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
3321    /// `?` / `!` followed by whitespace or end-of-paragraph).
3322    ///
3323    /// Pure function — no cursor mutation.
3324    ///
3325    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3326    /// grammar migration (kryptic-sh/hjkl#70).
3327    pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3328        vim::text_object_inner_sentence_bridge(self)
3329    }
3330
3331    /// Resolve `as` (around sentence) at the cursor position.
3332    ///
3333    /// Like `is` but includes trailing whitespace after the sentence
3334    /// terminator.
3335    ///
3336    /// Pure function — no cursor mutation.
3337    ///
3338    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3339    /// grammar migration (kryptic-sh/hjkl#70).
3340    pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3341        vim::text_object_around_sentence_bridge(self)
3342    }
3343
3344    // ── Paragraph text objects (ip / ap) ──────────────────────────────────
3345
3346    /// Resolve `ip` (inner paragraph) at the cursor position.
3347    ///
3348    /// A paragraph is a block of non-blank lines bounded by blank lines or
3349    /// buffer edges. Returns `None` when the cursor is on a blank line.
3350    ///
3351    /// Pure function — no cursor mutation.
3352    ///
3353    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3354    /// grammar migration (kryptic-sh/hjkl#70).
3355    pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3356        vim::text_object_inner_paragraph_bridge(self)
3357    }
3358
3359    /// Resolve `ap` (around paragraph) at the cursor position.
3360    ///
3361    /// Like `ip` but includes one trailing blank line when present.
3362    ///
3363    /// Pure function — no cursor mutation.
3364    ///
3365    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3366    /// grammar migration (kryptic-sh/hjkl#70).
3367    pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3368        vim::text_object_around_paragraph_bridge(self)
3369    }
3370
3371    // ── Tag text objects (it / at) ────────────────────────────────────────
3372
3373    /// Resolve `it` (inner tag) at the cursor position.
3374    ///
3375    /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
3376    /// inner content between the open and close tags (excluding the tags
3377    /// themselves).
3378    ///
3379    /// Pure function — no cursor mutation.
3380    ///
3381    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3382    /// grammar migration (kryptic-sh/hjkl#70).
3383    pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3384        vim::text_object_inner_tag_bridge(self)
3385    }
3386
3387    /// Resolve `at` (around tag) at the cursor position.
3388    ///
3389    /// Like `it` but includes the open and close tag delimiters themselves.
3390    ///
3391    /// Pure function — no cursor mutation.
3392    ///
3393    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3394    /// grammar migration (kryptic-sh/hjkl#70).
3395    pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3396        vim::text_object_around_tag_bridge(self)
3397    }
3398
3399    /// Execute a named cursor motion `kind` repeated `count` times.
3400    ///
3401    /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
3402    /// motion primitives, bypassing the engine FSM. Identical cursor semantics
3403    /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
3404    /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
3405    /// used by the FSM arms.
3406    ///
3407    /// Introduced in 0.6.1 as the host entry point for Phase 3a of
3408    /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
3409    /// calls this method rather than re-entering the engine FSM.
3410    ///
3411    /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
3412    /// intact for macro-replay coverage (macros re-feed raw keys through the
3413    /// FSM). This method is the keymap / controller path only.
3414    pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
3415        vim::apply_motion_kind(self, kind, count);
3416    }
3417
3418    /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
3419    /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
3420    /// chars are silently ignored (no-op), matching the engine FSM's
3421    /// `handle_select_register` behaviour.
3422    ///
3423    /// Promoted to the public surface in 0.5.17 so the hjkl-vim
3424    /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
3425    /// without re-entering the engine FSM. `handle_select_register` (engine FSM
3426    /// path for macro-replay / defensive coverage) delegates here to avoid
3427    /// logic duplication.
3428    pub fn set_pending_register(&mut self, reg: char) {
3429        if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
3430            self.vim.pending_register = Some(reg);
3431        }
3432        // Invalid chars silently no-op (matches engine FSM behavior).
3433    }
3434
3435    /// Record a mark named `ch` at the current cursor position.
3436    ///
3437    /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
3438    /// rules). Invalid chars are silently ignored (no-op), matching the engine
3439    /// FSM's `handle_set_mark` behaviour.
3440    ///
3441    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3442    /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
3443    /// without re-entering the engine FSM. `handle_set_mark` delegates here.
3444    pub fn set_mark_at_cursor(&mut self, ch: char) {
3445        vim::set_mark_at_cursor(self, ch);
3446    }
3447
3448    /// `.` dot-repeat: replay the last buffered change at the current cursor.
3449    /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
3450    /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
3451    ///
3452    /// Storage of `LastChange` stays inside engine for now; Phase 5c of
3453    /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
3454    /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
3455    /// FSM `.` arm stays for macro-replay defensive coverage.
3456    pub fn replay_last_change(&mut self, count: usize) {
3457        vim::replay_last_change(self, count);
3458    }
3459
3460    /// Jump to the mark named `ch`, linewise (row only; col snaps to first
3461    /// non-blank). Pushes the pre-jump position onto the jumplist if the
3462    /// cursor actually moved.
3463    ///
3464    /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
3465    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3466    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3467    /// are silently ignored (no-op), matching the engine FSM's
3468    /// `handle_goto_mark` behaviour.
3469    ///
3470    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3471    /// `PendingState::GotoMarkLine` reducer can dispatch
3472    /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
3473    pub fn goto_mark_line(&mut self, ch: char) {
3474        vim::goto_mark(self, ch, true);
3475    }
3476
3477    /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
3478    /// pre-jump position onto the jumplist if the cursor actually moved.
3479    ///
3480    /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
3481    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3482    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3483    /// are silently ignored (no-op), matching the engine FSM's
3484    /// `handle_goto_mark` behaviour.
3485    ///
3486    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3487    /// `PendingState::GotoMarkChar` reducer can dispatch
3488    /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
3489    pub fn goto_mark_char(&mut self, ch: char) {
3490        vim::goto_mark(self, ch, false);
3491    }
3492
3493    // ── Macro controller API (Phase 5b) ──────────────────────────────────────
3494
3495    /// Begin recording keystrokes into register `reg`. The caller (app) is
3496    /// responsible for stopping the recording via `stop_macro_record` when the
3497    /// user presses bare `q`.
3498    ///
3499    /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
3500    ///   recording by pre-seeding `recording_keys` with the decoded text of the
3501    ///   matching lowercase register, matching vim's capital-register append
3502    ///   semantics.
3503    /// - Lowercase `reg` clears `recording_keys` (fresh recording).
3504    /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
3505    ///
3506    /// Promoted to the public surface in Phase 5b so the app's
3507    /// `route_chord_key` can start a recording without re-entering the engine
3508    /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
3509    /// defensive coverage) continues to use the same logic via delegation.
3510    pub fn start_macro_record(&mut self, reg: char) {
3511        if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
3512            return;
3513        }
3514        self.vim.recording_macro = Some(reg);
3515        if reg.is_ascii_uppercase() {
3516            // Seed recording_keys with the existing lowercase register's text
3517            // decoded back to inputs so capital-register append continues from
3518            // where the previous recording left off.
3519            let lower = reg.to_ascii_lowercase();
3520            let text = self
3521                .registers
3522                .read(lower)
3523                .map(|s| s.text.clone())
3524                .unwrap_or_default();
3525            self.vim.recording_keys = crate::input::decode_macro(&text);
3526        } else {
3527            self.vim.recording_keys.clear();
3528        }
3529    }
3530
3531    /// Finalize the active recording: encode `recording_keys` as text and write
3532    /// to the matching (lowercase) named register. Clears both `recording_macro`
3533    /// and `recording_keys`. No-ops if no recording is active.
3534    ///
3535    /// Promoted to the public surface in Phase 5b so the app's `QChord` action
3536    /// can stop a recording when the user presses bare `q` without re-entering
3537    /// the engine FSM.
3538    pub fn stop_macro_record(&mut self) {
3539        let Some(reg) = self.vim.recording_macro.take() else {
3540            return;
3541        };
3542        let keys = std::mem::take(&mut self.vim.recording_keys);
3543        let text = crate::input::encode_macro(&keys);
3544        self.set_named_register_text(reg.to_ascii_lowercase(), text);
3545    }
3546
3547    /// Returns `true` while a `q{reg}` recording is in progress.
3548    /// Hosts use this to show a "recording @r" status indicator and to decide
3549    /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
3550    /// chord.
3551    pub fn is_recording_macro(&self) -> bool {
3552        self.vim.recording_macro.is_some()
3553    }
3554
3555    /// Returns `true` while a macro is being replayed. The app sets this flag
3556    /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
3557    /// re-feed loop so the recorder hook can skip double-capture.
3558    pub fn is_replaying_macro(&self) -> bool {
3559        self.vim.replaying_macro
3560    }
3561
3562    /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
3563    /// prepare for replay, returning the inputs the app should re-feed through
3564    /// `route_chord_key`.
3565    ///
3566    /// Resolves `reg`:
3567    /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
3568    /// - Any other char → lowercase it, read the register, decode.
3569    ///
3570    /// Side-effects:
3571    /// - Sets `vim.last_macro` to the resolved register.
3572    /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
3573    ///   replay. The app calls `end_macro_replay` after the loop finishes.
3574    ///
3575    /// Returns an empty vec (and no side-effects for `'@'`) if the register is
3576    /// unset or empty.
3577    pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
3578        let resolved = if reg == '@' {
3579            match self.vim.last_macro {
3580                Some(r) => r,
3581                None => return vec![],
3582            }
3583        } else {
3584            reg.to_ascii_lowercase()
3585        };
3586        let text = match self.registers.read(resolved) {
3587            Some(slot) if !slot.text.is_empty() => slot.text.clone(),
3588            _ => return vec![],
3589        };
3590        let keys = crate::input::decode_macro(&text);
3591        self.vim.last_macro = Some(resolved);
3592        self.vim.replaying_macro = true;
3593        // Multiply by count (minimum 1).
3594        keys.repeat(count.max(1))
3595    }
3596
3597    /// Clear the `replaying_macro` flag. Called by the app after the
3598    /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
3599    pub fn end_macro_replay(&mut self) {
3600        self.vim.replaying_macro = false;
3601    }
3602
3603    /// Append `input` to the active recording (`recording_keys`) if and only
3604    /// if a recording is in progress AND we are not currently replaying.
3605    /// Called by the app's `route_chord_key` recorder hook so that user
3606    /// keystrokes captured through the app-level chord path are recorded
3607    /// (rather than relying solely on the engine FSM's in-step hook).
3608    pub fn record_input(&mut self, input: crate::input::Input) {
3609        if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
3610            self.vim.recording_keys.push(input);
3611        }
3612    }
3613
3614    // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
3615    //
3616    // Each method is the publicly callable form of one insert-mode action.
3617    // All logic lives in the corresponding `vim::*_bridge` free function;
3618    // these methods are thin delegators so the public surface stays on `Editor`.
3619    //
3620    // Invariants (enforced by the bridge fns):
3621    //   - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
3622    //   - Navigation keys call `break_undo_group_in_insert` when the FSM did.
3623    //   - `push_buffer_cursor_to_textarea` is called after every mutation
3624    //     (currently a no-op, kept for migration hygiene).
3625
3626    /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
3627    /// the cursor instead of inserting; at end-of-line, always appends. With
3628    /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
3629    /// dedent on an otherwise-whitespace line.
3630    ///
3631    /// Callers must ensure the editor is in Insert or Replace mode before
3632    /// calling this method.
3633    pub fn insert_char(&mut self, ch: char) {
3634        let mutated = vim::insert_char_bridge(self, ch);
3635        if mutated {
3636            self.mark_content_dirty();
3637            let (row, _) = self.cursor();
3638            self.vim.widen_insert_row(row);
3639        }
3640    }
3641
3642    /// Insert a newline at the cursor, applying autoindent / smartindent to
3643    /// prefix the new line with the appropriate leading whitespace.
3644    ///
3645    /// Callers must ensure the editor is in Insert mode before calling.
3646    pub fn insert_newline(&mut self) {
3647        let mutated = vim::insert_newline_bridge(self);
3648        if mutated {
3649            self.mark_content_dirty();
3650            let (row, _) = self.cursor();
3651            self.vim.widen_insert_row(row);
3652        }
3653    }
3654
3655    /// Insert a tab character (or spaces up to the next `softtabstop` boundary
3656    /// when `expandtab` is set).
3657    ///
3658    /// Callers must ensure the editor is in Insert mode before calling.
3659    pub fn insert_tab(&mut self) {
3660        let mutated = vim::insert_tab_bridge(self);
3661        if mutated {
3662            self.mark_content_dirty();
3663            let (row, _) = self.cursor();
3664            self.vim.widen_insert_row(row);
3665        }
3666    }
3667
3668    /// Delete the character before the cursor (Backspace). With `softtabstop`
3669    /// active, deletes the entire soft-tab run at an aligned boundary. Joins
3670    /// with the previous line when at column 0.
3671    ///
3672    /// Callers must ensure the editor is in Insert mode before calling.
3673    pub fn insert_backspace(&mut self) {
3674        let mutated = vim::insert_backspace_bridge(self);
3675        if mutated {
3676            self.mark_content_dirty();
3677            let (row, _) = self.cursor();
3678            self.vim.widen_insert_row(row);
3679        }
3680    }
3681
3682    /// Delete the character under the cursor (Delete key). Joins with the
3683    /// next line when at end-of-line.
3684    ///
3685    /// Callers must ensure the editor is in Insert mode before calling.
3686    pub fn insert_delete(&mut self) {
3687        let mutated = vim::insert_delete_bridge(self);
3688        if mutated {
3689            self.mark_content_dirty();
3690            let (row, _) = self.cursor();
3691            self.vim.widen_insert_row(row);
3692        }
3693    }
3694
3695    /// Move the cursor one step in `dir` (arrow key), breaking the undo group
3696    /// per `undo_break_on_motion`.
3697    ///
3698    /// Callers must ensure the editor is in Insert mode before calling.
3699    pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
3700        vim::insert_arrow_bridge(self, dir);
3701        let (row, _) = self.cursor();
3702        self.vim.widen_insert_row(row);
3703    }
3704
3705    /// Move the cursor to the start of the current line (Home key), breaking
3706    /// the undo group.
3707    ///
3708    /// Callers must ensure the editor is in Insert mode before calling.
3709    pub fn insert_home(&mut self) {
3710        vim::insert_home_bridge(self);
3711        let (row, _) = self.cursor();
3712        self.vim.widen_insert_row(row);
3713    }
3714
3715    /// Move the cursor to the end of the current line (End key), breaking the
3716    /// undo group.
3717    ///
3718    /// Callers must ensure the editor is in Insert mode before calling.
3719    pub fn insert_end(&mut self) {
3720        vim::insert_end_bridge(self);
3721        let (row, _) = self.cursor();
3722        self.vim.widen_insert_row(row);
3723    }
3724
3725    /// Scroll up one full viewport height (PageUp), moving the cursor with it.
3726    /// `viewport_h` is the current viewport height in rows; pass
3727    /// `self.viewport_height_value()` if the stored value is current.
3728    ///
3729    /// Callers must ensure the editor is in Insert mode before calling.
3730    pub fn insert_pageup(&mut self, viewport_h: u16) {
3731        vim::insert_pageup_bridge(self, viewport_h);
3732        let (row, _) = self.cursor();
3733        self.vim.widen_insert_row(row);
3734    }
3735
3736    /// Scroll down one full viewport height (PageDown), moving the cursor with
3737    /// it. `viewport_h` is the current viewport height in rows.
3738    ///
3739    /// Callers must ensure the editor is in Insert mode before calling.
3740    pub fn insert_pagedown(&mut self, viewport_h: u16) {
3741        vim::insert_pagedown_bridge(self, viewport_h);
3742        let (row, _) = self.cursor();
3743        self.vim.widen_insert_row(row);
3744    }
3745
3746    /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
3747    /// At column 0, joins with the previous line (vim `b`-motion semantics).
3748    ///
3749    /// Callers must ensure the editor is in Insert mode before calling.
3750    pub fn insert_ctrl_w(&mut self) {
3751        let mutated = vim::insert_ctrl_w_bridge(self);
3752        if mutated {
3753            self.mark_content_dirty();
3754            let (row, _) = self.cursor();
3755            self.vim.widen_insert_row(row);
3756        }
3757    }
3758
3759    /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
3760    /// No-op when already at column 0.
3761    ///
3762    /// Callers must ensure the editor is in Insert mode before calling.
3763    pub fn insert_ctrl_u(&mut self) {
3764        let mutated = vim::insert_ctrl_u_bridge(self);
3765        if mutated {
3766            self.mark_content_dirty();
3767            let (row, _) = self.cursor();
3768            self.vim.widen_insert_row(row);
3769        }
3770    }
3771
3772    /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
3773    /// insert mode. Joins with the previous line when at col 0.
3774    ///
3775    /// Callers must ensure the editor is in Insert mode before calling.
3776    pub fn insert_ctrl_h(&mut self) {
3777        let mutated = vim::insert_ctrl_h_bridge(self);
3778        if mutated {
3779            self.mark_content_dirty();
3780            let (row, _) = self.cursor();
3781            self.vim.widen_insert_row(row);
3782        }
3783    }
3784
3785    /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
3786    /// complete normal-mode command, then return to insert automatically.
3787    ///
3788    /// Callers must ensure the editor is in Insert mode before calling.
3789    pub fn insert_ctrl_o_arm(&mut self) {
3790        vim::insert_ctrl_o_bridge(self);
3791    }
3792
3793    /// Arm the register-paste selector (`Ctrl-R`). The next call to
3794    /// `insert_paste_register(reg)` will insert the register contents.
3795    /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
3796    /// the armed state and paste register `c`.
3797    ///
3798    /// Callers must ensure the editor is in Insert mode before calling.
3799    pub fn insert_ctrl_r_arm(&mut self) {
3800        vim::insert_ctrl_r_bridge(self);
3801    }
3802
3803    /// Indent the current line by one `shiftwidth` and shift the cursor right
3804    /// by the same amount (`Ctrl-T`).
3805    ///
3806    /// Callers must ensure the editor is in Insert mode before calling.
3807    pub fn insert_ctrl_t(&mut self) {
3808        let mutated = vim::insert_ctrl_t_bridge(self);
3809        if mutated {
3810            self.mark_content_dirty();
3811            let (row, _) = self.cursor();
3812            self.vim.widen_insert_row(row);
3813        }
3814    }
3815
3816    /// Outdent the current line by up to one `shiftwidth` and shift the cursor
3817    /// left by the amount stripped (`Ctrl-D`).
3818    ///
3819    /// Callers must ensure the editor is in Insert mode before calling.
3820    pub fn insert_ctrl_d(&mut self) {
3821        let mutated = vim::insert_ctrl_d_bridge(self);
3822        if mutated {
3823            self.mark_content_dirty();
3824            let (row, _) = self.cursor();
3825            self.vim.widen_insert_row(row);
3826        }
3827    }
3828
3829    /// Paste the contents of register `reg` at the cursor (the commit arm of
3830    /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
3831    ///
3832    /// Callers must ensure the editor is in Insert mode before calling.
3833    pub fn insert_paste_register(&mut self, reg: char) {
3834        vim::insert_paste_register_bridge(self, reg);
3835        let (row, _) = self.cursor();
3836        self.vim.widen_insert_row(row);
3837    }
3838
3839    /// Exit insert mode to Normal: finish the insert session, step the cursor
3840    /// one cell left (vim convention on Esc), record the `gi` target position,
3841    /// and update the sticky column.
3842    ///
3843    /// Callers must ensure the editor is in Insert mode before calling.
3844    pub fn leave_insert_to_normal(&mut self) {
3845        vim::leave_insert_to_normal_bridge(self);
3846    }
3847
3848    // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
3849    //
3850    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
3851    // `vim.rs` following the same pattern as Phase 6.1. The FSM's
3852    // `handle_normal_only` now calls the same bridges so both paths are
3853    // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
3854
3855    /// `i` — transition to Insert mode at the current cursor position.
3856    /// `count` is stored in the insert session and replayed by dot-repeat
3857    /// as a repeat count on the inserted text.
3858    pub fn enter_insert_i(&mut self, count: usize) {
3859        vim::enter_insert_i_bridge(self, count);
3860    }
3861
3862    /// `I` — move to the first non-blank character on the line, then
3863    /// transition to Insert mode. `count` is stored for dot-repeat.
3864    pub fn enter_insert_shift_i(&mut self, count: usize) {
3865        vim::enter_insert_shift_i_bridge(self, count);
3866    }
3867
3868    /// `a` — advance the cursor one cell past the current position, then
3869    /// transition to Insert mode (append). `count` is stored for dot-repeat.
3870    pub fn enter_insert_a(&mut self, count: usize) {
3871        vim::enter_insert_a_bridge(self, count);
3872    }
3873
3874    /// `A` — move the cursor to the end of the line, then transition to
3875    /// Insert mode (append at end). `count` is stored for dot-repeat.
3876    pub fn enter_insert_shift_a(&mut self, count: usize) {
3877        vim::enter_insert_shift_a_bridge(self, count);
3878    }
3879
3880    /// `o` — open a new line below the current line with smart-indent, then
3881    /// transition to Insert mode. `count` is stored for dot-repeat replay.
3882    pub fn open_line_below(&mut self, count: usize) {
3883        vim::open_line_below_bridge(self, count);
3884    }
3885
3886    /// `O` — open a new line above the current line with smart-indent, then
3887    /// transition to Insert mode. `count` is stored for dot-repeat replay.
3888    pub fn open_line_above(&mut self, count: usize) {
3889        vim::open_line_above_bridge(self, count);
3890    }
3891
3892    /// `R` — enter Replace mode: subsequent typed characters overstrike the
3893    /// cell under the cursor rather than inserting. `count` is for replay.
3894    pub fn enter_replace_mode(&mut self, count: usize) {
3895        vim::enter_replace_mode_bridge(self, count);
3896    }
3897
3898    /// `x` — delete `count` characters forward from the cursor and write them
3899    /// to the unnamed register. No-op on an empty line. Records for `.`.
3900    pub fn delete_char_forward(&mut self, count: usize) {
3901        vim::delete_char_forward_bridge(self, count);
3902    }
3903
3904    /// `X` — delete `count` characters backward from the cursor and write
3905    /// them to the unnamed register. No-op at column 0. Records for `.`.
3906    pub fn delete_char_backward(&mut self, count: usize) {
3907        vim::delete_char_backward_bridge(self, count);
3908    }
3909
3910    /// `s` — substitute `count` characters: delete them (writing to the
3911    /// unnamed register) then enter Insert mode. Equivalent to `cl`.
3912    /// Records as `OpMotion { Change, Right }` for dot-repeat.
3913    pub fn substitute_char(&mut self, count: usize) {
3914        vim::substitute_char_bridge(self, count);
3915    }
3916
3917    /// `S` — substitute the current line: wipe its contents (writing to the
3918    /// unnamed register) then enter Insert mode. Equivalent to `cc`.
3919    /// Records as `LineOp { Change }` for dot-repeat.
3920    pub fn substitute_line(&mut self, count: usize) {
3921        vim::substitute_line_bridge(self, count);
3922    }
3923
3924    /// `D` — delete from the cursor to end-of-line, writing to the unnamed
3925    /// register. The cursor parks on the new last character. Records for `.`.
3926    pub fn delete_to_eol(&mut self) {
3927        vim::delete_to_eol_bridge(self);
3928    }
3929
3930    /// `C` — change from the cursor to end-of-line: delete to EOL then enter
3931    /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
3932    /// (the insert session records `DeleteToEol` on exit, like `c` motions).
3933    pub fn change_to_eol(&mut self) {
3934        vim::change_to_eol_bridge(self);
3935    }
3936
3937    /// `Y` — yank from the cursor to end-of-line into the unnamed register.
3938    /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
3939    pub fn yank_to_eol(&mut self, count: usize) {
3940        vim::yank_to_eol_bridge(self, count);
3941    }
3942
3943    /// `J` — join `count` lines (default 2) onto the current line, inserting
3944    /// a single space between each non-empty pair. Records for dot-repeat.
3945    pub fn join_line(&mut self, count: usize) {
3946        vim::join_line_bridge(self, count);
3947    }
3948
3949    /// `~` — toggle the case of `count` characters from the cursor, advancing
3950    /// right after each toggle. Records `ToggleCase` for dot-repeat.
3951    pub fn toggle_case_at_cursor(&mut self, count: usize) {
3952        vim::toggle_case_at_cursor_bridge(self, count);
3953    }
3954
3955    /// `p` — paste the unnamed register (or the register selected via `"r`)
3956    /// after the cursor. Linewise content opens a new line below; charwise
3957    /// content is inserted inline. Records `Paste { before: false }` for `.`.
3958    pub fn paste_after(&mut self, count: usize) {
3959        vim::paste_after_bridge(self, count);
3960    }
3961
3962    /// `P` — paste the unnamed register (or the `"r` register) before the
3963    /// cursor. Linewise content opens a new line above; charwise is inline.
3964    /// Records `Paste { before: true }` for dot-repeat.
3965    pub fn paste_before(&mut self, count: usize) {
3966        vim::paste_before_bridge(self, count);
3967    }
3968
3969    /// `<C-o>` — jump back `count` entries in the jumplist, saving the
3970    /// current position on the forward stack so `<C-i>` can return.
3971    pub fn jump_back(&mut self, count: usize) {
3972        vim::jump_back_bridge(self, count);
3973    }
3974
3975    /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
3976    /// saving the current position on the backward stack.
3977    pub fn jump_forward(&mut self, count: usize) {
3978        vim::jump_forward_bridge(self, count);
3979    }
3980
3981    /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
3982    /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
3983    /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
3984    pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
3985        vim::scroll_full_page_bridge(self, dir, count);
3986    }
3987
3988    /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
3989    /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
3990    pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
3991        vim::scroll_half_page_bridge(self, dir, count);
3992    }
3993
3994    /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
3995    /// the cursor (cursor is clamped to the new visible region if necessary).
3996    /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
3997    pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
3998        vim::scroll_line_bridge(self, dir, count);
3999    }
4000
4001    /// `n` — repeat the last `/` or `?` search `count` times in its original
4002    /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4003    pub fn search_repeat(&mut self, forward: bool, count: usize) {
4004        vim::search_repeat_bridge(self, forward, count);
4005    }
4006
4007    /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4008    /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4009    /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4010    pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4011        vim::word_search_bridge(self, forward, whole_word, count);
4012    }
4013
4014    // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4015    //
4016    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4017    // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4018    // and these wrappers write `current_mode` so `vim_mode()` returns correct
4019    // values regardless of which path performed the transition.
4020    // See kryptic-sh/hjkl#89 for the full promotion plan.
4021
4022    /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4023    /// at the current cursor position.
4024    pub fn enter_visual_char(&mut self) {
4025        vim::enter_visual_char_bridge(self);
4026    }
4027
4028    /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4029    /// line. Motions extend the selection by whole lines.
4030    pub fn enter_visual_line(&mut self) {
4031        vim::enter_visual_line_bridge(self);
4032    }
4033
4034    /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4035    /// rectangle whose corners are the anchor and the live cursor.
4036    pub fn enter_visual_block(&mut self) {
4037        vim::enter_visual_block_bridge(self);
4038    }
4039
4040    /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4041    /// for `gv` re-entry, then return to Normal mode.
4042    pub fn exit_visual_to_normal(&mut self) {
4043        vim::exit_visual_to_normal_bridge(self);
4044    }
4045
4046    /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4047    /// so the user can extend the other end of the selection. Does NOT
4048    /// mutate the selection range; only the active endpoint changes.
4049    pub fn visual_o_toggle(&mut self) {
4050        vim::visual_o_toggle_bridge(self);
4051    }
4052
4053    /// `gv` — restore the last visual selection (mode + anchor + cursor
4054    /// position). No-op when no visual selection has been exited yet.
4055    pub fn reenter_last_visual(&mut self) {
4056        vim::reenter_last_visual_bridge(self);
4057    }
4058
4059    /// Direct mode-transition entry point. Sets both the internal FSM mode
4060    /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4061    ///
4062    /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4063    /// …) which also set up required bookkeeping (anchors, sessions, …).
4064    /// Use `set_mode` only when you need a raw mode flip without side-effects.
4065    pub fn set_mode(&mut self, mode: VimMode) {
4066        vim::set_mode_bridge(self, mode);
4067    }
4068}
4069
4070// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4071//
4072// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4073// `Editor` accessors and mutators defined in this block. Each method gets a
4074// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4075// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4076
4077/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4078///
4079/// Treat as opaque — construct by calling `begin_step` and pass the
4080/// returned value directly into `end_step` without modification.
4081/// The fields capture per-step pre-dispatch state that the epilogue
4082/// needs to run its invariants correctly.
4083pub struct StepBookkeeping {
4084    /// True when the pending chord before this step was a macro-chord
4085    /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4086    /// keys so that only the *payload* keys enter `recording_keys`.
4087    pub pending_was_macro_chord: bool,
4088    /// True when the mode was Insert *before* the FSM body ran. Used by
4089    /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4090    /// back into Insert.
4091    pub was_insert: bool,
4092    /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4093    /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4094    /// store `last_visual` for `gv`.
4095    pub pre_visual_snapshot: Option<vim::LastVisual>,
4096}
4097
4098impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4099    // ── Pending chord ─────────────────────────────────────────────────────────
4100
4101    /// Return a clone of the current pending chord state.
4102    pub fn pending(&self) -> vim::Pending {
4103        self.vim.pending.clone()
4104    }
4105
4106    /// Overwrite the pending chord state.
4107    pub fn set_pending(&mut self, p: vim::Pending) {
4108        self.vim.pending = p;
4109    }
4110
4111    /// Atomically take the pending chord, replacing it with `Pending::None`.
4112    pub fn take_pending(&mut self) -> vim::Pending {
4113        std::mem::take(&mut self.vim.pending)
4114    }
4115
4116    // ── Count prefix ──────────────────────────────────────────────────────────
4117
4118    /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4119    pub fn count(&self) -> usize {
4120        self.vim.count
4121    }
4122
4123    /// Overwrite the digit-prefix count directly.
4124    pub fn set_count(&mut self, c: usize) {
4125        self.vim.count = c;
4126    }
4127
4128    /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4129    pub fn accumulate_count_digit(&mut self, digit: usize) {
4130        self.vim.count = self.vim.count.saturating_mul(10) + digit;
4131    }
4132
4133    /// Reset the count prefix to zero (no pending count).
4134    pub fn reset_count(&mut self) {
4135        self.vim.count = 0;
4136    }
4137
4138    /// Consume the count and return it; resets to zero. Returns `1` when no
4139    /// prefix was typed (mirrors `take_count` in vim.rs).
4140    pub fn take_count(&mut self) -> usize {
4141        if self.vim.count > 0 {
4142            let n = self.vim.count;
4143            self.vim.count = 0;
4144            n
4145        } else {
4146            1
4147        }
4148    }
4149
4150    // ── Internal FSM mode ─────────────────────────────────────────────────────
4151
4152    /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4153    pub fn fsm_mode(&self) -> vim::Mode {
4154        self.vim.mode
4155    }
4156
4157    /// Overwrite the FSM-internal mode without side-effects. Prefer the
4158    /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4159    pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4160        self.vim.mode = m;
4161        self.vim.current_mode = self.vim.public_mode();
4162    }
4163
4164    // ── Replaying flag ────────────────────────────────────────────────────────
4165
4166    /// `true` while the `.` dot-repeat replay is running.
4167    pub fn is_replaying(&self) -> bool {
4168        self.vim.replaying
4169    }
4170
4171    /// Set or clear the dot-replay flag.
4172    pub fn set_replaying(&mut self, v: bool) {
4173        self.vim.replaying = v;
4174    }
4175
4176    // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4177
4178    /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4179    /// to Insert after the next complete command.
4180    pub fn is_one_shot_normal(&self) -> bool {
4181        self.vim.one_shot_normal
4182    }
4183
4184    /// Set or clear the Ctrl-o one-shot-normal flag.
4185    pub fn set_one_shot_normal(&mut self, v: bool) {
4186        self.vim.one_shot_normal = v;
4187    }
4188
4189    // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4190
4191    /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4192    /// `None` before any find command was executed.
4193    pub fn last_find(&self) -> Option<(char, bool, bool)> {
4194        self.vim.last_find
4195    }
4196
4197    /// Overwrite the stored last-find target.
4198    pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4199        self.vim.last_find = target;
4200    }
4201
4202    // ── Last change (dot-repeat payload) ─────────────────────────────────────
4203
4204    /// Return a clone of the last recorded mutating change, or `None` before
4205    /// any change has been made.
4206    pub fn last_change(&self) -> Option<vim::LastChange> {
4207        self.vim.last_change.clone()
4208    }
4209
4210    /// Overwrite the stored last-change record.
4211    pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4212        self.vim.last_change = lc;
4213    }
4214
4215    /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4216    /// field after the insert session completes).
4217    pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4218        self.vim.last_change.as_mut()
4219    }
4220
4221    // ── Insert session ────────────────────────────────────────────────────────
4222
4223    /// Borrow the active insert session, or `None` when not in Insert mode.
4224    pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4225        self.vim.insert_session.as_ref()
4226    }
4227
4228    /// Borrow the active insert session mutably.
4229    pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4230        self.vim.insert_session.as_mut()
4231    }
4232
4233    /// Atomically take the insert session out, leaving `None`.
4234    pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4235        self.vim.insert_session.take()
4236    }
4237
4238    /// Install a new insert session, replacing any existing one.
4239    pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4240        self.vim.insert_session = s;
4241    }
4242
4243    // ── Visual anchors ────────────────────────────────────────────────────────
4244
4245    /// Return the charwise Visual-mode anchor `(row, col)`.
4246    pub fn visual_anchor(&self) -> (usize, usize) {
4247        self.vim.visual_anchor
4248    }
4249
4250    /// Overwrite the charwise Visual-mode anchor.
4251    pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4252        self.vim.visual_anchor = anchor;
4253    }
4254
4255    /// Return the VisualLine anchor row.
4256    pub fn visual_line_anchor(&self) -> usize {
4257        self.vim.visual_line_anchor
4258    }
4259
4260    /// Overwrite the VisualLine anchor row.
4261    pub fn set_visual_line_anchor(&mut self, row: usize) {
4262        self.vim.visual_line_anchor = row;
4263    }
4264
4265    /// Return the VisualBlock anchor `(row, col)`.
4266    pub fn block_anchor(&self) -> (usize, usize) {
4267        self.vim.block_anchor
4268    }
4269
4270    /// Overwrite the VisualBlock anchor.
4271    pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4272        self.vim.block_anchor = anchor;
4273    }
4274
4275    /// Return the VisualBlock virtual column used to survive j/k row clamping.
4276    pub fn block_vcol(&self) -> usize {
4277        self.vim.block_vcol
4278    }
4279
4280    /// Overwrite the VisualBlock virtual column.
4281    pub fn set_block_vcol(&mut self, vcol: usize) {
4282        self.vim.block_vcol = vcol;
4283    }
4284
4285    // ── Yank linewise flag ────────────────────────────────────────────────────
4286
4287    /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
4288    pub fn yank_linewise(&self) -> bool {
4289        self.vim.yank_linewise
4290    }
4291
4292    /// Set or clear the linewise-yank flag.
4293    pub fn set_yank_linewise(&mut self, v: bool) {
4294        self.vim.yank_linewise = v;
4295    }
4296
4297    // ── Pending register selector ─────────────────────────────────────────────
4298    // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
4299    // Only the mutators are new here.
4300
4301    /// Overwrite the pending register selector (Phase 6.6b mutator companion to
4302    /// the existing `pending_register()` getter).
4303    pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
4304        self.vim.pending_register = reg;
4305    }
4306
4307    /// Atomically take the pending register, returning `None` afterward.
4308    pub fn take_pending_register_raw(&mut self) -> Option<char> {
4309        self.vim.pending_register.take()
4310    }
4311
4312    // ── Macro recording ───────────────────────────────────────────────────────
4313
4314    /// Return the register currently being recorded into, or `None`.
4315    pub fn recording_macro(&self) -> Option<char> {
4316        self.vim.recording_macro
4317    }
4318
4319    /// Overwrite the recording-macro target register.
4320    pub fn set_recording_macro(&mut self, reg: Option<char>) {
4321        self.vim.recording_macro = reg;
4322    }
4323
4324    /// Append one input to the in-progress macro recording buffer.
4325    pub fn push_recording_key(&mut self, input: crate::input::Input) {
4326        self.vim.recording_keys.push(input);
4327    }
4328
4329    /// Atomically take the recorded key sequence, leaving an empty vec.
4330    pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
4331        std::mem::take(&mut self.vim.recording_keys)
4332    }
4333
4334    /// Overwrite the recording-keys buffer (e.g. to seed from a register).
4335    pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
4336        self.vim.recording_keys = keys;
4337    }
4338
4339    // ── Macro replay flag ─────────────────────────────────────────────────────
4340
4341    /// `true` while `@reg` macro replay is running (suppresses re-recording).
4342    pub fn is_replaying_macro_raw(&self) -> bool {
4343        self.vim.replaying_macro
4344    }
4345
4346    /// Set or clear the macro-replay-in-progress flag.
4347    pub fn set_replaying_macro_raw(&mut self, v: bool) {
4348        self.vim.replaying_macro = v;
4349    }
4350
4351    // ── Last macro register ───────────────────────────────────────────────────
4352
4353    /// Return the register of the most recently played macro (`@@` source).
4354    pub fn last_macro(&self) -> Option<char> {
4355        self.vim.last_macro
4356    }
4357
4358    /// Overwrite the last-played-macro register.
4359    pub fn set_last_macro(&mut self, reg: Option<char>) {
4360        self.vim.last_macro = reg;
4361    }
4362
4363    // ── Last insert position ──────────────────────────────────────────────────
4364
4365    /// Return the cursor position when Insert mode was last exited (for `gi`).
4366    pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
4367        self.vim.last_insert_pos
4368    }
4369
4370    /// Overwrite the stored last-insert position.
4371    pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
4372        self.vim.last_insert_pos = pos;
4373    }
4374
4375    // ── Last visual selection ─────────────────────────────────────────────────
4376
4377    /// Return the saved visual selection snapshot for `gv`, or `None`.
4378    pub fn last_visual(&self) -> Option<vim::LastVisual> {
4379        self.vim.last_visual
4380    }
4381
4382    /// Overwrite the saved visual selection snapshot.
4383    pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
4384        self.vim.last_visual = snap;
4385    }
4386
4387    // ── Viewport-pinned flag ──────────────────────────────────────────────────
4388
4389    /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
4390    /// the end-of-step scrolloff pass).
4391    pub fn viewport_pinned(&self) -> bool {
4392        self.vim.viewport_pinned
4393    }
4394
4395    /// Set or clear the viewport-pinned flag.
4396    pub fn set_viewport_pinned(&mut self, v: bool) {
4397        self.vim.viewport_pinned = v;
4398    }
4399
4400    // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
4401
4402    /// `true` while waiting for the register-name key after `Ctrl-R` in
4403    /// Insert mode.
4404    pub fn insert_pending_register(&self) -> bool {
4405        self.vim.insert_pending_register
4406    }
4407
4408    /// Set or clear the `Ctrl-R` register-wait flag.
4409    pub fn set_insert_pending_register(&mut self, v: bool) {
4410        self.vim.insert_pending_register = v;
4411    }
4412
4413    // ── Change-mark start ─────────────────────────────────────────────────────
4414
4415    /// Return the stashed `[` mark start for a Change operation, or `None`.
4416    pub fn change_mark_start(&self) -> Option<(usize, usize)> {
4417        self.vim.change_mark_start
4418    }
4419
4420    /// Atomically take the change-mark start, leaving `None`.
4421    pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
4422        self.vim.change_mark_start.take()
4423    }
4424
4425    /// Overwrite the change-mark start.
4426    pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
4427        self.vim.change_mark_start = pos;
4428    }
4429
4430    // ── Timeout tracking ──────────────────────────────────────────────────────
4431
4432    /// Return the wall-clock `Instant` of the last keystroke.
4433    pub fn last_input_at(&self) -> Option<std::time::Instant> {
4434        self.vim.last_input_at
4435    }
4436
4437    /// Overwrite the wall-clock last-input timestamp.
4438    pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
4439        self.vim.last_input_at = t;
4440    }
4441
4442    /// Return the `Host::now()` duration at the last keystroke.
4443    pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
4444        self.vim.last_input_host_at
4445    }
4446
4447    /// Overwrite the host-clock last-input timestamp.
4448    pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
4449        self.vim.last_input_host_at = d;
4450    }
4451
4452    // ── Search prompt ──────────────────────────────────────────────────────────
4453
4454    /// Borrow the live search prompt, or `None` when not in search-prompt mode.
4455    pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
4456        self.vim.search_prompt.as_ref()
4457    }
4458
4459    /// Borrow the live search prompt mutably.
4460    pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
4461        self.vim.search_prompt.as_mut()
4462    }
4463
4464    /// Atomically take the search prompt, leaving `None`.
4465    pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
4466        self.vim.search_prompt.take()
4467    }
4468
4469    /// Install a new search prompt (entering search-prompt mode).
4470    pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
4471        self.vim.search_prompt = prompt;
4472    }
4473
4474    // ── Last search pattern / direction ───────────────────────────────────────
4475    // Note: `last_search_forward()` getter already exists at line ~1909.
4476    // `set_last_search()` combined mutator exists at line ~1918.
4477    // Only new / complementary accessors are added here.
4478
4479    /// Return the most recently committed search pattern, or `None`.
4480    pub fn last_search_pattern(&self) -> Option<&str> {
4481        self.vim.last_search.as_deref()
4482    }
4483
4484    /// Overwrite the stored last-search pattern without changing direction
4485    /// (use the existing `set_last_search` for the combined update).
4486    pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
4487        self.vim.last_search = pattern;
4488    }
4489
4490    /// Overwrite only the last-search direction flag.
4491    pub fn set_last_search_forward_only(&mut self, forward: bool) {
4492        self.vim.last_search_forward = forward;
4493    }
4494
4495    // ── Search history ────────────────────────────────────────────────────────
4496
4497    /// Borrow the committed search-pattern history (oldest first).
4498    pub fn search_history(&self) -> &[String] {
4499        &self.vim.search_history
4500    }
4501
4502    /// Borrow the search history mutably (e.g. to push a new entry).
4503    pub fn search_history_mut(&mut self) -> &mut Vec<String> {
4504        &mut self.vim.search_history
4505    }
4506
4507    /// Return the current search-history navigation cursor index.
4508    pub fn search_history_cursor(&self) -> Option<usize> {
4509        self.vim.search_history_cursor
4510    }
4511
4512    /// Overwrite the search-history navigation cursor.
4513    pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
4514        self.vim.search_history_cursor = idx;
4515    }
4516
4517    // ── Jump lists ────────────────────────────────────────────────────────────
4518
4519    /// Borrow the back half of the jump list (entries Ctrl-o pops from).
4520    pub fn jump_back_list(&self) -> &[(usize, usize)] {
4521        &self.vim.jump_back
4522    }
4523
4524    /// Borrow the back jump list mutably (push / pop).
4525    pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4526        &mut self.vim.jump_back
4527    }
4528
4529    /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
4530    pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
4531        &self.vim.jump_fwd
4532    }
4533
4534    /// Borrow the forward jump list mutably (push / pop / clear).
4535    pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4536        &mut self.vim.jump_fwd
4537    }
4538
4539    // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
4540    //
4541    // `push_search_pattern`, `push_jump`, `record_search_history`, and
4542    // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
4543    // search-prompt and normal-mode FSM can call them via the public API.
4544
4545    /// Compile `pattern` into a regex and install it as the active search
4546    /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
4547    /// invalid pattern clears the highlight without raising an error.
4548    pub fn push_search_pattern(&mut self, pattern: &str) {
4549        let compiled = if pattern.is_empty() {
4550            None
4551        } else {
4552            let case_insensitive = self.settings().ignore_case
4553                && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
4554            let effective: std::borrow::Cow<'_, str> = if case_insensitive {
4555                std::borrow::Cow::Owned(format!("(?i){pattern}"))
4556            } else {
4557                std::borrow::Cow::Borrowed(pattern)
4558            };
4559            regex::Regex::new(&effective).ok()
4560        };
4561        let wrap = self.settings().wrapscan;
4562        self.set_search_pattern(compiled);
4563        self.search_state_mut().wrap_around = wrap;
4564    }
4565
4566    /// Record a pre-jump cursor position onto the back jumplist. Called
4567    /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
4568    /// committed `/` or `?`, …). Branching off the history clears the
4569    /// forward half, matching vim's "redo-is-lost" semantics.
4570    pub fn push_jump(&mut self, from: (usize, usize)) {
4571        self.vim.jump_back.push(from);
4572        if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
4573            self.vim.jump_back.remove(0);
4574        }
4575        self.vim.jump_fwd.clear();
4576    }
4577
4578    /// Push `pattern` onto the committed search history. Skips if the
4579    /// most recent entry already matches (consecutive dedupe) and trims
4580    /// the oldest entries beyond the history cap.
4581    pub fn record_search_history(&mut self, pattern: &str) {
4582        if pattern.is_empty() {
4583            return;
4584        }
4585        if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
4586            return;
4587        }
4588        self.vim.search_history.push(pattern.to_string());
4589        let len = self.vim.search_history.len();
4590        if len > vim::SEARCH_HISTORY_MAX {
4591            self.vim
4592                .search_history
4593                .drain(0..len - vim::SEARCH_HISTORY_MAX);
4594        }
4595    }
4596
4597    /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
4598    /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
4599    /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
4600    /// active search prompt.
4601    pub fn walk_search_history(&mut self, dir: isize) {
4602        if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
4603            return;
4604        }
4605        let len = self.vim.search_history.len();
4606        let next_idx = match (self.vim.search_history_cursor, dir) {
4607            (None, -1) => Some(len - 1),
4608            (None, 1) => return,
4609            (Some(i), -1) => i.checked_sub(1),
4610            (Some(i), 1) if i + 1 < len => Some(i + 1),
4611            _ => None,
4612        };
4613        let Some(idx) = next_idx else {
4614            return;
4615        };
4616        self.vim.search_history_cursor = Some(idx);
4617        let text = self.vim.search_history[idx].clone();
4618        if let Some(prompt) = self.vim.search_prompt.as_mut() {
4619            prompt.cursor = text.chars().count();
4620            prompt.text = text.clone();
4621        }
4622        self.push_search_pattern(&text);
4623    }
4624
4625    // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
4626    //
4627    // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
4628    // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
4629
4630    /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
4631    ///
4632    /// Call this at the start of every step; pass the returned
4633    /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
4634    ///
4635    /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
4636    /// Returns `Err(consumed)` when the prelude itself handled the input
4637    /// (macro-stop chord); in that case skip the FSM body and do NOT call
4638    /// `end_step` — the macro-stop path is a true short-circuit with no
4639    /// epilogue needed.
4640    ///
4641    /// This method does NOT handle the search-prompt intercept — callers
4642    /// must check `search_prompt_state().is_some()` before calling `begin_step`
4643    /// and dispatch to the search-prompt FSM body directly.
4644    pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
4645        use crate::input::Key;
4646        use vim::{Mode, Pending};
4647        // ── Timestamps ───────────────────────────────────────────────────────
4648        // Phase 7f: sync buffer before motion handlers see it.
4649        self.sync_buffer_content_from_textarea();
4650        // `:set timeoutlen` chord-timeout handling.
4651        let now = std::time::Instant::now();
4652        let host_now = self.host.now();
4653        let timed_out = match self.vim.last_input_host_at {
4654            Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
4655            None => false,
4656        };
4657        if timed_out {
4658            let chord_in_flight = !matches!(self.vim.pending, Pending::None)
4659                || self.vim.count != 0
4660                || self.vim.pending_register.is_some()
4661                || self.vim.insert_pending_register;
4662            if chord_in_flight {
4663                self.vim.clear_pending_prefix();
4664            }
4665        }
4666        self.vim.last_input_at = Some(now);
4667        self.vim.last_input_host_at = Some(host_now);
4668        // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
4669        if self.vim.recording_macro.is_some()
4670            && !self.vim.replaying_macro
4671            && matches!(self.vim.pending, Pending::None)
4672            && self.vim.mode != Mode::Insert
4673            && input.key == Key::Char('q')
4674            && !input.ctrl
4675            && !input.alt
4676        {
4677            let reg = self.vim.recording_macro.take().unwrap();
4678            let keys = std::mem::take(&mut self.vim.recording_keys);
4679            let text = crate::input::encode_macro(&keys);
4680            self.set_named_register_text(reg.to_ascii_lowercase(), text);
4681            return Err(true);
4682        }
4683        // ── Snapshots for epilogue ────────────────────────────────────────────
4684        let pending_was_macro_chord = matches!(
4685            self.vim.pending,
4686            Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
4687        );
4688        let was_insert = self.vim.mode == Mode::Insert;
4689        let pre_visual_snapshot = match self.vim.mode {
4690            Mode::Visual => Some(vim::LastVisual {
4691                mode: Mode::Visual,
4692                anchor: self.vim.visual_anchor,
4693                cursor: self.cursor(),
4694                block_vcol: 0,
4695            }),
4696            Mode::VisualLine => Some(vim::LastVisual {
4697                mode: Mode::VisualLine,
4698                anchor: (self.vim.visual_line_anchor, 0),
4699                cursor: self.cursor(),
4700                block_vcol: 0,
4701            }),
4702            Mode::VisualBlock => Some(vim::LastVisual {
4703                mode: Mode::VisualBlock,
4704                anchor: self.vim.block_anchor,
4705                cursor: self.cursor(),
4706                block_vcol: self.vim.block_vcol,
4707            }),
4708            _ => None,
4709        };
4710        Ok(StepBookkeeping {
4711            pending_was_macro_chord,
4712            was_insert,
4713            pre_visual_snapshot,
4714        })
4715    }
4716
4717    /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
4718    ///
4719    /// `input` is the same input that was passed to `begin_step`.
4720    /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
4721    /// `consumed` is the return value of the FSM body; this method returns
4722    /// it after running all epilogue invariants.
4723    ///
4724    /// Must NOT be called when `begin_step` returned `Err(...)`.
4725    pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
4726        use crate::input::Key;
4727        use vim::{Mode, Pending};
4728        let StepBookkeeping {
4729            pending_was_macro_chord,
4730            was_insert,
4731            pre_visual_snapshot,
4732        } = bk;
4733        // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
4734        if let Some(snap) = pre_visual_snapshot
4735            && !matches!(
4736                self.vim.mode,
4737                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
4738            )
4739        {
4740            let (lo, hi) = match snap.mode {
4741                Mode::Visual => {
4742                    if snap.anchor <= snap.cursor {
4743                        (snap.anchor, snap.cursor)
4744                    } else {
4745                        (snap.cursor, snap.anchor)
4746                    }
4747                }
4748                Mode::VisualLine => {
4749                    let r_lo = snap.anchor.0.min(snap.cursor.0);
4750                    let r_hi = snap.anchor.0.max(snap.cursor.0);
4751                    let last_col = self
4752                        .buffer()
4753                        .lines()
4754                        .get(r_hi)
4755                        .map(|l| l.chars().count().saturating_sub(1))
4756                        .unwrap_or(0);
4757                    ((r_lo, 0), (r_hi, last_col))
4758                }
4759                Mode::VisualBlock => {
4760                    let (r1, c1) = snap.anchor;
4761                    let (r2, c2) = snap.cursor;
4762                    ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
4763                }
4764                _ => {
4765                    if snap.anchor <= snap.cursor {
4766                        (snap.anchor, snap.cursor)
4767                    } else {
4768                        (snap.cursor, snap.anchor)
4769                    }
4770                }
4771            };
4772            self.set_mark('<', lo);
4773            self.set_mark('>', hi);
4774            self.vim.last_visual = Some(snap);
4775        }
4776        // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
4777        if !was_insert
4778            && self.vim.one_shot_normal
4779            && self.vim.mode == Mode::Normal
4780            && matches!(self.vim.pending, Pending::None)
4781        {
4782            self.vim.one_shot_normal = false;
4783            self.vim.mode = Mode::Insert;
4784        }
4785        // ── Content + viewport sync ───────────────────────────────────────────
4786        self.sync_buffer_content_from_textarea();
4787        if !self.vim.viewport_pinned {
4788            self.ensure_cursor_in_scrolloff();
4789        }
4790        self.vim.viewport_pinned = false;
4791        // ── Recorder hook ─────────────────────────────────────────────────────
4792        if self.vim.recording_macro.is_some()
4793            && !self.vim.replaying_macro
4794            && input.key != Key::Char('q')
4795            && !pending_was_macro_chord
4796        {
4797            self.vim.recording_keys.push(input);
4798        }
4799        // ── Phase 6.3: current_mode sync ─────────────────────────────────────
4800        self.vim.current_mode = self.vim.public_mode();
4801        consumed
4802    }
4803
4804    // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
4805
4806    /// `true` when the editor is in any visual mode (Visual / VisualLine /
4807    /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
4808    pub fn is_visual(&self) -> bool {
4809        matches!(
4810            self.vim.mode,
4811            vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
4812        )
4813    }
4814
4815    /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
4816    /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
4817    /// virtual column, which survives j/k clamping to shorter rows).
4818    ///
4819    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
4820    /// extents needed for VisualBlock `I` / `A` / `r` without accessing
4821    /// engine-private helpers.
4822    pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
4823        let (ar, ac) = self.vim.block_anchor;
4824        let (cr, _) = self.cursor();
4825        let cc = self.vim.block_vcol;
4826        let top = ar.min(cr);
4827        let bot = ar.max(cr);
4828        let left = ac.min(cc);
4829        let right = ac.max(cc);
4830        (top, bot, left, right)
4831    }
4832
4833    /// Return the character count (code-point count) of line `row`, or `0`
4834    /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
4835    /// I / A column computations.
4836    pub fn line_char_count(&self, row: usize) -> usize {
4837        buf_line_chars(&self.buffer, row)
4838    }
4839
4840    /// Apply operator over `motion` with `count` repetitions. The full
4841    /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
4842    ///
4843    /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
4844    /// relocated `handle_after_op` can call it directly with a parsed `Motion`
4845    /// without re-entering the engine FSM.
4846    pub fn apply_op_with_motion_direct(
4847        &mut self,
4848        op: crate::vim::Operator,
4849        motion: &crate::vim::Motion,
4850        count: usize,
4851    ) {
4852        vim::apply_op_with_motion(self, op, motion, count);
4853    }
4854
4855    /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
4856    /// `delta = 1` increments; `delta = -1` decrements; larger deltas
4857    /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
4858    /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
4859    pub fn adjust_number(&mut self, delta: i64) {
4860        vim::adjust_number(self, delta);
4861    }
4862
4863    /// Open the `/` or `?` search prompt. `forward = true` for `/`,
4864    /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
4865    /// dispatch `/` and `?` without re-entering the engine FSM.
4866    pub fn enter_search(&mut self, forward: bool) {
4867        vim::enter_search(self, forward);
4868    }
4869
4870    /// Enter Insert mode at the left edge of a VisualBlock selection for
4871    /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
4872    /// then begins an insert session with `InsertReason::BlockEdge`.
4873    ///
4874    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
4875    /// VisualBlock `I` command without accessing engine-private helpers.
4876    pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
4877        self.jump_cursor(top, col);
4878        self.vim.mode = vim::Mode::Normal;
4879        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
4880    }
4881
4882    /// Enter Insert mode at the right edge of a VisualBlock selection for
4883    /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
4884    /// then begins an insert session with `InsertReason::BlockEdge`.
4885    ///
4886    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
4887    /// VisualBlock `A` command without accessing engine-private helpers.
4888    pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
4889        self.jump_cursor(top, col);
4890        self.vim.mode = vim::Mode::Normal;
4891        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
4892    }
4893
4894    /// Execute a motion (cursor movement), push to the jumplist for big jumps,
4895    /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
4896    /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
4897    pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
4898        vim::execute_motion(self, motion, count);
4899    }
4900
4901    /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
4902    /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
4903    /// non-h/l motions leave it alone so the intended column survives clamping
4904    /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
4905    pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
4906        vim::update_block_vcol(self, motion);
4907    }
4908
4909    /// Apply `op` over the current visual selection (char-wise, linewise, or
4910    /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
4911    /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
4912    pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
4913        vim::apply_visual_operator(self, op);
4914    }
4915
4916    /// Replace each character cell in the current VisualBlock selection with
4917    /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
4918    /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
4919    pub fn replace_block_char(&mut self, ch: char) {
4920        vim::block_replace(self, ch);
4921    }
4922
4923    /// Extend the current visual selection to cover the text object identified
4924    /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
4925    /// via `text_object_range`, then updates the visual anchor and cursor.
4926    ///
4927    /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
4928    /// in `hjkl-vim::normal::handle_visual_text_obj`.
4929    pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
4930        use crate::vim::{Mode, TextObject};
4931        let obj = match ch {
4932            'w' => TextObject::Word { big: false },
4933            'W' => TextObject::Word { big: true },
4934            '"' | '\'' | '`' => TextObject::Quote(ch),
4935            '(' | ')' | 'b' => TextObject::Bracket('('),
4936            '[' | ']' => TextObject::Bracket('['),
4937            '{' | '}' | 'B' => TextObject::Bracket('{'),
4938            '<' | '>' => TextObject::Bracket('<'),
4939            'p' => TextObject::Paragraph,
4940            't' => TextObject::XmlTag,
4941            's' => TextObject::Sentence,
4942            _ => return,
4943        };
4944        let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
4945            return;
4946        };
4947        match kind {
4948            crate::vim::RangeKind::Linewise => {
4949                self.vim.visual_line_anchor = start.0;
4950                self.vim.mode = Mode::VisualLine;
4951                self.vim.current_mode = VimMode::VisualLine;
4952                self.jump_cursor(end.0, 0);
4953            }
4954            _ => {
4955                self.vim.mode = Mode::Visual;
4956                self.vim.current_mode = VimMode::Visual;
4957                self.vim.visual_anchor = (start.0, start.1);
4958                let (er, ec) = vim::retreat_one(self, end);
4959                self.jump_cursor(er, ec);
4960            }
4961        }
4962    }
4963}
4964
4965/// Visual column of the character at `char_col` in `line`, treating `\t`
4966/// as expansion to the next `tab_width` stop and every other char as
4967/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
4968/// the cursor math elsewhere also assumes single-cell chars.
4969fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
4970    let mut visual = 0usize;
4971    for (i, ch) in line.chars().enumerate() {
4972        if i >= char_col {
4973            break;
4974        }
4975        if ch == '\t' {
4976            visual += tab_width - (visual % tab_width);
4977        } else {
4978            visual += 1;
4979        }
4980    }
4981    visual
4982}
4983
4984#[cfg(feature = "crossterm")]
4985impl From<KeyEvent> for Input {
4986    fn from(key: KeyEvent) -> Self {
4987        let k = match key.code {
4988            KeyCode::Char(c) => Key::Char(c),
4989            KeyCode::Backspace => Key::Backspace,
4990            KeyCode::Delete => Key::Delete,
4991            KeyCode::Enter => Key::Enter,
4992            KeyCode::Left => Key::Left,
4993            KeyCode::Right => Key::Right,
4994            KeyCode::Up => Key::Up,
4995            KeyCode::Down => Key::Down,
4996            KeyCode::Home => Key::Home,
4997            KeyCode::End => Key::End,
4998            KeyCode::Tab => Key::Tab,
4999            KeyCode::Esc => Key::Esc,
5000            _ => Key::Null,
5001        };
5002        Input {
5003            key: k,
5004            ctrl: key.modifiers.contains(KeyModifiers::CONTROL),
5005            alt: key.modifiers.contains(KeyModifiers::ALT),
5006            shift: key.modifiers.contains(KeyModifiers::SHIFT),
5007        }
5008    }
5009}
5010
5011/// Crossterm `KeyEvent` → engine `Input`. Thin wrapper that delegates
5012/// to the [`From`] impl above; kept as a free fn for in-tree callers.
5013#[cfg(feature = "crossterm")]
5014pub fn crossterm_to_input(key: KeyEvent) -> Input {
5015    Input::from(key)
5016}
5017
5018#[cfg(all(test, feature = "crossterm"))]
5019mod tests {
5020    use super::*;
5021    use crate::types::Host;
5022    use crossterm::event::KeyEvent;
5023
5024    #[allow(dead_code)]
5025    fn key(code: KeyCode) -> KeyEvent {
5026        KeyEvent::new(code, KeyModifiers::NONE)
5027    }
5028    #[allow(dead_code)]
5029    fn shift_key(code: KeyCode) -> KeyEvent {
5030        KeyEvent::new(code, KeyModifiers::SHIFT)
5031    }
5032    #[allow(dead_code)]
5033    fn ctrl_key(code: KeyCode) -> KeyEvent {
5034        KeyEvent::new(code, KeyModifiers::CONTROL)
5035    }
5036
5037    #[test]
5038    fn intern_style_dedups_engine_native_styles() {
5039        use crate::types::{Attrs, Color, Style};
5040        let mut e = Editor::new(
5041            hjkl_buffer::Buffer::new(),
5042            crate::types::DefaultHost::new(),
5043            crate::types::Options::default(),
5044        );
5045        let s = Style {
5046            fg: Some(Color(255, 0, 0)),
5047            bg: None,
5048            attrs: Attrs::BOLD,
5049        };
5050        let id_a = e.intern_style(s);
5051        // Re-interning the same engine style returns the same id.
5052        let id_b = e.intern_style(s);
5053        assert_eq!(id_a, id_b);
5054        // Engine accessor returns the same style back.
5055        let back = e.engine_style_at(id_a).expect("interned");
5056        assert_eq!(back, s);
5057    }
5058
5059    #[test]
5060    fn engine_style_at_out_of_range_returns_none() {
5061        let e = Editor::new(
5062            hjkl_buffer::Buffer::new(),
5063            crate::types::DefaultHost::new(),
5064            crate::types::Options::default(),
5065        );
5066        assert!(e.engine_style_at(99).is_none());
5067    }
5068
5069    #[test]
5070    fn options_bridge_roundtrip() {
5071        let mut e = Editor::new(
5072            hjkl_buffer::Buffer::new(),
5073            crate::types::DefaultHost::new(),
5074            crate::types::Options::default(),
5075        );
5076        let opts = e.current_options();
5077        // 0.2.0: defaults flipped to modern editor norms — 4-space soft tabs.
5078        assert_eq!(opts.shiftwidth, 4);
5079        assert_eq!(opts.tabstop, 4);
5080
5081        let new_opts = crate::types::Options {
5082            shiftwidth: 4,
5083            tabstop: 2,
5084            ignorecase: true,
5085            ..crate::types::Options::default()
5086        };
5087        e.apply_options(&new_opts);
5088
5089        let after = e.current_options();
5090        assert_eq!(after.shiftwidth, 4);
5091        assert_eq!(after.tabstop, 2);
5092        assert!(after.ignorecase);
5093    }
5094
5095    #[test]
5096    fn selection_highlight_none_in_normal() {
5097        let mut e = Editor::new(
5098            hjkl_buffer::Buffer::new(),
5099            crate::types::DefaultHost::new(),
5100            crate::types::Options::default(),
5101        );
5102        e.set_content("hello");
5103        assert!(e.selection_highlight().is_none());
5104    }
5105
5106    #[test]
5107    fn highlights_emit_search_matches() {
5108        use crate::types::HighlightKind;
5109        let mut e = Editor::new(
5110            hjkl_buffer::Buffer::new(),
5111            crate::types::DefaultHost::new(),
5112            crate::types::Options::default(),
5113        );
5114        e.set_content("foo bar foo\nbaz qux\n");
5115        // 0.0.35: arm via the engine search state. The buffer
5116        // accessor still works (deprecated) but new code goes
5117        // through Editor.
5118        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5119        let hs = e.highlights_for_line(0);
5120        assert_eq!(hs.len(), 2);
5121        for h in &hs {
5122            assert_eq!(h.kind, HighlightKind::SearchMatch);
5123            assert_eq!(h.range.start.line, 0);
5124            assert_eq!(h.range.end.line, 0);
5125        }
5126    }
5127
5128    #[test]
5129    fn highlights_empty_without_pattern() {
5130        let mut e = Editor::new(
5131            hjkl_buffer::Buffer::new(),
5132            crate::types::DefaultHost::new(),
5133            crate::types::Options::default(),
5134        );
5135        e.set_content("foo bar");
5136        assert!(e.highlights_for_line(0).is_empty());
5137    }
5138
5139    #[test]
5140    fn highlights_empty_for_out_of_range_line() {
5141        let mut e = Editor::new(
5142            hjkl_buffer::Buffer::new(),
5143            crate::types::DefaultHost::new(),
5144            crate::types::Options::default(),
5145        );
5146        e.set_content("foo");
5147        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5148        assert!(e.highlights_for_line(99).is_empty());
5149    }
5150
5151    #[test]
5152    fn snapshot_roundtrips_through_restore() {
5153        use crate::types::SnapshotMode;
5154        let mut e = Editor::new(
5155            hjkl_buffer::Buffer::new(),
5156            crate::types::DefaultHost::new(),
5157            crate::types::Options::default(),
5158        );
5159        e.set_content("alpha\nbeta\ngamma");
5160        e.jump_cursor(2, 3);
5161        let snap = e.take_snapshot();
5162        assert_eq!(snap.mode, SnapshotMode::Normal);
5163        assert_eq!(snap.cursor, (2, 3));
5164        assert_eq!(snap.lines.len(), 3);
5165
5166        let mut other = Editor::new(
5167            hjkl_buffer::Buffer::new(),
5168            crate::types::DefaultHost::new(),
5169            crate::types::Options::default(),
5170        );
5171        other.restore_snapshot(snap).expect("restore");
5172        assert_eq!(other.cursor(), (2, 3));
5173        assert_eq!(other.buffer().lines().len(), 3);
5174    }
5175
5176    #[test]
5177    fn restore_snapshot_rejects_version_mismatch() {
5178        let mut e = Editor::new(
5179            hjkl_buffer::Buffer::new(),
5180            crate::types::DefaultHost::new(),
5181            crate::types::Options::default(),
5182        );
5183        let mut snap = e.take_snapshot();
5184        snap.version = 9999;
5185        match e.restore_snapshot(snap) {
5186            Err(crate::EngineError::SnapshotVersion(got, want)) => {
5187                assert_eq!(got, 9999);
5188                assert_eq!(want, crate::types::EditorSnapshot::VERSION);
5189            }
5190            other => panic!("expected SnapshotVersion err, got {other:?}"),
5191        }
5192    }
5193
5194    #[test]
5195    fn take_content_change_returns_some_on_first_dirty() {
5196        let mut e = Editor::new(
5197            hjkl_buffer::Buffer::new(),
5198            crate::types::DefaultHost::new(),
5199            crate::types::Options::default(),
5200        );
5201        e.set_content("hello");
5202        let first = e.take_content_change();
5203        assert!(first.is_some());
5204        let second = e.take_content_change();
5205        assert!(second.is_none());
5206    }
5207
5208    fn many_lines(n: usize) -> String {
5209        (0..n)
5210            .map(|i| format!("line{i}"))
5211            .collect::<Vec<_>>()
5212            .join("\n")
5213    }
5214
5215    #[allow(dead_code)]
5216    fn prime_viewport<H: Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, height: u16) {
5217        e.set_viewport_height(height);
5218    }
5219
5220    /// Contract that the TUI drain relies on: `set_content` flags the
5221    /// editor dirty (so the next `take_dirty` call reports the change),
5222    /// and a second `take_dirty` returns `false` after consumption. The
5223    /// TUI drains this flag after every programmatic content load so
5224    /// opening a tab doesn't get mistaken for a user edit and mark the
5225    /// tab dirty (which would then trigger the quit-prompt on `:q`).
5226    #[test]
5227    fn set_content_dirties_then_take_dirty_clears() {
5228        let mut e = Editor::new(
5229            hjkl_buffer::Buffer::new(),
5230            crate::types::DefaultHost::new(),
5231            crate::types::Options::default(),
5232        );
5233        e.set_content("hello");
5234        assert!(
5235            e.take_dirty(),
5236            "set_content should leave content_dirty=true"
5237        );
5238        assert!(!e.take_dirty(), "take_dirty should clear the flag");
5239    }
5240
5241    #[test]
5242    fn content_arc_cache_invalidated_by_set_content() {
5243        let mut e = Editor::new(
5244            hjkl_buffer::Buffer::new(),
5245            crate::types::DefaultHost::new(),
5246            crate::types::Options::default(),
5247        );
5248        e.set_content("one");
5249        let a = e.content_arc();
5250        e.set_content("two");
5251        let b = e.content_arc();
5252        assert!(!std::sync::Arc::ptr_eq(&a, &b));
5253        assert!(b.starts_with("two"));
5254    }
5255
5256    // ── lnum_width ──────────────────────────────────────────────────────────
5257
5258    #[test]
5259    fn lnum_width_numberwidth_floor_enforced() {
5260        let mut e = Editor::new(
5261            hjkl_buffer::Buffer::new(),
5262            crate::types::DefaultHost::new(),
5263            crate::types::Options::default(),
5264        );
5265        // Default: number=true, numberwidth=4; buffer has 1 line → digits=1,
5266        // needed=2 which is less than floor of 4.
5267        e.set_content("single line");
5268        assert_eq!(e.lnum_width(), 4, "should be floored to numberwidth (4)");
5269    }
5270
5271    #[test]
5272    fn lnum_width_zero_when_both_flags_off() {
5273        let mut e = Editor::new(
5274            hjkl_buffer::Buffer::new(),
5275            crate::types::DefaultHost::new(),
5276            crate::types::Options {
5277                number: false,
5278                relativenumber: false,
5279                ..crate::types::Options::default()
5280            },
5281        );
5282        e.set_content("some content");
5283        assert_eq!(
5284            e.lnum_width(),
5285            0,
5286            "gutter should be 0 when number flags are off"
5287        );
5288    }
5289
5290    // ── doc-coord mouse primitives (Phase 1 — issue #114) ──────────────────
5291
5292    #[test]
5293    fn mouse_click_doc_moves_cursor_to_doc_coords() {
5294        let mut e = Editor::new(
5295            hjkl_buffer::Buffer::new(),
5296            crate::types::DefaultHost::new(),
5297            crate::types::Options::default(),
5298        );
5299        e.set_content("hello\nworld");
5300        e.mouse_click_doc(1, 2);
5301        assert_eq!(e.cursor(), (1, 2));
5302    }
5303
5304    #[test]
5305    fn mouse_click_doc_normal_mode_clamps_past_eol_to_last_char() {
5306        let mut e = Editor::new(
5307            hjkl_buffer::Buffer::new(),
5308            crate::types::DefaultHost::new(),
5309            crate::types::Options::default(),
5310        );
5311        e.set_content("hello");
5312        // Normal mode (default after construction): "hello" has 5 chars,
5313        // past-EOL click clamps to col=4 (last char 'o' — never on the
5314        // implicit \n, vim/neovim convention).
5315        e.mouse_click_doc(0, 99);
5316        assert_eq!(e.cursor(), (0, 4));
5317    }
5318
5319    #[test]
5320    fn mouse_click_doc_normal_mode_clamps_past_eol_multibyte() {
5321        let mut e = Editor::new(
5322            hjkl_buffer::Buffer::new(),
5323            crate::types::DefaultHost::new(),
5324            crate::types::Options::default(),
5325        );
5326        // 5 chars, 6 bytes — clamping must be char-counted, not byte-counted.
5327        e.set_content("héllo");
5328        e.mouse_click_doc(0, 99);
5329        assert_eq!(e.cursor(), (0, 4));
5330    }
5331
5332    #[test]
5333    fn mouse_click_doc_insert_mode_allows_one_past_eol() {
5334        let mut e = Editor::new(
5335            hjkl_buffer::Buffer::new(),
5336            crate::types::DefaultHost::new(),
5337            crate::types::Options::default(),
5338        );
5339        e.set_content("hello");
5340        e.enter_insert_i(1);
5341        // Insert mode allows the one-past-EOL position (col=5 for 5-char
5342        // line) — that's the canonical insert-here sentinel.
5343        e.mouse_click_doc(0, 99);
5344        assert_eq!(e.cursor(), (0, 5));
5345    }
5346
5347    #[test]
5348    fn mouse_click_doc_resets_sticky_col() {
5349        let mut e = Editor::new(
5350            hjkl_buffer::Buffer::new(),
5351            crate::types::DefaultHost::new(),
5352            crate::types::Options::default(),
5353        );
5354        e.set_content("aaaaa\nbb\naaaaa");
5355        // Pretend a previous keyboard motion put intended col at 4 (e.g.
5356        // user navigated $ on row 0).
5357        e.sticky_col = Some(4);
5358        // Click on row 1, col 1 (the second 'b' on a short line).
5359        e.mouse_click_doc(1, 1);
5360        assert_eq!(e.cursor(), (1, 1));
5361        assert_eq!(
5362            e.sticky_col,
5363            Some(1),
5364            "click must reset sticky_col so a subsequent j/k uses the clicked column \
5365             as the intended visual column (not the previous keyboard-tracked col)"
5366        );
5367    }
5368
5369    #[test]
5370    fn mouse_click_doc_exits_visual_mode() {
5371        use crate::VimMode;
5372        let mut e = Editor::new(
5373            hjkl_buffer::Buffer::new(),
5374            crate::types::DefaultHost::new(),
5375            crate::types::Options::default(),
5376        );
5377        e.set_content("hello");
5378        e.enter_visual_char();
5379        assert_eq!(e.vim_mode(), VimMode::Visual);
5380        e.mouse_click_doc(0, 2);
5381        assert_eq!(e.vim_mode(), VimMode::Normal);
5382        assert_eq!(e.cursor(), (0, 2));
5383    }
5384
5385    #[test]
5386    fn set_cursor_doc_clamps_past_last_row() {
5387        let mut e = Editor::new(
5388            hjkl_buffer::Buffer::new(),
5389            crate::types::DefaultHost::new(),
5390            crate::types::Options::default(),
5391        );
5392        e.set_content("one\ntwo");
5393        // doc has 2 rows (0 and 1); row 99 clamps to 1.
5394        e.set_cursor_doc(99, 0);
5395        assert_eq!(e.cursor(), (1, 0));
5396    }
5397
5398    #[test]
5399    fn mouse_begin_drag_enters_visual_char() {
5400        use crate::VimMode;
5401        let mut e = Editor::new(
5402            hjkl_buffer::Buffer::new(),
5403            crate::types::DefaultHost::new(),
5404            crate::types::Options::default(),
5405        );
5406        e.set_content("hello");
5407        e.mouse_begin_drag();
5408        assert_eq!(e.vim_mode(), VimMode::Visual);
5409    }
5410
5411    #[test]
5412    fn mouse_extend_drag_doc_moves_cursor_leaving_visual_anchor() {
5413        use crate::VimMode;
5414        let mut e = Editor::new(
5415            hjkl_buffer::Buffer::new(),
5416            crate::types::DefaultHost::new(),
5417            crate::types::Options::default(),
5418        );
5419        e.set_content("hello world");
5420        e.mouse_begin_drag(); // anchor at (0,0)
5421        e.mouse_extend_drag_doc(0, 5);
5422        assert_eq!(e.vim_mode(), VimMode::Visual);
5423        assert_eq!(e.cursor(), (0, 5));
5424    }
5425
5426    // ── Patch B (0.0.29): Host trait wired into Editor ──
5427
5428    #[test]
5429    fn host_clipboard_round_trip_via_default_host() {
5430        // DefaultHost stores write_clipboard in-memory; read_clipboard
5431        // returns the most recent payload.
5432        let mut e = Editor::new(
5433            hjkl_buffer::Buffer::new(),
5434            crate::types::DefaultHost::new(),
5435            crate::types::Options::default(),
5436        );
5437        e.host_mut().write_clipboard("payload".to_string());
5438        assert_eq!(e.host_mut().read_clipboard().as_deref(), Some("payload"));
5439    }
5440
5441    // ── ContentEdit emission ─────────────────────────────────────────
5442
5443    fn fresh_editor(initial: &str) -> Editor {
5444        let buffer = hjkl_buffer::Buffer::from_str(initial);
5445        Editor::new(
5446            buffer,
5447            crate::types::DefaultHost::new(),
5448            crate::types::Options::default(),
5449        )
5450    }
5451
5452    #[test]
5453    fn content_edit_insert_char_at_origin() {
5454        let mut e = fresh_editor("");
5455        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5456            at: hjkl_buffer::Position::new(0, 0),
5457            ch: 'a',
5458        });
5459        let edits = e.take_content_edits();
5460        assert_eq!(edits.len(), 1);
5461        let ce = &edits[0];
5462        assert_eq!(ce.start_byte, 0);
5463        assert_eq!(ce.old_end_byte, 0);
5464        assert_eq!(ce.new_end_byte, 1);
5465        assert_eq!(ce.start_position, (0, 0));
5466        assert_eq!(ce.old_end_position, (0, 0));
5467        assert_eq!(ce.new_end_position, (0, 1));
5468    }
5469
5470    #[test]
5471    fn content_edit_insert_str_multiline() {
5472        // Buffer "x\ny" — insert "ab\ncd" at end of row 0.
5473        let mut e = fresh_editor("x\ny");
5474        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertStr {
5475            at: hjkl_buffer::Position::new(0, 1),
5476            text: "ab\ncd".into(),
5477        });
5478        let edits = e.take_content_edits();
5479        assert_eq!(edits.len(), 1);
5480        let ce = &edits[0];
5481        assert_eq!(ce.start_byte, 1);
5482        assert_eq!(ce.old_end_byte, 1);
5483        assert_eq!(ce.new_end_byte, 1 + 5);
5484        assert_eq!(ce.start_position, (0, 1));
5485        // Insertion contains one '\n', so row+1, col = bytes after last '\n' = 2.
5486        assert_eq!(ce.new_end_position, (1, 2));
5487    }
5488
5489    #[test]
5490    fn content_edit_delete_range_charwise() {
5491        // "abcdef" — delete chars 1..4 ("bcd").
5492        let mut e = fresh_editor("abcdef");
5493        let _ = e.mutate_edit(hjkl_buffer::Edit::DeleteRange {
5494            start: hjkl_buffer::Position::new(0, 1),
5495            end: hjkl_buffer::Position::new(0, 4),
5496            kind: hjkl_buffer::MotionKind::Char,
5497        });
5498        let edits = e.take_content_edits();
5499        assert_eq!(edits.len(), 1);
5500        let ce = &edits[0];
5501        assert_eq!(ce.start_byte, 1);
5502        assert_eq!(ce.old_end_byte, 4);
5503        assert_eq!(ce.new_end_byte, 1);
5504        assert!(ce.old_end_byte > ce.new_end_byte);
5505    }
5506
5507    #[test]
5508    fn content_edit_set_content_resets() {
5509        let mut e = fresh_editor("foo");
5510        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5511            at: hjkl_buffer::Position::new(0, 0),
5512            ch: 'X',
5513        });
5514        // set_content should clear queued edits and raise the reset
5515        // flag on the next take_content_reset.
5516        e.set_content("brand new");
5517        assert!(e.take_content_reset());
5518        // Subsequent call clears the flag.
5519        assert!(!e.take_content_reset());
5520        // Edits cleared on reset.
5521        assert!(e.take_content_edits().is_empty());
5522    }
5523
5524    #[test]
5525    fn content_edit_multiple_replaces_in_order() {
5526        // Three Replace edits applied left-to-right (mimics the
5527        // substitute path's per-match Replace fan-out). Verify each
5528        // mutation queues exactly one ContentEdit and they're drained
5529        // in source-order with structurally valid byte spans.
5530        let mut e = fresh_editor("xax xbx xcx");
5531        let _ = e.take_content_edits();
5532        let _ = e.take_content_reset();
5533        // Replace each "x" with "yy", left to right. After each replace,
5534        // the next match's char-col shifts by +1 (since "yy" is 1 char
5535        // longer than "x" but they're both ASCII so byte = char here).
5536        let positions = [(0usize, 0usize), (0, 4), (0, 8)];
5537        for (row, col) in positions {
5538            let _ = e.mutate_edit(hjkl_buffer::Edit::Replace {
5539                start: hjkl_buffer::Position::new(row, col),
5540                end: hjkl_buffer::Position::new(row, col + 1),
5541                with: "yy".into(),
5542            });
5543        }
5544        let edits = e.take_content_edits();
5545        assert_eq!(edits.len(), 3);
5546        for ce in &edits {
5547            assert!(ce.start_byte <= ce.old_end_byte);
5548            assert!(ce.start_byte <= ce.new_end_byte);
5549        }
5550        // Document order.
5551        for w in edits.windows(2) {
5552            assert!(w[0].start_byte <= w[1].start_byte);
5553        }
5554    }
5555
5556    #[test]
5557    fn replace_char_at_replaces_single_char_under_cursor() {
5558        // Matches vim's `rx` semantics: replace char under cursor.
5559        let mut e = fresh_editor("abc");
5560        e.jump_cursor(0, 1); // cursor on 'b'
5561        e.replace_char_at('X', 1);
5562        let got = e.content();
5563        let got = got.trim_end_matches('\n');
5564        assert_eq!(
5565            got, "aXc",
5566            "replace_char_at(X, 1) must replace 'b' with 'X'"
5567        );
5568        // Cursor stays on the replaced char.
5569        assert_eq!(e.cursor(), (0, 1));
5570    }
5571
5572    #[test]
5573    fn replace_char_at_count_replaces_multiple_chars() {
5574        // `3rx` in vim replaces 3 chars starting at cursor.
5575        let mut e = fresh_editor("abcde");
5576        e.jump_cursor(0, 0);
5577        e.replace_char_at('Z', 3);
5578        let got = e.content();
5579        let got = got.trim_end_matches('\n');
5580        assert_eq!(
5581            got, "ZZZde",
5582            "replace_char_at(Z, 3) must replace first 3 chars"
5583        );
5584    }
5585
5586    #[test]
5587    fn find_char_method_moves_to_target() {
5588        // buffer "abcabc", cursor (0,0), f<c> → cursor (0,2).
5589        let mut e = fresh_editor("abcabc");
5590        e.jump_cursor(0, 0);
5591        e.find_char('c', true, false, 1);
5592        assert_eq!(
5593            e.cursor(),
5594            (0, 2),
5595            "find_char('c', forward=true, till=false, count=1) must land on 'c' at col 2"
5596        );
5597    }
5598
5599    // ── after_g unit tests (Phase 2b-ii) ────────────────────────────────────
5600
5601    #[test]
5602    fn after_g_gg_jumps_to_top() {
5603        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5604        let mut e = fresh_editor(&content);
5605        e.jump_cursor(15, 0);
5606        e.after_g('g', 1);
5607        assert_eq!(e.cursor().0, 0, "gg must move cursor to row 0");
5608    }
5609
5610    #[test]
5611    fn after_g_gg_with_count_jumps_line() {
5612        // 5gg → row 4 (0-indexed).
5613        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5614        let mut e = fresh_editor(&content);
5615        e.jump_cursor(0, 0);
5616        e.after_g('g', 5);
5617        assert_eq!(e.cursor().0, 4, "5gg must land on row 4");
5618    }
5619
5620    #[test]
5621    fn after_g_gj_moves_down() {
5622        let mut e = fresh_editor("line0\nline1\nline2\n");
5623        e.jump_cursor(0, 0);
5624        e.after_g('j', 1);
5625        assert_eq!(e.cursor().0, 1, "gj must move down one display row");
5626    }
5627
5628    #[test]
5629    fn after_g_gu_sets_operator_pending() {
5630        // gU enters operator-pending with Uppercase op; next key applies it.
5631        let mut e = fresh_editor("hello\n");
5632        e.after_g('U', 1);
5633        // The engine should now be chord-pending (Pending::Op set).
5634        assert!(
5635            e.is_chord_pending(),
5636            "gU must set engine chord-pending (Pending::Op)"
5637        );
5638    }
5639
5640    #[test]
5641    fn after_g_g_star_searches_forward_non_whole_word() {
5642        // g* on word "foo" in "foobar" should find the match.
5643        let mut e = fresh_editor("foo foobar\n");
5644        e.jump_cursor(0, 0); // cursor on 'f' of "foo"
5645        e.after_g('*', 1);
5646        // After g* the cursor should have moved (ScreenDown motion is
5647        // not applicable here; WordAtCursor forward moves to next match).
5648        // At minimum: no panic and mode stays Normal.
5649        assert_eq!(e.vim_mode(), VimMode::Normal, "g* must stay in Normal mode");
5650    }
5651
5652    // ── apply_motion controller tests (Phase 3a) ────────────────────────────
5653
5654    #[test]
5655    fn apply_motion_char_left_moves_cursor() {
5656        let mut e = fresh_editor("hello\n");
5657        e.jump_cursor(0, 3);
5658        e.apply_motion(crate::MotionKind::CharLeft, 1);
5659        assert_eq!(e.cursor(), (0, 2), "CharLeft moves one col left");
5660    }
5661
5662    #[test]
5663    fn apply_motion_char_left_clamps_at_col_zero() {
5664        let mut e = fresh_editor("hello\n");
5665        e.jump_cursor(0, 0);
5666        e.apply_motion(crate::MotionKind::CharLeft, 1);
5667        assert_eq!(e.cursor(), (0, 0), "CharLeft at col 0 must not wrap");
5668    }
5669
5670    #[test]
5671    fn apply_motion_char_left_with_count() {
5672        let mut e = fresh_editor("hello\n");
5673        e.jump_cursor(0, 4);
5674        e.apply_motion(crate::MotionKind::CharLeft, 3);
5675        assert_eq!(e.cursor(), (0, 1), "CharLeft count=3 moves three cols left");
5676    }
5677
5678    #[test]
5679    fn apply_motion_char_right_moves_cursor() {
5680        let mut e = fresh_editor("hello\n");
5681        e.jump_cursor(0, 0);
5682        e.apply_motion(crate::MotionKind::CharRight, 1);
5683        assert_eq!(e.cursor(), (0, 1), "CharRight moves one col right");
5684    }
5685
5686    #[test]
5687    fn apply_motion_char_right_clamps_at_last_char() {
5688        let mut e = fresh_editor("hello\n");
5689        // "hello" has chars at 0..=4; normal mode clamps at 4.
5690        e.jump_cursor(0, 4);
5691        e.apply_motion(crate::MotionKind::CharRight, 1);
5692        assert_eq!(
5693            e.cursor(),
5694            (0, 4),
5695            "CharRight at end must not go past last char"
5696        );
5697    }
5698
5699    #[test]
5700    fn apply_motion_line_down_moves_cursor() {
5701        let mut e = fresh_editor("line0\nline1\nline2\n");
5702        e.jump_cursor(0, 0);
5703        e.apply_motion(crate::MotionKind::LineDown, 1);
5704        assert_eq!(e.cursor().0, 1, "LineDown moves one row down");
5705    }
5706
5707    #[test]
5708    fn apply_motion_line_down_with_count() {
5709        let mut e = fresh_editor("line0\nline1\nline2\n");
5710        e.jump_cursor(0, 0);
5711        e.apply_motion(crate::MotionKind::LineDown, 2);
5712        assert_eq!(e.cursor().0, 2, "LineDown count=2 moves two rows down");
5713    }
5714
5715    #[test]
5716    fn apply_motion_line_up_moves_cursor() {
5717        let mut e = fresh_editor("line0\nline1\nline2\n");
5718        e.jump_cursor(2, 0);
5719        e.apply_motion(crate::MotionKind::LineUp, 1);
5720        assert_eq!(e.cursor().0, 1, "LineUp moves one row up");
5721    }
5722
5723    #[test]
5724    fn apply_motion_line_up_clamps_at_top() {
5725        let mut e = fresh_editor("line0\nline1\n");
5726        e.jump_cursor(0, 0);
5727        e.apply_motion(crate::MotionKind::LineUp, 1);
5728        assert_eq!(e.cursor().0, 0, "LineUp at top must not go negative");
5729    }
5730
5731    #[test]
5732    fn apply_motion_first_non_blank_down_moves_and_lands_on_non_blank() {
5733        // Line 0: "  hello" (indent 2), line 1: "  world" (indent 2).
5734        let mut e = fresh_editor("  hello\n  world\n");
5735        e.jump_cursor(0, 0);
5736        e.apply_motion(crate::MotionKind::FirstNonBlankDown, 1);
5737        assert_eq!(e.cursor().0, 1, "FirstNonBlankDown must move to next row");
5738        assert_eq!(
5739            e.cursor().1,
5740            2,
5741            "FirstNonBlankDown must land on first non-blank col"
5742        );
5743    }
5744
5745    #[test]
5746    fn apply_motion_first_non_blank_up_moves_and_lands_on_non_blank() {
5747        let mut e = fresh_editor("  hello\n  world\n");
5748        e.jump_cursor(1, 4);
5749        e.apply_motion(crate::MotionKind::FirstNonBlankUp, 1);
5750        assert_eq!(e.cursor().0, 0, "FirstNonBlankUp must move to prev row");
5751        assert_eq!(
5752            e.cursor().1,
5753            2,
5754            "FirstNonBlankUp must land on first non-blank col"
5755        );
5756    }
5757
5758    #[test]
5759    fn apply_motion_count_zero_treated_as_one() {
5760        // count=0 must be normalised to 1 (count.max(1) in apply_motion_kind).
5761        let mut e = fresh_editor("hello\n");
5762        e.jump_cursor(0, 3);
5763        e.apply_motion(crate::MotionKind::CharLeft, 0);
5764        assert_eq!(e.cursor(), (0, 2), "count=0 treated as 1 for CharLeft");
5765    }
5766
5767    // ── apply_motion controller tests (Phase 3b) — word motions ─────────────
5768
5769    #[test]
5770    fn apply_motion_word_forward_moves_to_next_word() {
5771        // "hello world\n": 'w' from col 0 lands on 'w' of "world" at col 6.
5772        let mut e = fresh_editor("hello world\n");
5773        e.jump_cursor(0, 0);
5774        e.apply_motion(crate::MotionKind::WordForward, 1);
5775        assert_eq!(
5776            e.cursor(),
5777            (0, 6),
5778            "WordForward moves to start of next word"
5779        );
5780    }
5781
5782    #[test]
5783    fn apply_motion_word_forward_with_count() {
5784        // "one two three\n": 2w from col 0 → start of "three" at col 8.
5785        let mut e = fresh_editor("one two three\n");
5786        e.jump_cursor(0, 0);
5787        e.apply_motion(crate::MotionKind::WordForward, 2);
5788        assert_eq!(e.cursor(), (0, 8), "WordForward count=2 skips two words");
5789    }
5790
5791    #[test]
5792    fn apply_motion_big_word_forward_moves_to_next_big_word() {
5793        // "foo.bar baz\n": W from col 0 skips entire "foo.bar" (one WORD) to 'b' at col 8.
5794        let mut e = fresh_editor("foo.bar baz\n");
5795        e.jump_cursor(0, 0);
5796        e.apply_motion(crate::MotionKind::BigWordForward, 1);
5797        assert_eq!(e.cursor(), (0, 8), "BigWordForward skips the whole WORD");
5798    }
5799
5800    #[test]
5801    fn apply_motion_big_word_forward_with_count() {
5802        // "aa bb cc\n": 2W from col 0 → start of "cc" at col 6.
5803        let mut e = fresh_editor("aa bb cc\n");
5804        e.jump_cursor(0, 0);
5805        e.apply_motion(crate::MotionKind::BigWordForward, 2);
5806        assert_eq!(e.cursor(), (0, 6), "BigWordForward count=2 skips two WORDs");
5807    }
5808
5809    #[test]
5810    fn apply_motion_word_backward_moves_to_prev_word() {
5811        // "hello world\n": 'b' from col 6 ('w') lands back at col 0 ('h').
5812        let mut e = fresh_editor("hello world\n");
5813        e.jump_cursor(0, 6);
5814        e.apply_motion(crate::MotionKind::WordBackward, 1);
5815        assert_eq!(
5816            e.cursor(),
5817            (0, 0),
5818            "WordBackward moves to start of prev word"
5819        );
5820    }
5821
5822    #[test]
5823    fn apply_motion_word_backward_with_count() {
5824        // "one two three\n": 2b from col 8 ('t' of "three") → col 0 ('o' of "one").
5825        let mut e = fresh_editor("one two three\n");
5826        e.jump_cursor(0, 8);
5827        e.apply_motion(crate::MotionKind::WordBackward, 2);
5828        assert_eq!(
5829            e.cursor(),
5830            (0, 0),
5831            "WordBackward count=2 skips two words back"
5832        );
5833    }
5834
5835    #[test]
5836    fn apply_motion_big_word_backward_moves_to_prev_big_word() {
5837        // "foo.bar baz\n": B from col 8 ('b' of "baz") → col 0 (start of "foo.bar" WORD).
5838        let mut e = fresh_editor("foo.bar baz\n");
5839        e.jump_cursor(0, 8);
5840        e.apply_motion(crate::MotionKind::BigWordBackward, 1);
5841        assert_eq!(
5842            e.cursor(),
5843            (0, 0),
5844            "BigWordBackward jumps to start of prev WORD"
5845        );
5846    }
5847
5848    #[test]
5849    fn apply_motion_big_word_backward_with_count() {
5850        // "aa bb cc\n": 2B from col 6 ('c') → col 0 ('a').
5851        let mut e = fresh_editor("aa bb cc\n");
5852        e.jump_cursor(0, 6);
5853        e.apply_motion(crate::MotionKind::BigWordBackward, 2);
5854        assert_eq!(
5855            e.cursor(),
5856            (0, 0),
5857            "BigWordBackward count=2 skips two WORDs back"
5858        );
5859    }
5860
5861    #[test]
5862    fn apply_motion_word_end_moves_to_end_of_word() {
5863        // "hello world\n": 'e' from col 0 lands on 'o' of "hello" at col 4.
5864        let mut e = fresh_editor("hello world\n");
5865        e.jump_cursor(0, 0);
5866        e.apply_motion(crate::MotionKind::WordEnd, 1);
5867        assert_eq!(e.cursor(), (0, 4), "WordEnd moves to end of current word");
5868    }
5869
5870    #[test]
5871    fn apply_motion_word_end_with_count() {
5872        // "one two three\n": 2e from col 0 → end of "two" at col 6.
5873        let mut e = fresh_editor("one two three\n");
5874        e.jump_cursor(0, 0);
5875        e.apply_motion(crate::MotionKind::WordEnd, 2);
5876        assert_eq!(
5877            e.cursor(),
5878            (0, 6),
5879            "WordEnd count=2 lands on end of second word"
5880        );
5881    }
5882
5883    #[test]
5884    fn apply_motion_big_word_end_moves_to_end_of_big_word() {
5885        // "foo.bar baz\n": E from col 0 → end of "foo.bar" WORD at col 6.
5886        let mut e = fresh_editor("foo.bar baz\n");
5887        e.jump_cursor(0, 0);
5888        e.apply_motion(crate::MotionKind::BigWordEnd, 1);
5889        assert_eq!(e.cursor(), (0, 6), "BigWordEnd lands on end of WORD");
5890    }
5891
5892    #[test]
5893    fn apply_motion_big_word_end_with_count() {
5894        // "aa bb cc\n": 2E from col 0 → end of "bb" at col 4.
5895        let mut e = fresh_editor("aa bb cc\n");
5896        e.jump_cursor(0, 0);
5897        e.apply_motion(crate::MotionKind::BigWordEnd, 2);
5898        assert_eq!(
5899            e.cursor(),
5900            (0, 4),
5901            "BigWordEnd count=2 lands on end of second WORD"
5902        );
5903    }
5904
5905    // ── apply_motion controller tests (Phase 3c) — line-anchor motions ────────
5906
5907    #[test]
5908    fn apply_motion_line_start_lands_at_col_zero() {
5909        // "  foo bar  \n": `0` from col 5 → col 0 unconditionally.
5910        let mut e = fresh_editor("  foo bar  \n");
5911        e.jump_cursor(0, 5);
5912        e.apply_motion(crate::MotionKind::LineStart, 1);
5913        assert_eq!(e.cursor(), (0, 0), "LineStart lands at col 0");
5914    }
5915
5916    #[test]
5917    fn apply_motion_line_start_from_beginning_stays_at_col_zero() {
5918        // Already at col 0 — motion is a no-op but must not panic.
5919        let mut e = fresh_editor("  foo bar  \n");
5920        e.jump_cursor(0, 0);
5921        e.apply_motion(crate::MotionKind::LineStart, 1);
5922        assert_eq!(e.cursor(), (0, 0), "LineStart from col 0 stays at col 0");
5923    }
5924
5925    #[test]
5926    fn apply_motion_first_non_blank_lands_on_first_non_blank() {
5927        // "  foo bar  \n": `^` from col 0 → col 2 ('f').
5928        let mut e = fresh_editor("  foo bar  \n");
5929        e.jump_cursor(0, 0);
5930        e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
5931        assert_eq!(
5932            e.cursor(),
5933            (0, 2),
5934            "FirstNonBlank lands on first non-blank char"
5935        );
5936    }
5937
5938    #[test]
5939    fn apply_motion_first_non_blank_on_blank_line_lands_at_zero() {
5940        // "   \n": all whitespace — `^` must land at col 0.
5941        let mut e = fresh_editor("   \n");
5942        e.jump_cursor(0, 2);
5943        e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
5944        assert_eq!(
5945            e.cursor(),
5946            (0, 0),
5947            "FirstNonBlank on blank line stays at col 0"
5948        );
5949    }
5950
5951    #[test]
5952    fn apply_motion_line_end_lands_on_last_char() {
5953        // "  foo bar  \n": last char is the second space at col 10.
5954        let mut e = fresh_editor("  foo bar  \n");
5955        e.jump_cursor(0, 0);
5956        e.apply_motion(crate::MotionKind::LineEnd, 1);
5957        assert_eq!(e.cursor(), (0, 10), "LineEnd lands on last char of line");
5958    }
5959
5960    #[test]
5961    fn apply_motion_line_end_on_empty_line_stays_at_zero() {
5962        // "\n": empty line — `$` must stay at col 0.
5963        let mut e = fresh_editor("\n");
5964        e.jump_cursor(0, 0);
5965        e.apply_motion(crate::MotionKind::LineEnd, 1);
5966        assert_eq!(e.cursor(), (0, 0), "LineEnd on empty line stays at col 0");
5967    }
5968
5969    // ── apply_motion controller tests (Phase 3d) — doc-level motion ───────────
5970
5971    #[test]
5972    fn goto_line_count_1_lands_on_last_line() {
5973        // "foo\nbar\nbaz\n": bare `G` (count=1) → last content line (row 2).
5974        // Count convention: apply_motion_kind normalises 1 → execute_motion
5975        // with count=1 → FileBottom arm sees count <= 1 → move_bottom(0) =
5976        // last content row.
5977        let mut e = fresh_editor("foo\nbar\nbaz\n");
5978        e.jump_cursor(0, 0);
5979        e.apply_motion(crate::MotionKind::GotoLine, 1);
5980        assert_eq!(e.cursor(), (2, 0), "bare G lands on last content row");
5981    }
5982
5983    #[test]
5984    fn goto_line_count_5_lands_on_line_5() {
5985        // 6-line buffer (rows 0-5); `5G` → row 4 (1-based line 5).
5986        let mut e = fresh_editor("a\nb\nc\nd\ne\nf\n");
5987        e.jump_cursor(0, 0);
5988        e.apply_motion(crate::MotionKind::GotoLine, 5);
5989        assert_eq!(e.cursor(), (4, 0), "5G lands on row 4 (1-based line 5)");
5990    }
5991
5992    #[test]
5993    fn goto_line_count_past_buffer_clamps_to_last_line() {
5994        // "foo\nbar\nbaz\n": `100G` → last content line (row 2), clamped.
5995        let mut e = fresh_editor("foo\nbar\nbaz\n");
5996        e.jump_cursor(0, 0);
5997        e.apply_motion(crate::MotionKind::GotoLine, 100);
5998        assert_eq!(e.cursor(), (2, 0), "100G clamps to last content row");
5999    }
6000
6001    // ── FindRepeat / FindRepeatReverse controller tests (Phase 3e) ────────────
6002
6003    #[test]
6004    fn find_repeat_after_f_finds_next_occurrence() {
6005        // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` repeats → (0,5).
6006        let mut e = fresh_editor("abcabc");
6007        e.jump_cursor(0, 0);
6008        e.find_char('c', true, false, 1);
6009        assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6010        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6011        assert_eq!(
6012            e.cursor(),
6013            (0, 5),
6014            "find_repeat (;) must advance to second 'c'"
6015        );
6016    }
6017
6018    #[test]
6019    fn find_repeat_reverse_after_f_finds_prev_occurrence() {
6020        // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` → (0,5). `,` back → (0,2).
6021        let mut e = fresh_editor("abcabc");
6022        e.jump_cursor(0, 0);
6023        e.find_char('c', true, false, 1);
6024        assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6025        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6026        assert_eq!(e.cursor(), (0, 5), "; must advance to second 'c'");
6027        e.apply_motion(crate::MotionKind::FindRepeatReverse, 1);
6028        assert_eq!(
6029            e.cursor(),
6030            (0, 2),
6031            "find_repeat_reverse (,) must go back to first 'c'"
6032        );
6033    }
6034
6035    #[test]
6036    fn find_repeat_with_no_prior_find_is_noop() {
6037        // Fresh editor, no prior find — `;` must not move cursor.
6038        let mut e = fresh_editor("abcabc");
6039        e.jump_cursor(0, 3);
6040        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6041        assert_eq!(
6042            e.cursor(),
6043            (0, 3),
6044            "find_repeat with no prior find must be a no-op"
6045        );
6046    }
6047
6048    #[test]
6049    fn find_repeat_with_count_advances_count_times() {
6050        // "aXaXaX", cursor (0,0). `fX` → (0,1). `3;` → repeats 3× → (0,5).
6051        let mut e = fresh_editor("aXaXaX");
6052        e.jump_cursor(0, 0);
6053        e.find_char('X', true, false, 1);
6054        assert_eq!(e.cursor(), (0, 1), "fX must land on first 'X' at col 1");
6055        e.apply_motion(crate::MotionKind::FindRepeat, 3);
6056        assert_eq!(
6057            e.cursor(),
6058            (0, 5),
6059            "3; must advance 3 times from col 1 to col 5"
6060        );
6061    }
6062
6063    // ── BracketMatch controller tests (Phase 3f) ───────────────────────────────
6064
6065    #[test]
6066    fn bracket_match_jumps_to_matching_close_paren() {
6067        // "(abc)", cursor at (0,0) on `(` — `%` must jump to `)` at (0,4).
6068        let mut e = fresh_editor("(abc)");
6069        e.jump_cursor(0, 0);
6070        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6071        assert_eq!(
6072            e.cursor(),
6073            (0, 4),
6074            "% on '(' must land on matching ')' at col 4"
6075        );
6076    }
6077
6078    #[test]
6079    fn bracket_match_jumps_to_matching_open_paren() {
6080        // "(abc)", cursor at (0,4) on `)` — `%` must jump back to `(` at (0,0).
6081        let mut e = fresh_editor("(abc)");
6082        e.jump_cursor(0, 4);
6083        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6084        assert_eq!(
6085            e.cursor(),
6086            (0, 0),
6087            "% on ')' must land on matching '(' at col 0"
6088        );
6089    }
6090
6091    #[test]
6092    fn bracket_match_with_no_match_on_line_is_noop_or_engine_behaviour() {
6093        // "abcd", cursor at (0,2) — no bracket under cursor; engine returns
6094        // false from matching_bracket, cursor must not move.
6095        let mut e = fresh_editor("abcd");
6096        e.jump_cursor(0, 2);
6097        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6098        assert_eq!(
6099            e.cursor(),
6100            (0, 2),
6101            "% with no bracket under cursor must be a no-op"
6102        );
6103    }
6104
6105    // ── Scroll / viewport motion controller tests (Phase 3g) ──────────────────
6106
6107    /// Helper: build a 20-line buffer, set viewport to rows [5..14] (height=10).
6108    fn fresh_viewport_editor() -> Editor {
6109        let content = many_lines(20);
6110        let mut e = Editor::new(
6111            hjkl_buffer::Buffer::from_str(&content),
6112            crate::types::DefaultHost::new(),
6113            crate::types::Options::default(),
6114        );
6115        // height=10, top_row=5 → visible rows 5..14.
6116        // set_viewport_height stores to the atomic; sync_buffer_from_textarea
6117        // propagates it to host.viewport_mut().height so motion helpers see it.
6118        e.set_viewport_height(10);
6119        e.sync_buffer_from_textarea();
6120        e.host_mut().viewport_mut().top_row = 5;
6121        e
6122    }
6123
6124    #[test]
6125    fn viewport_top_lands_on_first_visible_row() {
6126        // Viewport top=5, height=10. H (count=1) should land on row 5
6127        // (the first visible row, offset = count-1 = 0).
6128        let mut e = fresh_viewport_editor();
6129        e.jump_cursor(10, 0);
6130        e.apply_motion(crate::MotionKind::ViewportTop, 1);
6131        assert_eq!(
6132            e.cursor().0,
6133            5,
6134            "H (count=1) must land on viewport top row (5)"
6135        );
6136    }
6137
6138    #[test]
6139    fn viewport_top_with_count_offsets_down() {
6140        // H with count=3 → viewport top + (3-1) = 5 + 2 = row 7.
6141        let mut e = fresh_viewport_editor();
6142        e.jump_cursor(12, 0);
6143        e.apply_motion(crate::MotionKind::ViewportTop, 3);
6144        assert_eq!(e.cursor().0, 7, "3H must land at viewport top + 2 = row 7");
6145    }
6146
6147    #[test]
6148    fn viewport_middle_lands_on_middle_visible_row() {
6149        // Viewport top=5, height=10 → last visible = 14, mid = 5 + (14-5)/2 = 9.
6150        let mut e = fresh_viewport_editor();
6151        e.jump_cursor(0, 0);
6152        e.apply_motion(crate::MotionKind::ViewportMiddle, 1);
6153        assert_eq!(e.cursor().0, 9, "M must land on middle visible row (9)");
6154    }
6155
6156    #[test]
6157    fn viewport_bottom_lands_on_last_visible_row() {
6158        // L (count=1) → viewport bottom, offset = count-1 = 0 → row 14.
6159        let mut e = fresh_viewport_editor();
6160        e.jump_cursor(5, 0);
6161        e.apply_motion(crate::MotionKind::ViewportBottom, 1);
6162        assert_eq!(
6163            e.cursor().0,
6164            14,
6165            "L (count=1) must land on viewport bottom row (14)"
6166        );
6167    }
6168
6169    #[test]
6170    fn half_page_down_moves_cursor_by_half_window() {
6171        // viewport height=10, so half=5. Cursor at row 0 → row 5 after C-d.
6172        let mut e = Editor::new(
6173            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6174            crate::types::DefaultHost::new(),
6175            crate::types::Options::default(),
6176        );
6177        e.set_viewport_height(10);
6178        e.jump_cursor(0, 0);
6179        e.apply_motion(crate::MotionKind::HalfPageDown, 1);
6180        assert_eq!(
6181            e.cursor().0,
6182            5,
6183            "<C-d> from row 0 with viewport height=10 must land on row 5"
6184        );
6185    }
6186
6187    #[test]
6188    fn half_page_up_moves_cursor_by_half_window_reverse() {
6189        // viewport height=10, half=5. Cursor at row 10 → row 5 after C-u.
6190        let mut e = Editor::new(
6191            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6192            crate::types::DefaultHost::new(),
6193            crate::types::Options::default(),
6194        );
6195        e.set_viewport_height(10);
6196        e.jump_cursor(10, 0);
6197        e.apply_motion(crate::MotionKind::HalfPageUp, 1);
6198        assert_eq!(
6199            e.cursor().0,
6200            5,
6201            "<C-u> from row 10 with viewport height=10 must land on row 5"
6202        );
6203    }
6204
6205    #[test]
6206    fn full_page_down_moves_cursor_by_full_window() {
6207        // viewport height=10, full = 10 - 2 = 8. Cursor at row 0 → row 8.
6208        let mut e = Editor::new(
6209            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6210            crate::types::DefaultHost::new(),
6211            crate::types::Options::default(),
6212        );
6213        e.set_viewport_height(10);
6214        e.jump_cursor(0, 0);
6215        e.apply_motion(crate::MotionKind::FullPageDown, 1);
6216        assert_eq!(
6217            e.cursor().0,
6218            8,
6219            "<C-f> from row 0 with viewport height=10 must land on row 8"
6220        );
6221    }
6222
6223    #[test]
6224    fn full_page_up_moves_cursor_by_full_window_reverse() {
6225        // viewport height=10, full=8. Cursor at row 10 → row 2.
6226        let mut e = Editor::new(
6227            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6228            crate::types::DefaultHost::new(),
6229            crate::types::Options::default(),
6230        );
6231        e.set_viewport_height(10);
6232        e.jump_cursor(10, 0);
6233        e.apply_motion(crate::MotionKind::FullPageUp, 1);
6234        assert_eq!(
6235            e.cursor().0,
6236            2,
6237            "<C-b> from row 10 with viewport height=10 must land on row 2"
6238        );
6239    }
6240
6241    // ── set_mark_at_cursor unit tests ─────────────────────────────────────────
6242
6243    #[test]
6244    fn set_mark_at_cursor_alphabetic_records() {
6245        // `ma` at (0, 2) — mark 'a' must store (0, 2).
6246        let mut e = fresh_editor("hello");
6247        e.jump_cursor(0, 2);
6248        e.set_mark_at_cursor('a');
6249        assert_eq!(
6250            e.mark('a'),
6251            Some((0, 2)),
6252            "mark 'a' must record current pos"
6253        );
6254    }
6255
6256    #[test]
6257    fn set_mark_at_cursor_invalid_char_no_op() {
6258        // Invalid chars (digits, special) must not store a mark.
6259        let mut e = fresh_editor("hello");
6260        e.jump_cursor(0, 1);
6261        e.set_mark_at_cursor('1'); // digit — not alphanumeric in vim mark sense
6262        assert_eq!(e.mark('1'), None, "digit mark must be a no-op");
6263        e.set_mark_at_cursor('['); // special — only goto uses '[', not set_mark
6264        assert_eq!(
6265            e.mark('['),
6266            None,
6267            "bracket char must be a no-op for set_mark"
6268        );
6269    }
6270
6271    #[test]
6272    fn set_mark_at_cursor_special_left_bracket() {
6273        // Confirm '[' is NOT stored by set_mark_at_cursor (vim's `m[` is invalid).
6274        // The `[` mark is only set automatically by operator paths, not `m[`.
6275        let mut e = fresh_editor("hello");
6276        e.jump_cursor(0, 3);
6277        e.set_mark_at_cursor('[');
6278        assert_eq!(
6279            e.mark('['),
6280            None,
6281            "set_mark_at_cursor must reject '[' (vim: m[ is invalid)"
6282        );
6283    }
6284
6285    // ── goto_mark_line unit tests ─────────────────────────────────────────────
6286
6287    #[test]
6288    fn goto_mark_line_jumps_to_first_non_blank() {
6289        // Set mark 'a' at (1, 3), then jump back to (0, 0).
6290        // `'a` (linewise) must land on row 1, first non-blank column.
6291        let mut e = fresh_editor("hello\n  world\n");
6292        e.jump_cursor(1, 3);
6293        e.set_mark_at_cursor('a');
6294        e.jump_cursor(0, 0);
6295        e.goto_mark_line('a');
6296        assert_eq!(e.cursor().0, 1, "goto_mark_line must jump to mark row");
6297        // "  world" — first non-blank is col 2.
6298        assert_eq!(
6299            e.cursor().1,
6300            2,
6301            "goto_mark_line must land on first non-blank column"
6302        );
6303    }
6304
6305    #[test]
6306    fn goto_mark_line_unset_mark_no_op() {
6307        // Jumping to an unset mark must not move the cursor.
6308        let mut e = fresh_editor("hello\nworld\n");
6309        e.jump_cursor(1, 2);
6310        e.goto_mark_line('z'); // 'z' not set
6311        assert_eq!(e.cursor(), (1, 2), "unset mark jump must be a no-op");
6312    }
6313
6314    #[test]
6315    fn goto_mark_line_invalid_char_no_op() {
6316        // '!' is not a valid mark char — must not move cursor.
6317        let mut e = fresh_editor("hello\nworld\n");
6318        e.jump_cursor(0, 0);
6319        e.goto_mark_line('!');
6320        assert_eq!(e.cursor(), (0, 0), "invalid mark char must be a no-op");
6321    }
6322
6323    // ── goto_mark_char unit tests ─────────────────────────────────────────────
6324
6325    #[test]
6326    fn goto_mark_char_jumps_to_exact_pos() {
6327        // Set mark 'b' at (1, 4), then jump back to (0, 0).
6328        // `` `b `` (charwise) must land on (1, 4) exactly.
6329        let mut e = fresh_editor("hello\nworld\n");
6330        e.jump_cursor(1, 4);
6331        e.set_mark_at_cursor('b');
6332        e.jump_cursor(0, 0);
6333        e.goto_mark_char('b');
6334        assert_eq!(
6335            e.cursor(),
6336            (1, 4),
6337            "goto_mark_char must jump to exact mark position"
6338        );
6339    }
6340
6341    #[test]
6342    fn goto_mark_char_unset_mark_no_op() {
6343        // Jumping to an unset mark must not move the cursor.
6344        let mut e = fresh_editor("hello\nworld\n");
6345        e.jump_cursor(1, 1);
6346        e.goto_mark_char('x'); // 'x' not set
6347        assert_eq!(
6348            e.cursor(),
6349            (1, 1),
6350            "unset charwise mark jump must be a no-op"
6351        );
6352    }
6353
6354    #[test]
6355    fn goto_mark_char_invalid_char_no_op() {
6356        // '#' is not a valid mark char — must not move cursor.
6357        let mut e = fresh_editor("hello\nworld\n");
6358        e.jump_cursor(0, 2);
6359        e.goto_mark_char('#');
6360        assert_eq!(
6361            e.cursor(),
6362            (0, 2),
6363            "invalid charwise mark char must be a no-op"
6364        );
6365    }
6366
6367    // ── Macro controller API tests (Phase 5b) ─────────────────────────────────
6368
6369    #[test]
6370    fn start_macro_record_records_register() {
6371        let mut e = fresh_editor("hello");
6372        assert!(!e.is_recording_macro());
6373        e.start_macro_record('a');
6374        assert!(e.is_recording_macro());
6375        assert_eq!(e.recording_register(), Some('a'));
6376    }
6377
6378    #[test]
6379    fn start_macro_record_capital_seeds_existing() {
6380        // `qa` records "h", stop. Then `qA` should seed from existing 'a' reg.
6381        let mut e = fresh_editor("hello");
6382        e.start_macro_record('a');
6383        e.record_input(crate::input::Input {
6384            key: crate::input::Key::Char('h'),
6385            ..Default::default()
6386        });
6387        e.stop_macro_record();
6388        // Start capital 'A' — should seed from existing 'a' register.
6389        e.start_macro_record('A');
6390        // recording_keys should now contain 1 input (the seeded 'h').
6391        assert_eq!(
6392            e.vim.recording_keys.len(),
6393            1,
6394            "capital record must seed from existing lowercase reg"
6395        );
6396    }
6397
6398    #[test]
6399    fn stop_macro_record_writes_register() {
6400        let mut e = fresh_editor("hello");
6401        e.start_macro_record('a');
6402        e.record_input(crate::input::Input {
6403            key: crate::input::Key::Char('h'),
6404            ..Default::default()
6405        });
6406        e.record_input(crate::input::Input {
6407            key: crate::input::Key::Char('l'),
6408            ..Default::default()
6409        });
6410        e.stop_macro_record();
6411        assert!(!e.is_recording_macro());
6412        // Register 'a' should contain "hl".
6413        let text = e
6414            .registers()
6415            .read('a')
6416            .map(|s| s.text.clone())
6417            .unwrap_or_default();
6418        assert_eq!(
6419            text, "hl",
6420            "stop_macro_record must write encoded keys to register"
6421        );
6422    }
6423
6424    #[test]
6425    fn is_recording_macro_reflects_state() {
6426        let mut e = fresh_editor("hello");
6427        assert!(!e.is_recording_macro());
6428        e.start_macro_record('b');
6429        assert!(e.is_recording_macro());
6430        e.stop_macro_record();
6431        assert!(!e.is_recording_macro());
6432    }
6433
6434    #[test]
6435    fn play_macro_returns_decoded_inputs() {
6436        let mut e = fresh_editor("hello");
6437        // Write "jj" into register 'a'.
6438        e.set_named_register_text('a', "jj".to_string());
6439        let inputs = e.play_macro('a', 1);
6440        assert_eq!(inputs.len(), 2);
6441        assert_eq!(inputs[0].key, crate::input::Key::Char('j'));
6442        assert_eq!(inputs[1].key, crate::input::Key::Char('j'));
6443        assert!(e.is_replaying_macro(), "play_macro must set replaying flag");
6444        e.end_macro_replay();
6445        assert!(!e.is_replaying_macro());
6446    }
6447
6448    #[test]
6449    fn play_macro_at_uses_last_macro() {
6450        let mut e = fresh_editor("hello");
6451        e.set_named_register_text('a', "k".to_string());
6452        // Play 'a' first to set last_macro.
6453        let _ = e.play_macro('a', 1);
6454        e.end_macro_replay();
6455        // Now `@@` should replay 'a' again.
6456        let inputs = e.play_macro('@', 1);
6457        assert_eq!(inputs.len(), 1);
6458        assert_eq!(inputs[0].key, crate::input::Key::Char('k'));
6459        e.end_macro_replay();
6460    }
6461
6462    #[test]
6463    fn play_macro_with_count_repeats() {
6464        let mut e = fresh_editor("hello");
6465        e.set_named_register_text('a', "j".to_string());
6466        let inputs = e.play_macro('a', 3);
6467        assert_eq!(inputs.len(), 3, "3@a must produce 3 inputs");
6468        e.end_macro_replay();
6469    }
6470
6471    #[test]
6472    fn record_input_appends_when_recording() {
6473        let mut e = fresh_editor("hello");
6474        // Not recording: record_input is a no-op.
6475        e.record_input(crate::input::Input {
6476            key: crate::input::Key::Char('j'),
6477            ..Default::default()
6478        });
6479        assert_eq!(e.vim.recording_keys.len(), 0);
6480        // Start recording: record_input appends.
6481        e.start_macro_record('a');
6482        e.record_input(crate::input::Input {
6483            key: crate::input::Key::Char('j'),
6484            ..Default::default()
6485        });
6486        e.record_input(crate::input::Input {
6487            key: crate::input::Key::Char('k'),
6488            ..Default::default()
6489        });
6490        assert_eq!(e.vim.recording_keys.len(), 2);
6491        // During replay: record_input must NOT append.
6492        e.vim.replaying_macro = true;
6493        e.record_input(crate::input::Input {
6494            key: crate::input::Key::Char('l'),
6495            ..Default::default()
6496        });
6497        assert_eq!(
6498            e.vim.recording_keys.len(),
6499            2,
6500            "record_input must skip during replay"
6501        );
6502        e.vim.replaying_macro = false;
6503        e.stop_macro_record();
6504    }
6505
6506    // ── Phase 6.1 insert-mode primitive tests (kryptic-sh/hjkl#87) ────────────
6507
6508    /// Helper: enter insert mode via the public bridge, then call the method under test.
6509    fn enter_insert(e: &mut Editor) {
6510        e.enter_insert_i(1);
6511        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6512    }
6513
6514    #[test]
6515    fn insert_char_basic() {
6516        let mut e = fresh_editor("hello");
6517        enter_insert(&mut e);
6518        e.insert_char('X');
6519        assert_eq!(e.buffer().lines()[0], "Xhello");
6520        assert!(e.take_dirty());
6521    }
6522
6523    #[test]
6524    fn insert_newline_splits_line() {
6525        let mut e = fresh_editor("hello");
6526        // Move to col 3 so we split "hel" | "lo".
6527        e.jump_cursor(0, 3);
6528        enter_insert(&mut e);
6529        e.insert_newline();
6530        let lines = e.buffer().lines().to_vec();
6531        assert_eq!(lines[0], "hel");
6532        assert_eq!(lines[1], "lo");
6533    }
6534
6535    #[test]
6536    fn insert_tab_expandtab_inserts_spaces() {
6537        let mut e = fresh_editor("");
6538        // Default options: expandtab=true, softtabstop=4, tabstop=4.
6539        enter_insert(&mut e);
6540        e.insert_tab();
6541        // At col 0 with sts=4: 4 spaces inserted.
6542        assert_eq!(e.buffer().lines()[0], "    ");
6543    }
6544
6545    #[test]
6546    fn insert_tab_real_tab_when_noexpandtab() {
6547        let opts = crate::types::Options {
6548            expandtab: false,
6549            ..crate::types::Options::default()
6550        };
6551        let mut e = Editor::new(
6552            hjkl_buffer::Buffer::new(),
6553            crate::types::DefaultHost::new(),
6554            opts,
6555        );
6556        e.set_content("");
6557        enter_insert(&mut e);
6558        e.insert_tab();
6559        assert_eq!(e.buffer().lines()[0], "\t");
6560    }
6561
6562    #[test]
6563    fn insert_backspace_single_char() {
6564        // Cursor at col 3 in "hello", backspace deletes 'l'.
6565        let mut e = fresh_editor("hello");
6566        e.jump_cursor(0, 3);
6567        enter_insert(&mut e);
6568        e.insert_backspace();
6569        assert_eq!(e.buffer().lines()[0], "helo");
6570    }
6571
6572    #[test]
6573    fn insert_backspace_softtabstop() {
6574        // With sts=4, expandtab: 4 spaces at col 4 → one backspace deletes all 4.
6575        let mut e = fresh_editor("    hello");
6576        e.jump_cursor(0, 4);
6577        enter_insert(&mut e);
6578        e.insert_backspace();
6579        assert_eq!(e.buffer().lines()[0], "hello");
6580    }
6581
6582    #[test]
6583    fn insert_backspace_join_up() {
6584        // At col 0 on row 1, backspace joins with the previous line.
6585        let mut e = fresh_editor("foo\nbar");
6586        e.jump_cursor(1, 0);
6587        enter_insert(&mut e);
6588        e.insert_backspace();
6589        // Two rows merged into one.
6590        assert_eq!(e.buffer().lines().len(), 1);
6591        assert_eq!(e.buffer().lines()[0], "foobar");
6592    }
6593
6594    #[test]
6595    fn leave_insert_steps_back_col() {
6596        // Esc in insert mode should move the cursor one cell left (vim convention).
6597        let mut e = fresh_editor("hello");
6598        e.jump_cursor(0, 3);
6599        enter_insert(&mut e);
6600        // Type one char so cursor is at col 4, then call leave_insert_to_normal.
6601        e.insert_char('X');
6602        // cursor is now at col 4 (after the inserted 'X').
6603        let pre_col = e.cursor().1;
6604        e.leave_insert_to_normal();
6605        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6606        // Cursor stepped back one.
6607        assert_eq!(e.cursor().1, pre_col - 1);
6608    }
6609
6610    #[test]
6611    fn insert_ctrl_w_word_back() {
6612        // Ctrl-W deletes from cursor back to word start.
6613        // "hello world" — cursor at end of "world" (col 11).
6614        let mut e = fresh_editor("hello world");
6615        // Normal mode clamps cursor to col 10 (last char); jump_cursor doesn't clamp.
6616        e.jump_cursor(0, 11);
6617        enter_insert(&mut e);
6618        e.insert_ctrl_w();
6619        // "world" (5 chars) deleted, leaving "hello ".
6620        assert_eq!(e.buffer().lines()[0], "hello ");
6621    }
6622
6623    #[test]
6624    fn insert_ctrl_u_deletes_to_line_start() {
6625        let mut e = fresh_editor("hello world");
6626        e.jump_cursor(0, 5);
6627        enter_insert(&mut e);
6628        e.insert_ctrl_u();
6629        assert_eq!(e.buffer().lines()[0], " world");
6630    }
6631
6632    #[test]
6633    fn insert_ctrl_h_single_backspace() {
6634        // Ctrl-H is an alias for Backspace in insert mode.
6635        let mut e = fresh_editor("hello");
6636        e.jump_cursor(0, 3);
6637        enter_insert(&mut e);
6638        e.insert_ctrl_h();
6639        assert_eq!(e.buffer().lines()[0], "helo");
6640    }
6641
6642    #[test]
6643    fn insert_ctrl_h_join_up() {
6644        let mut e = fresh_editor("foo\nbar");
6645        e.jump_cursor(1, 0);
6646        enter_insert(&mut e);
6647        e.insert_ctrl_h();
6648        assert_eq!(e.buffer().lines().len(), 1);
6649        assert_eq!(e.buffer().lines()[0], "foobar");
6650    }
6651
6652    #[test]
6653    fn insert_ctrl_t_indents_current_line() {
6654        let mut e = Editor::new(
6655            hjkl_buffer::Buffer::new(),
6656            crate::types::DefaultHost::new(),
6657            crate::types::Options {
6658                shiftwidth: 4,
6659                ..crate::types::Options::default()
6660            },
6661        );
6662        e.set_content("hello");
6663        enter_insert(&mut e);
6664        e.insert_ctrl_t();
6665        assert_eq!(e.buffer().lines()[0], "    hello");
6666    }
6667
6668    #[test]
6669    fn insert_ctrl_d_outdents_current_line() {
6670        let mut e = Editor::new(
6671            hjkl_buffer::Buffer::new(),
6672            crate::types::DefaultHost::new(),
6673            crate::types::Options {
6674                shiftwidth: 4,
6675                ..crate::types::Options::default()
6676            },
6677        );
6678        e.set_content("    hello");
6679        enter_insert(&mut e);
6680        e.insert_ctrl_d();
6681        assert_eq!(e.buffer().lines()[0], "hello");
6682    }
6683
6684    #[test]
6685    fn insert_ctrl_o_arm_sets_one_shot_normal() {
6686        let mut e = fresh_editor("hello");
6687        enter_insert(&mut e);
6688        e.insert_ctrl_o_arm();
6689        // Mode should flip to Normal (one-shot).
6690        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6691    }
6692
6693    #[test]
6694    fn insert_ctrl_r_arm_sets_pending_register() {
6695        let mut e = fresh_editor("hello");
6696        enter_insert(&mut e);
6697        e.insert_ctrl_r_arm();
6698        // pending register flag set; mode stays Insert.
6699        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6700        assert!(e.vim.insert_pending_register);
6701    }
6702
6703    #[test]
6704    fn insert_delete_removes_char_under_cursor() {
6705        let mut e = fresh_editor("hello");
6706        e.jump_cursor(0, 2);
6707        enter_insert(&mut e);
6708        e.insert_delete();
6709        assert_eq!(e.buffer().lines()[0], "helo");
6710    }
6711
6712    #[test]
6713    fn insert_delete_joins_lines_at_eol() {
6714        let mut e = fresh_editor("foo\nbar");
6715        // Position at end of row 0 (col 3 = past last char).
6716        e.jump_cursor(0, 3);
6717        enter_insert(&mut e);
6718        e.insert_delete();
6719        assert_eq!(e.buffer().lines().len(), 1);
6720        assert_eq!(e.buffer().lines()[0], "foobar");
6721    }
6722
6723    #[test]
6724    fn insert_arrow_left_moves_cursor() {
6725        let mut e = fresh_editor("hello");
6726        e.jump_cursor(0, 3);
6727        enter_insert(&mut e);
6728        e.insert_arrow(crate::vim::InsertDir::Left);
6729        assert_eq!(e.cursor().1, 2);
6730    }
6731
6732    #[test]
6733    fn insert_arrow_right_moves_cursor() {
6734        let mut e = fresh_editor("hello");
6735        e.jump_cursor(0, 2);
6736        enter_insert(&mut e);
6737        e.insert_arrow(crate::vim::InsertDir::Right);
6738        assert_eq!(e.cursor().1, 3);
6739    }
6740
6741    #[test]
6742    fn insert_arrow_up_moves_cursor() {
6743        let mut e = fresh_editor("foo\nbar");
6744        e.jump_cursor(1, 0);
6745        enter_insert(&mut e);
6746        e.insert_arrow(crate::vim::InsertDir::Up);
6747        assert_eq!(e.cursor().0, 0);
6748    }
6749
6750    #[test]
6751    fn insert_arrow_down_moves_cursor() {
6752        let mut e = fresh_editor("foo\nbar");
6753        e.jump_cursor(0, 0);
6754        enter_insert(&mut e);
6755        e.insert_arrow(crate::vim::InsertDir::Down);
6756        assert_eq!(e.cursor().0, 1);
6757    }
6758
6759    #[test]
6760    fn insert_home_moves_to_line_start() {
6761        let mut e = fresh_editor("hello");
6762        e.jump_cursor(0, 4);
6763        enter_insert(&mut e);
6764        e.insert_home();
6765        assert_eq!(e.cursor().1, 0);
6766    }
6767
6768    #[test]
6769    fn insert_end_moves_to_line_end() {
6770        let mut e = fresh_editor("hello");
6771        e.jump_cursor(0, 0);
6772        enter_insert(&mut e);
6773        e.insert_end();
6774        // move_line_end lands on the last char (col 4) for "hello".
6775        assert_eq!(e.cursor().1, 4);
6776    }
6777
6778    #[test]
6779    fn insert_pageup_does_not_panic() {
6780        let mut e = fresh_editor("line1\nline2\nline3");
6781        e.jump_cursor(2, 0);
6782        enter_insert(&mut e);
6783        // Viewport height 0 → no crash (viewport_h saturates to 1 row effectively).
6784        e.insert_pageup(24);
6785    }
6786
6787    #[test]
6788    fn insert_pagedown_does_not_panic() {
6789        let mut e = fresh_editor("line1\nline2\nline3");
6790        e.jump_cursor(0, 0);
6791        enter_insert(&mut e);
6792        e.insert_pagedown(24);
6793    }
6794
6795    #[test]
6796    fn leave_insert_to_normal_exits_mode() {
6797        let mut e = fresh_editor("hello");
6798        enter_insert(&mut e);
6799        e.leave_insert_to_normal();
6800        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6801    }
6802
6803    #[test]
6804    fn insert_backspace_at_buffer_start_is_noop() {
6805        let mut e = fresh_editor("hello");
6806        e.jump_cursor(0, 0);
6807        enter_insert(&mut e);
6808        // No previous char and no previous row — should not panic.
6809        e.insert_backspace();
6810        assert_eq!(e.buffer().lines()[0], "hello");
6811    }
6812
6813    #[test]
6814    fn insert_delete_at_buffer_end_is_noop() {
6815        let mut e = fresh_editor("hello");
6816        // Cursor at col 5 (past last char index of 4), no next row.
6817        e.jump_cursor(0, 5);
6818        enter_insert(&mut e);
6819        // col 5 >= line_chars (5), no next row → no-op.
6820        e.insert_delete();
6821        assert_eq!(e.buffer().lines()[0], "hello");
6822    }
6823
6824    // ── Phase 6.2: normal-mode primitive tests (kryptic-sh/hjkl#88) ─────────
6825
6826    // Helper: set content and ensure we are in Normal mode.
6827    fn normal_editor(initial: &str) -> Editor {
6828        let e = fresh_editor(initial);
6829        // fresh_editor starts in Normal; this is just a readability alias.
6830        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6831        e
6832    }
6833
6834    // ── Insert-mode entry ────────────────────────────────────────────────────
6835
6836    #[test]
6837    fn enter_insert_i_lands_in_insert_at_cursor() {
6838        let mut e = normal_editor("hello");
6839        e.jump_cursor(0, 2);
6840        e.enter_insert_i(1);
6841        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6842        assert_eq!(e.cursor(), (0, 2));
6843    }
6844
6845    #[test]
6846    fn enter_insert_shift_i_moves_to_first_non_blank_then_insert() {
6847        let mut e = normal_editor("  hello");
6848        e.jump_cursor(0, 5);
6849        e.enter_insert_shift_i(1);
6850        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6851        // First non-blank of "  hello" is col 2.
6852        assert_eq!(e.cursor().1, 2);
6853    }
6854
6855    #[test]
6856    fn enter_insert_a_advances_one_then_insert() {
6857        let mut e = normal_editor("hello");
6858        e.jump_cursor(0, 0);
6859        e.enter_insert_a(1);
6860        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6861        assert_eq!(e.cursor().1, 1);
6862    }
6863
6864    #[test]
6865    fn enter_insert_shift_a_lands_at_eol() {
6866        let mut e = normal_editor("hello");
6867        e.enter_insert_shift_a(1);
6868        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6869        assert_eq!(e.cursor().1, 5);
6870    }
6871
6872    #[test]
6873    fn open_line_below_creates_new_line_and_insert() {
6874        let mut e = normal_editor("hello\nworld");
6875        e.open_line_below(1);
6876        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6877        assert_eq!(e.buffer().lines().len(), 3);
6878    }
6879
6880    #[test]
6881    fn open_line_above_creates_line_before_cursor() {
6882        let mut e = normal_editor("hello\nworld");
6883        e.jump_cursor(1, 0);
6884        e.open_line_above(1);
6885        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6886        assert_eq!(e.buffer().lines().len(), 3);
6887        assert_eq!(e.cursor().0, 1);
6888    }
6889
6890    #[test]
6891    fn open_line_above_at_row_0_creates_blank_first_line() {
6892        let mut e = normal_editor("hello");
6893        e.open_line_above(1);
6894        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6895        // New blank line is row 0; old "hello" is row 1.
6896        assert_eq!(e.cursor().0, 0);
6897        assert_eq!(e.buffer().lines()[1], "hello");
6898    }
6899
6900    #[test]
6901    fn enter_replace_mode_sets_insert_mode() {
6902        let mut e = normal_editor("hello");
6903        e.enter_replace_mode(1);
6904        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6905    }
6906
6907    // ── Char / line ops ──────────────────────────────────────────────────────
6908
6909    #[test]
6910    fn delete_char_forward_removes_one_char() {
6911        let mut e = normal_editor("hello");
6912        e.jump_cursor(0, 1);
6913        e.delete_char_forward(1);
6914        assert_eq!(e.buffer().lines()[0], "hllo");
6915    }
6916
6917    #[test]
6918    fn delete_char_forward_count_5_removes_five() {
6919        let mut e = normal_editor("hello world");
6920        e.delete_char_forward(5);
6921        assert_eq!(e.buffer().lines()[0], " world");
6922    }
6923
6924    #[test]
6925    fn delete_char_forward_noop_on_empty_line() {
6926        let mut e = normal_editor("");
6927        let before = e.content().to_string();
6928        e.delete_char_forward(1);
6929        // Empty buffer: no chars to delete, content unchanged.
6930        assert_eq!(e.content(), before.as_str());
6931    }
6932
6933    #[test]
6934    fn delete_char_backward_removes_char_before_cursor() {
6935        let mut e = normal_editor("hello");
6936        e.jump_cursor(0, 3);
6937        e.delete_char_backward(1);
6938        assert_eq!(e.buffer().lines()[0], "helo");
6939    }
6940
6941    #[test]
6942    fn delete_char_backward_noop_at_col_0() {
6943        let mut e = normal_editor("hello");
6944        e.jump_cursor(0, 0);
6945        e.delete_char_backward(1);
6946        assert_eq!(e.buffer().lines()[0], "hello");
6947    }
6948
6949    #[test]
6950    fn substitute_char_deletes_and_enters_insert() {
6951        let mut e = normal_editor("hello");
6952        e.jump_cursor(0, 0);
6953        e.substitute_char(1);
6954        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6955        assert_eq!(e.buffer().lines()[0], "ello");
6956    }
6957
6958    #[test]
6959    fn substitute_char_count_3_deletes_three() {
6960        let mut e = normal_editor("hello");
6961        e.substitute_char(3);
6962        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6963        assert_eq!(e.buffer().lines()[0], "lo");
6964    }
6965
6966    #[test]
6967    fn substitute_line_clears_content_and_enters_insert() {
6968        let mut e = normal_editor("hello world");
6969        e.substitute_line(1);
6970        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6971        assert_eq!(e.buffer().lines()[0], "");
6972    }
6973
6974    #[test]
6975    fn delete_to_eol_removes_from_cursor_to_end() {
6976        let mut e = normal_editor("hello world");
6977        e.jump_cursor(0, 5);
6978        e.delete_to_eol();
6979        // col 5 is ' ' — deletes " world", leaving "hello".
6980        assert_eq!(e.buffer().lines()[0], "hello");
6981    }
6982
6983    #[test]
6984    fn delete_to_eol_noop_when_cursor_past_end() {
6985        let mut e = normal_editor("hi");
6986        e.jump_cursor(0, 2);
6987        e.delete_to_eol();
6988        assert_eq!(e.buffer().lines()[0], "hi");
6989    }
6990
6991    #[test]
6992    fn change_to_eol_enters_insert() {
6993        let mut e = normal_editor("hello world");
6994        e.jump_cursor(0, 5);
6995        e.change_to_eol();
6996        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6997        // col 5 is ' ' — deletes " world", leaving "hello".
6998        assert_eq!(e.buffer().lines()[0], "hello");
6999    }
7000
7001    #[test]
7002    fn yank_to_eol_fills_register() {
7003        let mut e = normal_editor("hello world");
7004        e.jump_cursor(0, 6);
7005        e.yank_to_eol(1);
7006        // Yank does not change mode.
7007        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7008        // Unnamed register holds the yanked text (col 6 is 'w' in "world").
7009        assert!(
7010            e.registers().unnamed.text.starts_with("world")
7011                || e.registers().unnamed.text.contains("world")
7012        );
7013    }
7014
7015    #[test]
7016    fn join_line_merges_next_line_with_space() {
7017        let mut e = normal_editor("foo\nbar");
7018        e.join_line(1);
7019        assert_eq!(e.buffer().lines()[0], "foo bar");
7020    }
7021
7022    #[test]
7023    fn join_line_count_2_merges_three_lines() {
7024        let mut e = normal_editor("a\nb\nc");
7025        e.join_line(2);
7026        // Our bridge calls join_line() `count` times, each joining the
7027        // current line with the next → 2 iterations: "a b c".
7028        assert_eq!(e.buffer().lines()[0], "a b c");
7029    }
7030
7031    #[test]
7032    fn join_line_noop_on_last_line() {
7033        let mut e = normal_editor("only");
7034        e.join_line(1);
7035        assert_eq!(e.buffer().lines()[0], "only");
7036    }
7037
7038    #[test]
7039    fn toggle_case_at_cursor_flips_letter() {
7040        let mut e = normal_editor("hello");
7041        e.toggle_case_at_cursor(1);
7042        assert_eq!(e.buffer().lines()[0], "Hello");
7043    }
7044
7045    #[test]
7046    fn toggle_case_at_cursor_count_3_flips_three() {
7047        let mut e = normal_editor("hello");
7048        e.toggle_case_at_cursor(3);
7049        assert_eq!(e.buffer().lines()[0], "HELlo");
7050    }
7051
7052    // ── Undo / redo round-trip ───────────────────────────────────────────────
7053
7054    #[test]
7055    fn undo_redo_roundtrip_via_public_methods() {
7056        let mut e = normal_editor("hello");
7057        e.delete_char_forward(1);
7058        assert_eq!(e.buffer().lines()[0], "ello");
7059        e.undo();
7060        assert_eq!(e.buffer().lines()[0], "hello");
7061        e.redo();
7062        assert_eq!(e.buffer().lines()[0], "ello");
7063    }
7064
7065    // ── Jump / scroll ────────────────────────────────────────────────────────
7066
7067    #[test]
7068    fn jump_back_and_forward_roundtrip() {
7069        let mut e = fresh_editor("a\nb\nc\nd");
7070        e.set_viewport_height(10);
7071        e.jump_cursor(3, 0);
7072        // Push current pos onto jumplist (big motion done externally; use
7073        // `run_keys` shortcut: `gg` pushes jump then `G` jumps).
7074        // Simpler: just call jump_back with empty stack → no-op (shouldn't panic).
7075        e.jump_back(1);
7076        e.jump_forward(1);
7077    }
7078
7079    #[test]
7080    fn scroll_full_page_down_moves_cursor() {
7081        use crate::vim::ScrollDir;
7082        let lines = (0..30)
7083            .map(|i| format!("line{i}"))
7084            .collect::<Vec<_>>()
7085            .join("\n");
7086        let mut e = fresh_editor(&lines);
7087        e.set_viewport_height(10);
7088        let before = e.cursor().0;
7089        e.scroll_full_page(ScrollDir::Down, 1);
7090        assert!(e.cursor().0 > before);
7091    }
7092
7093    #[test]
7094    fn scroll_full_page_up_moves_cursor() {
7095        use crate::vim::ScrollDir;
7096        let lines = (0..30)
7097            .map(|i| format!("line{i}"))
7098            .collect::<Vec<_>>()
7099            .join("\n");
7100        let mut e = fresh_editor(&lines);
7101        e.set_viewport_height(10);
7102        e.jump_cursor(25, 0);
7103        let before = e.cursor().0;
7104        e.scroll_full_page(ScrollDir::Up, 1);
7105        assert!(e.cursor().0 < before);
7106    }
7107
7108    #[test]
7109    fn scroll_half_page_down_moves_cursor() {
7110        use crate::vim::ScrollDir;
7111        let lines = (0..30)
7112            .map(|i| format!("line{i}"))
7113            .collect::<Vec<_>>()
7114            .join("\n");
7115        let mut e = fresh_editor(&lines);
7116        e.set_viewport_height(10);
7117        let before = e.cursor().0;
7118        e.scroll_half_page(ScrollDir::Down, 1);
7119        assert!(e.cursor().0 > before);
7120    }
7121
7122    #[test]
7123    fn scroll_half_page_up_at_top_is_noop() {
7124        use crate::vim::ScrollDir;
7125        let lines = (0..30)
7126            .map(|i| format!("line{i}"))
7127            .collect::<Vec<_>>()
7128            .join("\n");
7129        let mut e = fresh_editor(&lines);
7130        e.set_viewport_height(10);
7131        // Already at top, scrolling up should not panic and cursor stays at 0.
7132        e.scroll_half_page(ScrollDir::Up, 1);
7133        assert_eq!(e.cursor().0, 0);
7134    }
7135
7136    #[test]
7137    fn scroll_line_down_shifts_viewport_without_moving_cursor() {
7138        use crate::vim::ScrollDir;
7139        let lines = (0..30)
7140            .map(|i| format!("line{i}"))
7141            .collect::<Vec<_>>()
7142            .join("\n");
7143        let mut e = fresh_editor(&lines);
7144        e.set_viewport_height(10);
7145        // Park cursor in the middle of a large buffer.
7146        e.jump_cursor(15, 0);
7147        e.set_viewport_top(10);
7148        let cursor_before = e.cursor().0;
7149        e.scroll_line(ScrollDir::Down, 1);
7150        // Viewport top advances; cursor stays.
7151        assert_eq!(e.cursor().0, cursor_before);
7152        assert_eq!(e.host().viewport().top_row, 11);
7153    }
7154
7155    #[test]
7156    fn scroll_line_up_shifts_viewport() {
7157        use crate::vim::ScrollDir;
7158        let lines = (0..30)
7159            .map(|i| format!("line{i}"))
7160            .collect::<Vec<_>>()
7161            .join("\n");
7162        let mut e = fresh_editor(&lines);
7163        e.set_viewport_height(10);
7164        e.jump_cursor(15, 0);
7165        e.set_viewport_top(10);
7166        let cursor_before = e.cursor().0;
7167        e.scroll_line(ScrollDir::Up, 1);
7168        assert_eq!(e.cursor().0, cursor_before);
7169        assert_eq!(e.host().viewport().top_row, 9);
7170    }
7171
7172    #[test]
7173    fn scroll_line_clamps_cursor_when_off_screen() {
7174        use crate::vim::ScrollDir;
7175        let lines = (0..30)
7176            .map(|i| format!("line{i}"))
7177            .collect::<Vec<_>>()
7178            .join("\n");
7179        let mut e = fresh_editor(&lines);
7180        e.set_viewport_height(10);
7181        // Cursor at viewport top; scrolling down pushes it off — must clamp.
7182        e.jump_cursor(5, 0);
7183        e.set_viewport_top(5);
7184        e.scroll_line(ScrollDir::Down, 3);
7185        // New top = 8; cursor was at 5, which is now off-screen (< 8).
7186        // Cursor clamped to new top.
7187        assert!(e.cursor().0 >= 8);
7188    }
7189
7190    #[test]
7191    fn scroll_doesnt_crash_at_buffer_edges() {
7192        use crate::vim::ScrollDir;
7193        let mut e = normal_editor("single line");
7194        e.set_viewport_height(10);
7195        // Should not panic on any of these at-the-edge scrolls.
7196        e.scroll_full_page(ScrollDir::Down, 99);
7197        e.scroll_full_page(ScrollDir::Up, 99);
7198        e.scroll_half_page(ScrollDir::Down, 99);
7199        e.scroll_half_page(ScrollDir::Up, 99);
7200        e.scroll_line(ScrollDir::Down, 99);
7201        e.scroll_line(ScrollDir::Up, 99);
7202    }
7203
7204    // ── Horizontal scroll ────────────────────────────────────────────────────
7205
7206    #[test]
7207    fn scroll_right_advances_top_col() {
7208        let mut e = fresh_editor("hello world");
7209        e.set_viewport_height(10);
7210        e.scroll_right(5);
7211        assert_eq!(e.host().viewport().top_col, 5);
7212    }
7213
7214    #[test]
7215    fn scroll_left_does_not_underflow() {
7216        let mut e = fresh_editor("hello world");
7217        e.set_viewport_height(10);
7218        e.scroll_right(2);
7219        e.scroll_left(10);
7220        assert_eq!(e.host().viewport().top_col, 0);
7221    }
7222
7223    #[test]
7224    fn scroll_left_then_right_roundtrip() {
7225        let mut e = fresh_editor("hello world");
7226        e.set_viewport_height(10);
7227        e.scroll_right(10);
7228        e.scroll_left(3);
7229        assert_eq!(e.host().viewport().top_col, 7);
7230    }
7231
7232    // ── Search ───────────────────────────────────────────────────────────────
7233
7234    #[test]
7235    fn search_repeat_advances_to_next_match() {
7236        let mut e = fresh_editor("foo bar foo baz");
7237        // Use word_search to seed the search state (no search prompt needed).
7238        // `*` on "foo" at col 0 finds the second "foo" and sets last_search.
7239        e.word_search(true, true, 1);
7240        // Repeating forward wraps and finds the first "foo" again at col 0.
7241        e.search_repeat(true, 1);
7242        // Just ensure no panic and search state is valid.
7243        assert!(e.cursor().0 < e.buffer().lines().len());
7244    }
7245
7246    #[test]
7247    fn search_repeat_no_pattern_is_noop() {
7248        let mut e = normal_editor("hello world");
7249        let before = e.cursor();
7250        // No search pattern loaded — should not panic.
7251        e.search_repeat(true, 1);
7252        assert_eq!(e.cursor(), before);
7253    }
7254
7255    #[test]
7256    fn word_search_finds_word_under_cursor() {
7257        let mut e = fresh_editor("foo bar foo");
7258        // cursor starts at col 0 on "foo"
7259        e.word_search(true, true, 1);
7260        // Should jump to the second "foo" at col 8.
7261        assert_eq!(e.cursor().1, 8);
7262    }
7263
7264    #[test]
7265    fn word_search_whole_word_false_extracts_word_under_cursor() {
7266        // `g*` on "foo" (no `\b`) — use two lines so wrap can find the next match.
7267        let mut e = fresh_editor("foobar\nfoo baz");
7268        // Cursor on second line "foo" at col 0.
7269        e.jump_cursor(1, 0);
7270        // g* with whole_word=false: pattern = "foo", advance forward (skip current).
7271        // Starting at (1, 0), skip "foo" at (1,0), wrap to (0, 0) which matches "foo"
7272        // inside "foobar".
7273        e.word_search(true, false, 1);
7274        // Cursor should land on "foo" at row 0, col 0.
7275        assert_eq!(e.cursor(), (0, 0));
7276    }
7277
7278    #[test]
7279    fn word_search_backward_finds_previous_match() {
7280        let mut e = fresh_editor("foo bar foo");
7281        e.jump_cursor(0, 8); // on second "foo"
7282        e.word_search(false, true, 1);
7283        // Cursor should land on col 0 (first "foo").
7284        assert_eq!(e.cursor().1, 0);
7285    }
7286
7287    // ── Edge cases ───────────────────────────────────────────────────────────
7288
7289    #[test]
7290    fn delete_char_forward_on_single_char_line() {
7291        let mut e = normal_editor("x");
7292        e.delete_char_forward(1);
7293        assert_eq!(e.buffer().lines()[0], "");
7294    }
7295
7296    #[test]
7297    fn substitute_char_on_empty_line_is_noop_for_delete() {
7298        let mut e = normal_editor("");
7299        e.substitute_char(1);
7300        // Nothing to delete — but should enter Insert mode.
7301        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7302    }
7303
7304    #[test]
7305    fn join_line_10_iterations_clamps_gracefully() {
7306        let mut e = normal_editor("a\nb");
7307        // Joining 10 times on a 2-line buffer should not panic.
7308        e.join_line(10);
7309        // After the first join succeeds, the rest are no-ops.
7310        assert_eq!(e.buffer().lines()[0], "a b");
7311    }
7312
7313    #[test]
7314    fn toggle_case_past_line_end_is_noop() {
7315        let mut e = normal_editor("ab");
7316        e.jump_cursor(0, 5); // way past end
7317        e.toggle_case_at_cursor(1);
7318        // Should not panic.
7319        assert_eq!(e.buffer().lines()[0], "ab");
7320    }
7321
7322    // ── Phase 6.3: visual-mode primitive tests (kryptic-sh/hjkl#89) ──────────
7323
7324    // ── Visual entry ─────────────────────────────────────────────────────────
7325
7326    #[test]
7327    fn enter_visual_char_lands_in_visual_at_cursor() {
7328        let mut e = normal_editor("hello world");
7329        e.jump_cursor(0, 3);
7330        e.enter_visual_char();
7331        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7332        // Anchor should be at the cursor position we entered from.
7333        assert_eq!(e.vim.visual_anchor, (0, 3));
7334    }
7335
7336    #[test]
7337    fn enter_visual_line_lands_in_visual_line() {
7338        let mut e = normal_editor("hello\nworld");
7339        e.jump_cursor(1, 2);
7340        e.enter_visual_line();
7341        assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7342        // Line anchor should be the current row.
7343        assert_eq!(e.vim.visual_line_anchor, 1);
7344    }
7345
7346    #[test]
7347    fn enter_visual_block_lands_in_visual_block() {
7348        let mut e = normal_editor("hello\nworld");
7349        e.jump_cursor(0, 2);
7350        e.enter_visual_block();
7351        assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7352        // Block anchor and vcol should match the cursor column.
7353        assert_eq!(e.vim.block_anchor, (0, 2));
7354        assert_eq!(e.vim.block_vcol, 2);
7355    }
7356
7357    // ── Visual exit ──────────────────────────────────────────────────────────
7358
7359    #[test]
7360    fn exit_visual_to_normal_sets_marks_and_returns_to_normal() {
7361        let mut e = normal_editor("hello world");
7362        // Enter charwise visual at col 2, extend to col 5.
7363        e.jump_cursor(0, 2);
7364        e.enter_visual_char();
7365        e.jump_cursor(0, 5);
7366        e.exit_visual_to_normal();
7367        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7368        // `<` = (0, 2), `>` = (0, 5).
7369        assert_eq!(e.mark('<'), Some((0, 2)));
7370        assert_eq!(e.mark('>'), Some((0, 5)));
7371    }
7372
7373    #[test]
7374    fn exit_visual_to_normal_stores_last_visual() {
7375        let mut e = normal_editor("hello world");
7376        e.jump_cursor(0, 1);
7377        e.enter_visual_char();
7378        e.jump_cursor(0, 4);
7379        e.exit_visual_to_normal();
7380        // last_visual should be set so gv can restore it.
7381        assert!(e.vim.last_visual.is_some());
7382        let lv = e.vim.last_visual.unwrap();
7383        assert_eq!(lv.anchor, (0, 1));
7384        assert_eq!(lv.cursor, (0, 4));
7385    }
7386
7387    #[test]
7388    fn exit_visual_line_sets_marks_at_line_boundaries() {
7389        let mut e = normal_editor("alpha\nbeta\ngamma");
7390        e.enter_visual_line(); // row 0
7391        e.jump_cursor(1, 3);
7392        e.exit_visual_to_normal();
7393        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7394        // `<` snaps to (min_row, 0), `>` snaps to (max_row, last_col).
7395        assert_eq!(e.mark('<'), Some((0, 0)));
7396        let last_col_of_beta = "beta".chars().count() - 1;
7397        assert_eq!(e.mark('>'), Some((1, last_col_of_beta)));
7398    }
7399
7400    // ── visual_o_toggle ───────────────────────────────────────────────────────
7401
7402    #[test]
7403    fn visual_o_toggle_swaps_anchor_and_cursor_charwise() {
7404        let mut e = normal_editor("hello world");
7405        // Enter visual at col 0, extend to col 4.
7406        e.enter_visual_char(); // anchor = (0,0)
7407        e.jump_cursor(0, 4); // cursor at col 4
7408        // Selection bounds before toggle: anchor=0, cursor=4.
7409        let pre_anchor = e.vim.visual_anchor;
7410        let pre_cursor = e.cursor();
7411        e.visual_o_toggle();
7412        // After toggle: cursor jumps to old anchor, anchor = old cursor.
7413        assert_eq!(e.cursor(), pre_anchor, "cursor should move to old anchor");
7414        assert_eq!(
7415            e.vim.visual_anchor, pre_cursor,
7416            "anchor should take old cursor position"
7417        );
7418        // Mode is unchanged.
7419        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7420    }
7421
7422    #[test]
7423    fn visual_o_toggle_double_returns_to_start() {
7424        let mut e = normal_editor("hello world");
7425        e.enter_visual_char();
7426        e.jump_cursor(0, 4);
7427        let anchor0 = e.vim.visual_anchor;
7428        let cursor0 = e.cursor();
7429        e.visual_o_toggle();
7430        e.visual_o_toggle();
7431        // Two toggles restore original positions.
7432        assert_eq!(e.vim.visual_anchor, anchor0);
7433        assert_eq!(e.cursor(), cursor0);
7434    }
7435
7436    #[test]
7437    fn visual_o_toggle_linewise_swaps_anchor_row() {
7438        let mut e = normal_editor("alpha\nbeta\ngamma");
7439        e.enter_visual_line(); // anchor row = 0
7440        e.jump_cursor(2, 0); // cursor on row 2
7441        e.visual_o_toggle();
7442        // Cursor should jump to old anchor row.
7443        assert_eq!(e.cursor().0, 0, "cursor row should be old anchor row");
7444        // Anchor row should now be the old cursor row.
7445        assert_eq!(e.vim.visual_line_anchor, 2);
7446    }
7447
7448    // ── reenter_last_visual ───────────────────────────────────────────────────
7449
7450    #[test]
7451    fn reenter_last_visual_after_vdollar_esc_restores() {
7452        let mut e = normal_editor("hello world");
7453        // v$ then Esc via FSM to store a real last_visual.
7454        e.enter_visual_char(); // anchor = (0,0)
7455        e.jump_cursor(0, 5); // move cursor to col 5 to create a range
7456        e.exit_visual_to_normal();
7457        // Should be back to Normal.
7458        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7459        // gv — should restore Visual mode.
7460        e.reenter_last_visual();
7461        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7462        // Cursor should be at the stored last position (col 5).
7463        assert_eq!(e.cursor().1, 5);
7464    }
7465
7466    #[test]
7467    fn reenter_last_visual_noop_when_no_history() {
7468        let mut e = normal_editor("hello");
7469        // No prior visual — should be a no-op, not a panic.
7470        e.reenter_last_visual();
7471        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7472    }
7473
7474    // ── set_mode ─────────────────────────────────────────────────────────────
7475
7476    #[test]
7477    fn set_mode_insert_flips_vim_mode_to_insert() {
7478        let mut e = normal_editor("hello");
7479        e.set_mode(crate::VimMode::Insert);
7480        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7481    }
7482
7483    #[test]
7484    fn set_mode_roundtrip_normal_insert_normal() {
7485        let mut e = normal_editor("hello");
7486        e.set_mode(crate::VimMode::Insert);
7487        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7488        e.set_mode(crate::VimMode::Normal);
7489        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7490    }
7491
7492    #[test]
7493    fn set_mode_visual_variants() {
7494        let mut e = normal_editor("hello");
7495        e.set_mode(crate::VimMode::Visual);
7496        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7497        e.set_mode(crate::VimMode::VisualLine);
7498        assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7499        e.set_mode(crate::VimMode::VisualBlock);
7500        assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7501        e.set_mode(crate::VimMode::Normal);
7502        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7503    }
7504
7505    // ── current_mode / vim_mode consistency ───────────────────────────────────
7506
7507    // ── Phase 6.6b: FSM state accessor smoke tests ────────────────────────────
7508
7509    #[test]
7510    fn pending_round_trips() {
7511        let mut e = normal_editor("hello");
7512        assert!(matches!(e.pending(), crate::vim::Pending::None));
7513        e.set_pending(crate::vim::Pending::G);
7514        assert!(matches!(e.pending(), crate::vim::Pending::G));
7515        let taken = e.take_pending();
7516        assert!(matches!(taken, crate::vim::Pending::G));
7517        assert!(matches!(e.pending(), crate::vim::Pending::None));
7518    }
7519
7520    #[test]
7521    fn count_round_trips() {
7522        let mut e = normal_editor("hello");
7523        assert_eq!(e.count(), 0);
7524        e.set_count(5);
7525        assert_eq!(e.count(), 5);
7526        e.accumulate_count_digit(3);
7527        assert_eq!(e.count(), 53);
7528        e.reset_count();
7529        assert_eq!(e.count(), 0);
7530    }
7531
7532    #[test]
7533    fn take_count_returns_one_when_zero() {
7534        let mut e = normal_editor("hello");
7535        assert_eq!(e.take_count(), 1);
7536    }
7537
7538    #[test]
7539    fn take_count_returns_value_and_resets() {
7540        let mut e = normal_editor("hello");
7541        e.set_count(7);
7542        assert_eq!(e.take_count(), 7);
7543        assert_eq!(e.count(), 0);
7544    }
7545
7546    #[test]
7547    fn fsm_mode_round_trips() {
7548        let mut e = normal_editor("hello");
7549        assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7550        e.set_fsm_mode(crate::vim::Mode::Insert);
7551        assert_eq!(e.fsm_mode(), crate::vim::Mode::Insert);
7552        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7553        e.set_fsm_mode(crate::vim::Mode::Normal);
7554        assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7555    }
7556
7557    #[test]
7558    fn replaying_flag_round_trips() {
7559        let mut e = normal_editor("hello");
7560        assert!(!e.is_replaying());
7561        e.set_replaying(true);
7562        assert!(e.is_replaying());
7563        e.set_replaying(false);
7564        assert!(!e.is_replaying());
7565    }
7566
7567    #[test]
7568    fn one_shot_normal_flag_round_trips() {
7569        let mut e = normal_editor("hello");
7570        assert!(!e.is_one_shot_normal());
7571        e.set_one_shot_normal(true);
7572        assert!(e.is_one_shot_normal());
7573        e.set_one_shot_normal(false);
7574        assert!(!e.is_one_shot_normal());
7575    }
7576
7577    #[test]
7578    fn last_find_round_trips() {
7579        let mut e = normal_editor("hello");
7580        assert_eq!(e.last_find(), None);
7581        e.set_last_find(Some(('x', true, false)));
7582        assert_eq!(e.last_find(), Some(('x', true, false)));
7583        e.set_last_find(None);
7584        assert_eq!(e.last_find(), None);
7585    }
7586
7587    #[test]
7588    fn last_change_round_trips() {
7589        let mut e = normal_editor("hello");
7590        assert!(e.last_change().is_none());
7591        e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 2 }));
7592        let lc = e.last_change();
7593        assert!(matches!(
7594            lc,
7595            Some(crate::vim::LastChange::ToggleCase { count: 2 })
7596        ));
7597        e.set_last_change(None);
7598        assert!(e.last_change().is_none());
7599    }
7600
7601    #[test]
7602    fn last_change_mut_allows_in_place_edit() {
7603        let mut e = normal_editor("hello");
7604        e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 1 }));
7605        if let Some(crate::vim::LastChange::ToggleCase { count }) = e.last_change_mut() {
7606            *count = 42;
7607        }
7608        assert!(matches!(
7609            e.last_change(),
7610            Some(crate::vim::LastChange::ToggleCase { count: 42 })
7611        ));
7612    }
7613
7614    #[test]
7615    fn insert_session_round_trips() {
7616        let mut e = normal_editor("hello");
7617        assert!(e.insert_session().is_none());
7618        e.set_insert_session(Some(crate::vim::InsertSession {
7619            count: 3,
7620            row_min: 0,
7621            row_max: 0,
7622            before_lines: vec!["hello".to_string()],
7623            reason: crate::vim::InsertReason::Enter(crate::vim::InsertEntry::I),
7624        }));
7625        assert_eq!(e.insert_session().map(|s| s.count), Some(3));
7626        let taken = e.take_insert_session();
7627        assert!(taken.is_some());
7628        assert!(e.insert_session().is_none());
7629    }
7630
7631    #[test]
7632    fn visual_anchor_round_trips() {
7633        let mut e = normal_editor("hello");
7634        e.set_visual_anchor((1, 3));
7635        assert_eq!(e.visual_anchor(), (1, 3));
7636    }
7637
7638    #[test]
7639    fn visual_line_anchor_round_trips() {
7640        let mut e = normal_editor("hello\nworld");
7641        e.set_visual_line_anchor(1);
7642        assert_eq!(e.visual_line_anchor(), 1);
7643    }
7644
7645    #[test]
7646    fn block_anchor_and_vcol_round_trip() {
7647        let mut e = normal_editor("hello");
7648        e.set_block_anchor((0, 2));
7649        e.set_block_vcol(4);
7650        assert_eq!(e.block_anchor(), (0, 2));
7651        assert_eq!(e.block_vcol(), 4);
7652    }
7653
7654    #[test]
7655    fn yank_linewise_round_trips() {
7656        let mut e = normal_editor("hello");
7657        assert!(!e.yank_linewise());
7658        e.set_yank_linewise(true);
7659        assert!(e.yank_linewise());
7660    }
7661
7662    #[test]
7663    fn pending_register_raw_round_trips() {
7664        let mut e = normal_editor("hello");
7665        assert_eq!(e.pending_register(), None);
7666        e.set_pending_register_raw(Some('a'));
7667        assert_eq!(e.pending_register(), Some('a'));
7668        let taken = e.take_pending_register_raw();
7669        assert_eq!(taken, Some('a'));
7670        assert_eq!(e.pending_register(), None);
7671    }
7672
7673    #[test]
7674    fn recording_macro_round_trips() {
7675        let mut e = normal_editor("hello");
7676        assert_eq!(e.recording_macro(), None);
7677        e.set_recording_macro(Some('q'));
7678        assert_eq!(e.recording_macro(), Some('q'));
7679        e.set_recording_macro(None);
7680        assert_eq!(e.recording_macro(), None);
7681    }
7682
7683    #[test]
7684    fn recording_keys_round_trips() {
7685        let mut e = normal_editor("hello");
7686        let input = crate::Input {
7687            key: crate::Key::Char('j'),
7688            ctrl: false,
7689            alt: false,
7690            shift: false,
7691        };
7692        e.push_recording_key(input);
7693        assert_eq!(e.take_recording_keys(), vec![input]);
7694        assert!(e.take_recording_keys().is_empty());
7695    }
7696
7697    #[test]
7698    fn replaying_macro_raw_round_trips() {
7699        let mut e = normal_editor("hello");
7700        assert!(!e.is_replaying_macro_raw());
7701        e.set_replaying_macro_raw(true);
7702        assert!(e.is_replaying_macro_raw());
7703        e.set_replaying_macro_raw(false);
7704        assert!(!e.is_replaying_macro_raw());
7705    }
7706
7707    #[test]
7708    fn last_macro_round_trips() {
7709        let mut e = normal_editor("hello");
7710        assert_eq!(e.last_macro(), None);
7711        e.set_last_macro(Some('m'));
7712        assert_eq!(e.last_macro(), Some('m'));
7713    }
7714
7715    #[test]
7716    fn last_insert_pos_round_trips() {
7717        let mut e = normal_editor("hello");
7718        assert_eq!(e.last_insert_pos(), None);
7719        e.set_last_insert_pos(Some((1, 2)));
7720        assert_eq!(e.last_insert_pos(), Some((1, 2)));
7721    }
7722
7723    #[test]
7724    fn last_visual_round_trips() {
7725        let mut e = normal_editor("hello");
7726        assert!(e.last_visual().is_none());
7727        let snap = crate::vim::LastVisual {
7728            mode: crate::vim::Mode::Visual,
7729            anchor: (0, 0),
7730            cursor: (0, 3),
7731            block_vcol: 0,
7732        };
7733        e.set_last_visual(Some(snap));
7734        assert!(e.last_visual().is_some());
7735        e.set_last_visual(None);
7736        assert!(e.last_visual().is_none());
7737    }
7738
7739    #[test]
7740    fn viewport_pinned_round_trips() {
7741        let mut e = normal_editor("hello");
7742        assert!(!e.viewport_pinned());
7743        e.set_viewport_pinned(true);
7744        assert!(e.viewport_pinned());
7745        e.set_viewport_pinned(false);
7746        assert!(!e.viewport_pinned());
7747    }
7748
7749    #[test]
7750    fn insert_pending_register_round_trips() {
7751        let mut e = normal_editor("hello");
7752        assert!(!e.insert_pending_register());
7753        e.set_insert_pending_register(true);
7754        assert!(e.insert_pending_register());
7755    }
7756
7757    #[test]
7758    fn change_mark_start_round_trips() {
7759        let mut e = normal_editor("hello");
7760        assert_eq!(e.change_mark_start(), None);
7761        e.set_change_mark_start(Some((2, 5)));
7762        assert_eq!(e.change_mark_start(), Some((2, 5)));
7763        let taken = e.take_change_mark_start();
7764        assert_eq!(taken, Some((2, 5)));
7765        assert_eq!(e.change_mark_start(), None);
7766    }
7767
7768    #[test]
7769    fn search_prompt_state_round_trips() {
7770        let mut e = normal_editor("hello");
7771        assert!(e.search_prompt_state().is_none());
7772        e.set_search_prompt_state(Some(crate::vim::SearchPrompt {
7773            text: "foo".to_string(),
7774            cursor: 3,
7775            forward: true,
7776        }));
7777        assert_eq!(
7778            e.search_prompt_state().map(|p| p.text.as_str()),
7779            Some("foo")
7780        );
7781        let taken = e.take_search_prompt_state();
7782        assert!(taken.is_some());
7783        assert!(e.search_prompt_state().is_none());
7784    }
7785
7786    #[test]
7787    fn last_search_pattern_and_direction_round_trips() {
7788        let mut e = normal_editor("hello");
7789        assert_eq!(e.last_search_pattern(), None);
7790        e.set_last_search_pattern_only(Some("world".to_string()));
7791        assert_eq!(e.last_search_pattern(), Some("world"));
7792        e.set_last_search_forward_only(false);
7793        assert!(!e.last_search_forward());
7794    }
7795
7796    #[test]
7797    fn search_history_round_trips() {
7798        let mut e = normal_editor("hello");
7799        assert!(e.search_history().is_empty());
7800        e.search_history_mut().push("pattern1".to_string());
7801        assert_eq!(e.search_history(), &["pattern1"]);
7802        e.set_search_history_cursor(Some(0));
7803        assert_eq!(e.search_history_cursor(), Some(0));
7804        e.set_search_history_cursor(None);
7805        assert_eq!(e.search_history_cursor(), None);
7806    }
7807
7808    #[test]
7809    fn jump_lists_round_trips() {
7810        let mut e = normal_editor("hello");
7811        assert!(e.jump_back_list().is_empty());
7812        assert!(e.jump_fwd_list().is_empty());
7813        e.jump_back_list_mut().push((1, 2));
7814        e.jump_fwd_list_mut().push((3, 4));
7815        assert_eq!(e.jump_back_list(), &[(1, 2)]);
7816        assert_eq!(e.jump_fwd_list(), &[(3, 4)]);
7817    }
7818
7819    #[test]
7820    fn last_input_timing_round_trips() {
7821        let mut e = normal_editor("hello");
7822        assert!(e.last_input_at().is_none());
7823        assert!(e.last_input_host_at().is_none());
7824        let now = std::time::Instant::now();
7825        e.set_last_input_at(Some(now));
7826        assert!(e.last_input_at().is_some());
7827        let dur = core::time::Duration::from_millis(100);
7828        e.set_last_input_host_at(Some(dur));
7829        assert_eq!(e.last_input_host_at(), Some(dur));
7830    }
7831
7832    // ── auto_indent_range tests ──────────────────────────────────────────────
7833
7834    /// Helper: build an editor with `expandtab=true` and the given shiftwidth.
7835    fn indent_editor(initial: &str, shiftwidth: usize, expandtab: bool) -> Editor {
7836        let mut e = fresh_editor(initial);
7837        e.settings_mut().shiftwidth = shiftwidth;
7838        e.settings_mut().expandtab = expandtab;
7839        e
7840    }
7841
7842    #[test]
7843    fn auto_indent_single_line_under_open_brace() {
7844        // `{\nfoo\n}` — "foo" is at depth 1 under the `{`.
7845        // With shiftwidth=4 expandtab=true it should become "    foo".
7846        let mut e = indent_editor("{\nfoo\n}", 4, true);
7847        // auto-indent only row 1 ("foo").
7848        e.auto_indent_range((1, 0), (1, 0));
7849        let lines = e.buffer().lines();
7850        assert_eq!(lines[1], "    foo", "foo should be indented by 4 spaces");
7851    }
7852
7853    #[test]
7854    fn auto_indent_close_brace_outdents() {
7855        // `{\n    inner\n}` — the `}` is at depth 1 but starts with a close
7856        // bracket so effective_depth = 0.
7857        let mut e = indent_editor("{\n    inner\n}", 4, true);
7858        e.auto_indent_range((2, 0), (2, 0));
7859        let lines = e.buffer().lines();
7860        assert_eq!(lines[2], "}", "`}}` should have zero indent");
7861    }
7862
7863    #[test]
7864    fn auto_indent_whole_buffer_normalizes_mixed_indent() {
7865        // Mixed-indent input: first line un-indented `{`, second line 1-tab
7866        // indented body, third line un-indented `}`.
7867        let src = "{\n\tbody\n}";
7868        let mut e = indent_editor(src, 4, true);
7869        let total = e.buffer().lines().len();
7870        e.auto_indent_range((0, 0), (total - 1, 0));
7871        let lines = e.buffer().lines();
7872        // `{` — depth 0 at start.
7873        assert_eq!(lines[0], "{");
7874        // `body` — depth 1 after `{`.
7875        assert_eq!(lines[1], "    body");
7876        // `}` — depth 1 but starts with close → effective_depth 0.
7877        assert_eq!(lines[2], "}");
7878    }
7879
7880    #[test]
7881    fn auto_indent_respects_expandtab_false_uses_tabs() {
7882        // Same buffer, but expandtab=false → indent unit is `\t`.
7883        let src = "{\nbody\n}";
7884        let mut e = indent_editor(src, 4, false);
7885        let total = e.buffer().lines().len();
7886        e.auto_indent_range((0, 0), (total - 1, 0));
7887        let lines = e.buffer().lines();
7888        assert_eq!(lines[0], "{");
7889        assert_eq!(lines[1], "\tbody");
7890        assert_eq!(lines[2], "}");
7891    }
7892
7893    #[test]
7894    fn auto_indent_empty_line_stays_empty() {
7895        // `{\n\nfoo\n}` — blank line in the middle should stay blank.
7896        let src = "{\n\nfoo\n}";
7897        let mut e = indent_editor(src, 4, true);
7898        let total = e.buffer().lines().len();
7899        e.auto_indent_range((0, 0), (total - 1, 0));
7900        let lines = e.buffer().lines();
7901        assert_eq!(lines[1], "", "blank line should stay blank");
7902        assert_eq!(lines[2], "    foo");
7903    }
7904
7905    #[test]
7906    fn auto_indent_cursor_lands_on_first_nonws_of_start_row() {
7907        // After `==` / `auto_indent_range` the cursor should be at the first
7908        // non-whitespace character of start_row (vim parity).
7909        let src = "{\nfoo\n}";
7910        let mut e = indent_editor(src, 4, true);
7911        // Reindent only row 1.
7912        e.auto_indent_range((1, 0), (1, 0));
7913        // Row 1 after reindent is "    foo"; first non-ws is col 4.
7914        let (row, col) = e.cursor();
7915        assert_eq!(row, 1, "cursor should stay on start_row");
7916        assert_eq!(col, 4, "cursor should land on first non-ws char (col 4)");
7917    }
7918
7919    #[test]
7920    fn auto_indent_sets_last_indent_range() {
7921        // After `auto_indent_range` the engine must store the touched row span.
7922        let src = "{\nfoo\nbar\n}";
7923        let mut e = indent_editor(src, 4, true);
7924        let total = e.buffer().lines().len();
7925        e.auto_indent_range((0, 0), (total - 1, 0));
7926        assert_eq!(
7927            e.take_last_indent_range(),
7928            Some((0, total - 1)),
7929            "take_last_indent_range must return Some with the touched rows"
7930        );
7931    }
7932
7933    #[test]
7934    fn take_last_indent_range_clears() {
7935        // A second call after draining must return None.
7936        let src = "{\nfoo\n}";
7937        let mut e = indent_editor(src, 4, true);
7938        e.auto_indent_range((0, 0), (2, 0));
7939        let _ = e.take_last_indent_range(); // drain
7940        assert_eq!(
7941            e.take_last_indent_range(),
7942            None,
7943            "second take_last_indent_range must return None"
7944        );
7945    }
7946
7947    // ── Diagnostic: auto_indent vs cargo fmt on a real source file ────────
7948    //
7949    // Loads `motions.rs` (~1400 LOC, mixed real-world Rust patterns: method
7950    // chains, multi-line fn args, match arms, where clauses, closures, nested
7951    // types) at compile time, runs `auto_indent_range` over every row, and
7952    // diffs per-line leading-whitespace counts against the cargo-fmt'd source
7953    // (the file is in the repo, fmt'd by CI on every commit).
7954    //
7955    // The test PRINTS divergences and only fails if more than `THRESHOLD`
7956    // lines disagree — the dumb shiftwidth+bracket algorithm is documented
7957    // to mishandle some patterns (chains, where clauses, etc.). A full
7958    // language-aware indenter is a v2 follow-up. The point of this test is
7959    // to surface the divergence list so we can decide which patterns the
7960    // dumb algo CAN be taught to handle without going full tree-sitter.
7961    //
7962    // To diagnose: run with `--nocapture` to see the full diff.
7963    #[test]
7964    #[ignore = "diagnostic — run with --ignored --nocapture to see auto-indent vs cargo fmt diffs"]
7965    fn auto_indent_vs_cargo_fmt_motions_diagnostic() {
7966        let original = include_str!("motions.rs");
7967
7968        let mut e = Editor::new(
7969            hjkl_buffer::Buffer::new(),
7970            crate::types::DefaultHost::new(),
7971            crate::types::Options {
7972                shiftwidth: 4,
7973                expandtab: true,
7974                tabstop: 4,
7975                ..crate::types::Options::default()
7976            },
7977        );
7978        e.set_content(original);
7979
7980        let row_count = buf_row_count(&e.buffer);
7981        e.auto_indent_range((0, 0), (row_count.saturating_sub(1), 0));
7982
7983        let after_lines: Vec<String> = (0..row_count)
7984            .filter_map(|r| buf_line(&e.buffer, r))
7985            .collect();
7986        let original_lines: Vec<&str> = original.lines().collect();
7987
7988        let leading_ws = |s: &str| s.chars().take_while(|c| c.is_whitespace()).count();
7989
7990        let mut diffs: Vec<(usize, String, usize, usize)> = Vec::new();
7991        for (i, (orig, after)) in original_lines.iter().zip(after_lines.iter()).enumerate() {
7992            let want = leading_ws(orig);
7993            let got = leading_ws(after);
7994            if want != got {
7995                diffs.push((i + 1, orig.trim().chars().take(80).collect(), want, got));
7996            }
7997        }
7998
7999        // Print the first 50 divergences for diagnosis.
8000        eprintln!(
8001            "auto_indent_vs_cargo_fmt: {} lines differ out of {} ({}%)",
8002            diffs.len(),
8003            original_lines.len(),
8004            (diffs.len() * 100) / original_lines.len().max(1),
8005        );
8006        for (line_no, content, want, got) in diffs.iter().take(50) {
8007            eprintln!("  L{line_no:5} want={want:2} got={got:2}  {content}");
8008        }
8009        if diffs.len() > 50 {
8010            eprintln!("  ... and {} more", diffs.len() - 50);
8011        }
8012
8013        // Soft assertion — track divergence count over time. If the algo
8014        // gets smarter, this number should drop. If a regression makes it
8015        // jump, we'll notice. Set the cap generously above current baseline.
8016        let pct = (diffs.len() * 100) / original_lines.len().max(1);
8017        // 2026-05-16 baseline after fixing bracket scan + chain continuation:
8018        // 5 divergences / 1416 lines (<1%). Remaining lines are a single
8019        // \`let X = if {} else {};\` trailing-\`=\` continuation pattern —
8020        // documented v2 follow-up. Cap at 2% so any regression in the
8021        // bracket scan or chain detection trips the test.
8022        assert!(
8023            pct < 2,
8024            "auto_indent diverges from cargo fmt on {pct}% of lines — regression from <1% baseline"
8025        );
8026    }
8027}