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;
10use crate::vim::{self, VimState};
11use crate::{KeybindingMode, VimMode};
12use std::sync::atomic::{AtomicU16, Ordering};
13
14/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
15/// [`crate::types::Edit`] (`EditOp`) records.
16///
17/// Most buffer edits map to a single EditOp. Block ops
18/// ([`hjkl_buffer::Edit::InsertBlock`] /
19/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
20/// touched — they edit non-contiguous cells and a single
21/// `range..range` can't represent the rectangle.
22///
23/// Returns an empty vec when the edit isn't representable (no buffer
24/// variant currently fails this check).
25fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
26    use crate::types::{Edit as Op, Pos};
27    use hjkl_buffer::Edit as B;
28    let to_pos = |p: hjkl_buffer::Position| Pos {
29        line: p.row as u32,
30        col: p.col as u32,
31    };
32    match edit {
33        B::InsertChar { at, ch } => vec![Op {
34            range: to_pos(*at)..to_pos(*at),
35            replacement: ch.to_string(),
36        }],
37        B::InsertStr { at, text } => vec![Op {
38            range: to_pos(*at)..to_pos(*at),
39            replacement: text.clone(),
40        }],
41        B::DeleteRange { start, end, .. } => vec![Op {
42            range: to_pos(*start)..to_pos(*end),
43            replacement: String::new(),
44        }],
45        B::Replace { start, end, with } => vec![Op {
46            range: to_pos(*start)..to_pos(*end),
47            replacement: with.clone(),
48        }],
49        B::JoinLines {
50            row,
51            count,
52            with_space,
53        } => {
54            // Joining `count` rows after `row` collapses
55            // [(row+1, 0) .. (row+count, EOL)] into the joined
56            // sentinel. The replacement is either an empty string
57            // (gJ) or " " between segments (J).
58            let start = Pos {
59                line: *row as u32 + 1,
60                col: 0,
61            };
62            let end = Pos {
63                line: (*row + *count) as u32,
64                col: u32::MAX, // covers to EOL of the last source row
65            };
66            vec![Op {
67                range: start..end,
68                replacement: if *with_space {
69                    " ".into()
70                } else {
71                    String::new()
72                },
73            }]
74        }
75        B::SplitLines {
76            row,
77            cols,
78            inserted_space: _,
79        } => {
80            // SplitLines reverses a JoinLines: insert a `\n`
81            // (and optional dropped space) at each col on `row`.
82            cols.iter()
83                .map(|c| {
84                    let p = Pos {
85                        line: *row as u32,
86                        col: *c as u32,
87                    };
88                    Op {
89                        range: p..p,
90                        replacement: "\n".into(),
91                    }
92                })
93                .collect()
94        }
95        B::InsertBlock { at, chunks } => {
96            // One EditOp per row in the block — non-contiguous edits.
97            chunks
98                .iter()
99                .enumerate()
100                .map(|(i, chunk)| {
101                    let p = Pos {
102                        line: at.row as u32 + i as u32,
103                        col: at.col as u32,
104                    };
105                    Op {
106                        range: p..p,
107                        replacement: chunk.clone(),
108                    }
109                })
110                .collect()
111        }
112        B::DeleteBlockChunks { at, widths } => {
113            // One EditOp per row, deleting `widths[i]` chars at
114            // `(at.row + i, at.col)`.
115            widths
116                .iter()
117                .enumerate()
118                .map(|(i, w)| {
119                    let start = Pos {
120                        line: at.row as u32 + i as u32,
121                        col: at.col as u32,
122                    };
123                    let end = Pos {
124                        line: at.row as u32 + i as u32,
125                        col: at.col as u32 + *w as u32,
126                    };
127                    Op {
128                        range: start..end,
129                        replacement: String::new(),
130                    }
131                })
132                .collect()
133        }
134    }
135}
136
137/// Sum of bytes from the start of the buffer to the start of `row`.
138/// Byte offset of the first byte of `row` within the canonical
139/// `lines().join("\n")` byte rendering. Pre-rope this walked every row
140/// from 0 to `row` allocating a `String` per row to read its `.len()` —
141/// O(row) allocations per call, fired from `position_to_byte_coords` on
142/// every `insert_char`. At the bottom of a 1.86 M-line buffer that was
143/// 1.86 M String allocations per keystroke (the dominant cost of the
144/// "edits at the bottom of the file are slow" symptom).
145///
146/// Now O(log N): ropey's `line_to_byte` walks the B-tree's per-node
147/// byte counts. No String materialization.
148#[inline]
149fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
150    let rope = buf.rope();
151    let row = row.min(rope.len_lines());
152    rope.line_to_byte(row)
153}
154
155/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
156/// coordinates `(byte_within_buffer, (row, col_byte))` against the
157/// **pre-edit** buffer.
158fn position_to_byte_coords(
159    buf: &hjkl_buffer::Buffer,
160    pos: hjkl_buffer::Position,
161) -> (usize, (u32, u32)) {
162    let row = pos.row.min(buf.row_count().saturating_sub(1));
163    let rope = buf.rope();
164    let line = hjkl_buffer::rope_line_str(&rope, row);
165    let col_byte = pos.byte_offset(&line);
166    let byte = buffer_byte_of_row(buf, row) + col_byte;
167    (byte, (row as u32, col_byte as u32))
168}
169
170/// Walk `bytes[..end]` counting newlines and return the (row, col_byte)
171/// position at byte offset `end`. `col_byte` is the byte distance from
172/// the most recent `\n` (or buffer start). Used to translate a byte
173/// offset into a tree-sitter `Point`.
174fn byte_to_row_col(bytes: &[u8], end: usize) -> (u32, u32) {
175    let end = end.min(bytes.len());
176    let mut row: u32 = 0;
177    let mut row_start: usize = 0;
178    for (i, &b) in bytes[..end].iter().enumerate() {
179        if b == b'\n' {
180            row += 1;
181            row_start = i + 1;
182        }
183    }
184    (row, (end - row_start) as u32)
185}
186
187/// Rope-backed minimal content-edit diff for the undo/redo
188/// `restore_text` path. Walks `old_rope` chunk-by-chunk for the
189/// common-prefix / common-suffix scan instead of forcing a full
190/// `content_joined()` materialization (~3 MB per undo on huge files).
191///
192/// `ropey::Rope::bytes()` and `bytes_at(n).reversed()` give O(log N)
193/// seek + O(1)-per-byte step, so the scan cost matches the contiguous
194/// `&[u8]` version without the materialization alloc.
195fn minimal_content_edit_rope(old_rope: &ropey::Rope, new_text: &str) -> crate::types::ContentEdit {
196    let new_bytes = new_text.as_bytes();
197    let old_len = old_rope.len_bytes();
198    let new_len = new_bytes.len();
199    let common = old_len.min(new_len);
200
201    // Common prefix length — forward walk through rope bytes.
202    let mut prefix = 0;
203    let mut fwd = old_rope.bytes();
204    while prefix < common {
205        match fwd.next() {
206            Some(b) if b == new_bytes[prefix] => prefix += 1,
207            _ => break,
208        }
209    }
210    while prefix > 0 && prefix < old_len && (old_rope.byte(prefix) & 0b1100_0000) == 0b1000_0000 {
211        prefix -= 1;
212    }
213
214    // Common suffix length — backward walk through rope bytes.
215    let mut suffix = 0;
216    let max_suffix = (old_len - prefix).min(new_len - prefix);
217    let mut rev = old_rope.bytes_at(old_len).reversed();
218    while suffix < max_suffix {
219        match rev.next() {
220            Some(b) if b == new_bytes[new_len - 1 - suffix] => suffix += 1,
221            _ => break,
222        }
223    }
224    while suffix > 0
225        && suffix < old_len
226        && (old_rope.byte(old_len - suffix) & 0b1100_0000) == 0b1000_0000
227    {
228        suffix -= 1;
229    }
230
231    let start_byte = prefix;
232    let old_end_byte = old_len - suffix;
233    let new_end_byte = new_len - suffix;
234
235    crate::types::ContentEdit {
236        start_byte,
237        old_end_byte,
238        new_end_byte,
239        start_position: rope_byte_to_row_col(old_rope, start_byte),
240        old_end_position: rope_byte_to_row_col(old_rope, old_end_byte),
241        new_end_position: byte_to_row_col(new_bytes, new_end_byte),
242    }
243}
244
245#[inline]
246fn rope_byte_to_row_col(rope: &ropey::Rope, byte_idx: usize) -> (u32, u32) {
247    let byte_idx = byte_idx.min(rope.len_bytes());
248    let line = rope.byte_to_line(byte_idx);
249    let line_start = rope.line_to_byte(line);
250    (line as u32, (byte_idx - line_start) as u32)
251}
252
253/// Compute the byte position after inserting `text` starting at
254/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
255fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
256    let new_end_byte = start_byte + text.len();
257    let newlines = text.bytes().filter(|&b| b == b'\n').count();
258    let end_pos = if newlines == 0 {
259        (start_pos.0, start_pos.1 + text.len() as u32)
260    } else {
261        // Bytes after the last newline determine the trailing column.
262        let last_nl = text.rfind('\n').unwrap();
263        let tail_bytes = (text.len() - last_nl - 1) as u32;
264        (start_pos.0 + newlines as u32, tail_bytes)
265    };
266    (new_end_byte, end_pos)
267}
268
269/// Translate a single `hjkl_buffer::Edit` into one or more
270/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
271/// state for byte/position lookups. Block ops fan out to one entry per
272/// touched row (matches `edit_to_editops`).
273fn content_edits_from_buffer_edit(
274    buf: &hjkl_buffer::Buffer,
275    edit: &hjkl_buffer::Edit,
276) -> Vec<crate::types::ContentEdit> {
277    use hjkl_buffer::Edit as B;
278    use hjkl_buffer::Position;
279
280    let mut out: Vec<crate::types::ContentEdit> = Vec::new();
281
282    match edit {
283        B::InsertChar { at, ch } => {
284            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
285            let new_end_byte = start_byte + ch.len_utf8();
286            let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
287            out.push(crate::types::ContentEdit {
288                start_byte,
289                old_end_byte: start_byte,
290                new_end_byte,
291                start_position: start_pos,
292                old_end_position: start_pos,
293                new_end_position: new_end_pos,
294            });
295        }
296        B::InsertStr { at, text } => {
297            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
298            let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
299            out.push(crate::types::ContentEdit {
300                start_byte,
301                old_end_byte: start_byte,
302                new_end_byte,
303                start_position: start_pos,
304                old_end_position: start_pos,
305                new_end_position: new_end_pos,
306            });
307        }
308        B::DeleteRange { start, end, kind } => {
309            let (start, end) = if start <= end {
310                (*start, *end)
311            } else {
312                (*end, *start)
313            };
314            match kind {
315                hjkl_buffer::MotionKind::Char => {
316                    let (start_byte, start_pos) = position_to_byte_coords(buf, start);
317                    let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
318                    out.push(crate::types::ContentEdit {
319                        start_byte,
320                        old_end_byte,
321                        new_end_byte: start_byte,
322                        start_position: start_pos,
323                        old_end_position: old_end_pos,
324                        new_end_position: start_pos,
325                    });
326                }
327                hjkl_buffer::MotionKind::Line => {
328                    // Linewise delete drops rows [start.row..=end.row]. Map
329                    // to a span from start of `start.row` through start of
330                    // (end.row + 1). The buffer's own `do_delete_range`
331                    // collapses to row `start.row` after dropping.
332                    let lo = start.row;
333                    let hi = end.row.min(buf.row_count().saturating_sub(1));
334                    let start_byte = buffer_byte_of_row(buf, lo);
335                    let next_row_byte = if hi + 1 < buf.row_count() {
336                        buffer_byte_of_row(buf, hi + 1)
337                    } else {
338                        // No row after; clamp to end-of-buffer byte.
339                        let last_row = buf.row_count().saturating_sub(1);
340                        buffer_byte_of_row(buf, buf.row_count())
341                            + hjkl_buffer::rope_line_bytes(&buf.rope(), last_row)
342                    };
343                    out.push(crate::types::ContentEdit {
344                        start_byte,
345                        old_end_byte: next_row_byte,
346                        new_end_byte: start_byte,
347                        start_position: (lo as u32, 0),
348                        old_end_position: ((hi + 1) as u32, 0),
349                        new_end_position: (lo as u32, 0),
350                    });
351                }
352                hjkl_buffer::MotionKind::Block => {
353                    // Block delete removes a rectangle of chars per row.
354                    // Fan out to one ContentEdit per row.
355                    let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
356                    for row in start.row..=end.row {
357                        let row_start_pos = Position::new(row, left_col);
358                        let row_end_pos = Position::new(row, right_col + 1);
359                        let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
360                        let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
361                        if eb <= sb {
362                            continue;
363                        }
364                        out.push(crate::types::ContentEdit {
365                            start_byte: sb,
366                            old_end_byte: eb,
367                            new_end_byte: sb,
368                            start_position: sp,
369                            old_end_position: ep,
370                            new_end_position: sp,
371                        });
372                    }
373                }
374            }
375        }
376        B::Replace { start, end, with } => {
377            let (start, end) = if start <= end {
378                (*start, *end)
379            } else {
380                (*end, *start)
381            };
382            let (start_byte, start_pos) = position_to_byte_coords(buf, start);
383            let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
384            let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
385            out.push(crate::types::ContentEdit {
386                start_byte,
387                old_end_byte,
388                new_end_byte,
389                start_position: start_pos,
390                old_end_position: old_end_pos,
391                new_end_position: new_end_pos,
392            });
393        }
394        B::JoinLines {
395            row,
396            count,
397            with_space,
398        } => {
399            // Joining `count` rows after `row` collapses the bytes
400            // between EOL of `row` and EOL of `row + count` into either
401            // an empty string (gJ) or a single space per join (J — but
402            // only when both sides are non-empty; we approximate with
403            // a single space for simplicity).
404            let row = (*row).min(buf.row_count().saturating_sub(1));
405            let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
406            let buf_rope = buf.rope();
407            let line = hjkl_buffer::rope_line_str(&buf_rope, row);
408            let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
409            let row_eol_col = line.len() as u32;
410            let next_row_after = last_join_row + 1;
411            let old_end_byte = if next_row_after < buf.row_count() {
412                buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
413            } else {
414                let last_row = buf.row_count().saturating_sub(1);
415                buffer_byte_of_row(buf, buf.row_count())
416                    + hjkl_buffer::rope_line_bytes(&buf_rope, last_row)
417            };
418            let last_line = hjkl_buffer::rope_line_str(&buf_rope, last_join_row);
419            let old_end_pos = (last_join_row as u32, last_line.len() as u32);
420            let replacement_len = if *with_space { 1 } else { 0 };
421            let new_end_byte = row_eol_byte + replacement_len;
422            let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
423            out.push(crate::types::ContentEdit {
424                start_byte: row_eol_byte,
425                old_end_byte,
426                new_end_byte,
427                start_position: (row as u32, row_eol_col),
428                old_end_position: old_end_pos,
429                new_end_position: new_end_pos,
430            });
431        }
432        B::SplitLines {
433            row,
434            cols,
435            inserted_space,
436        } => {
437            // Splits insert "\n" (or "\n " inverse) at each col on `row`.
438            // The buffer applies all splits left-to-right via the
439            // do_split_lines path; we emit one ContentEdit per col,
440            // each treated as an insert at that col on `row`. Note: the
441            // buffer state during emission is *pre-edit*, so all cols
442            // index into the same pre-edit row.
443            let row = (*row).min(buf.row_count().saturating_sub(1));
444            let split_rope = buf.rope();
445            let line = hjkl_buffer::rope_line_str(&split_rope, row);
446            let row_byte = buffer_byte_of_row(buf, row);
447            let insert = if *inserted_space { "\n " } else { "\n" };
448            for &c in cols {
449                let pos = Position::new(row, c);
450                let col_byte = pos.byte_offset(&line);
451                let start_byte = row_byte + col_byte;
452                let start_pos = (row as u32, col_byte as u32);
453                let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
454                out.push(crate::types::ContentEdit {
455                    start_byte,
456                    old_end_byte: start_byte,
457                    new_end_byte,
458                    start_position: start_pos,
459                    old_end_position: start_pos,
460                    new_end_position: new_end_pos,
461                });
462            }
463        }
464        B::InsertBlock { at, chunks } => {
465            // One ContentEdit per chunk; each lands at `(at.row + i,
466            // at.col)` in the pre-edit buffer.
467            for (i, chunk) in chunks.iter().enumerate() {
468                let pos = Position::new(at.row + i, at.col);
469                let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
470                let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
471                out.push(crate::types::ContentEdit {
472                    start_byte,
473                    old_end_byte: start_byte,
474                    new_end_byte,
475                    start_position: start_pos,
476                    old_end_position: start_pos,
477                    new_end_position: new_end_pos,
478                });
479            }
480        }
481        B::DeleteBlockChunks { at, widths } => {
482            for (i, w) in widths.iter().enumerate() {
483                let row = at.row + i;
484                let start_pos = Position::new(row, at.col);
485                let end_pos = Position::new(row, at.col + *w);
486                let (sb, sp) = position_to_byte_coords(buf, start_pos);
487                let (eb, ep) = position_to_byte_coords(buf, end_pos);
488                if eb <= sb {
489                    continue;
490                }
491                out.push(crate::types::ContentEdit {
492                    start_byte: sb,
493                    old_end_byte: eb,
494                    new_end_byte: sb,
495                    start_position: sp,
496                    old_end_position: ep,
497                    new_end_position: sp,
498                });
499            }
500        }
501    }
502
503    out
504}
505
506/// Where the cursor should land in the viewport after a `z`-family
507/// scroll (`zz` / `zt` / `zb`).
508#[derive(Debug, Clone, Copy, PartialEq, Eq)]
509pub(super) enum CursorScrollTarget {
510    Center,
511    Top,
512    Bottom,
513}
514
515// ── Trait-surface cast helpers ────────────────────────────────────
516//
517// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
518// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
519// their reaches through the same primitives. Re-import via
520// `use` so the editor body keeps its terse call shape.
521
522use crate::buf_helpers::{
523    apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
524    buf_row_count, buf_set_cursor_rc,
525};
526
527/// Return value from the engine's `try_goto_mark_*` methods. Tells the
528/// caller (app layer) whether a cross-buffer switch is required.
529///
530/// - `SameBuffer` — cursor moved (or mark was unset → no-op) within the
531///   same buffer; no buffer switch needed.
532/// - `CrossBuffer` — the mark lives in a different buffer. The app must
533///   switch to the slot whose `buffer_id` matches, then position the cursor
534///   at `(row, col)` using `Editor::jump_cursor`.
535/// - `Unset` — mark not set; no action needed.
536#[derive(Debug, Clone, PartialEq, Eq)]
537pub enum MarkJump {
538    SameBuffer,
539    CrossBuffer {
540        buffer_id: u64,
541        row: usize,
542        col: usize,
543    },
544    Unset,
545}
546
547pub struct Editor<
548    B: crate::types::Buffer = hjkl_buffer::Buffer,
549    H: crate::types::Host = crate::types::DefaultHost,
550> {
551    pub keybinding_mode: KeybindingMode,
552    /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
553    pub last_yank: Option<String>,
554    /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
555    /// Internal — exposed via Editor accessor methods
556    /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
557    /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
558    pub(crate) vim: VimState,
559    /// Undo history: each entry is `(joined_document, cursor)` before the
560    /// edit. Stored as `Arc<String>` so it shares the
561    /// Undo history: snapshots taken via `Buffer::rope()` — `ropey::Rope::clone`
562    /// is O(1) (Arc-clone of the B-tree root). Previously stored
563    /// `Arc<String>` from `content_joined()`, which on the rope storage
564    /// builds the entire document `String` via `rope.to_string()` — that
565    /// turned every `i` / `o` keystroke into a ~3 MB allocation on a
566    /// 1.86 M-line file.
567    pub(crate) undo_stack: Vec<(ropey::Rope, (usize, usize))>,
568    /// Redo history: entries pushed when undoing.
569    pub(super) redo_stack: Vec<(ropey::Rope, (usize, usize))>,
570    /// Set whenever the buffer content changes; cleared by `take_dirty`.
571    pub(super) content_dirty: bool,
572    /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
573    /// so repeated `content_arc()` calls within the same un-mutated
574    /// window are free (ref-count bump instead of a full-buffer join).
575    /// Invalidated by every [`mark_content_dirty`] call.
576    pub(super) cached_content: Option<std::sync::Arc<String>>,
577    /// Last rendered viewport height (text rows only, no chrome). Written
578    /// by the draw path via [`set_viewport_height`] so the scroll helpers
579    /// can clamp the cursor to stay visible without plumbing the height
580    /// through every call.
581    pub(super) viewport_height: AtomicU16,
582    /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
583    /// goto-definition). The host app drains this each step and fires
584    /// the matching request against its own LSP client.
585    pub(super) pending_lsp: Option<LspIntent>,
586    /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
587    /// the `:fold*` Ex commands, or the edit pipeline's
588    /// "edits-inside-a-fold open it" invalidation. Drained by hosts
589    /// via [`Editor::take_fold_ops`]; the engine also applies each op
590    /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
591    /// so the in-tree buffer fold storage stays in sync without host
592    /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
593    pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
594    /// Buffer storage.
595    ///
596    /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
597    /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
598    /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
599    /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
600    pub(super) buffer: B,
601    /// Engine-native style intern table. Opaque `Span::style` ids index
602    /// into this table; the render path resolves ids back to
603    /// [`crate::types::Style`]. Ratatui hosts convert at the boundary via
604    /// `hjkl_engine_tui::style_to_ratatui`. Always present — no cfg-mutex.
605    pub(super) style_table: Vec<crate::types::Style>,
606    /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
607    /// every `p` / `P` via the active selector (default unnamed).
608    /// Internal — read via [`Editor::registers`]; mutated by yank /
609    /// delete / paste FSM paths and by [`Editor::seed_yank`].
610    pub(crate) registers: crate::registers::Registers,
611    /// Per-row syntax styling in engine-native form. Always present —
612    /// populated by [`Editor::install_syntax_spans`]. Ratatui hosts use
613    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`.
614    pub styled_spans: Vec<Vec<(usize, usize, crate::types::Style)>>,
615    /// Per-editor settings tweakable via `:set`. Exposed by reference
616    /// so handlers (indent, search) read the live value rather than a
617    /// snapshot taken at startup. Read via [`Editor::settings`];
618    /// mutate via [`Editor::settings_mut`].
619    pub(crate) settings: Settings,
620    /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
621    /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
622    /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
623    /// "file marks" that survive [`Editor::set_content`] calls so
624    /// they persist across tab swaps within the same Editor.
625    ///
626    /// 0.0.36: consolidated from three former storages:
627    /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
628    /// - `vim::VimState::marks` (lowercase) (deleted).
629    /// - `Editor::file_marks` (uppercase) (replaced by this map).
630    ///
631    /// `BTreeMap` so iteration is deterministic for snapshot tests
632    /// and the `:marks` ex command. Mark-shift on edits is handled
633    /// by [`Editor::shift_marks_after_edit`].
634    pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
635    /// Global (uppercase) marks that carry a `buffer_id` so they can jump
636    /// across buffers. Keyed by `'A'`–`'Z'`; values are
637    /// `(buffer_id, row, col)`. Set by `m{A-Z}`, resolved by
638    /// `try_goto_mark_line` / `try_goto_mark_char`.
639    pub(crate) global_marks: std::collections::BTreeMap<char, (u64, usize, usize)>,
640    /// The `buffer_id` this editor instance is currently attached to.
641    /// Updated by the host app on every `switch_to` / slot creation so
642    /// global-mark writes record the correct id without requiring the app
643    /// to pass the id on every keystroke.
644    pub(crate) current_buffer_id: u64,
645    /// Block ranges (`(start_row, end_row)` inclusive) the host has
646    /// extracted from a syntax tree. `:foldsyntax` reads these to
647    /// populate folds. The host refreshes them on every re-parse via
648    /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
649    /// [`Editor::syntax_fold_ranges`].
650    pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
651    /// Pending edit log drained by [`Editor::take_changes`]. Each entry
652    /// is a SPEC [`crate::types::Edit`] mapped from the underlying
653    /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
654    /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
655    /// best-effort EditOp covering the touched range; hosts wanting
656    /// per-cell deltas should diff their own snapshot of `lines()`.
657    /// Sealed at 0.1.0 trait extraction.
658    /// Drained by [`Editor::take_changes`].
659    pub(crate) change_log: Vec<crate::types::Edit>,
660    /// Vim's "sticky column" (curswant). `None` before the first
661    /// motion — the next vertical motion bootstraps from the live
662    /// cursor column. Horizontal motions refresh this to the new
663    /// column; vertical motions read it back so bouncing through a
664    /// shorter row doesn't drag the cursor to col 0. Hoisted out of
665    /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
666    /// the single owner now. Buffer motion methods that need it
667    /// take a `&mut Option<usize>` parameter.
668    pub(crate) sticky_col: Option<usize>,
669    /// Host adapter for clipboard, cursor-shape, time, viewport, and
670    /// search-prompt / cancellation side-channels.
671    ///
672    /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
673    /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
674    /// dyn-shim is gone — every method now dispatches through `H`'s
675    /// `Host` trait surface directly.
676    pub(crate) host: H,
677    /// Last public mode the cursor-shape emitter saw. Drives
678    /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
679    /// fires exactly once per mode transition without sprinkling the
680    /// call across every `vim.mode = ...` site.
681    pub(crate) last_emitted_mode: crate::VimMode,
682    /// Search FSM state (pattern + per-row match cache + wrapscan).
683    /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
684    /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
685    /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
686    /// gone; `BufferView` now takes the active regex as a `&Regex`
687    /// parameter, sourced from `Editor::search_state().pattern`.
688    pub(crate) search_state: crate::search::SearchState,
689    /// Per-row syntax span overlay. Source of truth for the host's
690    /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
691    /// [`Editor::install_syntax_spans`] (ratatui hosts use
692    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`)
693    /// and, in due course, by `Host::syntax_highlights` once the engine
694    /// drives that path directly.
695    ///
696    /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
697    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
698    /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
699    pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
700    /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
701    /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
702    /// tree (or any other content-change observer that needs byte-level
703    /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
704    pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
705    /// Pending "reset" flag set when the entire buffer is replaced
706    /// (e.g. `set_content` / `restore`). Supersedes any queued
707    /// `pending_content_edits` on the same frame: hosts call
708    /// [`Editor::take_content_reset`] before draining edits.
709    pub(crate) pending_content_reset: bool,
710    /// Row range touched by the most recent `auto_indent_rows` call.
711    /// `(top_row, bot_row)` inclusive. Set by the engine after every
712    /// auto-indent operation; drained (and cleared) by the host via
713    /// [`Editor::take_last_indent_range`] so it can display a brief
714    /// visual flash over the reindented rows.
715    pub(crate) last_indent_range: Option<(usize, usize)>,
716}
717
718/// Vim-style options surfaced by `:set`. New fields land here as
719/// individual ex commands gain `:set` plumbing.
720#[derive(Debug, Clone)]
721pub struct Settings {
722    /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
723    pub shiftwidth: usize,
724    /// Visual width of a `\t` character. Stored for future render
725    /// hookup; not yet consumed by the buffer renderer.
726    pub tabstop: usize,
727    /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
728    /// without an explicit `i` flag.
729    pub ignore_case: bool,
730    /// When true *and* `ignore_case` is true, an uppercase letter in
731    /// the pattern flips that search back to case-sensitive. Matches
732    /// vim's `:set smartcase`. Default `false`.
733    pub smartcase: bool,
734    /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
735    /// Default `true`.
736    pub wrapscan: bool,
737    /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
738    pub textwidth: usize,
739    /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
740    /// instead of a literal `\t`. Matches vim's `:set expandtab`.
741    /// Default `false`.
742    pub expandtab: bool,
743    /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
744    /// next softtabstop boundary (when `expandtab`), and Backspace at the
745    /// end of a softtabstop-aligned space run deletes the entire run as
746    /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
747    pub softtabstop: usize,
748    /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
749    /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
750    /// past the right edge and `top_col` clips the left side.
751    /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
752    /// to word-break wrap; `:set nowrap` resets.
753    pub wrap: hjkl_buffer::Wrap,
754    /// When true, the engine drops every edit before it touches the
755    /// buffer — undo, dirty flag, and change log all stay clean.
756    /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
757    pub readonly: bool,
758    /// When `true`, pressing Enter in insert mode copies the leading
759    /// whitespace of the current line onto the new line. Matches vim's
760    /// `:set autoindent`. Default `true` (vim parity).
761    pub autoindent: bool,
762    /// When `true`, bumps indent by one `shiftwidth` after a line ending
763    /// in `{` / `(` / `[`, and strips one indent unit when the user types
764    /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
765    /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
766    pub smartindent: bool,
767    /// Cap on undo-stack length. Older entries are pruned past this
768    /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
769    /// Default `1000`.
770    pub undo_levels: u32,
771    /// When `true`, cursor motions inside insert mode break the
772    /// current undo group (so a single `u` only reverses the run of
773    /// keystrokes that preceded the motion). Default `true`.
774    /// Currently a no-op — engine doesn't yet break the undo group
775    /// on insert-mode motions; field is wired through `:set
776    /// undobreak` for forward compatibility.
777    pub undo_break_on_motion: bool,
778    /// Vim-flavoured "what counts as a word" character class.
779    /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
780    /// `_`, `48-57` = decimal char range, bare integer = single char
781    /// code, single ASCII punctuation = literal. Default
782    /// `"@,48-57,_,192-255"` matches vim.
783    pub iskeyword: String,
784    /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
785    /// pauses longer than this between keys, any pending prefix is
786    /// abandoned and the next key starts a fresh sequence. Matches
787    /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
788    pub timeout_len: core::time::Duration,
789    /// When true, render absolute line numbers in the gutter. Matches
790    /// vim's `:set number` / `:set nu`. Default `true`.
791    pub number: bool,
792    /// When true, render line numbers as offsets from the cursor row.
793    /// Combined with `number`, the cursor row shows its absolute number
794    /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
795    /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
796    pub relativenumber: bool,
797    /// Minimum gutter width in cells for the line-number column.
798    /// Width grows past this to fit the largest displayed number.
799    /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
800    /// Range 1..=20.
801    pub numberwidth: usize,
802    /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
803    /// Default `false`.
804    pub cursorline: bool,
805    /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
806    /// Default `false`.
807    pub cursorcolumn: bool,
808    /// Sign-column display mode. Matches vim's `:set signcolumn`.
809    /// Default [`crate::types::SignColumnMode::Auto`].
810    pub signcolumn: crate::types::SignColumnMode,
811    /// Number of cells reserved for a fold-marker gutter.
812    /// Matches vim's `:set foldcolumn`. Default `0`.
813    pub foldcolumn: u32,
814    /// Comma-separated 1-based column indices for vertical rulers.
815    /// Matches vim's `:set colorcolumn`. Default `""`.
816    pub colorcolumn: String,
817    /// Format options flags (subset of vim's `formatoptions`).
818    /// `r` — auto-continue line comments on `<Enter>` in insert mode.
819    /// `o` — auto-continue line comments on `o` / `O` in normal mode.
820    /// Default: both on (`"ro"`).
821    pub formatoptions: String,
822    /// Active filetype (language name) for the current buffer.
823    /// Used by comment-continuation and future language-aware features.
824    /// Matches vim's `:set filetype` / `:set ft`. Default `""` (plain text).
825    pub filetype: String,
826    /// Override comment-string for the current buffer.
827    ///
828    /// When non-empty, used by `toggle_comment_range` instead of the
829    /// per-filetype default from `hjkl_lang::comment::commentstring_for_lang`.
830    /// Follows vim's `:set commentstring=…` — use `%s` as the text placeholder
831    /// (e.g. `"// %s"`) for compatibility; the toggle strips/inserts only the
832    /// prefix/suffix portion (before/after `%s`).  An empty string means "use
833    /// the filetype default".  Default `""`.
834    pub commentstring: String,
835    /// When `true`, typing an opening bracket or quote automatically inserts
836    /// the matching close character and parks the cursor between them.
837    /// Matches vim's `set autopairs` (Neovim) / nvim-autopairs behaviour.
838    /// Default `true`.
839    pub autopair: bool,
840    /// When `true`, typing `>` to close an HTML/XML opening tag automatically
841    /// inserts `</tagname>` after the cursor. Only fires for filetypes in the
842    /// HTML/XML family (`html`, `xml`, `svg`, `jsx`, `tsx`, `vue`, `svelte`).
843    /// Matches common editor "autoclose tag" behaviour. Default: `true` for
844    /// those filetypes (the caller gates on filetype), `true` stored here so
845    /// `:set noautoclose-tag` can disable it globally.
846    pub autoclose_tag: bool,
847}
848
849impl Default for Settings {
850    fn default() -> Self {
851        Self {
852            shiftwidth: 4,
853            tabstop: 4,
854            softtabstop: 4,
855            ignore_case: false,
856            smartcase: false,
857            wrapscan: true,
858            textwidth: 79,
859            expandtab: true,
860            wrap: hjkl_buffer::Wrap::None,
861            readonly: false,
862            autoindent: true,
863            smartindent: true,
864            undo_levels: 1000,
865            undo_break_on_motion: true,
866            iskeyword: "@,48-57,_,192-255".to_string(),
867            timeout_len: core::time::Duration::from_millis(1000),
868            number: true,
869            relativenumber: false,
870            numberwidth: 4,
871            cursorline: false,
872            cursorcolumn: false,
873            signcolumn: crate::types::SignColumnMode::Auto,
874            foldcolumn: 0,
875            colorcolumn: String::new(),
876            formatoptions: "ro".to_string(),
877            filetype: String::new(),
878            commentstring: String::new(),
879            autopair: true,
880            autoclose_tag: true,
881        }
882    }
883}
884
885/// Translate a SPEC [`crate::types::Options`] into the engine's
886/// internal [`Settings`] representation. Field-by-field map; the
887/// shapes are isomorphic except for type widths
888/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
889/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
890/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
891/// is the canonical entry point.
892fn settings_from_options(o: &crate::types::Options) -> Settings {
893    Settings {
894        shiftwidth: o.shiftwidth as usize,
895        tabstop: o.tabstop as usize,
896        softtabstop: o.softtabstop as usize,
897        ignore_case: o.ignorecase,
898        smartcase: o.smartcase,
899        wrapscan: o.wrapscan,
900        textwidth: o.textwidth as usize,
901        expandtab: o.expandtab,
902        wrap: match o.wrap {
903            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
904            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
905            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
906        },
907        readonly: o.readonly,
908        autoindent: o.autoindent,
909        smartindent: o.smartindent,
910        undo_levels: o.undo_levels,
911        undo_break_on_motion: o.undo_break_on_motion,
912        iskeyword: o.iskeyword.clone(),
913        timeout_len: o.timeout_len,
914        number: o.number,
915        relativenumber: o.relativenumber,
916        numberwidth: o.numberwidth,
917        cursorline: o.cursorline,
918        cursorcolumn: o.cursorcolumn,
919        signcolumn: o.signcolumn,
920        foldcolumn: o.foldcolumn,
921        colorcolumn: o.colorcolumn.clone(),
922        formatoptions: o.formatoptions.clone(),
923        filetype: o.filetype.clone(),
924        commentstring: String::new(),
925        autopair: true,
926        autoclose_tag: true,
927    }
928}
929
930/// Host-observable LSP requests triggered by editor bindings. The
931/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
932/// intent that the TUI layer picks up and routes to `sqls`.
933#[derive(Debug, Clone, Copy, PartialEq, Eq)]
934pub enum LspIntent {
935    /// `gd` — textDocument/definition at the cursor.
936    GotoDefinition,
937}
938
939impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
940    /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
941    ///
942    /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
943    /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
944    /// `with_host` / `with_options` triad — there is no shim.
945    ///
946    /// Consumers that don't need a custom host pass
947    /// [`crate::types::DefaultHost::new()`]; consumers that don't need
948    /// custom options pass [`crate::types::Options::default()`].
949    pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
950        let settings = settings_from_options(&options);
951        Self {
952            keybinding_mode: KeybindingMode::Vim,
953            last_yank: None,
954            vim: VimState::default(),
955            undo_stack: Vec::new(),
956            redo_stack: Vec::new(),
957            content_dirty: false,
958            cached_content: None,
959            viewport_height: AtomicU16::new(0),
960            pending_lsp: None,
961            pending_fold_ops: Vec::new(),
962            buffer,
963            style_table: Vec::new(),
964            registers: crate::registers::Registers::default(),
965            styled_spans: Vec::new(),
966            settings,
967            marks: std::collections::BTreeMap::new(),
968            global_marks: std::collections::BTreeMap::new(),
969            current_buffer_id: 0,
970            syntax_fold_ranges: Vec::new(),
971            change_log: Vec::new(),
972            sticky_col: None,
973            host,
974            last_emitted_mode: crate::VimMode::Normal,
975            search_state: crate::search::SearchState::new(),
976            buffer_spans: Vec::new(),
977            pending_content_edits: Vec::new(),
978            pending_content_reset: false,
979            last_indent_range: None,
980        }
981    }
982}
983
984impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
985    /// Borrow the buffer (typed `&B`). Host renders through this via
986    /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
987    pub fn buffer(&self) -> &B {
988        &self.buffer
989    }
990
991    /// Mutably borrow the buffer (typed `&mut B`).
992    pub fn buffer_mut(&mut self) -> &mut B {
993        &mut self.buffer
994    }
995
996    /// Borrow the host adapter directly (typed `&H`).
997    pub fn host(&self) -> &H {
998        &self.host
999    }
1000
1001    /// Mutably borrow the host adapter (typed `&mut H`).
1002    pub fn host_mut(&mut self) -> &mut H {
1003        &mut self.host
1004    }
1005}
1006
1007impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
1008    /// Update the active `iskeyword` spec for word motions
1009    /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
1010    /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
1011    /// single owner now. Equivalent to assigning
1012    /// `settings_mut().iskeyword` directly; the dedicated setter is
1013    /// retained for source-compatibility with 0.0.27 callers.
1014    pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
1015        self.settings.iskeyword = spec.into();
1016    }
1017
1018    /// Emit `Host::emit_cursor_shape` if the public mode has changed
1019    /// since the last emit. Engine calls this at the end of every input
1020    /// step so mode transitions surface to the host without sprinkling
1021    /// the call across every `vim.mode = ...` site.
1022    pub fn emit_cursor_shape_if_changed(&mut self) {
1023        let mode = self.vim_mode();
1024        if mode == self.last_emitted_mode {
1025            return;
1026        }
1027        let shape = match mode {
1028            crate::VimMode::Insert => crate::types::CursorShape::Bar,
1029            _ => crate::types::CursorShape::Block,
1030        };
1031        self.host.emit_cursor_shape(shape);
1032        self.last_emitted_mode = mode;
1033    }
1034
1035    /// Record a yank/cut payload. Writes both the legacy
1036    /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
1037    /// hosts) and the new [`crate::types::Host::write_clipboard`]
1038    /// side-channel (Patch B). Consumers should migrate to a `Host`
1039    /// impl whose `write_clipboard` queues the platform-clipboard
1040    /// write; the `last_yank` mirror will be removed at 0.1.0.
1041    pub(crate) fn record_yank_to_host(&mut self, text: String) {
1042        self.host.write_clipboard(text.clone());
1043        self.last_yank = Some(text);
1044    }
1045
1046    /// Vim's sticky column (curswant). `None` before the first motion;
1047    /// hosts shouldn't normally need to read this directly — it's
1048    /// surfaced for migration off `Buffer::sticky_col` and for
1049    /// snapshot tests.
1050    pub fn sticky_col(&self) -> Option<usize> {
1051        self.sticky_col
1052    }
1053
1054    /// Replace the sticky column. Hosts should rarely touch this —
1055    /// motion code maintains it through the standard horizontal /
1056    /// vertical motion paths.
1057    pub fn set_sticky_col(&mut self, col: Option<usize>) {
1058        self.sticky_col = col;
1059    }
1060
1061    /// Host hook: replace the cached syntax-derived block ranges that
1062    /// `:foldsyntax` consumes. the host calls this on every re-parse;
1063    /// the cost is just a `Vec` swap.
1064    /// Look up a named mark by character. Returns `(row, col)` if
1065    /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
1066    /// uppercase (`'A`–`'Z`) marks live in the same unified
1067    /// [`Editor::marks`] map as of 0.0.36.
1068    pub fn mark(&self, c: char) -> Option<(usize, usize)> {
1069        self.marks.get(&c).copied()
1070    }
1071
1072    /// Set the named mark `c` to `(row, col)`. Used by the FSM's
1073    /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
1074    pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
1075        self.marks.insert(c, pos);
1076    }
1077
1078    /// Remove the named mark `c` (no-op if unset).
1079    pub fn clear_mark(&mut self, c: char) {
1080        self.marks.remove(&c);
1081    }
1082
1083    /// Look up an uppercase global mark by letter. Returns
1084    /// `(buffer_id, row, col)` if set; `None` otherwise.
1085    pub fn global_mark(&self, c: char) -> Option<(u64, usize, usize)> {
1086        self.global_marks.get(&c).copied()
1087    }
1088
1089    /// Set an uppercase global mark `c` to `(buffer_id, row, col)`.
1090    pub fn set_global_mark(&mut self, c: char, buffer_id: u64, pos: (usize, usize)) {
1091        self.global_marks.insert(c, (buffer_id, pos.0, pos.1));
1092    }
1093
1094    /// Return the `buffer_id` this editor is currently attached to.
1095    pub fn current_buffer_id(&self) -> u64 {
1096        self.current_buffer_id
1097    }
1098
1099    /// Update the `buffer_id` this editor is attached to. Called by the
1100    /// app on every `switch_to` so global-mark sets record the correct id.
1101    pub fn set_current_buffer_id(&mut self, id: u64) {
1102        self.current_buffer_id = id;
1103    }
1104
1105    /// Iterate all global marks (`'A'`–`'Z'`), yielding
1106    /// `(mark_char, buffer_id, row, col)`.
1107    pub fn global_marks_iter(&self) -> impl Iterator<Item = (char, u64, usize, usize)> + '_ {
1108        self.global_marks
1109            .iter()
1110            .map(|(c, &(bid, r, col))| (*c, bid, r, col))
1111    }
1112
1113    /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
1114    /// thin wrapper over [`Editor::mark`] for source compatibility
1115    /// with pre-0.0.36 callers; new code should call
1116    /// [`Editor::mark`] directly.
1117    #[deprecated(
1118        since = "0.0.36",
1119        note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
1120    )]
1121    pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
1122        self.mark(c)
1123    }
1124
1125    /// Discard the most recent undo entry. Used by ex commands that
1126    /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
1127    /// matching nothing — popping prevents a no-op undo step from
1128    /// polluting the user's history.
1129    ///
1130    /// Returns `true` if an entry was discarded.
1131    pub fn pop_last_undo(&mut self) -> bool {
1132        self.undo_stack.pop().is_some()
1133    }
1134
1135    /// Read all named marks set this session — both lowercase
1136    /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
1137    /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
1138    /// output is stable.
1139    pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1140        self.marks.iter().map(|(c, p)| (*c, *p))
1141    }
1142
1143    /// Read all buffer-local lowercase marks. Kept for source
1144    /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
1145    /// command); new code should use [`Editor::marks`] which
1146    /// iterates the unified map.
1147    #[deprecated(
1148        since = "0.0.36",
1149        note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
1150    )]
1151    pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1152        self.marks
1153            .iter()
1154            .filter(|(c, _)| c.is_ascii_lowercase())
1155            .map(|(c, p)| (*c, *p))
1156    }
1157
1158    /// Position the cursor was at when the user last jumped via
1159    /// `<C-o>` / `g;` / similar. `None` before any jump.
1160    pub fn last_jump_back(&self) -> Option<(usize, usize)> {
1161        self.vim.jump_back.last().copied()
1162    }
1163
1164    /// Position of the last edit (where `.` would replay). `None` if
1165    /// no edit has happened yet in this session.
1166    pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
1167        self.vim.last_edit_pos
1168    }
1169
1170    /// Read-only view of the file-marks table — uppercase / "file"
1171    /// marks (`'A`–`'Z`) the host has set this session. Returns an
1172    /// iterator of `(mark_char, (row, col))` pairs.
1173    ///
1174    /// Mutate via the FSM (`m{A-Z}` keystroke) or via
1175    /// [`Editor::restore_snapshot`].
1176    ///
1177    /// 0.0.36: file marks now live in the unified [`Editor::marks`]
1178    /// map; this accessor is kept for source compatibility and
1179    /// filters the unified map to uppercase entries.
1180    pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1181        self.marks
1182            .iter()
1183            .filter(|(c, _)| c.is_ascii_uppercase())
1184            .map(|(c, p)| (*c, *p))
1185    }
1186
1187    /// Read-only view of the cached syntax-derived block ranges that
1188    /// `:foldsyntax` consumes. Returns the slice the host last
1189    /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1190    /// no syntax integration is active.
1191    pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1192        &self.syntax_fold_ranges
1193    }
1194
1195    pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1196        self.syntax_fold_ranges = ranges;
1197    }
1198
1199    /// Live settings (read-only). `:set` mutates these via
1200    /// [`Editor::settings_mut`].
1201    pub fn settings(&self) -> &Settings {
1202        &self.settings
1203    }
1204
1205    /// Live settings (mutable). `:set` flows through here to mutate
1206    /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1207    /// configuring at startup typically construct a [`Settings`]
1208    /// snapshot and overwrite via `*editor.settings_mut() = …`.
1209    pub fn settings_mut(&mut self) -> &mut Settings {
1210        &mut self.settings
1211    }
1212
1213    /// Set the active filetype (language name) for the current buffer.
1214    /// Used by comment-continuation and future language-aware features.
1215    /// Equivalent to `:set filetype=<lang>`. Pass `""` to clear.
1216    pub fn set_filetype(&mut self, lang: &str) {
1217        self.settings.filetype = lang.to_string();
1218    }
1219
1220    /// Returns `true` when `:set readonly` is active. Convenience
1221    /// accessor for hosts that cannot import the internal [`Settings`]
1222    /// type. Phase 5 binary uses this to gate `:w` writes.
1223    pub fn is_readonly(&self) -> bool {
1224        self.settings.readonly
1225    }
1226
1227    /// Borrow the engine search state. Hosts inspecting the
1228    /// committed `/` / `?` pattern (e.g. for status-line display) or
1229    /// feeding the active regex into `BufferView::search_pattern`
1230    /// read it from here.
1231    pub fn search_state(&self) -> &crate::search::SearchState {
1232        &self.search_state
1233    }
1234
1235    /// Mutable engine search state. Hosts driving search
1236    /// programmatically (test fixtures, scripted demos) write the
1237    /// pattern through here.
1238    pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1239        &mut self.search_state
1240    }
1241
1242    /// Install `pattern` as the active search regex on the engine
1243    /// state and clear the cached row matches. Pass `None` to clear.
1244    /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1245    /// — `BufferView` now takes the regex through its `search_pattern`
1246    /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1247    pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1248        self.search_state.set_pattern(pattern);
1249    }
1250
1251    /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1252    /// to the next match of `search_state.pattern` from the cursor's
1253    /// current position. Returns `true` when a match was found.
1254    /// `skip_current = true` excludes a match the cursor sits on.
1255    pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1256        crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1257    }
1258
1259    /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1260    pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1261        crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1262    }
1263
1264    /// Snapshot of the unnamed register (the default `p` / `P` source).
1265    pub fn yank(&self) -> &str {
1266        &self.registers.unnamed.text
1267    }
1268
1269    /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1270    pub fn registers(&self) -> &crate::registers::Registers {
1271        &self.registers
1272    }
1273
1274    /// Mutably borrow the full register bank. Hosts that share registers
1275    /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1276    /// the slots here on buffer switch.
1277    pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1278        &mut self.registers
1279    }
1280
1281    /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1282    /// register slot. the host calls this before letting vim consume a
1283    /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1284    /// stale snapshot from the last yank.
1285    pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1286        self.registers.set_clipboard(text, linewise);
1287    }
1288
1289    /// Return the user's pending register selection (set via `"<reg>` chord
1290    /// before an operator). `None` if no register was selected — caller should
1291    /// use the unnamed register `"`.
1292    ///
1293    /// Read-only — does not consume / clear the pending selection. The
1294    /// register is cleared by the engine after the next operator fires.
1295    ///
1296    /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1297    /// honor `"a` + visual op chord sequences.
1298    pub fn pending_register(&self) -> Option<char> {
1299        self.vim.pending_register
1300    }
1301
1302    /// True when the user's pending register selector is `+` or `*`.
1303    /// the host peeks this so it can refresh `sync_clipboard_register`
1304    /// only when a clipboard read is actually about to happen.
1305    pub fn pending_register_is_clipboard(&self) -> bool {
1306        matches!(self.vim.pending_register, Some('+') | Some('*'))
1307    }
1308
1309    /// Register currently being recorded into via `q{reg}`. `None` when
1310    /// no recording is active. Hosts use this to surface a "recording @r"
1311    /// indicator in the status line.
1312    pub fn recording_register(&self) -> Option<char> {
1313        self.vim.recording_macro
1314    }
1315
1316    /// Pending repeat count the user has typed but not yet resolved
1317    /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1318    /// Hosts surface this in a "showcmd" area.
1319    pub fn pending_count(&self) -> Option<u32> {
1320        self.vim.pending_count_val()
1321    }
1322
1323    /// The operator character for any in-flight operator that is waiting
1324    /// for a motion (e.g. `d` after the user types `d` but before a
1325    /// motion). Returns `None` when no operator is pending.
1326    pub fn pending_op(&self) -> Option<char> {
1327        self.vim.pending_op_char()
1328    }
1329
1330    /// `true` when the engine is in any pending chord state — waiting for
1331    /// the next key to complete a command (e.g. `r<char>` replace,
1332    /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1333    /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1334    /// register selection `"<reg>`, macro recording target, etc).
1335    ///
1336    /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1337    /// and forward keys directly to the engine so in-flight commands can
1338    /// complete without the host eating their continuation keys.
1339    pub fn is_chord_pending(&self) -> bool {
1340        self.vim.is_chord_pending()
1341    }
1342
1343    /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1344    /// is waiting for the next typed character to name the register to paste.
1345    /// The dispatcher should call `insert_paste_register(c)` instead of
1346    /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1347    ///
1348    /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1349    /// without having to drive the full FSM.
1350    pub fn is_insert_register_pending(&self) -> bool {
1351        self.vim.insert_pending_register
1352    }
1353
1354    /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1355    /// before `insert_paste_register(c)` in app-level dispatchers so that the
1356    /// flag does not persist into the next key. Call before
1357    /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1358    ///
1359    /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1360    pub fn clear_insert_register_pending(&mut self) {
1361        self.vim.insert_pending_register = false;
1362    }
1363
1364    /// Read-only view of the jump-back list (positions pushed on "big"
1365    /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1366    #[allow(clippy::type_complexity)]
1367    pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1368        (&self.vim.jump_back, &self.vim.jump_fwd)
1369    }
1370
1371    /// Read-only view of the change list (positions of recent edits) plus
1372    /// the current walk cursor. Newest entry is at the back.
1373    pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1374        (&self.vim.change_list, self.vim.change_list_cursor)
1375    }
1376
1377    /// Replace the unnamed register without touching any other slot.
1378    /// For host-driven imports (e.g. system clipboard); operator
1379    /// code uses [`record_yank`] / [`record_delete`].
1380    pub fn set_yank(&mut self, text: impl Into<String>) {
1381        let text = text.into();
1382        let linewise = self.vim.yank_linewise;
1383        self.registers.unnamed = crate::registers::Slot { text, linewise };
1384    }
1385
1386    /// Record a yank into `"` and `"0`, plus the named target if the
1387    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1388    /// paste path.
1389    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1390        self.vim.yank_linewise = linewise;
1391        let target = self.vim.pending_register.take();
1392        self.registers.record_yank(text, linewise, target);
1393    }
1394
1395    /// Direct write to a named register slot — bypasses the unnamed
1396    /// `"` and `"0` updates that `record_yank` does. Used by the
1397    /// macro recorder so finishing a `q{reg}` recording doesn't
1398    /// pollute the user's last yank.
1399    pub fn set_named_register_text(&mut self, reg: char, text: String) {
1400        if let Some(slot) = match reg {
1401            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1402            'A'..='Z' => {
1403                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1404            }
1405            _ => None,
1406        } {
1407            slot.text = text;
1408            slot.linewise = false;
1409        }
1410    }
1411
1412    /// Record a delete / change into `"` and the `"1`–`"9` ring.
1413    /// Honours the active named-register prefix.
1414    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1415        self.vim.yank_linewise = linewise;
1416        let target = self.vim.pending_register.take();
1417        self.registers.record_delete(text, linewise, target);
1418    }
1419
1420    /// Install styled syntax spans using the engine-native
1421    /// [`crate::types::Style`]. Always available — engine is ratatui-free.
1422    /// Ratatui hosts use
1423    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`
1424    /// which converts at the boundary and delegates here.
1425    ///
1426    /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1427    /// 0.1.0 freeze the unprefixed name is the universally-available
1428    /// engine-native variant.
1429    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1430        // Note: do NOT pre-collect `line_byte_lens` here. `buf_line` clones
1431        // the row string under a content-mutex lock; pre-collecting for
1432        // every row turns a 10k-row file's install into 10k mutex-locked
1433        // String clones (visible as j/k cursor lag). The typical install
1434        // has spans on at most a few hundred rows (the parsed viewport
1435        // window); lazy lookup keeps the cost proportional to populated
1436        // rows, not file size.
1437        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1438        let mut engine_spans: Vec<Vec<(usize, usize, crate::types::Style)>> =
1439            Vec::with_capacity(spans.len());
1440        for (row, row_spans) in spans.iter().enumerate() {
1441            if row_spans.is_empty() {
1442                by_row.push(Vec::new());
1443                engine_spans.push(Vec::new());
1444                continue;
1445            }
1446            let line_len = buf_line(&self.buffer, row).map(|s| s.len()).unwrap_or(0);
1447            let mut translated = Vec::with_capacity(row_spans.len());
1448            let mut translated_e = Vec::with_capacity(row_spans.len());
1449            for (start, end, style) in row_spans {
1450                let end_clamped = (*end).min(line_len);
1451                if end_clamped <= *start {
1452                    continue;
1453                }
1454                let id = self.intern_style(*style);
1455                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1456                translated_e.push((*start, end_clamped, *style));
1457            }
1458            by_row.push(translated);
1459            engine_spans.push(translated_e);
1460        }
1461        self.buffer_spans = by_row;
1462        self.styled_spans = engine_spans;
1463    }
1464
1465    /// Patch only `rows` of the installed `buffer_spans` / `styled_spans`,
1466    /// leaving rows outside that range untouched. `spans` is indexed by
1467    /// row offset within `rows` — `spans[0]` is for `rows.start`,
1468    /// `spans[1]` for `rows.start + 1`, etc.
1469    ///
1470    /// Use this instead of [`Self::install_syntax_spans`] when a sync
1471    /// `query_viewport` produced spans for the visible region only.
1472    /// Walking the full `line_count` and re-installing every row on
1473    /// every j/k that nudges the viewport dominated the per-keystroke
1474    /// cost on large files; patching just the changed range keeps the
1475    /// cost proportional to viewport size, not file size.
1476    ///
1477    /// Ensures `buffer_spans` / `styled_spans` are sized to the buffer's
1478    /// current `line_count` (resizes if a row-count edit shifted them).
1479    pub fn patch_syntax_spans_range(
1480        &mut self,
1481        rows: std::ops::Range<usize>,
1482        spans: &[Vec<(usize, usize, crate::types::Style)>],
1483    ) {
1484        let line_count = buf_row_count(&self.buffer);
1485        if self.buffer_spans.len() != line_count {
1486            self.buffer_spans.resize_with(line_count, Vec::new);
1487        }
1488        if self.styled_spans.len() != line_count {
1489            self.styled_spans.resize_with(line_count, Vec::new);
1490        }
1491        for (i, row_spans) in spans.iter().enumerate() {
1492            let row = rows.start + i;
1493            if row >= line_count {
1494                break;
1495            }
1496            if row_spans.is_empty() {
1497                self.buffer_spans[row] = Vec::new();
1498                self.styled_spans[row] = Vec::new();
1499                continue;
1500            }
1501            let line_len = buf_line(&self.buffer, row).map(|s| s.len()).unwrap_or(0);
1502            let mut translated = Vec::with_capacity(row_spans.len());
1503            let mut translated_e = Vec::with_capacity(row_spans.len());
1504            for (start, end, style) in row_spans {
1505                let end_clamped = (*end).min(line_len);
1506                if end_clamped <= *start {
1507                    continue;
1508                }
1509                let id = self.intern_style(*style);
1510                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1511                translated_e.push((*start, end_clamped, *style));
1512            }
1513            self.buffer_spans[row] = translated;
1514            self.styled_spans[row] = translated_e;
1515        }
1516    }
1517
1518    /// Translate the cached `buffer_spans` / `styled_spans` row indices
1519    /// in-place to track a batch of [`crate::types::ContentEdit`]s without
1520    /// blanking the cache.
1521    ///
1522    /// Why: spans are installed by the async syntax worker, which can lag
1523    /// the buffer by one or more frames after an edit. If the edit changes
1524    /// the row count and we keep the old span rows in place, the renderer
1525    /// paints last-frame's spans at the wrong line — visibly garbled colours.
1526    /// The historical fix was to blank `buffer_spans` whenever a row-count
1527    /// change came through, but that produces a white flash on every Enter
1528    /// or backspace-at-BOL.
1529    ///
1530    /// What this does instead: for each edit, insert empty span rows where
1531    /// the edit grew the buffer and drain rows where it shrank, so the
1532    /// surviving rows still index the right line. Spans on the edited row
1533    /// itself stay (they'll show stale colours for that one row until the
1534    /// worker delivers a fresh parse, which is invisible compared to the
1535    /// blank flash).
1536    ///
1537    /// Edits are applied in order — each edit's `(row, col)` positions are
1538    /// taken to be relative to the post-state of the prior edits in the
1539    /// batch (matching the order the engine emitted them).
1540    pub fn shift_syntax_spans_for_edits(&mut self, edits: &[crate::types::ContentEdit]) {
1541        for edit in edits {
1542            let oer = edit.old_end_position.0 as usize;
1543            let ner = edit.new_end_position.0 as usize;
1544            if ner == oer {
1545                continue;
1546            }
1547            let start_row = edit.start_position.0 as usize;
1548            let start_col = edit.start_position.1 as usize;
1549            // Insert/drain index depends on whether the edit starts at
1550            // the BEGINNING of `start_row` or somewhere INSIDE it.
1551            //   col == 0 → edit is at the very start of `start_row`; new
1552            //              rows go BEFORE row `start_row`, so the affected
1553            //              indices begin AT `start_row`.
1554            //   col > 0 → edit is inside `start_row`; new rows go AFTER
1555            //              `start_row`, so affected indices begin at
1556            //              `start_row + 1`.
1557            //
1558            // Pre-fix this always used `oer + 1` (the col-> 0 branch),
1559            // which left row `start_row`'s spans at its old index while
1560            // the file's row `start_row` was now the freshly-pasted
1561            // content — visible as wrong-row colour mappings after
1562            // `ggP` / `P` / any insert at column 0.
1563            let affected_idx = if start_col == 0 {
1564                start_row
1565            } else {
1566                start_row + 1
1567            };
1568            if ner > oer {
1569                let n = ner - oer;
1570                // O(len + n) via splice; the prior per-row `insert(idx, ...)`
1571                // loop was O(n × (len - idx)), which on a 60k-row paste at
1572                // the BOL became ~1.8 G memmove ops (87 % of paste CPU per
1573                // samply). Splice memmove-shifts once, then fills.
1574                let idx = affected_idx.min(self.buffer_spans.len());
1575                self.buffer_spans
1576                    .splice(idx..idx, std::iter::repeat_with(Vec::new).take(n));
1577                let idx_s = affected_idx.min(self.styled_spans.len());
1578                self.styled_spans
1579                    .splice(idx_s..idx_s, std::iter::repeat_with(Vec::new).take(n));
1580            } else {
1581                let n = oer - ner;
1582                let len_b = self.buffer_spans.len();
1583                let start_b = affected_idx.min(len_b);
1584                let end_b = (start_b + n).min(len_b);
1585                if end_b > start_b {
1586                    self.buffer_spans.drain(start_b..end_b);
1587                }
1588                let len_s = self.styled_spans.len();
1589                let start_s = affected_idx.min(len_s);
1590                let end_s = (start_s + n).min(len_s);
1591                if end_s > start_s {
1592                    self.styled_spans.drain(start_s..end_s);
1593                }
1594            }
1595        }
1596    }
1597
1598    /// Read-only view of the style table in engine-native form —
1599    /// id `i` → `style_table[i]`. Always available, no cfg gate.
1600    ///
1601    /// Ratatui hosts that need a `ratatui::style::Style` slice should
1602    /// use `hjkl_engine_tui::EditorRatatuiExt::ratatui_style_table` or
1603    /// convert individual entries via `hjkl_engine_tui::style_to_ratatui`.
1604    pub fn style_table(&self) -> &[crate::types::Style] {
1605        &self.style_table
1606    }
1607
1608    /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1609    /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1610    /// per draw frame.
1611    ///
1612    /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1613    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1614    /// caches spans; they live on the engine and route through the
1615    /// `Host::syntax_highlights` pipeline.
1616    pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1617        &self.buffer_spans
1618    }
1619
1620    /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1621    /// Engine-native — the unified `style_table` is always engine-native.
1622    /// Linear-scan dedup — the table grows only as new tree-sitter token
1623    /// kinds appear, so it stays tiny. Ratatui callers use
1624    /// `hjkl_engine_tui::EditorRatatuiExt::intern_ratatui_style` which
1625    /// converts at the boundary and delegates here.
1626    ///
1627    /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1628    /// the unprefixed name is the universally-available engine-native
1629    /// variant.
1630    pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1631        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1632            return idx as u32;
1633        }
1634        self.style_table.push(style);
1635        (self.style_table.len() - 1) as u32
1636    }
1637
1638    /// Look up an interned style by id and return it as a SPEC
1639    /// [`crate::types::Style`]. Returns `None` for ids past the end
1640    /// of the table.
1641    pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1642        self.style_table.get(id as usize).copied()
1643    }
1644
1645    /// Historical reverse-sync hook from when the textarea mirrored
1646    /// the buffer. Now that Buffer is the cursor authority this is a
1647    /// no-op; call sites can remain in place during the migration.
1648    pub fn push_buffer_cursor_to_textarea(&mut self) {}
1649
1650    /// Force the host viewport's top row without touching the
1651    /// cursor. Used by tests that simulate a scroll without the
1652    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1653    /// apply.
1654    ///
1655    /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1656    /// instead of the (now-deleted) `Buffer::viewport_mut`.
1657    pub fn set_viewport_top(&mut self, row: usize) {
1658        let last = buf_row_count(&self.buffer).saturating_sub(1);
1659        let target = row.min(last);
1660        self.host.viewport_mut().top_row = target;
1661    }
1662
1663    /// Set the cursor to `(row, col)`, clamped to the buffer's
1664    /// content. Hosts use this for goto-line, jump-to-mark, and
1665    /// programmatic cursor placement.
1666    ///
1667    /// Resets `sticky_col` (curswant) to `col` — every explicit jump
1668    /// (goto-line, jump-to-mark, search hit, click, `]d`) follows vim
1669    /// semantics. Only `j`/`k`/`+`/`-` READ `sticky_col`; everything
1670    /// else resets it to the column where the cursor actually landed.
1671    pub fn jump_cursor(&mut self, row: usize, col: usize) {
1672        buf_set_cursor_rc(&mut self.buffer, row, col);
1673        self.sticky_col = Some(col);
1674    }
1675
1676    /// Set the cursor to `(row, col)` without modifying `sticky_col`.
1677    ///
1678    /// Use this for host-side state restores (viewport sync, snapshot
1679    /// replay) where the cursor was already at this position semantically
1680    /// and the host's sticky tracking should remain authoritative.
1681    ///
1682    /// For user-facing jumps (goto-line, search hit, picker `<CR>`, `]d`,
1683    /// click), use [`Editor::jump_cursor`] which DOES reset `sticky_col`
1684    /// per vim curswant semantics.
1685    pub fn set_cursor_quiet(&mut self, row: usize, col: usize) {
1686        buf_set_cursor_rc(&mut self.buffer, row, col);
1687    }
1688
1689    /// `(row, col)` cursor read sourced from the migration buffer.
1690    /// Equivalent to `self.textarea.cursor()` when the two are in
1691    /// sync — which is the steady state during Phase 7f because
1692    /// every step opens with `sync_buffer_content_from_textarea` and
1693    /// every ported motion pushes the result back. Prefer this over
1694    /// `self.textarea.cursor()` so call sites keep working unchanged
1695    /// once the textarea field is ripped.
1696    pub fn cursor(&self) -> (usize, usize) {
1697        buf_cursor_rc(&self.buffer)
1698    }
1699
1700    /// Drain any pending LSP intent raised by the last key. Returns
1701    /// `None` when no intent is armed.
1702    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1703        self.pending_lsp.take()
1704    }
1705
1706    /// Drain every [`crate::types::FoldOp`] raised since the last
1707    /// call. Hosts that mirror the engine's fold storage (or that
1708    /// project folds onto a separate fold tree, LSP folding ranges,
1709    /// …) drain this each step and dispatch as their own
1710    /// [`crate::types::Host::Intent`] requires.
1711    ///
1712    /// The engine has already applied every op locally against the
1713    /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1714    /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1715    /// don't track folds independently can ignore the queue
1716    /// (or simply never call this drain).
1717    ///
1718    /// Introduced in 0.0.38 (Patch C-δ.4).
1719    pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1720        std::mem::take(&mut self.pending_fold_ops)
1721    }
1722
1723    /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1724    /// surface: queue it for host observation (drained by
1725    /// [`Editor::take_fold_ops`]) and apply it locally against the
1726    /// in-tree buffer fold storage via
1727    /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1728    /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1729    /// invalidation) route every fold mutation through this method.
1730    ///
1731    /// Introduced in 0.0.38 (Patch C-δ.4).
1732    pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1733        use crate::types::FoldProvider;
1734        self.pending_fold_ops.push(op);
1735        let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1736        provider.apply(op);
1737    }
1738
1739    /// Refresh the host viewport's height from the cached
1740    /// `viewport_height_value()`. Called from the per-step
1741    /// boilerplate; was the textarea → buffer mirror before Phase 7f
1742    /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1743    /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1744    /// `Host::viewport_mut`.
1745    pub fn sync_buffer_from_textarea(&mut self) {
1746        let height = self.viewport_height_value();
1747        self.host.viewport_mut().height = height;
1748    }
1749
1750    /// Was the full textarea → buffer content sync. Buffer is the
1751    /// content authority now; this remains as a no-op so the per-step
1752    /// call sites don't have to be ripped in the same patch.
1753    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1754        self.sync_buffer_from_textarea();
1755    }
1756
1757    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1758    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1759    /// the cursor without going through the vim engine's motion
1760    /// machinery, where push_jump fires automatically.
1761    pub fn record_jump(&mut self, pos: (usize, usize)) {
1762        const JUMPLIST_MAX: usize = 100;
1763        self.vim.jump_back.push(pos);
1764        if self.vim.jump_back.len() > JUMPLIST_MAX {
1765            self.vim.jump_back.remove(0);
1766        }
1767        self.vim.jump_fwd.clear();
1768    }
1769
1770    /// Host apps call this each draw with the current text area height so
1771    /// scroll helpers can clamp the cursor without recomputing layout.
1772    pub fn set_viewport_height(&self, height: u16) {
1773        self.viewport_height.store(height, Ordering::Relaxed);
1774    }
1775
1776    /// Last height published by `set_viewport_height` (in rows).
1777    pub fn viewport_height_value(&self) -> u16 {
1778        self.viewport_height.load(Ordering::Relaxed)
1779    }
1780
1781    /// Apply `edit` against the buffer and return the inverse so the
1782    /// host can push it onto an undo stack. Side effects: dirty
1783    /// flag, change-list ring, mark / jump-list shifts, change_log
1784    /// append, fold invalidation around the touched rows.
1785    ///
1786    /// The primary edit funnel — both FSM operators and ex commands
1787    /// route mutations through here so the side effects fire
1788    /// uniformly.
1789    pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1790        // `:set readonly` short-circuits every mutation funnel: no
1791        // buffer change, no dirty flag, no undo entry, no change-log
1792        // emission. We swallow the requested `edit` and hand back a
1793        // self-inverse no-op (`InsertStr` of an empty string at the
1794        // current cursor) so callers that push the return value onto
1795        // an undo stack still get a structurally valid round trip.
1796        if self.settings.readonly {
1797            let _ = edit;
1798            return hjkl_buffer::Edit::InsertStr {
1799                at: buf_cursor_pos(&self.buffer),
1800                text: String::new(),
1801            };
1802        }
1803        let pre_row = buf_cursor_row(&self.buffer);
1804        let pre_rows = buf_row_count(&self.buffer);
1805        // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1806        // Vim's `:h '.` says "the position where the last change was made",
1807        // meaning the change-start, not the post-insert cursor. We snap it
1808        // here before `apply_buffer_edit` moves the cursor.
1809        let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1810        // Map the underlying buffer edit to a SPEC EditOp for
1811        // change-log emission before consuming it. Coarse — see
1812        // change_log field doc on the struct.
1813        self.change_log.extend(edit_to_editops(&edit));
1814        // Compute ContentEdit fan-out from the pre-edit buffer state.
1815        // Done before `apply_buffer_edit` consumes `edit` so we can
1816        // inspect the operation's fields and the buffer's pre-edit row
1817        // bytes (needed for byte_of_row / col_byte conversion). Edits
1818        // are pushed onto `pending_content_edits` for host drain.
1819        let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1820        self.pending_content_edits.extend(content_edits);
1821        // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1822        // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1823        // the 0.0.42 plan — see that fn's doc comment). The free fn
1824        // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1825        // no longer carries a `self.buffer.<inherent>` hop.
1826        let inverse = apply_buffer_edit(&mut self.buffer, edit);
1827        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1828        // Drop any folds the edit's range overlapped — vim opens the
1829        // surrounding fold automatically when you edit inside it. The
1830        // approximation here invalidates folds covering either the
1831        // pre-edit cursor row or the post-edit cursor row, which
1832        // catches the common single-line / multi-line edit shapes.
1833        let lo = pre_row.min(pos_row);
1834        let hi = pre_row.max(pos_row);
1835        self.apply_fold_op(crate::types::FoldOp::Invalidate {
1836            start_row: lo,
1837            end_row: hi,
1838        });
1839        // Dot mark records the PRE-edit position (change start), matching
1840        // vim's `:h '.` semantics. Previously this stored the post-edit
1841        // cursor, which diverged from nvim on `iX<Esc>j`.
1842        self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1843        // Append to the change-list ring (skip when the cursor sits on
1844        // the same cell as the last entry — back-to-back keystrokes on
1845        // one column shouldn't pollute the ring). A new edit while
1846        // walking the ring trims the forward half, vim style.
1847        let entry = (pos_row, pos_col);
1848        if self.vim.change_list.last() != Some(&entry) {
1849            if let Some(idx) = self.vim.change_list_cursor.take() {
1850                self.vim.change_list.truncate(idx + 1);
1851            }
1852            self.vim.change_list.push(entry);
1853            let len = self.vim.change_list.len();
1854            if len > crate::vim::CHANGE_LIST_MAX {
1855                self.vim
1856                    .change_list
1857                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1858            }
1859        }
1860        self.vim.change_list_cursor = None;
1861        // Shift / drop marks + jump-list entries to track the row
1862        // delta the edit produced. Without this, every line-changing
1863        // edit silently invalidates `'a`-style positions.
1864        let post_rows = buf_row_count(&self.buffer);
1865        let delta = post_rows as isize - pre_rows as isize;
1866        if delta != 0 {
1867            self.shift_marks_after_edit(pre_row, delta);
1868        }
1869        self.push_buffer_content_to_textarea();
1870        self.mark_content_dirty();
1871        inverse
1872    }
1873
1874    /// Migrate user marks + jumplist entries when an edit at row
1875    /// `edit_start` changes the buffer's row count by `delta` (positive
1876    /// for inserts, negative for deletes). Marks tied to a deleted row
1877    /// are dropped; marks past the affected band shift by `delta`.
1878    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1879        if delta == 0 {
1880            return;
1881        }
1882        // Deleted-row band (only meaningful for delta < 0). Inclusive
1883        // start, exclusive end.
1884        let drop_end = if delta < 0 {
1885            edit_start.saturating_add((-delta) as usize)
1886        } else {
1887            edit_start
1888        };
1889        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1890
1891        // 0.0.36: lowercase + uppercase marks share the unified
1892        // `marks` map; one pass migrates both.
1893        let mut to_drop: Vec<char> = Vec::new();
1894        for (c, (row, _col)) in self.marks.iter_mut() {
1895            if (edit_start..drop_end).contains(row) {
1896                to_drop.push(*c);
1897            } else if *row >= shift_threshold {
1898                *row = ((*row as isize) + delta).max(0) as usize;
1899            }
1900        }
1901        for c in to_drop {
1902            self.marks.remove(&c);
1903        }
1904
1905        // Shift global marks that belong to the current buffer.
1906        let cur_bid = self.current_buffer_id;
1907        let mut global_to_drop: Vec<char> = Vec::new();
1908        for (c, (bid, row, _col)) in self.global_marks.iter_mut() {
1909            if *bid != cur_bid {
1910                continue;
1911            }
1912            if (edit_start..drop_end).contains(row) {
1913                global_to_drop.push(*c);
1914            } else if *row >= shift_threshold {
1915                *row = ((*row as isize) + delta).max(0) as usize;
1916            }
1917        }
1918        for c in global_to_drop {
1919            self.global_marks.remove(&c);
1920        }
1921
1922        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1923            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1924            for (row, _) in entries.iter_mut() {
1925                if *row >= shift_threshold {
1926                    *row = ((*row as isize) + delta).max(0) as usize;
1927                }
1928            }
1929        };
1930        shift_jumps(&mut self.vim.jump_back);
1931        shift_jumps(&mut self.vim.jump_fwd);
1932    }
1933
1934    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1935    /// the textarea from the buffer's lines + cursor, preserving yank
1936    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1937    /// textarea field disappears at the end of Phase 7f anyway.
1938    /// No-op since Buffer is the content authority. Retained as a
1939    /// shim so call sites in `mutate_edit` and friends don't have to
1940    /// be ripped in lockstep with the field removal.
1941    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1942
1943    /// Single choke-point for "the buffer just changed". Sets the
1944    /// dirty flag and drops the cached `content_arc` snapshot so
1945    /// subsequent reads rebuild from the live textarea. Callers
1946    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1947    /// path) must invoke this to keep the cache honest.
1948    pub fn mark_content_dirty(&mut self) {
1949        self.content_dirty = true;
1950        self.cached_content = None;
1951    }
1952
1953    /// Returns true if content changed since the last call, then clears the flag.
1954    pub fn take_dirty(&mut self) -> bool {
1955        let dirty = self.content_dirty;
1956        self.content_dirty = false;
1957        dirty
1958    }
1959
1960    /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1961    /// the last call. Each entry corresponds to a single buffer
1962    /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1963    /// fan out to one entry per row touched.
1964    ///
1965    /// Hosts call this each frame (after [`Editor::take_content_reset`])
1966    /// to fan edits into a tree-sitter parser via `Tree::edit`.
1967    pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1968        std::mem::take(&mut self.pending_content_edits)
1969    }
1970
1971    /// Returns `true` if a bulk buffer replacement happened since the
1972    /// last call (e.g. `set_content` / `restore` / undo restore), then
1973    /// clears the flag. When this returns `true`, hosts should drop
1974    /// any retained syntax tree before consuming
1975    /// [`Editor::take_content_edits`].
1976    pub fn take_content_reset(&mut self) -> bool {
1977        let r = self.pending_content_reset;
1978        self.pending_content_reset = false;
1979        r
1980    }
1981
1982    /// Pull-model coarse change observation. If content changed since
1983    /// the last call, returns `Some(Arc<String>)` with the new content
1984    /// and clears the dirty flag; otherwise returns `None`.
1985    ///
1986    /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1987    /// the character level) should diff against their own previous
1988    /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1989    /// once every edit path inside the engine is instrumented; this
1990    /// coarse form covers the pull-model use case in the meantime.
1991    pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1992        if !self.content_dirty {
1993            return None;
1994        }
1995        let arc = self.content_arc();
1996        self.content_dirty = false;
1997        Some(arc)
1998    }
1999
2000    /// Width in cells of the line-number gutter for the current buffer
2001    /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
2002    /// in front of the text column. Returns `0` when both `number` and
2003    /// `relativenumber` are off.
2004    pub fn lnum_width(&self) -> u16 {
2005        if self.settings.number || self.settings.relativenumber {
2006            let needed = buf_row_count(&self.buffer).to_string().len() + 1;
2007            needed.max(self.settings.numberwidth) as u16
2008        } else {
2009            0
2010        }
2011    }
2012
2013    /// Returns the cursor's row within the visible textarea (0-based), updating
2014    /// the stored viewport top so subsequent calls remain accurate.
2015    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
2016        let cursor = buf_cursor_row(&self.buffer);
2017        let top = self.host.viewport().top_row;
2018        cursor.saturating_sub(top).min(height as usize - 1) as u16
2019    }
2020
2021    /// Returns the cursor's screen position `(x, y)` for the textarea
2022    /// described by `(area_x, area_y, area_width, area_height)`.
2023    /// Accounts for line-number gutter, viewport scroll, and any extra
2024    /// gutter width to the left of the number column (sign column, fold
2025    /// column). Returns `None` if the cursor is outside the visible
2026    /// viewport. Always available (engine-native; no ratatui dependency).
2027    ///
2028    /// `extra_gutter_width` is added to the number-column width before
2029    /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
2030    /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
2031    /// when a dedicated sign or fold column is present.
2032    ///
2033    /// Renamed from `cursor_screen_pos_xywh` in 0.0.32.
2034    pub fn cursor_screen_pos(
2035        &self,
2036        area_x: u16,
2037        area_y: u16,
2038        area_width: u16,
2039        area_height: u16,
2040        extra_gutter_width: u16,
2041    ) -> Option<(u16, u16)> {
2042        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
2043        let v = self.host.viewport();
2044        if pos_row < v.top_row || pos_col < v.top_col {
2045            return None;
2046        }
2047        let lnum_width = self.lnum_width();
2048        // Full offset from the left edge of the window to the first text cell.
2049        let gutter_total = lnum_width + extra_gutter_width;
2050        let dy = (pos_row - v.top_row) as u16;
2051        // Convert char column to visual column so cursor lands on the
2052        // correct cell when the line contains tabs (which the renderer
2053        // expands to TAB_WIDTH stops). Tab width must match the renderer.
2054        let cursor_rope = self.buffer.rope();
2055        let pos_row_safe = pos_row.min(cursor_rope.len_lines().saturating_sub(1));
2056        let line = hjkl_buffer::rope_line_str(&cursor_rope, pos_row_safe);
2057        let tab_width = if v.tab_width == 0 {
2058            4
2059        } else {
2060            v.tab_width as usize
2061        };
2062        let visual_pos = visual_col_for_char(&line, pos_col, tab_width);
2063        let visual_top = visual_col_for_char(&line, v.top_col, tab_width);
2064        let dx = (visual_pos - visual_top) as u16;
2065        if dy >= area_height || dx + gutter_total >= area_width {
2066            return None;
2067        }
2068        Some((area_x + gutter_total + dx, area_y + dy))
2069    }
2070
2071    /// Returns the current vim mode. Phase 6.3: reads from the stable
2072    /// `current_mode` field (kept in sync by both the FSM step loop and
2073    /// the Phase 6.3 primitive bridges) rather than deriving from the
2074    /// FSM-internal `mode` field via `public_mode()`.
2075    pub fn vim_mode(&self) -> VimMode {
2076        self.vim.current_mode
2077    }
2078
2079    /// Bounds of the active visual-block rectangle as
2080    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
2081    /// `None` when we're not in VisualBlock mode.
2082    /// Read-only view of the live `/` or `?` prompt. `None` outside
2083    /// search-prompt mode.
2084    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
2085        self.vim.search_prompt.as_ref()
2086    }
2087
2088    /// Most recent committed search pattern (persists across `n` / `N`
2089    /// and across prompt exits). `None` before the first search.
2090    pub fn last_search(&self) -> Option<&str> {
2091        self.vim.last_search.as_deref()
2092    }
2093
2094    /// Whether the last committed search was a forward `/` (`true`) or
2095    /// a backward `?` (`false`). `n` and `N` consult this to honour the
2096    /// direction the user committed.
2097    pub fn last_search_forward(&self) -> bool {
2098        self.vim.last_search_forward
2099    }
2100
2101    /// Set the most recent committed search text + direction. Used by
2102    /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
2103    /// outside the engine's vim FSM) so `n` / `N` repeat the host's
2104    /// most recent commit with the right direction. Pass `None` /
2105    /// `true` to clear.
2106    pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
2107        self.vim.last_search = text;
2108        self.vim.last_search_forward = forward;
2109    }
2110
2111    /// The most recent successful `:s` command. `None` before the first substitute.
2112    /// Used by `:&` / `:&&` to repeat it.
2113    pub fn last_substitute(&self) -> Option<&crate::substitute::SubstituteCmd> {
2114        self.vim.last_substitute.as_ref()
2115    }
2116
2117    /// Store the last successful substitute so `:&` / `:&&` can repeat it.
2118    pub fn set_last_substitute(&mut self, cmd: crate::substitute::SubstituteCmd) {
2119        self.vim.last_substitute = Some(cmd);
2120    }
2121
2122    /// Start/end `(row, col)` of the active char-wise Visual selection
2123    /// (inclusive on both ends, positionally ordered). `None` when not
2124    /// in Visual mode.
2125    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
2126        if self.vim_mode() != VimMode::Visual {
2127            return None;
2128        }
2129        let anchor = self.vim.visual_anchor;
2130        let cursor = self.cursor();
2131        let (start, end) = if anchor <= cursor {
2132            (anchor, cursor)
2133        } else {
2134            (cursor, anchor)
2135        };
2136        Some((start, end))
2137    }
2138
2139    /// Top/bottom rows of the active VisualLine selection (inclusive).
2140    /// `None` when we're not in VisualLine mode.
2141    pub fn line_highlight(&self) -> Option<(usize, usize)> {
2142        if self.vim_mode() != VimMode::VisualLine {
2143            return None;
2144        }
2145        let anchor = self.vim.visual_line_anchor;
2146        let cursor = buf_cursor_row(&self.buffer);
2147        Some((anchor.min(cursor), anchor.max(cursor)))
2148    }
2149
2150    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
2151        if self.vim_mode() != VimMode::VisualBlock {
2152            return None;
2153        }
2154        let (ar, ac) = self.vim.block_anchor;
2155        let cr = buf_cursor_row(&self.buffer);
2156        let cc = self.vim.block_vcol;
2157        let top = ar.min(cr);
2158        let bot = ar.max(cr);
2159        let left = ac.min(cc);
2160        let right = ac.max(cc);
2161        Some((top, bot, left, right))
2162    }
2163
2164    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
2165    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
2166    /// straight to `BufferView` once render flips off textarea
2167    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
2168    /// switch).
2169    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
2170        use hjkl_buffer::{Position, Selection};
2171        match self.vim_mode() {
2172            VimMode::Visual => {
2173                let (ar, ac) = self.vim.visual_anchor;
2174                let head = buf_cursor_pos(&self.buffer);
2175                Some(Selection::Char {
2176                    anchor: Position::new(ar, ac),
2177                    head,
2178                })
2179            }
2180            VimMode::VisualLine => {
2181                let anchor_row = self.vim.visual_line_anchor;
2182                let head_row = buf_cursor_row(&self.buffer);
2183                Some(Selection::Line {
2184                    anchor_row,
2185                    head_row,
2186                })
2187            }
2188            VimMode::VisualBlock => {
2189                let (ar, ac) = self.vim.block_anchor;
2190                let cr = buf_cursor_row(&self.buffer);
2191                let cc = self.vim.block_vcol;
2192                Some(Selection::Block {
2193                    anchor: Position::new(ar, ac),
2194                    head: Position::new(cr, cc),
2195                })
2196            }
2197            _ => None,
2198        }
2199    }
2200
2201    /// Force back to normal mode (used when dismissing completions etc.)
2202    pub fn force_normal(&mut self) {
2203        self.vim.force_normal();
2204    }
2205
2206    pub fn content(&self) -> String {
2207        let n = buf_row_count(&self.buffer);
2208        let mut s = String::new();
2209        for r in 0..n {
2210            if r > 0 {
2211                s.push('\n');
2212            }
2213            s.push_str(&crate::types::Query::line(&self.buffer, r as u32));
2214        }
2215        s.push('\n');
2216        s
2217    }
2218
2219    /// Same logical output as [`content`], but returns a cached
2220    /// `Arc<String>` so back-to-back reads within an un-mutated window
2221    /// are ref-count bumps instead of multi-MB joins. The cache is
2222    /// invalidated by every [`mark_content_dirty`] call.
2223    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
2224        if let Some(arc) = &self.cached_content {
2225            return std::sync::Arc::clone(arc);
2226        }
2227        let arc = std::sync::Arc::new(self.content());
2228        self.cached_content = Some(std::sync::Arc::clone(&arc));
2229        arc
2230    }
2231
2232    pub fn set_content(&mut self, text: &str) {
2233        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2234        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2235            lines.pop();
2236        }
2237        if lines.is_empty() {
2238            lines.push(String::new());
2239        }
2240        let _ = lines;
2241        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2242        self.undo_stack.clear();
2243        self.redo_stack.clear();
2244        // Whole-buffer replace supersedes any queued ContentEdits.
2245        self.pending_content_edits.clear();
2246        self.pending_content_reset = true;
2247        self.mark_content_dirty();
2248    }
2249
2250    /// Whole-buffer replace that **preserves the undo history**.
2251    ///
2252    /// Equivalent to [`Editor::set_content`] but pushes the current buffer
2253    /// state onto the undo stack first, so a subsequent `u` walks back to
2254    /// the pre-replacement content. Use this for any operation the user
2255    /// expects to undo as a single step — e.g. external formatter output
2256    /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
2257    ///
2258    /// Like `push_undo`, this clears the redo stack (vim semantics: any
2259    /// new edit invalidates redo).
2260    pub fn set_content_undoable(&mut self, text: &str) {
2261        self.push_undo();
2262        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2263        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2264            lines.pop();
2265        }
2266        if lines.is_empty() {
2267            lines.push(String::new());
2268        }
2269        let _ = lines;
2270        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2271        // Whole-buffer replace supersedes any queued ContentEdits.
2272        self.pending_content_edits.clear();
2273        self.pending_content_reset = true;
2274        self.mark_content_dirty();
2275    }
2276
2277    /// Drain the pending change log produced by buffer mutations.
2278    ///
2279    /// Returns a `Vec<EditOp>` covering edits applied since the last
2280    /// call. Empty when no edits ran. Pull-model, complementary to
2281    /// [`Editor::take_content_change`] which gives back the new full
2282    /// content.
2283    ///
2284    /// Mapping coverage:
2285    /// - InsertChar / InsertStr → exact `EditOp` with empty range +
2286    ///   replacement.
2287    /// - DeleteRange (`Char` kind) → exact range + empty replacement.
2288    /// - Replace → exact range + new replacement.
2289    /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
2290    ///   InsertBlock, DeleteBlockChunks → best-effort placeholder
2291    ///   covering the touched range. Hosts wanting per-cell deltas
2292    ///   should diff their own `lines()` snapshot.
2293    pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
2294        std::mem::take(&mut self.change_log)
2295    }
2296
2297    /// Read the engine's current settings as a SPEC
2298    /// [`crate::types::Options`].
2299    ///
2300    /// Bridges between the legacy [`Settings`] (which carries fewer
2301    /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
2302    /// not present in `Settings` fall back to vim defaults (e.g.,
2303    /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
2304    /// Once trait extraction lands, this becomes the canonical config
2305    /// reader and `Settings` retires.
2306    pub fn current_options(&self) -> crate::types::Options {
2307        crate::types::Options {
2308            shiftwidth: self.settings.shiftwidth as u32,
2309            tabstop: self.settings.tabstop as u32,
2310            softtabstop: self.settings.softtabstop as u32,
2311            textwidth: self.settings.textwidth as u32,
2312            expandtab: self.settings.expandtab,
2313            ignorecase: self.settings.ignore_case,
2314            smartcase: self.settings.smartcase,
2315            wrapscan: self.settings.wrapscan,
2316            wrap: match self.settings.wrap {
2317                hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
2318                hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
2319                hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
2320            },
2321            readonly: self.settings.readonly,
2322            autoindent: self.settings.autoindent,
2323            smartindent: self.settings.smartindent,
2324            undo_levels: self.settings.undo_levels,
2325            undo_break_on_motion: self.settings.undo_break_on_motion,
2326            iskeyword: self.settings.iskeyword.clone(),
2327            timeout_len: self.settings.timeout_len,
2328            ..crate::types::Options::default()
2329        }
2330    }
2331
2332    /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
2333    /// Only the fields backed by today's [`Settings`] take effect;
2334    /// remaining options become live once trait extraction wires them
2335    /// through.
2336    pub fn apply_options(&mut self, opts: &crate::types::Options) {
2337        self.settings.shiftwidth = opts.shiftwidth as usize;
2338        self.settings.tabstop = opts.tabstop as usize;
2339        self.settings.softtabstop = opts.softtabstop as usize;
2340        self.settings.textwidth = opts.textwidth as usize;
2341        self.settings.expandtab = opts.expandtab;
2342        self.settings.ignore_case = opts.ignorecase;
2343        self.settings.smartcase = opts.smartcase;
2344        self.settings.wrapscan = opts.wrapscan;
2345        self.settings.wrap = match opts.wrap {
2346            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
2347            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
2348            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
2349        };
2350        self.settings.readonly = opts.readonly;
2351        self.settings.autoindent = opts.autoindent;
2352        self.settings.smartindent = opts.smartindent;
2353        self.settings.undo_levels = opts.undo_levels;
2354        self.settings.undo_break_on_motion = opts.undo_break_on_motion;
2355        self.set_iskeyword(opts.iskeyword.clone());
2356        self.settings.timeout_len = opts.timeout_len;
2357        self.settings.number = opts.number;
2358        self.settings.relativenumber = opts.relativenumber;
2359        self.settings.numberwidth = opts.numberwidth;
2360        self.settings.cursorline = opts.cursorline;
2361        self.settings.cursorcolumn = opts.cursorcolumn;
2362        self.settings.signcolumn = opts.signcolumn;
2363        self.settings.foldcolumn = opts.foldcolumn;
2364        self.settings.colorcolumn = opts.colorcolumn.clone();
2365    }
2366
2367    /// Active visual selection as a SPEC [`crate::types::Highlight`]
2368    /// with [`crate::types::HighlightKind::Selection`].
2369    ///
2370    /// Returns `None` when the editor isn't in a Visual mode.
2371    /// Visual-line and visual-block selections collapse to the
2372    /// bounding char range of the selection — the SPEC `Selection`
2373    /// kind doesn't carry sub-line info today; hosts that need full
2374    /// line / block geometry continue to read [`buffer_selection`]
2375    /// (the legacy [`hjkl_buffer::Selection`] shape).
2376    pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2377        use crate::types::{Highlight, HighlightKind, Pos};
2378        let sel = self.buffer_selection()?;
2379        let (start, end) = match sel {
2380            hjkl_buffer::Selection::Char { anchor, head } => {
2381                let a = (anchor.row, anchor.col);
2382                let h = (head.row, head.col);
2383                if a <= h { (a, h) } else { (h, a) }
2384            }
2385            hjkl_buffer::Selection::Line {
2386                anchor_row,
2387                head_row,
2388            } => {
2389                let (top, bot) = if anchor_row <= head_row {
2390                    (anchor_row, head_row)
2391                } else {
2392                    (head_row, anchor_row)
2393                };
2394                let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2395                ((top, 0), (bot, last_col))
2396            }
2397            hjkl_buffer::Selection::Block { anchor, head } => {
2398                let (top, bot) = if anchor.row <= head.row {
2399                    (anchor.row, head.row)
2400                } else {
2401                    (head.row, anchor.row)
2402                };
2403                let (left, right) = if anchor.col <= head.col {
2404                    (anchor.col, head.col)
2405                } else {
2406                    (head.col, anchor.col)
2407                };
2408                ((top, left), (bot, right))
2409            }
2410        };
2411        Some(Highlight {
2412            range: Pos {
2413                line: start.0 as u32,
2414                col: start.1 as u32,
2415            }..Pos {
2416                line: end.0 as u32,
2417                col: end.1 as u32,
2418            },
2419            kind: HighlightKind::Selection,
2420        })
2421    }
2422
2423    /// SPEC-typed highlights for `line`.
2424    ///
2425    /// Two emission modes:
2426    ///
2427    /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2428    ///   `Editor::search_prompt` is `Some`. Live-preview matches of
2429    ///   the in-flight pattern surface as
2430    ///   [`crate::types::HighlightKind::IncSearch`].
2431    /// - **SearchMatch**: the prompt has been committed (or absent)
2432    ///   and the buffer's armed pattern is non-empty. Matches surface
2433    ///   as [`crate::types::HighlightKind::SearchMatch`].
2434    ///
2435    /// Selection / MatchParen / Syntax(id) variants land once the
2436    /// trait extraction routes the FSM's selection set + the host's
2437    /// syntax pipeline through the [`crate::types::Host`] trait.
2438    ///
2439    /// Returns an empty vec when there is nothing to highlight or
2440    /// `line` is out of bounds.
2441    pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2442        use crate::types::{Highlight, HighlightKind, Pos};
2443        let row = line as usize;
2444        if row >= buf_row_count(&self.buffer) {
2445            return Vec::new();
2446        }
2447
2448        // Live preview while the prompt is open beats the committed
2449        // pattern.
2450        if let Some(prompt) = self.search_prompt() {
2451            if prompt.text.is_empty() {
2452                return Vec::new();
2453            }
2454            let translated = crate::search::vim_to_rust_regex(&prompt.text);
2455            let Ok(re) = regex::Regex::new(&translated) else {
2456                return Vec::new();
2457            };
2458            let Some(haystack) = buf_line(&self.buffer, row) else {
2459                return Vec::new();
2460            };
2461            return re
2462                .find_iter(&haystack)
2463                .map(|m| Highlight {
2464                    range: Pos {
2465                        line,
2466                        col: m.start() as u32,
2467                    }..Pos {
2468                        line,
2469                        col: m.end() as u32,
2470                    },
2471                    kind: HighlightKind::IncSearch,
2472                })
2473                .collect();
2474        }
2475
2476        if self.search_state.pattern.is_none() {
2477            return Vec::new();
2478        }
2479        let dgen = crate::types::Query::dirty_gen(&self.buffer);
2480        crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2481            .into_iter()
2482            .map(|(start, end)| Highlight {
2483                range: Pos {
2484                    line,
2485                    col: start as u32,
2486                }..Pos {
2487                    line,
2488                    col: end as u32,
2489                },
2490                kind: HighlightKind::SearchMatch,
2491            })
2492            .collect()
2493    }
2494
2495    /// Build the engine's [`crate::types::RenderFrame`] for the
2496    /// current state. Hosts call this once per redraw and diff
2497    /// across frames.
2498    ///
2499    /// Coarse today — covers mode + cursor + cursor shape + viewport
2500    /// top + line count. SPEC-target fields (selections, highlights,
2501    /// command line, search prompt, status line) land once trait
2502    /// extraction routes them through `SelectionSet` and the
2503    /// `Highlight` pipeline.
2504    pub fn render_frame(&self) -> crate::types::RenderFrame {
2505        use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2506        let (cursor_row, cursor_col) = self.cursor();
2507        let (mode, shape) = match self.vim_mode() {
2508            crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2509            crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2510            crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2511            crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2512            crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2513        };
2514        RenderFrame {
2515            mode,
2516            cursor_row: cursor_row as u32,
2517            cursor_col: cursor_col as u32,
2518            cursor_shape: shape,
2519            viewport_top: self.host.viewport().top_row as u32,
2520            line_count: crate::types::Query::line_count(&self.buffer),
2521        }
2522    }
2523
2524    /// Capture the editor's coarse state into a serde-friendly
2525    /// [`crate::types::EditorSnapshot`].
2526    ///
2527    /// Today's snapshot covers mode, cursor, lines, viewport top.
2528    /// Registers, marks, jump list, undo tree, and full options arrive
2529    /// once phase 5 trait extraction lands the generic
2530    /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2531    /// stays stable; only the snapshot's internal fields grow.
2532    ///
2533    /// Distinct from the internal `snapshot` used by undo (which
2534    /// returns `(Vec<String>, (usize, usize))`); host-facing
2535    /// persistence goes through this one.
2536    pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2537        use crate::types::{EditorSnapshot, SnapshotMode};
2538        let mode = match self.vim_mode() {
2539            crate::VimMode::Normal => SnapshotMode::Normal,
2540            crate::VimMode::Insert => SnapshotMode::Insert,
2541            crate::VimMode::Visual => SnapshotMode::Visual,
2542            crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2543            crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2544        };
2545        let cursor = self.cursor();
2546        let cursor = (cursor.0 as u32, cursor.1 as u32);
2547        let rope = crate::types::Query::rope(&self.buffer);
2548        let lines: Vec<String> = (0..rope.len_lines())
2549            .map(|r| {
2550                let s = rope.line(r).to_string();
2551                if s.ends_with('\n') {
2552                    s[..s.len() - 1].to_string()
2553                } else {
2554                    s
2555                }
2556            })
2557            .collect();
2558        let viewport_top = self.host.viewport().top_row as u32;
2559        let marks = self
2560            .marks
2561            .iter()
2562            .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2563            .collect();
2564        let global_marks = self
2565            .global_marks
2566            .iter()
2567            .map(|(c, &(bid, r, col))| (*c, (bid, r as u32, col as u32)))
2568            .collect();
2569        EditorSnapshot {
2570            version: EditorSnapshot::VERSION,
2571            mode,
2572            cursor,
2573            lines,
2574            viewport_top,
2575            registers: self.registers.clone(),
2576            marks,
2577            global_marks,
2578        }
2579    }
2580
2581    /// Restore editor state from an [`EditorSnapshot`]. Returns
2582    /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2583    /// `version` doesn't match [`EditorSnapshot::VERSION`].
2584    ///
2585    /// Mode is best-effort: `SnapshotMode` only round-trips the
2586    /// status-line summary, not the full FSM state. Visual / Insert
2587    /// mode entry happens through synthetic key dispatch when needed.
2588    pub fn restore_snapshot(
2589        &mut self,
2590        snap: crate::types::EditorSnapshot,
2591    ) -> Result<(), crate::EngineError> {
2592        use crate::types::EditorSnapshot;
2593        if snap.version != EditorSnapshot::VERSION {
2594            return Err(crate::EngineError::SnapshotVersion(
2595                snap.version,
2596                EditorSnapshot::VERSION,
2597            ));
2598        }
2599        let text = snap.lines.join("\n");
2600        self.set_content(&text);
2601        self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2602        self.host.viewport_mut().top_row = snap.viewport_top as usize;
2603        self.registers = snap.registers;
2604        self.marks = snap
2605            .marks
2606            .into_iter()
2607            .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2608            .collect();
2609        self.global_marks = snap
2610            .global_marks
2611            .into_iter()
2612            .map(|(c, (bid, r, col))| (c, (bid, r as usize, col as usize)))
2613            .collect();
2614        Ok(())
2615    }
2616
2617    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2618    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2619    /// shape their payload.
2620    pub fn seed_yank(&mut self, text: String) {
2621        let linewise = text.ends_with('\n');
2622        self.vim.yank_linewise = linewise;
2623        self.registers.unnamed = crate::registers::Slot { text, linewise };
2624    }
2625
2626    /// Scroll the viewport down by `rows`. The cursor stays on its
2627    /// absolute line (vim convention) unless the scroll would take it
2628    /// off-screen — in that case it's clamped to the first row still
2629    /// visible.
2630    pub fn scroll_down(&mut self, rows: i16) {
2631        self.scroll_viewport(rows);
2632    }
2633
2634    /// Scroll the viewport up by `rows`. Cursor stays unless it would
2635    /// fall off the bottom of the new viewport, then clamp to the
2636    /// bottom-most visible row.
2637    pub fn scroll_up(&mut self, rows: i16) {
2638        self.scroll_viewport(-rows);
2639    }
2640
2641    /// Scroll the viewport right by `cols` columns. Only the horizontal
2642    /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2643    /// vim's `zl` behaviour for horizontal scroll without wrap).
2644    pub fn scroll_right(&mut self, cols: i16) {
2645        let vp = self.host.viewport_mut();
2646        let cols_i = cols as isize;
2647        let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2648        vp.top_col = new_top;
2649    }
2650
2651    /// Scroll the viewport left by `cols` columns. Delegates to
2652    /// `scroll_right` with a negated argument so the floor-at-zero
2653    /// clamp is shared.
2654    pub fn scroll_left(&mut self, cols: i16) {
2655        self.scroll_right(-cols);
2656    }
2657
2658    /// Vim's `scrolloff` default — keep the cursor at least this many
2659    /// rows away from the top / bottom edge of the viewport while
2660    /// scrolling. Collapses to `height / 2` for tiny viewports.
2661    const SCROLLOFF: usize = 5;
2662
2663    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2664    /// rows from each edge. Replaces the bare
2665    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2666    /// don't park the cursor on the very last visible row.
2667    pub fn ensure_cursor_in_scrolloff(&mut self) {
2668        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2669        if height == 0 {
2670            // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2671            // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2672            // Disjoint-field borrow split: `self.buffer` (immutable via
2673            // `folds` snapshot + cursor) and `self.host` (mutable
2674            // viewport ref) live on distinct struct fields, so one
2675            // statement satisfies the borrow checker.
2676            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2677            crate::viewport_math::ensure_cursor_visible(
2678                &self.buffer,
2679                &folds,
2680                self.host.viewport_mut(),
2681            );
2682            return;
2683        }
2684        // Cap margin at (height - 1) / 2 so the upper + lower bands
2685        // can't overlap on tiny windows (margin=5 + height=10 would
2686        // otherwise produce contradictory clamp ranges).
2687        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2688        // Soft-wrap path: scrolloff math runs in *screen rows*, not
2689        // doc rows, since a wrapped doc row spans many visual lines.
2690        if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2691            self.ensure_scrolloff_wrap(height, margin);
2692            return;
2693        }
2694        let cursor_row = buf_cursor_row(&self.buffer);
2695        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2696        let v = self.host.viewport_mut();
2697        // Top edge: cursor_row should sit at >= top_row + margin.
2698        if cursor_row < v.top_row + margin {
2699            v.top_row = cursor_row.saturating_sub(margin);
2700        }
2701        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2702        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2703        if cursor_row > v.top_row + max_bottom {
2704            v.top_row = cursor_row.saturating_sub(max_bottom);
2705        }
2706        // Clamp top_row so we never scroll past the buffer's bottom.
2707        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2708        if v.top_row > max_top {
2709            v.top_row = max_top;
2710        }
2711        // Defer to Buffer for column-side scroll (no scrolloff for
2712        // horizontal scrolling — vim default `sidescrolloff = 0`).
2713        let cursor = buf_cursor_pos(&self.buffer);
2714        self.host.viewport_mut().ensure_visible(cursor);
2715    }
2716
2717    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2718    /// at a time so the cursor's *screen* row stays inside
2719    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2720    /// buffer's bottom never leaves blank rows below it.
2721    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2722        let cursor_row = buf_cursor_row(&self.buffer);
2723        // Step 1 — cursor above viewport: snap top to cursor row,
2724        // then we'll fix up the margin below.
2725        if cursor_row < self.host.viewport().top_row {
2726            let v = self.host.viewport_mut();
2727            v.top_row = cursor_row;
2728            v.top_col = 0;
2729        }
2730        // Step 2 — push top forward until cursor's screen row is
2731        // within the bottom margin (`csr <= height - 1 - margin`).
2732        // 0.0.33 (Patch C-γ): fold-iteration goes through the
2733        // [`crate::types::FoldProvider`] surface via
2734        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2735        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2736        // a `&Viewport` parameter; the host owns the viewport, so the
2737        // disjoint `(self.host, self.buffer)` borrows split cleanly.
2738        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2739        loop {
2740            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2741            let csr =
2742                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2743                    .unwrap_or(0);
2744            if csr <= max_csr {
2745                break;
2746            }
2747            let top = self.host.viewport().top_row;
2748            let row_count = buf_row_count(&self.buffer);
2749            let next = {
2750                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2751                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2752            };
2753            let Some(next) = next else {
2754                break;
2755            };
2756            // Don't walk past the cursor's row.
2757            if next > cursor_row {
2758                self.host.viewport_mut().top_row = cursor_row;
2759                break;
2760            }
2761            self.host.viewport_mut().top_row = next;
2762        }
2763        // Step 3 — pull top backward until cursor's screen row is
2764        // past the top margin (`csr >= margin`).
2765        loop {
2766            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2767            let csr =
2768                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2769                    .unwrap_or(0);
2770            if csr >= margin {
2771                break;
2772            }
2773            let top = self.host.viewport().top_row;
2774            let prev = {
2775                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2776                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2777            };
2778            let Some(prev) = prev else {
2779                break;
2780            };
2781            self.host.viewport_mut().top_row = prev;
2782        }
2783        // Step 4 — clamp top so the buffer's bottom doesn't leave
2784        // blank rows below it. `max_top_for_height` walks segments
2785        // backward from the last row until it accumulates `height`
2786        // screen rows.
2787        let max_top = {
2788            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2789            crate::viewport_math::max_top_for_height(
2790                &self.buffer,
2791                &folds,
2792                self.host.viewport(),
2793                height,
2794            )
2795        };
2796        if self.host.viewport().top_row > max_top {
2797            self.host.viewport_mut().top_row = max_top;
2798        }
2799        self.host.viewport_mut().top_col = 0;
2800    }
2801
2802    fn scroll_viewport(&mut self, delta: i16) {
2803        if delta == 0 {
2804            return;
2805        }
2806        // Bump the host viewport's top within bounds.
2807        let total_rows = buf_row_count(&self.buffer) as isize;
2808        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2809        let cur_top = self.host.viewport().top_row as isize;
2810        let new_top = (cur_top + delta as isize)
2811            .max(0)
2812            .min((total_rows - 1).max(0)) as usize;
2813        self.host.viewport_mut().top_row = new_top;
2814        // Mirror to textarea so its viewport reads (still consumed by
2815        // a couple of helpers) stay accurate.
2816        let _ = cur_top;
2817        if height == 0 {
2818            return;
2819        }
2820        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2821        // from the visible viewport edges.
2822        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2823        let margin = Self::SCROLLOFF.min(height / 2);
2824        let min_row = new_top + margin;
2825        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2826        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2827        if target_row != cursor_row {
2828            let line_len = buf_line(&self.buffer, target_row)
2829                .map(|l| l.chars().count())
2830                .unwrap_or(0);
2831            let target_col = cursor_col.min(line_len.saturating_sub(1));
2832            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2833        }
2834    }
2835
2836    pub fn goto_line(&mut self, line: usize) {
2837        let row = line.saturating_sub(1);
2838        let max = buf_row_count(&self.buffer).saturating_sub(1);
2839        let target = row.min(max);
2840        buf_set_cursor_rc(&mut self.buffer, target, 0);
2841        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2842        // the cursor lands off-screen and the user has to scroll
2843        // manually to see it.
2844        self.ensure_cursor_in_scrolloff();
2845    }
2846
2847    /// Scroll so the cursor row lands at the given viewport position:
2848    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2849    /// Cursor stays on its absolute line; only the viewport moves.
2850    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2851        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2852        if height == 0 {
2853            return;
2854        }
2855        let cur_row = buf_cursor_row(&self.buffer);
2856        let cur_top = self.host.viewport().top_row;
2857        // Scrolloff awareness: `zt` lands the cursor at the top edge
2858        // of the viable area (top + margin), `zb` at the bottom edge
2859        // (top + height - 1 - margin). Match the cap used by
2860        // `ensure_cursor_in_scrolloff` so contradictory bounds are
2861        // impossible on tiny viewports.
2862        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2863        let new_top = match pos {
2864            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2865            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2866            CursorScrollTarget::Bottom => {
2867                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2868            }
2869        };
2870        if new_top == cur_top {
2871            return;
2872        }
2873        self.host.viewport_mut().top_row = new_top;
2874    }
2875
2876    /// Jump the cursor to the given 1-based line/column, clamped to the document.
2877    pub fn jump_to(&mut self, line: usize, col: usize) {
2878        let r = line.saturating_sub(1);
2879        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2880        let r = r.min(max_row);
2881        let line_len = buf_line(&self.buffer, r)
2882            .map(|l| l.chars().count())
2883            .unwrap_or(0);
2884        let c = col.saturating_sub(1).min(line_len);
2885        buf_set_cursor_rc(&mut self.buffer, r, c);
2886    }
2887
2888    // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2889    //
2890    // These primitives operate on document (row, col) coordinates that the HOST
2891    // computes from its own layout knowledge (cell geometry for the TUI host,
2892    // pixel geometry for the future GUI host). The engine has no u16 terminal
2893    // assumption here — it just moves the cursor in doc-space.
2894
2895    /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2896    /// document bounds. Hosts use this for programmatic cursor placement and
2897    /// as the building block for the mouse-click path.
2898    ///
2899    /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2900    /// position); values beyond that are clamped to `char_count`.
2901    pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2902        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2903        let r = row.min(max_row);
2904        let line_len = buf_line(&self.buffer, r)
2905            .map(|l| l.chars().count())
2906            .unwrap_or(0);
2907        let c = col.min(line_len);
2908        buf_set_cursor_rc(&mut self.buffer, r, c);
2909    }
2910
2911    /// Handle a left-button click at doc-space `(row, col)`.
2912    ///
2913    /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2914    /// parity for `undo_break_on_motion`), then moves the cursor. The host
2915    /// performs cell→doc or pixel→doc translation before calling this.
2916    ///
2917    /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2918    /// cursor lives on chars and never on the implicit `\n` — `col` is
2919    /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2920    /// allows the one-past-EOL insert position (`col == chars().count()`).
2921    ///
2922    /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2923    /// motion uses the clicked column as the intended visual column
2924    /// (otherwise the cursor would snap back to the keyboard-tracked
2925    /// column on the first vertical motion after a click).
2926    pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2927        if self.vim.is_visual() {
2928            self.vim.force_normal();
2929        }
2930        // Mouse-position click counts as a motion — break the active
2931        // insert-mode undo group when the toggle is on (vim parity).
2932        crate::vim::break_undo_group_in_insert(self);
2933
2934        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2935        let r = row.min(max_row);
2936        let line_len = buf_line(&self.buffer, r)
2937            .map(|l| l.chars().count())
2938            .unwrap_or(0);
2939        let cap = if self.vim.current_mode == crate::VimMode::Insert {
2940            line_len
2941        } else {
2942            line_len.saturating_sub(1)
2943        };
2944        let c = col.min(cap);
2945        buf_set_cursor_rc(&mut self.buffer, r, c);
2946        self.sticky_col = Some(c);
2947    }
2948
2949    /// Begin a mouse-drag selection: anchor at the current cursor and enter
2950    /// Visual-char mode. Idempotent if already in Visual-char mode.
2951    pub fn mouse_begin_drag(&mut self) {
2952        if !self.vim.is_visual_char() {
2953            vim::enter_visual_char_bridge(self);
2954        }
2955    }
2956
2957    /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2958    ///
2959    /// Moves the live cursor; the Visual anchor stays where
2960    /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2961    /// translated the drag position to doc coordinates.
2962    pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2963        self.set_cursor_doc(row, col);
2964    }
2965
2966    pub fn insert_str(&mut self, text: &str) {
2967        let pos = crate::types::Cursor::cursor(&self.buffer);
2968        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2969        self.push_buffer_content_to_textarea();
2970        self.mark_content_dirty();
2971    }
2972
2973    pub fn accept_completion(&mut self, completion: &str) {
2974        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2975        let cursor_pos = CursorTrait::cursor(&self.buffer);
2976        let cursor_row = cursor_pos.line as usize;
2977        let cursor_col = cursor_pos.col as usize;
2978        let line = buf_line(&self.buffer, cursor_row).unwrap_or_default();
2979        let chars: Vec<char> = line.chars().collect();
2980        let prefix_len = chars[..cursor_col.min(chars.len())]
2981            .iter()
2982            .rev()
2983            .take_while(|c| c.is_alphanumeric() || **c == '_')
2984            .count();
2985        if prefix_len > 0 {
2986            let start = Pos {
2987                line: cursor_row as u32,
2988                col: (cursor_col - prefix_len) as u32,
2989            };
2990            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2991        }
2992        let cursor = CursorTrait::cursor(&self.buffer);
2993        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2994        self.push_buffer_content_to_textarea();
2995        self.mark_content_dirty();
2996    }
2997
2998    /// Capture the buffer state for undo / redo.  Uses
2999    /// [`Query::content_joined`], which the `Buffer` impl caches as an
3000    /// `Arc<String>` against `dirty_gen` — so when LSP / git / syntax
3001    /// already joined this generation, the snapshot is an `Arc::clone`
3002    /// (one ptr bump). Previously this cloned every line into a
3003    /// `Vec<String>` (162 k allocations on a 162 k-row buffer) and the
3004    /// matching `restore` re-joined them — samply showed it at ~9 % of
3005    /// CPU on a big-paste session.
3006    pub(super) fn snapshot(&self) -> (ropey::Rope, (usize, usize)) {
3007        use crate::types::Query;
3008        let rc = buf_cursor_rc(&self.buffer);
3009        (Query::rope(&self.buffer), rc)
3010    }
3011
3012    /// Walk one step back through the undo history. Equivalent to the
3013    /// user pressing `u` in normal mode. Drains the most recent undo
3014    /// entry and pushes it onto the redo stack.
3015    pub fn undo(&mut self) {
3016        crate::vim::do_undo(self);
3017    }
3018
3019    /// Walk one step forward through the redo history. Equivalent to
3020    /// `<C-r>` in normal mode.
3021    pub fn redo(&mut self) {
3022        crate::vim::do_redo(self);
3023    }
3024
3025    /// Snapshot current buffer state onto the undo stack and clear
3026    /// the redo stack. Bounded by `settings.undo_levels` — older
3027    /// entries pruned. Call before any group of buffer mutations the
3028    /// user might want to undo as a single step.
3029    pub fn push_undo(&mut self) {
3030        let snap = self.snapshot();
3031        self.undo_stack.push(snap);
3032        self.cap_undo();
3033        self.redo_stack.clear();
3034    }
3035
3036    /// Trim the undo stack down to `settings.undo_levels`, dropping
3037    /// the oldest entries. `undo_levels == 0` is treated as
3038    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
3039    /// skipped — guarding with `> 0` is one line shorter than gating
3040    /// the cap path with an explicit zero-check above the call site).
3041    pub(crate) fn cap_undo(&mut self) {
3042        let cap = self.settings.undo_levels as usize;
3043        if cap > 0 && self.undo_stack.len() > cap {
3044            let diff = self.undo_stack.len() - cap;
3045            self.undo_stack.drain(..diff);
3046        }
3047    }
3048
3049    /// Test-only accessor for the undo stack length.
3050    #[doc(hidden)]
3051    pub fn undo_stack_len(&self) -> usize {
3052        self.undo_stack.len()
3053    }
3054
3055    /// Replace the buffer with `lines` joined by `\n` and set the
3056    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
3057    /// paths. Marks the editor dirty.
3058    ///
3059    /// Emits a single whole-buffer `ContentEdit` describing the
3060    /// transition so the syntax layer can apply it as an `InputEdit`
3061    /// on the retained tree and run an INCREMENTAL parse — tree-sitter
3062    /// reuses unchanged subtrees and `Tree::changed_ranges` reports
3063    /// just the bytes that differ, which lets the install path walk
3064    /// only the changed rows instead of the full viewport. Big undos
3065    /// that revert a large paste now refresh in ~1ms per affected
3066    /// row instead of a ~30ms full-viewport sync walk.
3067    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
3068        let text = lines.join("\n");
3069        self.restore_text(&text, cursor);
3070    }
3071
3072    /// Restore the buffer from a `ropey::Rope` snapshot. Used by undo /
3073    /// redo: snapshots are stored as `Rope` (O(1) Arc-clone via
3074    /// `Buffer::rope()`), so this avoids the full-document `to_string`
3075    /// materialization that the old `Arc<String>` snapshot path forced
3076    /// on every undo group boundary.
3077    ///
3078    /// Internally materializes the rope to a `String` for `restore_text`
3079    /// — paying the cost on the restore side instead of the snapshot
3080    /// side trades one ~3 MB build per undo for none-per-snapshot. Undo
3081    /// is user-initiated and rare; snapshots fire on every `i` / `o`.
3082    pub fn restore_rope(&mut self, rope: ropey::Rope, cursor: (usize, usize)) {
3083        let text = rope.to_string();
3084        self.restore_text(&text, cursor);
3085    }
3086
3087    fn restore_text(&mut self, text: &str, cursor: (usize, usize)) {
3088        // Diff the old rope (O(1) Arc-clone) against the incoming text
3089        // to emit a minimal ContentEdit — without it the syntax layer's
3090        // tree.edit() marks the whole document changed and tree-sitter
3091        // cold-parses on every undo.
3092        let old_rope = self.buffer.rope();
3093        let edit = minimal_content_edit_rope(&old_rope, text);
3094
3095        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
3096        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
3097
3098        // Bulk replace supersedes any prior queued edits.
3099        self.pending_content_edits.clear();
3100        self.pending_content_edits.push(edit);
3101        self.mark_content_dirty();
3102    }
3103
3104    /// Returns true if the key was consumed by the editor.
3105    /// Replace the char under the cursor with `ch`, `count` times. Matches
3106    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
3107    /// snapshot taken once at start. Promoted to public surface in 0.5.5
3108    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
3109    /// re-entering the FSM.
3110    pub fn replace_char_at(&mut self, ch: char, count: usize) {
3111        vim::replace_char(self, ch, count);
3112    }
3113
3114    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
3115    /// to the `count`-th occurrence of `ch` on the current line, respecting
3116    /// `forward` (direction) and `till` (stop one char before target).
3117    /// Records `last_find` so `;` / `,` repeat work.
3118    ///
3119    /// No-op if the target char isn't on the current line within range.
3120    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
3121    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
3122        vim::apply_find_char(self, ch, forward, till, count.max(1));
3123    }
3124
3125    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
3126    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
3127    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
3128    ///
3129    /// Promoted to public surface in 0.5.10 so hjkl-vim's
3130    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
3131    /// re-entering the engine FSM.
3132    pub fn after_g(&mut self, ch: char, count: usize) {
3133        vim::apply_after_g(self, ch, count);
3134    }
3135
3136    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
3137    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
3138    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
3139    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
3140    ///
3141    /// Promoted to public surface in 0.5.11 so hjkl-vim's
3142    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
3143    /// re-entering the engine FSM.
3144    pub fn after_z(&mut self, ch: char, count: usize) {
3145        vim::apply_after_z(self, ch, count);
3146    }
3147
3148    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
3149    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
3150    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
3151    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
3152    /// then calls `apply_op_with_motion`. `total_count` is already the product of
3153    /// the prefix count and any inner count accumulated by the reducer.
3154    ///
3155    /// No-op when `motion_key` does not map to a known motion (engine silently
3156    /// cancels the operator, matching vim's behaviour on unknown motions).
3157    ///
3158    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
3159    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
3160    /// re-entering the engine FSM.
3161    pub fn apply_op_motion(
3162        &mut self,
3163        op: crate::vim::Operator,
3164        motion_key: char,
3165        total_count: usize,
3166    ) {
3167        vim::apply_op_motion_key(self, op, motion_key, total_count);
3168    }
3169
3170    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
3171    /// `total_count` is the product of prefix count and inner count.
3172    ///
3173    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
3174    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
3175    /// re-entering the engine FSM.
3176    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
3177        vim::apply_op_double(self, op, total_count);
3178    }
3179
3180    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
3181    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
3182    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
3183    /// updates `last_change` when `op` is Change (for dot-repeat).
3184    ///
3185    /// `total_count` is the product of prefix count and any inner count
3186    /// accumulated by the reducer — already folded at transition time.
3187    ///
3188    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
3189    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
3190    /// re-entering the engine FSM. `handle_op_find_target` (used by the
3191    /// chord-init op path) delegates here to avoid logic duplication.
3192    pub fn apply_op_find(
3193        &mut self,
3194        op: crate::vim::Operator,
3195        ch: char,
3196        forward: bool,
3197        till: bool,
3198        total_count: usize,
3199    ) {
3200        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
3201    }
3202
3203    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
3204    /// Maps `ch` to a `TextObject` per the standard vim table, calls
3205    /// `apply_op_with_text_object`, and records `last_change` when `op` is
3206    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
3207    /// matching the engine FSM's behaviour on unrecognised text-object chars.
3208    ///
3209    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
3210    /// `apply_op_find` but is currently unused — text objects don't repeat in
3211    /// vim's current grammar. Kept for future-proofing.
3212    ///
3213    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
3214    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
3215    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
3216    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
3217    /// duplication.
3218    pub fn apply_op_text_obj(
3219        &mut self,
3220        op: crate::vim::Operator,
3221        ch: char,
3222        inner: bool,
3223        total_count: usize,
3224    ) {
3225        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
3226    }
3227
3228    /// Apply an operator over a g-chord motion or case-op linewise form
3229    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
3230    ///
3231    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
3232    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
3233    /// - Otherwise maps `ch` to a motion:
3234    ///   - `'g'` → `Motion::FileTop` (gg)
3235    ///   - `'e'` → `Motion::WordEndBack` (ge)
3236    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
3237    ///   - `'j'` → `Motion::ScreenDown` (gj)
3238    ///   - `'k'` → `Motion::ScreenUp` (gk)
3239    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
3240    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
3241    ///
3242    /// `total_count` is the already-folded product of prefix and inner counts.
3243    ///
3244    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
3245    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
3246    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
3247    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
3248    /// duplication.
3249    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
3250        vim::apply_op_g_inner(self, op, ch, total_count);
3251    }
3252
3253    // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
3254
3255    /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
3256    /// row and the motion's target row. Used by the app layer to compute the
3257    /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
3258    /// format job.
3259    ///
3260    /// Returns `None` when `motion_key` does not map to a known motion (same
3261    /// condition that makes `apply_op_motion` a no-op).
3262    ///
3263    /// The cursor is restored to its original position after the probe —
3264    /// the buffer content is not touched.
3265    pub fn range_for_op_motion(
3266        &mut self,
3267        motion_key: char,
3268        total_count: usize,
3269    ) -> Option<(usize, usize)> {
3270        let start = self.cursor();
3271        // Reuse the same logic as apply_op_motion_key but only read the
3272        // target row — we parse the motion, apply it to move the cursor,
3273        // then immediately restore.
3274        let input = crate::input::Input {
3275            key: crate::input::Key::Char(motion_key),
3276            ctrl: false,
3277            alt: false,
3278            shift: false,
3279        };
3280        let motion = vim::parse_motion(&input)?;
3281        // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
3282        let motion = match motion {
3283            vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
3284                Some((ch, forward, till)) => vim::Motion::Find {
3285                    ch,
3286                    forward: if reverse { !forward } else { forward },
3287                    till,
3288                },
3289                None => return None,
3290            },
3291            m => m,
3292        };
3293        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3294        let end = self.cursor();
3295        // Restore cursor.
3296        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3297        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3298        Some((r0, r1))
3299    }
3300
3301    /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
3302    /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
3303    /// linewise forms that don't map to a row range.
3304    ///
3305    /// The cursor is restored after the probe.
3306    pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
3307        let start = self.cursor();
3308        let motion = match ch {
3309            'g' => vim::Motion::FileTop,
3310            'e' => vim::Motion::WordEndBack,
3311            'E' => vim::Motion::BigWordEndBack,
3312            'j' => vim::Motion::ScreenDown,
3313            'k' => vim::Motion::ScreenUp,
3314            _ => return None,
3315        };
3316        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3317        let end = self.cursor();
3318        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3319        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3320        Some((r0, r1))
3321    }
3322
3323    /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
3324    /// matched region. Returns `None` when `ch` is not a known text-object
3325    /// kind or the text object could not be resolved (e.g. no enclosing bracket).
3326    ///
3327    /// The buffer is not mutated.
3328    pub fn range_for_op_text_obj(
3329        &self,
3330        ch: char,
3331        inner: bool,
3332        _total_count: usize,
3333    ) -> Option<(usize, usize)> {
3334        let obj = match ch {
3335            'w' => vim::TextObject::Word { big: false },
3336            'W' => vim::TextObject::Word { big: true },
3337            '"' | '\'' | '`' => vim::TextObject::Quote(ch),
3338            '(' | ')' | 'b' => vim::TextObject::Bracket('('),
3339            '[' | ']' => vim::TextObject::Bracket('['),
3340            '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
3341            '<' | '>' => vim::TextObject::Bracket('<'),
3342            'p' => vim::TextObject::Paragraph,
3343            't' => vim::TextObject::XmlTag,
3344            's' => vim::TextObject::Sentence,
3345            _ => return None,
3346        };
3347        let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
3348        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3349        Some((r0, r1))
3350    }
3351
3352    // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
3353    //
3354    // These do not consume input — the caller (hjkl-vim's visual-mode operator
3355    // path, chunk 4e) has already resolved the range from the visual selection
3356    // before calling in. Normal-mode op dispatch continues to use
3357    // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
3358
3359    /// Delete the region `[start, end)` and stash the removed text in
3360    /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
3361    /// select named registers.
3362    ///
3363    /// Pure range-mutation primitive — does not consume input. Called by
3364    /// hjkl-vim's visual-mode operator path which has already resolved the range
3365    /// from the visual selection.
3366    ///
3367    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3368    /// grammar migration (kryptic-sh/hjkl#70).
3369    pub fn delete_range(
3370        &mut self,
3371        start: (usize, usize),
3372        end: (usize, usize),
3373        kind: crate::vim::RangeKind,
3374        register: char,
3375    ) {
3376        vim::delete_range_bridge(self, start, end, kind, register);
3377    }
3378
3379    /// Yank (copy) the region `[start, end)` into `register` without mutating
3380    /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
3381    /// register; `'a'`–`'z'` select named registers.
3382    ///
3383    /// Pure range-mutation primitive — does not consume input. Called by
3384    /// hjkl-vim's visual-mode operator path which has already resolved the range
3385    /// from the visual selection.
3386    ///
3387    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3388    /// grammar migration (kryptic-sh/hjkl#70).
3389    pub fn yank_range(
3390        &mut self,
3391        start: (usize, usize),
3392        end: (usize, usize),
3393        kind: crate::vim::RangeKind,
3394        register: char,
3395    ) {
3396        vim::yank_range_bridge(self, start, end, kind, register);
3397    }
3398
3399    /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
3400    /// operator). The deleted text is stashed in `register`. On return the
3401    /// editor is in Insert mode; the caller must not issue further normal-mode
3402    /// ops until the insert session ends.
3403    ///
3404    /// Pure range-mutation primitive — does not consume input. Called by
3405    /// hjkl-vim's visual-mode operator path which has already resolved the range
3406    /// from the visual selection.
3407    ///
3408    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3409    /// grammar migration (kryptic-sh/hjkl#70).
3410    pub fn change_range(
3411        &mut self,
3412        start: (usize, usize),
3413        end: (usize, usize),
3414        kind: crate::vim::RangeKind,
3415        register: char,
3416    ) {
3417        vim::change_range_bridge(self, start, end, kind, register);
3418    }
3419
3420    /// Indent (`count > 0`) or outdent (`count < 0`) the row span
3421    /// `[start.0, end.0]`. Column components are ignored — indent is always
3422    /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
3423    /// this call; pass `0` to use the current editor setting. `count == 0` is a
3424    /// no-op.
3425    ///
3426    /// Pure range-mutation primitive — does not consume input. Called by
3427    /// hjkl-vim's visual-mode operator path which has already resolved the range
3428    /// from the visual selection.
3429    ///
3430    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3431    /// grammar migration (kryptic-sh/hjkl#70).
3432    pub fn indent_range(
3433        &mut self,
3434        start: (usize, usize),
3435        end: (usize, usize),
3436        count: i32,
3437        shiftwidth: u32,
3438    ) {
3439        vim::indent_range_bridge(self, start, end, count, shiftwidth);
3440    }
3441
3442    /// Apply a case transformation (`Operator::Uppercase` /
3443    /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3444    /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3445    /// Yanks registers are left untouched — vim's case operators do not write
3446    /// to registers.
3447    ///
3448    /// Pure range-mutation primitive — does not consume input. Called by
3449    /// hjkl-vim's visual-mode operator path which has already resolved the range
3450    /// from the visual selection.
3451    ///
3452    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3453    /// grammar migration (kryptic-sh/hjkl#70).
3454    pub fn case_range(
3455        &mut self,
3456        start: (usize, usize),
3457        end: (usize, usize),
3458        kind: crate::vim::RangeKind,
3459        op: crate::vim::Operator,
3460    ) {
3461        vim::case_range_bridge(self, start, end, kind, op);
3462    }
3463
3464    // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3465    //
3466    // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3467    // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3468    // Ragged-edge handling (short lines not reaching `right_col`) matches the
3469    // engine FSM's `apply_block_operator` path — short lines lose only the
3470    // chars that exist.
3471    //
3472    // `register` is the target register; `'"'` selects the unnamed register.
3473
3474    /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3475    /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3476    /// bounds at the visual (display) column level. Ragged-edge handling
3477    /// matches engine FSM's VisualBlock op behavior — short lines that don't
3478    /// reach `right_col` lose only the chars that exist.
3479    ///
3480    /// `register` honors the user's pending register selection.
3481    ///
3482    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3483    pub fn delete_block(
3484        &mut self,
3485        top_row: usize,
3486        bot_row: usize,
3487        left_col: usize,
3488        right_col: usize,
3489        register: char,
3490    ) {
3491        vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3492    }
3493
3494    /// Yank a rectangular VisualBlock selection into `register` without
3495    /// mutating the buffer. `'"'` selects the unnamed register.
3496    ///
3497    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3498    pub fn yank_block(
3499        &mut self,
3500        top_row: usize,
3501        bot_row: usize,
3502        left_col: usize,
3503        right_col: usize,
3504        register: char,
3505    ) {
3506        vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3507    }
3508
3509    /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3510    /// operator). The deleted text is stashed in `register`. Mode is Insert
3511    /// on return; the caller must not issue further normal-mode ops until the
3512    /// insert session ends.
3513    ///
3514    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3515    pub fn change_block(
3516        &mut self,
3517        top_row: usize,
3518        bot_row: usize,
3519        left_col: usize,
3520        right_col: usize,
3521        register: char,
3522    ) {
3523        vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3524    }
3525
3526    /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3527    /// Column bounds are ignored — vim's block indent is always linewise.
3528    /// `count == 0` is a no-op.
3529    ///
3530    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3531    pub fn indent_block(
3532        &mut self,
3533        top_row: usize,
3534        bot_row: usize,
3535        _left_col: usize,
3536        _right_col: usize,
3537        count: i32,
3538    ) {
3539        vim::indent_block_bridge(self, top_row, bot_row, count);
3540    }
3541
3542    /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3543    /// Column components are ignored — auto-indent is always linewise.
3544    ///
3545    /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3546    /// row 0 to compute the correct depth at `start.0`, then for each line in
3547    /// the target range strips existing leading whitespace and prepends
3548    /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3549    /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3550    /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3551    /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3552    ///
3553    /// After the operation the cursor lands on the first non-whitespace
3554    /// character of `start_row` (vim parity for `==`).
3555    ///
3556    /// **v1 limitation**: the bracket scan does not detect brackets inside
3557    /// string literals or comments. Code such as `let s = "{";` will increment
3558    /// the depth counter even though the brace is not a structural opener.
3559    /// Tree-sitter / LSP indentation is deferred to a follow-up.
3560    pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3561        vim::auto_indent_range_bridge(self, start, end);
3562    }
3563
3564    /// Drain the row range set by the most recent auto-indent operation.
3565    ///
3566    /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3567    /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3568    /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3569    /// uses this to arm a brief visual flash over the reindented rows.
3570    pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3571        self.last_indent_range.take()
3572    }
3573
3574    /// Filter rows `top_row..=bot_row` through an external shell command.
3575    ///
3576    /// Spawns `sh -c "<command>"` (or `cmd /C "<command>"` on Windows), pipes
3577    /// the selected lines (joined by `\n`) to stdin, and waits up to
3578    /// `timeout_secs` seconds (default 10) for the process to finish.
3579    ///
3580    /// On success: the rows are replaced with stdout. No trailing-newline trim.
3581    /// On non-zero exit, spawn failure, or timeout: returns `Err(stderr_or_msg)`
3582    /// without mutating the buffer.
3583    ///
3584    /// `top_row` and `bot_row` are clamped to the buffer's valid row range.
3585    pub fn filter_range(
3586        &mut self,
3587        top_row: usize,
3588        bot_row: usize,
3589        command: &str,
3590        timeout_secs: Option<u64>,
3591    ) -> Result<(), String> {
3592        use std::io::Write;
3593        use std::process::{Command, Stdio};
3594        use std::thread;
3595        use std::time::Instant;
3596
3597        let timeout = std::time::Duration::from_secs(timeout_secs.unwrap_or(10));
3598        let rope = crate::types::Query::rope(self.buffer());
3599        let line_count = rope.len_lines();
3600        let top = top_row.min(line_count.saturating_sub(1));
3601        let bot = bot_row.min(line_count.saturating_sub(1));
3602        let (top, bot) = (top.min(bot), top.max(bot));
3603        let input_text = crate::vim::rope_row_range_str(&rope, top, bot);
3604        // Materialized for the splice-back after the command succeeds.
3605        let lines = crate::vim::rope_to_lines_vec(&rope);
3606
3607        tracing::debug!(
3608            top_row = top,
3609            bot_row = bot,
3610            command = command,
3611            "filter_range: spawning shell command"
3612        );
3613
3614        #[cfg(not(windows))]
3615        let mut child = Command::new("sh")
3616            .args(["-c", command])
3617            .stdin(Stdio::piped())
3618            .stdout(Stdio::piped())
3619            .stderr(Stdio::piped())
3620            .spawn()
3621            .map_err(|e| format!("spawn failed: {e}"))?;
3622
3623        #[cfg(windows)]
3624        let mut child = Command::new("cmd")
3625            .args(["/C", command])
3626            .stdin(Stdio::piped())
3627            .stdout(Stdio::piped())
3628            .stderr(Stdio::piped())
3629            .spawn()
3630            .map_err(|e| format!("spawn failed: {e}"))?;
3631
3632        // Write stdin on a thread to avoid deadlock when output > pipe buffer.
3633        let mut stdin = child.stdin.take().ok_or("no stdin handle")?;
3634        let input_bytes = input_text.into_bytes();
3635        thread::spawn(move || {
3636            let _ = stdin.write_all(&input_bytes);
3637            // stdin drops here, signalling EOF to the child.
3638        });
3639
3640        // Drain stdout/stderr on separate threads so the child's pipes don't
3641        // fill and deadlock the child. Keep `child` here so we can kill it on
3642        // timeout.
3643        let mut stdout_pipe = child.stdout.take().ok_or("no stdout handle")?;
3644        let mut stderr_pipe = child.stderr.take().ok_or("no stderr handle")?;
3645        let stdout_thread = thread::spawn(move || {
3646            let mut buf = Vec::new();
3647            let _ = std::io::Read::read_to_end(&mut stdout_pipe, &mut buf);
3648            buf
3649        });
3650        let stderr_thread = thread::spawn(move || {
3651            let mut buf = Vec::new();
3652            let _ = std::io::Read::read_to_end(&mut stderr_pipe, &mut buf);
3653            buf
3654        });
3655
3656        // Poll try_wait until exit or timeout. On timeout: SIGKILL the child
3657        // (std Child::kill sends SIGKILL on Unix / TerminateProcess on Windows).
3658        // A proper TERM→KILL escalation would need nix/libc; skip for v1.
3659        let start = Instant::now();
3660        let status = loop {
3661            match child.try_wait() {
3662                Ok(Some(status)) => break status,
3663                Ok(None) => {
3664                    if start.elapsed() >= timeout {
3665                        tracing::debug!(command, "filter_range: timeout — killing child");
3666                        let _ = child.kill();
3667                        let _ = child.wait(); // reap so the OS can free resources
3668                        return Err(format!("command timed out after {}s", timeout.as_secs()));
3669                    }
3670                    thread::sleep(std::time::Duration::from_millis(20));
3671                }
3672                Err(e) => return Err(format!("wait failed: {e}")),
3673            }
3674        };
3675
3676        let stdout_bytes = stdout_thread.join().unwrap_or_default();
3677        let stderr_bytes = stderr_thread.join().unwrap_or_default();
3678
3679        if !status.success() {
3680            let stderr = String::from_utf8_lossy(&stderr_bytes).into_owned();
3681            tracing::debug!(
3682                command,
3683                exit_code = ?status.code(),
3684                "filter_range: command exited with non-zero status"
3685            );
3686            return Err(if stderr.is_empty() {
3687                format!("command exited with status {}", status.code().unwrap_or(-1))
3688            } else {
3689                stderr
3690            });
3691        }
3692
3693        let stdout = String::from_utf8_lossy(&stdout_bytes).into_owned();
3694        tracing::debug!(
3695            command,
3696            stdout_bytes = stdout_bytes.len(),
3697            "filter_range: command succeeded, replacing rows"
3698        );
3699
3700        // Replace the row range with the stdout lines.
3701        let mut all_lines = lines;
3702        let new_lines: Vec<String> = stdout.lines().map(|l| l.to_owned()).collect();
3703        // If stdout ended with a newline, stdout.lines() drops the trailing empty
3704        // entry — this preserves vim's "no trailing-newline trim" spec because
3705        // a trailing '\n' from the command means the last replacement line is the
3706        // line BEFORE the newline, not an empty line after it.
3707        let after = all_lines.split_off(bot + 1);
3708        all_lines.truncate(top);
3709        all_lines.extend(new_lines);
3710        all_lines.extend(after);
3711
3712        self.push_undo();
3713        self.restore(all_lines, (top, 0));
3714        // Leave mode as Normal after a successful filter operation (vim parity).
3715        self.force_normal();
3716
3717        Ok(())
3718    }
3719
3720    // ─── Comment toggle (#187) ───────────────────────────────────────────────
3721
3722    /// Toggle line comments on rows `top_row..=bot_row` (0-based, inclusive).
3723    ///
3724    /// **Algorithm** (vim-commentary parity):
3725    ///
3726    /// 1. Determine the comment marker(s) for the active filetype.
3727    ///    Priority: `settings.commentstring` (`:set commentstring=…`) → per-filetype
3728    ///    default from `hjkl_lang::comment::commentstring_for_lang` → no-op.
3729    /// 2. Scan non-blank lines.  If every non-blank line is already commented →
3730    ///    strip the comment marker from each.  Otherwise → add it to all non-blank
3731    ///    lines.
3732    /// 3. Blank / whitespace-only lines are skipped (no marker added or removed).
3733    /// 4. The marker is inserted AFTER the leading whitespace (indent-preserving).
3734    /// 5. The entire operation is a single undo step.
3735    ///
3736    /// For block-comment languages (HTML, CSS) each line is individually wrapped
3737    /// as `start text end` (per-line block style, not one multi-line block).
3738    ///
3739    /// `top_row` and `bot_row` are clamped to the buffer's valid row range.
3740    pub fn toggle_comment_range(&mut self, top_row: usize, bot_row: usize) {
3741        use hjkl_lang::comment::commentstring_for_lang;
3742
3743        let lang = self.settings.filetype.clone();
3744
3745        // Resolve the comment markers.
3746        // If `settings.commentstring` is set (non-empty) parse `start %s end`
3747        // from it; otherwise fall back to the filetype table.
3748        let (start, end) = if !self.settings.commentstring.is_empty() {
3749            let cs = &self.settings.commentstring;
3750            if let Some(idx) = cs.find("%s") {
3751                let s = cs[..idx].trim_end().to_string();
3752                let e_raw = cs[idx + 2..].trim_start();
3753                let e: Option<String> = if e_raw.is_empty() {
3754                    None
3755                } else {
3756                    Some(e_raw.to_string())
3757                };
3758                (s, e)
3759            } else {
3760                // No %s placeholder — treat the whole string as start marker.
3761                (cs.clone(), None)
3762            }
3763        } else {
3764            match commentstring_for_lang(&lang) {
3765                Some((s, e)) => (s.to_string(), e.map(|v| v.to_string())),
3766                None => return, // no known comment syntax → no-op
3767            }
3768        };
3769
3770        let row_count = buf_row_count(&self.buffer);
3771        let top = top_row.min(row_count.saturating_sub(1));
3772        let bot = bot_row.min(row_count.saturating_sub(1));
3773
3774        // Collect all lines in the range.
3775        let lines: Vec<String> = (top..=bot)
3776            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
3777            .collect();
3778
3779        // Check whether every non-blank line is already commented.
3780        let all_commented = lines.iter().all(|line| {
3781            let trimmed = line.trim_start();
3782            if trimmed.is_empty() {
3783                return true; // blank lines don't count against "all commented"
3784            }
3785            if let Some(ref end_marker) = end {
3786                // Block style: line starts with start and ends with end.
3787                trimmed.starts_with(start.as_str())
3788                    && line.trim_end().ends_with(end_marker.as_str())
3789            } else {
3790                trimmed.starts_with(start.as_str())
3791            }
3792        });
3793
3794        let mut new_lines: Vec<String> = Vec::with_capacity(lines.len());
3795        for line in &lines {
3796            let trimmed = line.trim_start();
3797            if trimmed.is_empty() {
3798                // Blank line — leave as-is.
3799                new_lines.push(line.clone());
3800                continue;
3801            }
3802            let indent_len = line.len() - trimmed.len();
3803            let indent = &line[..indent_len];
3804
3805            if all_commented {
3806                // Uncomment: strip exactly one occurrence of start (+ optional space).
3807                if let Some(after_start) = trimmed.strip_prefix(start.as_str()) {
3808                    // Strip one leading space after the marker if present.
3809                    let after_space = after_start.strip_prefix(' ').unwrap_or(after_start);
3810                    // For block style also strip the trailing end marker.
3811                    let text = if let Some(ref end_marker) = end {
3812                        after_space
3813                            .trim_end()
3814                            .strip_suffix(end_marker.as_str())
3815                            .map(|s| s.trim_end())
3816                            .unwrap_or(after_space)
3817                    } else {
3818                        after_space
3819                    };
3820                    new_lines.push(format!("{indent}{text}"));
3821                } else {
3822                    new_lines.push(line.clone());
3823                }
3824            } else {
3825                // Comment: insert marker after indent.
3826                let commented = if let Some(ref end_marker) = end {
3827                    format!("{indent}{start} {trimmed} {end_marker}")
3828                } else {
3829                    format!("{indent}{start} {trimmed}")
3830                };
3831                new_lines.push(commented);
3832            }
3833        }
3834
3835        // Replace the row range in the buffer — single undo step.
3836        self.push_undo();
3837        let row_count_after = buf_row_count(&self.buffer);
3838        let all_before: Vec<String> = (0..top)
3839            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
3840            .collect();
3841        let all_after: Vec<String> = ((bot + 1)..row_count_after)
3842            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
3843            .collect();
3844        let mut all: Vec<String> = all_before;
3845        all.extend(new_lines);
3846        all.extend(all_after);
3847        self.restore(all, (top, 0));
3848    }
3849
3850    // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3851    //
3852    // Pure functions — no cursor mutation, no mode change, no register write.
3853    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3854    // the existing `word_text_object` private resolver in vim.rs.
3855    //
3856    // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3857    // before invoking a range-mutation primitive (`delete_range`, etc.).
3858    //
3859    // Return value: `Some((start, end))` where both positions are `(row, col)`
3860    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3861    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3862    // Returns `None` when the cursor is on an empty line or the resolver cannot
3863    // find a word boundary.
3864
3865    /// Resolve the range of `iw` (inner word) at the current cursor position.
3866    ///
3867    /// An inner word is the contiguous run of keyword characters (or punctuation
3868    /// characters if the cursor is on punctuation) under the cursor, without any
3869    /// surrounding whitespace. Whitespace-only positions return `None`.
3870    ///
3871    /// Pure function — does not move the cursor or change any editor state.
3872    /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3873    /// invoking a range-mutation primitive (`delete_range`, etc.).
3874    ///
3875    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3876    /// migration (kryptic-sh/hjkl#70).
3877    pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3878        vim::text_object_inner_word_bridge(self)
3879    }
3880
3881    /// Resolve the range of `aw` (around word) at the current cursor position.
3882    ///
3883    /// Like `iw` but extends the range to include trailing whitespace after the
3884    /// word. If no trailing whitespace exists, leading whitespace before the word
3885    /// is absorbed instead (vim `:help text-objects` behaviour).
3886    ///
3887    /// Pure function — does not move the cursor or change any editor state.
3888    ///
3889    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3890    /// migration (kryptic-sh/hjkl#70).
3891    pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3892        vim::text_object_around_word_bridge(self)
3893    }
3894
3895    /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3896    ///
3897    /// A WORD is any contiguous run of non-whitespace characters — punctuation
3898    /// is not treated as a word boundary. Returns the span of the WORD under the
3899    /// cursor, without surrounding whitespace.
3900    ///
3901    /// Pure function — does not move the cursor or change any editor state.
3902    ///
3903    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3904    /// migration (kryptic-sh/hjkl#70).
3905    pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3906        vim::text_object_inner_big_word_bridge(self)
3907    }
3908
3909    /// Resolve the range of `aW` (around WORD) at the current cursor position.
3910    ///
3911    /// Like `iW` but extends the range to include trailing whitespace after the
3912    /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3913    /// is absorbed instead.
3914    ///
3915    /// Pure function — does not move the cursor or change any editor state.
3916    ///
3917    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3918    /// migration (kryptic-sh/hjkl#70).
3919    pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3920        vim::text_object_around_big_word_bridge(self)
3921    }
3922
3923    // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3924    //
3925    // Pure functions — no cursor mutation, no mode change, no register write.
3926    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3927    // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3928    // in vim.rs.
3929    //
3930    // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3931    // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3932    // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3933    // hjkl-vim grammar layer normalises close→open before calling these methods.
3934    //
3935    // Return value: `Some((start, end))` where both positions are `(row, col)`
3936    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3937    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3938    // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3939    // ranges for multi-line pairs; that tag is stripped here — callers receive
3940    // the same flat shape as all other text-object resolvers.
3941
3942    /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3943    ///
3944    /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3945    /// cursor's line contains fewer than two occurrences of `quote`, or when no
3946    /// matching pair can be found around or ahead of the cursor.
3947    ///
3948    /// Inner range excludes the quote characters themselves.
3949    ///
3950    /// Pure function — no cursor mutation.
3951    ///
3952    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3953    /// migration (kryptic-sh/hjkl#70).
3954    pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3955        vim::text_object_inner_quote_bridge(self, quote)
3956    }
3957
3958    /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3959    ///
3960    /// Like `i<quote>` but includes the quote characters themselves plus
3961    /// surrounding whitespace on one side: trailing whitespace after the closing
3962    /// quote if any exists; otherwise leading whitespace before the opening
3963    /// quote. This matches vim `:help text-objects` behaviour.
3964    ///
3965    /// Pure function — no cursor mutation.
3966    ///
3967    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3968    /// migration (kryptic-sh/hjkl#70).
3969    pub fn text_object_around_quote(
3970        &self,
3971        quote: char,
3972    ) -> Option<((usize, usize), (usize, usize))> {
3973        vim::text_object_around_quote_bridge(self, quote)
3974    }
3975
3976    /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3977    ///
3978    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3979    /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3980    /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3981    /// calling this method. Returns `None` when no enclosing pair is found.
3982    ///
3983    /// The cursor may be anywhere inside the pair or on a bracket character
3984    /// itself. When not inside any pair the resolver falls back to a forward
3985    /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3986    ///
3987    /// Inner range excludes the bracket characters. Multi-line pairs are
3988    /// supported; the returned range spans the full content between the
3989    /// brackets.
3990    ///
3991    /// Pure function — no cursor mutation.
3992    ///
3993    /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3994    /// handled here.
3995    ///
3996    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3997    /// migration (kryptic-sh/hjkl#70).
3998    pub fn text_object_inner_bracket(
3999        &self,
4000        open: char,
4001    ) -> Option<((usize, usize), (usize, usize))> {
4002        vim::text_object_inner_bracket_bridge(self, open)
4003    }
4004
4005    /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
4006    ///
4007    /// Like `i<bracket>` but includes the bracket characters themselves.
4008    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
4009    ///
4010    /// Pure function — no cursor mutation.
4011    ///
4012    /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
4013    ///
4014    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
4015    /// migration (kryptic-sh/hjkl#70).
4016    pub fn text_object_around_bracket(
4017        &self,
4018        open: char,
4019    ) -> Option<((usize, usize), (usize, usize))> {
4020        vim::text_object_around_bracket_bridge(self, open)
4021    }
4022
4023    // ── Sentence text objects (is / as) ───────────────────────────────────
4024
4025    /// Resolve `is` (inner sentence) at the cursor position.
4026    ///
4027    /// Returns the range of the current sentence, excluding trailing
4028    /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
4029    /// `?` / `!` followed by whitespace or end-of-paragraph).
4030    ///
4031    /// Pure function — no cursor mutation.
4032    ///
4033    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4034    /// grammar migration (kryptic-sh/hjkl#70).
4035    pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
4036        vim::text_object_inner_sentence_bridge(self)
4037    }
4038
4039    /// Resolve `as` (around sentence) at the cursor position.
4040    ///
4041    /// Like `is` but includes trailing whitespace after the sentence
4042    /// terminator.
4043    ///
4044    /// Pure function — no cursor mutation.
4045    ///
4046    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4047    /// grammar migration (kryptic-sh/hjkl#70).
4048    pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
4049        vim::text_object_around_sentence_bridge(self)
4050    }
4051
4052    // ── Paragraph text objects (ip / ap) ──────────────────────────────────
4053
4054    /// Resolve `ip` (inner paragraph) at the cursor position.
4055    ///
4056    /// A paragraph is a block of non-blank lines bounded by blank lines or
4057    /// buffer edges. Returns `None` when the cursor is on a blank line.
4058    ///
4059    /// Pure function — no cursor mutation.
4060    ///
4061    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4062    /// grammar migration (kryptic-sh/hjkl#70).
4063    pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
4064        vim::text_object_inner_paragraph_bridge(self)
4065    }
4066
4067    /// Resolve `ap` (around paragraph) at the cursor position.
4068    ///
4069    /// Like `ip` but includes one trailing blank line when present.
4070    ///
4071    /// Pure function — no cursor mutation.
4072    ///
4073    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4074    /// grammar migration (kryptic-sh/hjkl#70).
4075    pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
4076        vim::text_object_around_paragraph_bridge(self)
4077    }
4078
4079    // ── Tag text objects (it / at) ────────────────────────────────────────
4080
4081    /// Resolve `it` (inner tag) at the cursor position.
4082    ///
4083    /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
4084    /// inner content between the open and close tags (excluding the tags
4085    /// themselves).
4086    ///
4087    /// Pure function — no cursor mutation.
4088    ///
4089    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4090    /// grammar migration (kryptic-sh/hjkl#70).
4091    pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
4092        vim::text_object_inner_tag_bridge(self)
4093    }
4094
4095    /// Resolve `at` (around tag) at the cursor position.
4096    ///
4097    /// Like `it` but includes the open and close tag delimiters themselves.
4098    ///
4099    /// Pure function — no cursor mutation.
4100    ///
4101    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4102    /// grammar migration (kryptic-sh/hjkl#70).
4103    pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
4104        vim::text_object_around_tag_bridge(self)
4105    }
4106
4107    /// Execute a named cursor motion `kind` repeated `count` times.
4108    ///
4109    /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
4110    /// motion primitives, bypassing the engine FSM. Identical cursor semantics
4111    /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
4112    /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
4113    /// used by the FSM arms.
4114    ///
4115    /// Introduced in 0.6.1 as the host entry point for Phase 3a of
4116    /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
4117    /// calls this method rather than re-entering the engine FSM.
4118    ///
4119    /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
4120    /// intact for macro-replay coverage (macros re-feed raw keys through the
4121    /// FSM). This method is the keymap / controller path only.
4122    pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
4123        vim::apply_motion_kind(self, kind, count);
4124    }
4125
4126    /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
4127    /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
4128    /// chars are silently ignored (no-op), matching the engine FSM's
4129    /// `handle_select_register` behaviour.
4130    ///
4131    /// Promoted to the public surface in 0.5.17 so the hjkl-vim
4132    /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
4133    /// without re-entering the engine FSM. `handle_select_register` (engine FSM
4134    /// path for macro-replay / defensive coverage) delegates here to avoid
4135    /// logic duplication.
4136    pub fn set_pending_register(&mut self, reg: char) {
4137        if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
4138            self.vim.pending_register = Some(reg);
4139        }
4140        // Invalid chars silently no-op (matches engine FSM behavior).
4141    }
4142
4143    /// Record a mark named `ch` at the current cursor position.
4144    ///
4145    /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
4146    /// rules). Invalid chars are silently ignored (no-op), matching the engine
4147    /// FSM's `handle_set_mark` behaviour.
4148    ///
4149    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4150    /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
4151    /// without re-entering the engine FSM. `handle_set_mark` delegates here.
4152    pub fn set_mark_at_cursor(&mut self, ch: char) {
4153        vim::set_mark_at_cursor(self, ch);
4154    }
4155
4156    /// `.` dot-repeat: replay the last buffered change at the current cursor.
4157    /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
4158    /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
4159    ///
4160    /// Storage of `LastChange` stays inside engine for now; Phase 5c of
4161    /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
4162    /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
4163    /// FSM `.` arm stays for macro-replay defensive coverage.
4164    pub fn replay_last_change(&mut self, count: usize) {
4165        vim::replay_last_change(self, count);
4166    }
4167
4168    /// Jump to the mark named `ch`, linewise (row only; col snaps to first
4169    /// non-blank). Pushes the pre-jump position onto the jumplist if the
4170    /// cursor actually moved.
4171    ///
4172    /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
4173    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
4174    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
4175    /// are silently ignored (no-op), matching the engine FSM's
4176    /// `handle_goto_mark` behaviour.
4177    ///
4178    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4179    /// `PendingState::GotoMarkLine` reducer can dispatch
4180    /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
4181    pub fn goto_mark_line(&mut self, ch: char) {
4182        vim::goto_mark(self, ch, true);
4183    }
4184
4185    /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
4186    /// pre-jump position onto the jumplist if the cursor actually moved.
4187    ///
4188    /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
4189    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
4190    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
4191    /// are silently ignored (no-op), matching the engine FSM's
4192    /// `handle_goto_mark` behaviour.
4193    ///
4194    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4195    /// `PendingState::GotoMarkChar` reducer can dispatch
4196    /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
4197    pub fn goto_mark_char(&mut self, ch: char) {
4198        vim::goto_mark(self, ch, false);
4199    }
4200
4201    /// Jump to the mark named `ch`, linewise. For uppercase marks (`'A'`–`'Z'`)
4202    /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
4203    /// app can switch slots before positioning the cursor. Returns
4204    /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
4205    /// `MarkJump::Unset` when the mark is not set.
4206    pub fn try_goto_mark_line(&mut self, ch: char) -> MarkJump {
4207        vim::try_goto_mark(self, ch, true)
4208    }
4209
4210    /// Jump to the mark named `ch`, charwise. For uppercase marks (`'A'`–`'Z'`)
4211    /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
4212    /// app can switch slots before positioning the cursor. Returns
4213    /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
4214    /// `MarkJump::Unset` when the mark is not set.
4215    pub fn try_goto_mark_char(&mut self, ch: char) -> MarkJump {
4216        vim::try_goto_mark(self, ch, false)
4217    }
4218
4219    // ── Macro controller API (Phase 5b) ──────────────────────────────────────
4220
4221    /// Begin recording keystrokes into register `reg`. The caller (app) is
4222    /// responsible for stopping the recording via `stop_macro_record` when the
4223    /// user presses bare `q`.
4224    ///
4225    /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
4226    ///   recording by pre-seeding `recording_keys` with the decoded text of the
4227    ///   matching lowercase register, matching vim's capital-register append
4228    ///   semantics.
4229    /// - Lowercase `reg` clears `recording_keys` (fresh recording).
4230    /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
4231    ///
4232    /// Promoted to the public surface in Phase 5b so the app's
4233    /// `route_chord_key` can start a recording without re-entering the engine
4234    /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
4235    /// defensive coverage) continues to use the same logic via delegation.
4236    pub fn start_macro_record(&mut self, reg: char) {
4237        if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
4238            return;
4239        }
4240        self.vim.recording_macro = Some(reg);
4241        if reg.is_ascii_uppercase() {
4242            // Seed recording_keys with the existing lowercase register's text
4243            // decoded back to inputs so capital-register append continues from
4244            // where the previous recording left off.
4245            let lower = reg.to_ascii_lowercase();
4246            let text = self
4247                .registers
4248                .read(lower)
4249                .map(|s| s.text.clone())
4250                .unwrap_or_default();
4251            self.vim.recording_keys = crate::input::decode_macro(&text);
4252        } else {
4253            self.vim.recording_keys.clear();
4254        }
4255    }
4256
4257    /// Finalize the active recording: encode `recording_keys` as text and write
4258    /// to the matching (lowercase) named register. Clears both `recording_macro`
4259    /// and `recording_keys`. No-ops if no recording is active.
4260    ///
4261    /// Promoted to the public surface in Phase 5b so the app's `QChord` action
4262    /// can stop a recording when the user presses bare `q` without re-entering
4263    /// the engine FSM.
4264    pub fn stop_macro_record(&mut self) {
4265        let Some(reg) = self.vim.recording_macro.take() else {
4266            return;
4267        };
4268        let keys = std::mem::take(&mut self.vim.recording_keys);
4269        let text = crate::input::encode_macro(&keys);
4270        self.set_named_register_text(reg.to_ascii_lowercase(), text);
4271    }
4272
4273    /// Returns `true` while a `q{reg}` recording is in progress.
4274    /// Hosts use this to show a "recording @r" status indicator and to decide
4275    /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
4276    /// chord.
4277    pub fn is_recording_macro(&self) -> bool {
4278        self.vim.recording_macro.is_some()
4279    }
4280
4281    /// Returns `true` while a macro is being replayed. The app sets this flag
4282    /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
4283    /// re-feed loop so the recorder hook can skip double-capture.
4284    pub fn is_replaying_macro(&self) -> bool {
4285        self.vim.replaying_macro
4286    }
4287
4288    /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
4289    /// prepare for replay, returning the inputs the app should re-feed through
4290    /// `route_chord_key`.
4291    ///
4292    /// Resolves `reg`:
4293    /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
4294    /// - Any other char → lowercase it, read the register, decode.
4295    ///
4296    /// Side-effects:
4297    /// - Sets `vim.last_macro` to the resolved register.
4298    /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
4299    ///   replay. The app calls `end_macro_replay` after the loop finishes.
4300    ///
4301    /// Returns an empty vec (and no side-effects for `'@'`) if the register is
4302    /// unset or empty.
4303    pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
4304        let resolved = if reg == '@' {
4305            match self.vim.last_macro {
4306                Some(r) => r,
4307                None => return vec![],
4308            }
4309        } else {
4310            reg.to_ascii_lowercase()
4311        };
4312        let text = match self.registers.read(resolved) {
4313            Some(slot) if !slot.text.is_empty() => slot.text.clone(),
4314            _ => return vec![],
4315        };
4316        let keys = crate::input::decode_macro(&text);
4317        self.vim.last_macro = Some(resolved);
4318        self.vim.replaying_macro = true;
4319        // Multiply by count (minimum 1).
4320        keys.repeat(count.max(1))
4321    }
4322
4323    /// Clear the `replaying_macro` flag. Called by the app after the
4324    /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
4325    pub fn end_macro_replay(&mut self) {
4326        self.vim.replaying_macro = false;
4327    }
4328
4329    /// Append `input` to the active recording (`recording_keys`) if and only
4330    /// if a recording is in progress AND we are not currently replaying.
4331    /// Called by the app's `route_chord_key` recorder hook so that user
4332    /// keystrokes captured through the app-level chord path are recorded
4333    /// (rather than relying solely on the engine FSM's in-step hook).
4334    pub fn record_input(&mut self, input: crate::input::Input) {
4335        if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
4336            self.vim.recording_keys.push(input);
4337        }
4338    }
4339
4340    // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
4341    //
4342    // Each method is the publicly callable form of one insert-mode action.
4343    // All logic lives in the corresponding `vim::*_bridge` free function;
4344    // these methods are thin delegators so the public surface stays on `Editor`.
4345    //
4346    // Invariants (enforced by the bridge fns):
4347    //   - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
4348    //   - Navigation keys call `break_undo_group_in_insert` when the FSM did.
4349    //   - `push_buffer_cursor_to_textarea` is called after every mutation
4350    //     (currently a no-op, kept for migration hygiene).
4351
4352    /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
4353    /// the cursor instead of inserting; at end-of-line, always appends. With
4354    /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
4355    /// dedent on an otherwise-whitespace line.
4356    ///
4357    /// Callers must ensure the editor is in Insert or Replace mode before
4358    /// calling this method.
4359    pub fn insert_char(&mut self, ch: char) {
4360        if vim::insert_char_bridge(self, ch) {
4361            self.after_insert_mutation();
4362        }
4363    }
4364
4365    /// Insert a newline at the cursor, applying autoindent / smartindent to
4366    /// prefix the new line with the appropriate leading whitespace.
4367    ///
4368    /// Callers must ensure the editor is in Insert mode before calling.
4369    pub fn insert_newline(&mut self) {
4370        if vim::insert_newline_bridge(self) {
4371            self.after_insert_mutation();
4372        }
4373    }
4374
4375    /// Common post-mutation sync for the `insert_*` primitives. The vim
4376    /// FSM's `step` runs `ensure_cursor_in_scrolloff` at the end of every
4377    /// normal/visual motion; insert-mode primitives bypass `step` and
4378    /// must self-correct or the cursor scrolls off the viewport (held
4379    /// Enter, multi-line backspace at BOL, arrow keys at edge, etc.).
4380    ///
4381    /// Marks the content dirty, widens the insert row's autoindent
4382    /// tracking, and re-checks scrolloff.
4383    fn after_insert_mutation(&mut self) {
4384        self.mark_content_dirty();
4385        let (row, _) = self.cursor();
4386        self.vim.widen_insert_row(row);
4387        self.ensure_cursor_in_scrolloff();
4388    }
4389
4390    /// Like `after_insert_mutation` but for cursor-only insert ops that
4391    /// don't change content (arrows, Home/End, PageUp/Down). Skips the
4392    /// dirty mark.
4393    fn after_insert_motion(&mut self) {
4394        let (row, _) = self.cursor();
4395        self.vim.widen_insert_row(row);
4396        self.ensure_cursor_in_scrolloff();
4397    }
4398
4399    /// Insert a tab character (or spaces up to the next `softtabstop` boundary
4400    /// when `expandtab` is set).
4401    ///
4402    /// Callers must ensure the editor is in Insert mode before calling.
4403    pub fn insert_tab(&mut self) {
4404        if vim::insert_tab_bridge(self) {
4405            self.after_insert_mutation();
4406        }
4407    }
4408
4409    /// Delete the character before the cursor (Backspace). With `softtabstop`
4410    /// active, deletes the entire soft-tab run at an aligned boundary. Joins
4411    /// with the previous line when at column 0.
4412    ///
4413    /// Callers must ensure the editor is in Insert mode before calling.
4414    pub fn insert_backspace(&mut self) {
4415        if vim::insert_backspace_bridge(self) {
4416            self.after_insert_mutation();
4417        }
4418    }
4419
4420    /// Delete the character under the cursor (Delete key). Joins with the
4421    /// next line when at end-of-line.
4422    ///
4423    /// Callers must ensure the editor is in Insert mode before calling.
4424    pub fn insert_delete(&mut self) {
4425        if vim::insert_delete_bridge(self) {
4426            self.after_insert_mutation();
4427        }
4428    }
4429
4430    /// Move the cursor one step in `dir` (arrow key), breaking the undo group
4431    /// per `undo_break_on_motion`.
4432    ///
4433    /// Callers must ensure the editor is in Insert mode before calling.
4434    pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
4435        vim::insert_arrow_bridge(self, dir);
4436        self.after_insert_motion();
4437    }
4438
4439    /// Move the cursor to the start of the current line (Home key), breaking
4440    /// the undo group.
4441    ///
4442    /// Callers must ensure the editor is in Insert mode before calling.
4443    pub fn insert_home(&mut self) {
4444        vim::insert_home_bridge(self);
4445        self.after_insert_motion();
4446    }
4447
4448    /// Move the cursor to the end of the current line (End key), breaking the
4449    /// undo group.
4450    ///
4451    /// Callers must ensure the editor is in Insert mode before calling.
4452    pub fn insert_end(&mut self) {
4453        vim::insert_end_bridge(self);
4454        self.after_insert_motion();
4455    }
4456
4457    /// Scroll up one full viewport height (PageUp), moving the cursor with it.
4458    /// `viewport_h` is the current viewport height in rows; pass
4459    /// `self.viewport_height_value()` if the stored value is current.
4460    ///
4461    /// Callers must ensure the editor is in Insert mode before calling.
4462    pub fn insert_pageup(&mut self, viewport_h: u16) {
4463        vim::insert_pageup_bridge(self, viewport_h);
4464        self.after_insert_motion();
4465    }
4466
4467    /// Scroll down one full viewport height (PageDown), moving the cursor with
4468    /// it. `viewport_h` is the current viewport height in rows.
4469    ///
4470    /// Callers must ensure the editor is in Insert mode before calling.
4471    pub fn insert_pagedown(&mut self, viewport_h: u16) {
4472        vim::insert_pagedown_bridge(self, viewport_h);
4473        self.after_insert_motion();
4474    }
4475
4476    /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
4477    /// At column 0, joins with the previous line (vim `b`-motion semantics).
4478    ///
4479    /// Callers must ensure the editor is in Insert mode before calling.
4480    pub fn insert_ctrl_w(&mut self) {
4481        if vim::insert_ctrl_w_bridge(self) {
4482            self.after_insert_mutation();
4483        }
4484    }
4485
4486    /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
4487    /// No-op when already at column 0.
4488    ///
4489    /// Callers must ensure the editor is in Insert mode before calling.
4490    pub fn insert_ctrl_u(&mut self) {
4491        if vim::insert_ctrl_u_bridge(self) {
4492            self.after_insert_mutation();
4493        }
4494    }
4495
4496    /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
4497    /// insert mode. Joins with the previous line when at col 0.
4498    ///
4499    /// Callers must ensure the editor is in Insert mode before calling.
4500    pub fn insert_ctrl_h(&mut self) {
4501        if vim::insert_ctrl_h_bridge(self) {
4502            self.after_insert_mutation();
4503        }
4504    }
4505
4506    /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
4507    /// complete normal-mode command, then return to insert automatically.
4508    ///
4509    /// Callers must ensure the editor is in Insert mode before calling.
4510    pub fn insert_ctrl_o_arm(&mut self) {
4511        vim::insert_ctrl_o_bridge(self);
4512    }
4513
4514    /// Arm the register-paste selector (`Ctrl-R`). The next call to
4515    /// `insert_paste_register(reg)` will insert the register contents.
4516    /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
4517    /// the armed state and paste register `c`.
4518    ///
4519    /// Callers must ensure the editor is in Insert mode before calling.
4520    pub fn insert_ctrl_r_arm(&mut self) {
4521        vim::insert_ctrl_r_bridge(self);
4522    }
4523
4524    /// Indent the current line by one `shiftwidth` and shift the cursor right
4525    /// by the same amount (`Ctrl-T`).
4526    ///
4527    /// Callers must ensure the editor is in Insert mode before calling.
4528    pub fn insert_ctrl_t(&mut self) {
4529        let mutated = vim::insert_ctrl_t_bridge(self);
4530        if mutated {
4531            self.mark_content_dirty();
4532            let (row, _) = self.cursor();
4533            self.vim.widen_insert_row(row);
4534        }
4535    }
4536
4537    /// Outdent the current line by up to one `shiftwidth` and shift the cursor
4538    /// left by the amount stripped (`Ctrl-D`).
4539    ///
4540    /// Callers must ensure the editor is in Insert mode before calling.
4541    pub fn insert_ctrl_d(&mut self) {
4542        let mutated = vim::insert_ctrl_d_bridge(self);
4543        if mutated {
4544            self.mark_content_dirty();
4545            let (row, _) = self.cursor();
4546            self.vim.widen_insert_row(row);
4547        }
4548    }
4549
4550    /// Paste the contents of register `reg` at the cursor (the commit arm of
4551    /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
4552    ///
4553    /// Callers must ensure the editor is in Insert mode before calling.
4554    pub fn insert_paste_register(&mut self, reg: char) {
4555        vim::insert_paste_register_bridge(self, reg);
4556        let (row, _) = self.cursor();
4557        self.vim.widen_insert_row(row);
4558    }
4559
4560    /// Exit insert mode to Normal: finish the insert session, step the cursor
4561    /// one cell left (vim convention on Esc), record the `gi` target position,
4562    /// and update the sticky column.
4563    ///
4564    /// Callers must ensure the editor is in Insert mode before calling.
4565    pub fn leave_insert_to_normal(&mut self) {
4566        vim::leave_insert_to_normal_bridge(self);
4567    }
4568
4569    // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
4570    //
4571    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4572    // `vim.rs` following the same pattern as Phase 6.1. The FSM's
4573    // `handle_normal_only` now calls the same bridges so both paths are
4574    // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
4575
4576    /// `i` — transition to Insert mode at the current cursor position.
4577    /// `count` is stored in the insert session and replayed by dot-repeat
4578    /// as a repeat count on the inserted text.
4579    pub fn enter_insert_i(&mut self, count: usize) {
4580        vim::enter_insert_i_bridge(self, count);
4581    }
4582
4583    /// `I` — move to the first non-blank character on the line, then
4584    /// transition to Insert mode. `count` is stored for dot-repeat.
4585    pub fn enter_insert_shift_i(&mut self, count: usize) {
4586        vim::enter_insert_shift_i_bridge(self, count);
4587    }
4588
4589    /// `a` — advance the cursor one cell past the current position, then
4590    /// transition to Insert mode (append). `count` is stored for dot-repeat.
4591    pub fn enter_insert_a(&mut self, count: usize) {
4592        vim::enter_insert_a_bridge(self, count);
4593    }
4594
4595    /// `A` — move the cursor to the end of the line, then transition to
4596    /// Insert mode (append at end). `count` is stored for dot-repeat.
4597    pub fn enter_insert_shift_a(&mut self, count: usize) {
4598        vim::enter_insert_shift_a_bridge(self, count);
4599    }
4600
4601    /// `o` — open a new line below the current line with smart-indent, then
4602    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4603    pub fn open_line_below(&mut self, count: usize) {
4604        vim::open_line_below_bridge(self, count);
4605    }
4606
4607    /// `O` — open a new line above the current line with smart-indent, then
4608    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4609    pub fn open_line_above(&mut self, count: usize) {
4610        vim::open_line_above_bridge(self, count);
4611    }
4612
4613    /// `R` — enter Replace mode: subsequent typed characters overstrike the
4614    /// cell under the cursor rather than inserting. `count` is for replay.
4615    pub fn enter_replace_mode(&mut self, count: usize) {
4616        vim::enter_replace_mode_bridge(self, count);
4617    }
4618
4619    /// `x` — delete `count` characters forward from the cursor and write them
4620    /// to the unnamed register. No-op on an empty line. Records for `.`.
4621    pub fn delete_char_forward(&mut self, count: usize) {
4622        vim::delete_char_forward_bridge(self, count);
4623    }
4624
4625    /// `X` — delete `count` characters backward from the cursor and write
4626    /// them to the unnamed register. No-op at column 0. Records for `.`.
4627    pub fn delete_char_backward(&mut self, count: usize) {
4628        vim::delete_char_backward_bridge(self, count);
4629    }
4630
4631    /// `s` — substitute `count` characters: delete them (writing to the
4632    /// unnamed register) then enter Insert mode. Equivalent to `cl`.
4633    /// Records as `OpMotion { Change, Right }` for dot-repeat.
4634    pub fn substitute_char(&mut self, count: usize) {
4635        vim::substitute_char_bridge(self, count);
4636    }
4637
4638    /// `S` — substitute the current line: wipe its contents (writing to the
4639    /// unnamed register) then enter Insert mode. Equivalent to `cc`.
4640    /// Records as `LineOp { Change }` for dot-repeat.
4641    pub fn substitute_line(&mut self, count: usize) {
4642        vim::substitute_line_bridge(self, count);
4643    }
4644
4645    /// `D` — delete from the cursor to end-of-line, writing to the unnamed
4646    /// register. The cursor parks on the new last character. Records for `.`.
4647    pub fn delete_to_eol(&mut self) {
4648        vim::delete_to_eol_bridge(self);
4649    }
4650
4651    /// `C` — change from the cursor to end-of-line: delete to EOL then enter
4652    /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
4653    /// (the insert session records `DeleteToEol` on exit, like `c` motions).
4654    pub fn change_to_eol(&mut self) {
4655        vim::change_to_eol_bridge(self);
4656    }
4657
4658    /// `Y` — yank from the cursor to end-of-line into the unnamed register.
4659    /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
4660    pub fn yank_to_eol(&mut self, count: usize) {
4661        vim::yank_to_eol_bridge(self, count);
4662    }
4663
4664    /// `J` — join `count` lines (default 2) onto the current line, inserting
4665    /// a single space between each non-empty pair. Records for dot-repeat.
4666    pub fn join_line(&mut self, count: usize) {
4667        vim::join_line_bridge(self, count);
4668    }
4669
4670    /// `~` — toggle the case of `count` characters from the cursor, advancing
4671    /// right after each toggle. Records `ToggleCase` for dot-repeat.
4672    pub fn toggle_case_at_cursor(&mut self, count: usize) {
4673        vim::toggle_case_at_cursor_bridge(self, count);
4674    }
4675
4676    /// `p` — paste the unnamed register (or the register selected via `"r`)
4677    /// after the cursor. Linewise content opens a new line below; charwise
4678    /// content is inserted inline. Records `Paste { before: false }` for `.`.
4679    pub fn paste_after(&mut self, count: usize) {
4680        vim::paste_after_bridge(self, count);
4681    }
4682
4683    /// `P` — paste the unnamed register (or the `"r` register) before the
4684    /// cursor. Linewise content opens a new line above; charwise is inline.
4685    /// Records `Paste { before: true }` for dot-repeat.
4686    pub fn paste_before(&mut self, count: usize) {
4687        vim::paste_before_bridge(self, count);
4688    }
4689
4690    /// `<C-o>` — jump back `count` entries in the jumplist, saving the
4691    /// current position on the forward stack so `<C-i>` can return.
4692    pub fn jump_back(&mut self, count: usize) {
4693        vim::jump_back_bridge(self, count);
4694    }
4695
4696    /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
4697    /// saving the current position on the backward stack.
4698    pub fn jump_forward(&mut self, count: usize) {
4699        vim::jump_forward_bridge(self, count);
4700    }
4701
4702    /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
4703    /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
4704    /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
4705    pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
4706        vim::scroll_full_page_bridge(self, dir, count);
4707    }
4708
4709    /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
4710    /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
4711    pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
4712        vim::scroll_half_page_bridge(self, dir, count);
4713    }
4714
4715    /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
4716    /// the cursor (cursor is clamped to the new visible region if necessary).
4717    /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
4718    pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
4719        vim::scroll_line_bridge(self, dir, count);
4720    }
4721
4722    /// `n` — repeat the last `/` or `?` search `count` times in its original
4723    /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4724    pub fn search_repeat(&mut self, forward: bool, count: usize) {
4725        vim::search_repeat_bridge(self, forward, count);
4726    }
4727
4728    /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4729    /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4730    /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4731    pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4732        vim::word_search_bridge(self, forward, whole_word, count);
4733    }
4734
4735    // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4736    //
4737    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4738    // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4739    // and these wrappers write `current_mode` so `vim_mode()` returns correct
4740    // values regardless of which path performed the transition.
4741    // See kryptic-sh/hjkl#89 for the full promotion plan.
4742
4743    /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4744    /// at the current cursor position.
4745    pub fn enter_visual_char(&mut self) {
4746        vim::enter_visual_char_bridge(self);
4747    }
4748
4749    /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4750    /// line. Motions extend the selection by whole lines.
4751    pub fn enter_visual_line(&mut self) {
4752        vim::enter_visual_line_bridge(self);
4753    }
4754
4755    /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4756    /// rectangle whose corners are the anchor and the live cursor.
4757    pub fn enter_visual_block(&mut self) {
4758        vim::enter_visual_block_bridge(self);
4759    }
4760
4761    /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4762    /// for `gv` re-entry, then return to Normal mode.
4763    pub fn exit_visual_to_normal(&mut self) {
4764        vim::exit_visual_to_normal_bridge(self);
4765    }
4766
4767    /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4768    /// so the user can extend the other end of the selection. Does NOT
4769    /// mutate the selection range; only the active endpoint changes.
4770    pub fn visual_o_toggle(&mut self) {
4771        vim::visual_o_toggle_bridge(self);
4772    }
4773
4774    /// `gv` — restore the last visual selection (mode + anchor + cursor
4775    /// position). No-op when no visual selection has been exited yet.
4776    pub fn reenter_last_visual(&mut self) {
4777        vim::reenter_last_visual_bridge(self);
4778    }
4779
4780    /// Direct mode-transition entry point. Sets both the internal FSM mode
4781    /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4782    ///
4783    /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4784    /// …) which also set up required bookkeeping (anchors, sessions, …).
4785    /// Use `set_mode` only when you need a raw mode flip without side-effects.
4786    pub fn set_mode(&mut self, mode: VimMode) {
4787        vim::set_mode_bridge(self, mode);
4788    }
4789}
4790
4791// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4792//
4793// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4794// `Editor` accessors and mutators defined in this block. Each method gets a
4795// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4796// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4797
4798/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4799///
4800/// Treat as opaque — construct by calling `begin_step` and pass the
4801/// returned value directly into `end_step` without modification.
4802/// The fields capture per-step pre-dispatch state that the epilogue
4803/// needs to run its invariants correctly.
4804pub struct StepBookkeeping {
4805    /// True when the pending chord before this step was a macro-chord
4806    /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4807    /// keys so that only the *payload* keys enter `recording_keys`.
4808    pub pending_was_macro_chord: bool,
4809    /// True when the mode was Insert *before* the FSM body ran. Used by
4810    /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4811    /// back into Insert.
4812    pub was_insert: bool,
4813    /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4814    /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4815    /// store `last_visual` for `gv`.
4816    pub pre_visual_snapshot: Option<vim::LastVisual>,
4817}
4818
4819impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4820    // ── Pending chord ─────────────────────────────────────────────────────────
4821
4822    /// Return a clone of the current pending chord state.
4823    pub fn pending(&self) -> vim::Pending {
4824        self.vim.pending.clone()
4825    }
4826
4827    /// Overwrite the pending chord state.
4828    pub fn set_pending(&mut self, p: vim::Pending) {
4829        self.vim.pending = p;
4830    }
4831
4832    /// Atomically take the pending chord, replacing it with `Pending::None`.
4833    pub fn take_pending(&mut self) -> vim::Pending {
4834        std::mem::take(&mut self.vim.pending)
4835    }
4836
4837    // ── Count prefix ──────────────────────────────────────────────────────────
4838
4839    /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4840    pub fn count(&self) -> usize {
4841        self.vim.count
4842    }
4843
4844    /// Overwrite the digit-prefix count directly.
4845    pub fn set_count(&mut self, c: usize) {
4846        self.vim.count = c;
4847    }
4848
4849    /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4850    pub fn accumulate_count_digit(&mut self, digit: usize) {
4851        self.vim.count = self.vim.count.saturating_mul(10) + digit;
4852    }
4853
4854    /// Reset the count prefix to zero (no pending count).
4855    pub fn reset_count(&mut self) {
4856        self.vim.count = 0;
4857    }
4858
4859    /// Consume the count and return it; resets to zero. Returns `1` when no
4860    /// prefix was typed (mirrors `take_count` in vim.rs).
4861    pub fn take_count(&mut self) -> usize {
4862        if self.vim.count > 0 {
4863            let n = self.vim.count;
4864            self.vim.count = 0;
4865            n
4866        } else {
4867            1
4868        }
4869    }
4870
4871    // ── Internal FSM mode ─────────────────────────────────────────────────────
4872
4873    /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4874    pub fn fsm_mode(&self) -> vim::Mode {
4875        self.vim.mode
4876    }
4877
4878    /// Overwrite the FSM-internal mode without side-effects. Prefer the
4879    /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4880    pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4881        self.vim.mode = m;
4882        self.vim.current_mode = self.vim.public_mode();
4883    }
4884
4885    // ── Replaying flag ────────────────────────────────────────────────────────
4886
4887    /// `true` while the `.` dot-repeat replay is running.
4888    pub fn is_replaying(&self) -> bool {
4889        self.vim.replaying
4890    }
4891
4892    /// Set or clear the dot-replay flag.
4893    pub fn set_replaying(&mut self, v: bool) {
4894        self.vim.replaying = v;
4895    }
4896
4897    // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4898
4899    /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4900    /// to Insert after the next complete command.
4901    pub fn is_one_shot_normal(&self) -> bool {
4902        self.vim.one_shot_normal
4903    }
4904
4905    /// Set or clear the Ctrl-o one-shot-normal flag.
4906    pub fn set_one_shot_normal(&mut self, v: bool) {
4907        self.vim.one_shot_normal = v;
4908    }
4909
4910    // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4911
4912    /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4913    /// `None` before any find command was executed.
4914    pub fn last_find(&self) -> Option<(char, bool, bool)> {
4915        self.vim.last_find
4916    }
4917
4918    /// Overwrite the stored last-find target.
4919    pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4920        self.vim.last_find = target;
4921    }
4922
4923    // ── Last change (dot-repeat payload) ─────────────────────────────────────
4924
4925    /// Return a clone of the last recorded mutating change, or `None` before
4926    /// any change has been made.
4927    pub fn last_change(&self) -> Option<vim::LastChange> {
4928        self.vim.last_change.clone()
4929    }
4930
4931    /// Overwrite the stored last-change record.
4932    pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4933        self.vim.last_change = lc;
4934    }
4935
4936    /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4937    /// field after the insert session completes).
4938    pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4939        self.vim.last_change.as_mut()
4940    }
4941
4942    // ── Insert session ────────────────────────────────────────────────────────
4943
4944    /// Borrow the active insert session, or `None` when not in Insert mode.
4945    pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4946        self.vim.insert_session.as_ref()
4947    }
4948
4949    /// Borrow the active insert session mutably.
4950    pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4951        self.vim.insert_session.as_mut()
4952    }
4953
4954    /// Atomically take the insert session out, leaving `None`.
4955    pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4956        self.vim.insert_session.take()
4957    }
4958
4959    /// Install a new insert session, replacing any existing one.
4960    pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4961        self.vim.insert_session = s;
4962    }
4963
4964    // ── Visual anchors ────────────────────────────────────────────────────────
4965
4966    /// Return the charwise Visual-mode anchor `(row, col)`.
4967    pub fn visual_anchor(&self) -> (usize, usize) {
4968        self.vim.visual_anchor
4969    }
4970
4971    /// Overwrite the charwise Visual-mode anchor.
4972    pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4973        self.vim.visual_anchor = anchor;
4974    }
4975
4976    /// Return the VisualLine anchor row.
4977    pub fn visual_line_anchor(&self) -> usize {
4978        self.vim.visual_line_anchor
4979    }
4980
4981    /// Overwrite the VisualLine anchor row.
4982    pub fn set_visual_line_anchor(&mut self, row: usize) {
4983        self.vim.visual_line_anchor = row;
4984    }
4985
4986    /// Return the VisualBlock anchor `(row, col)`.
4987    pub fn block_anchor(&self) -> (usize, usize) {
4988        self.vim.block_anchor
4989    }
4990
4991    /// Overwrite the VisualBlock anchor.
4992    pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4993        self.vim.block_anchor = anchor;
4994    }
4995
4996    /// Return the VisualBlock virtual column used to survive j/k row clamping.
4997    pub fn block_vcol(&self) -> usize {
4998        self.vim.block_vcol
4999    }
5000
5001    /// Overwrite the VisualBlock virtual column.
5002    pub fn set_block_vcol(&mut self, vcol: usize) {
5003        self.vim.block_vcol = vcol;
5004    }
5005
5006    // ── Yank linewise flag ────────────────────────────────────────────────────
5007
5008    /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
5009    pub fn yank_linewise(&self) -> bool {
5010        self.vim.yank_linewise
5011    }
5012
5013    /// Set or clear the linewise-yank flag.
5014    pub fn set_yank_linewise(&mut self, v: bool) {
5015        self.vim.yank_linewise = v;
5016    }
5017
5018    // ── Pending register selector ─────────────────────────────────────────────
5019    // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
5020    // Only the mutators are new here.
5021
5022    /// Overwrite the pending register selector (Phase 6.6b mutator companion to
5023    /// the existing `pending_register()` getter).
5024    pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
5025        self.vim.pending_register = reg;
5026    }
5027
5028    /// Atomically take the pending register, returning `None` afterward.
5029    pub fn take_pending_register_raw(&mut self) -> Option<char> {
5030        self.vim.pending_register.take()
5031    }
5032
5033    // ── Macro recording ───────────────────────────────────────────────────────
5034
5035    /// Return the register currently being recorded into, or `None`.
5036    pub fn recording_macro(&self) -> Option<char> {
5037        self.vim.recording_macro
5038    }
5039
5040    /// Overwrite the recording-macro target register.
5041    pub fn set_recording_macro(&mut self, reg: Option<char>) {
5042        self.vim.recording_macro = reg;
5043    }
5044
5045    /// Append one input to the in-progress macro recording buffer.
5046    pub fn push_recording_key(&mut self, input: crate::input::Input) {
5047        self.vim.recording_keys.push(input);
5048    }
5049
5050    /// Atomically take the recorded key sequence, leaving an empty vec.
5051    pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
5052        std::mem::take(&mut self.vim.recording_keys)
5053    }
5054
5055    /// Overwrite the recording-keys buffer (e.g. to seed from a register).
5056    pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
5057        self.vim.recording_keys = keys;
5058    }
5059
5060    /// Return the number of keys currently in the recording buffer.
5061    /// Useful for integration tests that verify macro-recording bookkeeping
5062    /// without draining the buffer via [`take_recording_keys`].
5063    pub fn recording_keys_len(&self) -> usize {
5064        self.vim.recording_keys.len()
5065    }
5066
5067    // ── Macro replay flag ─────────────────────────────────────────────────────
5068
5069    /// `true` while `@reg` macro replay is running (suppresses re-recording).
5070    pub fn is_replaying_macro_raw(&self) -> bool {
5071        self.vim.replaying_macro
5072    }
5073
5074    /// Set or clear the macro-replay-in-progress flag.
5075    pub fn set_replaying_macro_raw(&mut self, v: bool) {
5076        self.vim.replaying_macro = v;
5077    }
5078
5079    // ── Last macro register ───────────────────────────────────────────────────
5080
5081    /// Return the register of the most recently played macro (`@@` source).
5082    pub fn last_macro(&self) -> Option<char> {
5083        self.vim.last_macro
5084    }
5085
5086    /// Overwrite the last-played-macro register.
5087    pub fn set_last_macro(&mut self, reg: Option<char>) {
5088        self.vim.last_macro = reg;
5089    }
5090
5091    // ── Last insert position ──────────────────────────────────────────────────
5092
5093    /// Return the cursor position when Insert mode was last exited (for `gi`).
5094    pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
5095        self.vim.last_insert_pos
5096    }
5097
5098    /// Overwrite the stored last-insert position.
5099    pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
5100        self.vim.last_insert_pos = pos;
5101    }
5102
5103    // ── Last visual selection ─────────────────────────────────────────────────
5104
5105    /// Return the saved visual selection snapshot for `gv`, or `None`.
5106    pub fn last_visual(&self) -> Option<vim::LastVisual> {
5107        self.vim.last_visual
5108    }
5109
5110    /// Overwrite the saved visual selection snapshot.
5111    pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
5112        self.vim.last_visual = snap;
5113    }
5114
5115    // ── Viewport-pinned flag ──────────────────────────────────────────────────
5116
5117    /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
5118    /// the end-of-step scrolloff pass).
5119    pub fn viewport_pinned(&self) -> bool {
5120        self.vim.viewport_pinned
5121    }
5122
5123    /// Set or clear the viewport-pinned flag.
5124    pub fn set_viewport_pinned(&mut self, v: bool) {
5125        self.vim.viewport_pinned = v;
5126    }
5127
5128    // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
5129
5130    /// `true` while waiting for the register-name key after `Ctrl-R` in
5131    /// Insert mode.
5132    pub fn insert_pending_register(&self) -> bool {
5133        self.vim.insert_pending_register
5134    }
5135
5136    /// Set or clear the `Ctrl-R` register-wait flag.
5137    pub fn set_insert_pending_register(&mut self, v: bool) {
5138        self.vim.insert_pending_register = v;
5139    }
5140
5141    // ── Change-mark start ─────────────────────────────────────────────────────
5142
5143    /// Return the stashed `[` mark start for a Change operation, or `None`.
5144    pub fn change_mark_start(&self) -> Option<(usize, usize)> {
5145        self.vim.change_mark_start
5146    }
5147
5148    /// Atomically take the change-mark start, leaving `None`.
5149    pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
5150        self.vim.change_mark_start.take()
5151    }
5152
5153    /// Overwrite the change-mark start.
5154    pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
5155        self.vim.change_mark_start = pos;
5156    }
5157
5158    // ── Timeout tracking ──────────────────────────────────────────────────────
5159
5160    /// Return the wall-clock `Instant` of the last keystroke.
5161    pub fn last_input_at(&self) -> Option<std::time::Instant> {
5162        self.vim.last_input_at
5163    }
5164
5165    /// Overwrite the wall-clock last-input timestamp.
5166    pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
5167        self.vim.last_input_at = t;
5168    }
5169
5170    /// Return the `Host::now()` duration at the last keystroke.
5171    pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
5172        self.vim.last_input_host_at
5173    }
5174
5175    /// Overwrite the host-clock last-input timestamp.
5176    pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
5177        self.vim.last_input_host_at = d;
5178    }
5179
5180    // ── Search prompt ──────────────────────────────────────────────────────────
5181
5182    /// Borrow the live search prompt, or `None` when not in search-prompt mode.
5183    pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
5184        self.vim.search_prompt.as_ref()
5185    }
5186
5187    /// Borrow the live search prompt mutably.
5188    pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
5189        self.vim.search_prompt.as_mut()
5190    }
5191
5192    /// Atomically take the search prompt, leaving `None`.
5193    pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
5194        self.vim.search_prompt.take()
5195    }
5196
5197    /// Install a new search prompt (entering search-prompt mode).
5198    pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
5199        self.vim.search_prompt = prompt;
5200    }
5201
5202    // ── Last search pattern / direction ───────────────────────────────────────
5203    // Note: `last_search_forward()` getter already exists at line ~1909.
5204    // `set_last_search()` combined mutator exists at line ~1918.
5205    // Only new / complementary accessors are added here.
5206
5207    /// Return the most recently committed search pattern, or `None`.
5208    pub fn last_search_pattern(&self) -> Option<&str> {
5209        self.vim.last_search.as_deref()
5210    }
5211
5212    /// Overwrite the stored last-search pattern without changing direction
5213    /// (use the existing `set_last_search` for the combined update).
5214    pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
5215        self.vim.last_search = pattern;
5216    }
5217
5218    /// Overwrite only the last-search direction flag.
5219    pub fn set_last_search_forward_only(&mut self, forward: bool) {
5220        self.vim.last_search_forward = forward;
5221    }
5222
5223    // ── Search history ────────────────────────────────────────────────────────
5224
5225    /// Borrow the committed search-pattern history (oldest first).
5226    pub fn search_history(&self) -> &[String] {
5227        &self.vim.search_history
5228    }
5229
5230    /// Borrow the search history mutably (e.g. to push a new entry).
5231    pub fn search_history_mut(&mut self) -> &mut Vec<String> {
5232        &mut self.vim.search_history
5233    }
5234
5235    /// Return the current search-history navigation cursor index.
5236    pub fn search_history_cursor(&self) -> Option<usize> {
5237        self.vim.search_history_cursor
5238    }
5239
5240    /// Overwrite the search-history navigation cursor.
5241    pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
5242        self.vim.search_history_cursor = idx;
5243    }
5244
5245    // ── Jump lists ────────────────────────────────────────────────────────────
5246
5247    /// Borrow the back half of the jump list (entries Ctrl-o pops from).
5248    pub fn jump_back_list(&self) -> &[(usize, usize)] {
5249        &self.vim.jump_back
5250    }
5251
5252    /// Borrow the back jump list mutably (push / pop).
5253    pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
5254        &mut self.vim.jump_back
5255    }
5256
5257    /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
5258    pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
5259        &self.vim.jump_fwd
5260    }
5261
5262    /// Borrow the forward jump list mutably (push / pop / clear).
5263    pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
5264        &mut self.vim.jump_fwd
5265    }
5266
5267    // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
5268    //
5269    // `push_search_pattern`, `push_jump`, `record_search_history`, and
5270    // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
5271    // search-prompt and normal-mode FSM can call them via the public API.
5272
5273    /// Compile `pattern` into a regex and install it as the active search
5274    /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
5275    /// invalid pattern clears the highlight without raising an error.
5276    pub fn push_search_pattern(&mut self, pattern: &str) {
5277        let compiled = if pattern.is_empty() {
5278            None
5279        } else {
5280            let case_insensitive = self.settings().ignore_case
5281                && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
5282            let translated = crate::search::vim_to_rust_regex(pattern);
5283            let effective: std::borrow::Cow<'_, str> = if case_insensitive {
5284                std::borrow::Cow::Owned(format!("(?i){translated}"))
5285            } else {
5286                std::borrow::Cow::Owned(translated)
5287            };
5288            regex::Regex::new(&effective).ok()
5289        };
5290        let wrap = self.settings().wrapscan;
5291        self.set_search_pattern(compiled);
5292        self.search_state_mut().wrap_around = wrap;
5293    }
5294
5295    /// Record a pre-jump cursor position onto the back jumplist. Called
5296    /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
5297    /// committed `/` or `?`, …). Branching off the history clears the
5298    /// forward half, matching vim's "redo-is-lost" semantics.
5299    pub fn push_jump(&mut self, from: (usize, usize)) {
5300        self.vim.jump_back.push(from);
5301        if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
5302            self.vim.jump_back.remove(0);
5303        }
5304        self.vim.jump_fwd.clear();
5305    }
5306
5307    /// Push `pattern` onto the committed search history. Skips if the
5308    /// most recent entry already matches (consecutive dedupe) and trims
5309    /// the oldest entries beyond the history cap.
5310    pub fn record_search_history(&mut self, pattern: &str) {
5311        if pattern.is_empty() {
5312            return;
5313        }
5314        if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
5315            return;
5316        }
5317        self.vim.search_history.push(pattern.to_string());
5318        let len = self.vim.search_history.len();
5319        if len > vim::SEARCH_HISTORY_MAX {
5320            self.vim
5321                .search_history
5322                .drain(0..len - vim::SEARCH_HISTORY_MAX);
5323        }
5324    }
5325
5326    /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
5327    /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
5328    /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
5329    /// active search prompt.
5330    pub fn walk_search_history(&mut self, dir: isize) {
5331        if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
5332            return;
5333        }
5334        let len = self.vim.search_history.len();
5335        let next_idx = match (self.vim.search_history_cursor, dir) {
5336            (None, -1) => Some(len - 1),
5337            (None, 1) => return,
5338            (Some(i), -1) => i.checked_sub(1),
5339            (Some(i), 1) if i + 1 < len => Some(i + 1),
5340            _ => None,
5341        };
5342        let Some(idx) = next_idx else {
5343            return;
5344        };
5345        self.vim.search_history_cursor = Some(idx);
5346        let text = self.vim.search_history[idx].clone();
5347        if let Some(prompt) = self.vim.search_prompt.as_mut() {
5348            prompt.cursor = text.chars().count();
5349            prompt.text = text.clone();
5350        }
5351        self.push_search_pattern(&text);
5352    }
5353
5354    // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
5355    //
5356    // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
5357    // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
5358
5359    /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
5360    ///
5361    /// Call this at the start of every step; pass the returned
5362    /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
5363    ///
5364    /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
5365    /// Returns `Err(consumed)` when the prelude itself handled the input
5366    /// (macro-stop chord); in that case skip the FSM body and do NOT call
5367    /// `end_step` — the macro-stop path is a true short-circuit with no
5368    /// epilogue needed.
5369    ///
5370    /// This method does NOT handle the search-prompt intercept — callers
5371    /// must check `search_prompt_state().is_some()` before calling `begin_step`
5372    /// and dispatch to the search-prompt FSM body directly.
5373    pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
5374        use crate::input::Key;
5375        use vim::{Mode, Pending};
5376        // ── Timestamps ───────────────────────────────────────────────────────
5377        // Phase 7f: sync buffer before motion handlers see it.
5378        self.sync_buffer_content_from_textarea();
5379        // `:set timeoutlen` chord-timeout handling.
5380        let now = std::time::Instant::now();
5381        let host_now = self.host.now();
5382        let timed_out = match self.vim.last_input_host_at {
5383            Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
5384            None => false,
5385        };
5386        if timed_out {
5387            let chord_in_flight = !matches!(self.vim.pending, Pending::None)
5388                || self.vim.count != 0
5389                || self.vim.pending_register.is_some()
5390                || self.vim.insert_pending_register;
5391            if chord_in_flight {
5392                self.vim.clear_pending_prefix();
5393            }
5394        }
5395        self.vim.last_input_at = Some(now);
5396        self.vim.last_input_host_at = Some(host_now);
5397        // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
5398        if self.vim.recording_macro.is_some()
5399            && !self.vim.replaying_macro
5400            && matches!(self.vim.pending, Pending::None)
5401            && self.vim.mode != Mode::Insert
5402            && input.key == Key::Char('q')
5403            && !input.ctrl
5404            && !input.alt
5405        {
5406            let reg = self.vim.recording_macro.take().unwrap();
5407            let keys = std::mem::take(&mut self.vim.recording_keys);
5408            let text = crate::input::encode_macro(&keys);
5409            self.set_named_register_text(reg.to_ascii_lowercase(), text);
5410            return Err(true);
5411        }
5412        // ── Snapshots for epilogue ────────────────────────────────────────────
5413        let pending_was_macro_chord = matches!(
5414            self.vim.pending,
5415            Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
5416        );
5417        let was_insert = self.vim.mode == Mode::Insert;
5418        let pre_visual_snapshot = match self.vim.mode {
5419            Mode::Visual => Some(vim::LastVisual {
5420                mode: Mode::Visual,
5421                anchor: self.vim.visual_anchor,
5422                cursor: self.cursor(),
5423                block_vcol: 0,
5424            }),
5425            Mode::VisualLine => Some(vim::LastVisual {
5426                mode: Mode::VisualLine,
5427                anchor: (self.vim.visual_line_anchor, 0),
5428                cursor: self.cursor(),
5429                block_vcol: 0,
5430            }),
5431            Mode::VisualBlock => Some(vim::LastVisual {
5432                mode: Mode::VisualBlock,
5433                anchor: self.vim.block_anchor,
5434                cursor: self.cursor(),
5435                block_vcol: self.vim.block_vcol,
5436            }),
5437            _ => None,
5438        };
5439        Ok(StepBookkeeping {
5440            pending_was_macro_chord,
5441            was_insert,
5442            pre_visual_snapshot,
5443        })
5444    }
5445
5446    /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
5447    ///
5448    /// `input` is the same input that was passed to `begin_step`.
5449    /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
5450    /// `consumed` is the return value of the FSM body; this method returns
5451    /// it after running all epilogue invariants.
5452    ///
5453    /// Must NOT be called when `begin_step` returned `Err(...)`.
5454    pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
5455        use crate::input::Key;
5456        use vim::{Mode, Pending};
5457        let StepBookkeeping {
5458            pending_was_macro_chord,
5459            was_insert,
5460            pre_visual_snapshot,
5461        } = bk;
5462        // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
5463        if let Some(snap) = pre_visual_snapshot
5464            && !matches!(
5465                self.vim.mode,
5466                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
5467            )
5468        {
5469            let (lo, hi) = match snap.mode {
5470                Mode::Visual => {
5471                    if snap.anchor <= snap.cursor {
5472                        (snap.anchor, snap.cursor)
5473                    } else {
5474                        (snap.cursor, snap.anchor)
5475                    }
5476                }
5477                Mode::VisualLine => {
5478                    let r_lo = snap.anchor.0.min(snap.cursor.0);
5479                    let r_hi = snap.anchor.0.max(snap.cursor.0);
5480                    let vl_rope = self.buffer().rope();
5481                    let r_hi_clamped = r_hi.min(vl_rope.len_lines().saturating_sub(1));
5482                    let last_col = hjkl_buffer::rope_line_str(&vl_rope, r_hi_clamped)
5483                        .chars()
5484                        .count()
5485                        .saturating_sub(1);
5486                    ((r_lo, 0), (r_hi, last_col))
5487                }
5488                Mode::VisualBlock => {
5489                    let (r1, c1) = snap.anchor;
5490                    let (r2, c2) = snap.cursor;
5491                    ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
5492                }
5493                _ => {
5494                    if snap.anchor <= snap.cursor {
5495                        (snap.anchor, snap.cursor)
5496                    } else {
5497                        (snap.cursor, snap.anchor)
5498                    }
5499                }
5500            };
5501            self.set_mark('<', lo);
5502            self.set_mark('>', hi);
5503            self.vim.last_visual = Some(snap);
5504        }
5505        // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
5506        if !was_insert
5507            && self.vim.one_shot_normal
5508            && self.vim.mode == Mode::Normal
5509            && matches!(self.vim.pending, Pending::None)
5510        {
5511            self.vim.one_shot_normal = false;
5512            self.vim.mode = Mode::Insert;
5513        }
5514        // ── Content + viewport sync ───────────────────────────────────────────
5515        self.sync_buffer_content_from_textarea();
5516        if !self.vim.viewport_pinned {
5517            self.ensure_cursor_in_scrolloff();
5518        }
5519        self.vim.viewport_pinned = false;
5520        // ── Recorder hook ─────────────────────────────────────────────────────
5521        if self.vim.recording_macro.is_some()
5522            && !self.vim.replaying_macro
5523            && input.key != Key::Char('q')
5524            && !pending_was_macro_chord
5525        {
5526            self.vim.recording_keys.push(input);
5527        }
5528        // ── Phase 6.3: current_mode sync ─────────────────────────────────────
5529        self.vim.current_mode = self.vim.public_mode();
5530        consumed
5531    }
5532
5533    // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
5534
5535    /// `true` when the editor is in any visual mode (Visual / VisualLine /
5536    /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
5537    pub fn is_visual(&self) -> bool {
5538        matches!(
5539            self.vim.mode,
5540            vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
5541        )
5542    }
5543
5544    /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
5545    /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
5546    /// virtual column, which survives j/k clamping to shorter rows).
5547    ///
5548    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
5549    /// extents needed for VisualBlock `I` / `A` / `r` without accessing
5550    /// engine-private helpers.
5551    pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
5552        let (ar, ac) = self.vim.block_anchor;
5553        let (cr, _) = self.cursor();
5554        let cc = self.vim.block_vcol;
5555        let top = ar.min(cr);
5556        let bot = ar.max(cr);
5557        let left = ac.min(cc);
5558        let right = ac.max(cc);
5559        (top, bot, left, right)
5560    }
5561
5562    /// Return the character count (code-point count) of line `row`, or `0`
5563    /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
5564    /// I / A column computations.
5565    pub fn line_char_count(&self, row: usize) -> usize {
5566        buf_line_chars(&self.buffer, row)
5567    }
5568
5569    /// Apply operator over `motion` with `count` repetitions. The full
5570    /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
5571    ///
5572    /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
5573    /// relocated `handle_after_op` can call it directly with a parsed `Motion`
5574    /// without re-entering the engine FSM.
5575    pub fn apply_op_with_motion_direct(
5576        &mut self,
5577        op: crate::vim::Operator,
5578        motion: &crate::vim::Motion,
5579        count: usize,
5580    ) {
5581        vim::apply_op_with_motion(self, op, motion, count);
5582    }
5583
5584    /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
5585    /// `delta = 1` increments; `delta = -1` decrements; larger deltas
5586    /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
5587    /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
5588    pub fn adjust_number(&mut self, delta: i64) {
5589        vim::adjust_number(self, delta);
5590    }
5591
5592    /// Open the `/` or `?` search prompt. `forward = true` for `/`,
5593    /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
5594    /// dispatch `/` and `?` without re-entering the engine FSM.
5595    pub fn enter_search(&mut self, forward: bool) {
5596        vim::enter_search(self, forward);
5597    }
5598
5599    /// Enter Insert mode at the left edge of a VisualBlock selection for
5600    /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
5601    /// then begins an insert session with `InsertReason::BlockEdge`.
5602    ///
5603    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5604    /// VisualBlock `I` command without accessing engine-private helpers.
5605    pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
5606        self.jump_cursor(top, col);
5607        self.vim.mode = vim::Mode::Normal;
5608        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5609    }
5610
5611    /// Enter Insert mode at the right edge of a VisualBlock selection for
5612    /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
5613    /// then begins an insert session with `InsertReason::BlockEdge`.
5614    ///
5615    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5616    /// VisualBlock `A` command without accessing engine-private helpers.
5617    pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
5618        self.jump_cursor(top, col);
5619        self.vim.mode = vim::Mode::Normal;
5620        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5621    }
5622
5623    /// Execute a motion (cursor movement), push to the jumplist for big jumps,
5624    /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
5625    /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5626    pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
5627        vim::execute_motion(self, motion, count);
5628    }
5629
5630    /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
5631    /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
5632    /// non-h/l motions leave it alone so the intended column survives clamping
5633    /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5634    pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
5635        vim::update_block_vcol(self, motion);
5636    }
5637
5638    /// Apply `op` over the current visual selection (char-wise, linewise, or
5639    /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
5640    /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
5641    pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
5642        vim::apply_visual_operator(self, op);
5643    }
5644
5645    /// Replace each character cell in the current VisualBlock selection with
5646    /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
5647    /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
5648    pub fn replace_block_char(&mut self, ch: char) {
5649        vim::block_replace(self, ch);
5650    }
5651
5652    /// Extend the current visual selection to cover the text object identified
5653    /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
5654    /// via `text_object_range`, then updates the visual anchor and cursor.
5655    ///
5656    /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
5657    /// in `hjkl-vim::normal::handle_visual_text_obj`.
5658    pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
5659        use crate::vim::{Mode, TextObject};
5660        let obj = match ch {
5661            'w' => TextObject::Word { big: false },
5662            'W' => TextObject::Word { big: true },
5663            '"' | '\'' | '`' => TextObject::Quote(ch),
5664            '(' | ')' | 'b' => TextObject::Bracket('('),
5665            '[' | ']' => TextObject::Bracket('['),
5666            '{' | '}' | 'B' => TextObject::Bracket('{'),
5667            '<' | '>' => TextObject::Bracket('<'),
5668            'p' => TextObject::Paragraph,
5669            't' => TextObject::XmlTag,
5670            's' => TextObject::Sentence,
5671            _ => return,
5672        };
5673        let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
5674            return;
5675        };
5676        match kind {
5677            crate::vim::RangeKind::Linewise => {
5678                self.vim.visual_line_anchor = start.0;
5679                self.vim.mode = Mode::VisualLine;
5680                self.vim.current_mode = VimMode::VisualLine;
5681                self.jump_cursor(end.0, 0);
5682            }
5683            _ => {
5684                self.vim.mode = Mode::Visual;
5685                self.vim.current_mode = VimMode::Visual;
5686                self.vim.visual_anchor = (start.0, start.1);
5687                let (er, ec) = vim::retreat_one(self, end);
5688                self.jump_cursor(er, ec);
5689            }
5690        }
5691    }
5692}
5693
5694/// Visual column of the character at `char_col` in `line`, treating `\t`
5695/// as expansion to the next `tab_width` stop and every other char as
5696/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
5697/// the cursor math elsewhere also assumes single-cell chars.
5698fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
5699    let mut visual = 0usize;
5700    for (i, ch) in line.chars().enumerate() {
5701        if i >= char_col {
5702            break;
5703        }
5704        if ch == '\t' {
5705            visual += tab_width - (visual % tab_width);
5706        } else {
5707            visual += 1;
5708        }
5709    }
5710    visual
5711}
5712
5713#[cfg(test)]
5714mod shift_syntax_spans_tests {
5715    use super::*;
5716    use crate::types::{ContentEdit, DefaultHost, Options, Style};
5717    use hjkl_buffer::Buffer;
5718
5719    fn ed_with_spans(line_count: usize) -> Editor<Buffer, DefaultHost> {
5720        let text = (0..line_count)
5721            .map(|i| format!("row{i}"))
5722            .collect::<Vec<_>>()
5723            .join("\n");
5724        let buf = Buffer::from_str(&text);
5725        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
5726        // Synthesize span rows so we can detect which survive a shift.
5727        // Use a distinct fg colour per row so spans are identifiable.
5728        let style = Style::default();
5729        let spans: Vec<Vec<(usize, usize, Style)>> =
5730            (0..line_count).map(|_| vec![(0, 1, style)]).collect();
5731        e.install_syntax_spans(spans);
5732        e
5733    }
5734
5735    fn edit_insert_newline_at(row: u32, col: u32) -> ContentEdit {
5736        // Pressing Enter: zero-width insertion that produces one new row.
5737        ContentEdit {
5738            start_byte: 0,
5739            old_end_byte: 0,
5740            new_end_byte: 1,
5741            start_position: (row, col),
5742            old_end_position: (row, col),
5743            new_end_position: (row + 1, 0),
5744        }
5745    }
5746
5747    fn edit_join_rows(row: u32, col: u32) -> ContentEdit {
5748        // Backspace at start of `row+1`: removes the newline, joining the
5749        // two rows. old_end is on `row+1`, new_end on `row`.
5750        ContentEdit {
5751            start_byte: 0,
5752            old_end_byte: 1,
5753            new_end_byte: 0,
5754            start_position: (row, col),
5755            old_end_position: (row + 1, 0),
5756            new_end_position: (row, col),
5757        }
5758    }
5759
5760    #[test]
5761    fn insert_grows_buffer_spans_in_place() {
5762        let mut e = ed_with_spans(4);
5763        // Newline at row 1 → buffer grew by one row.
5764        e.shift_syntax_spans_for_edits(&[edit_insert_newline_at(1, 1)]);
5765        assert_eq!(
5766            e.buffer_spans().len(),
5767            5,
5768            "row-count grew → spans rows must match"
5769        );
5770        // The empty row should be at index 2 (right after the split point).
5771        assert!(e.buffer_spans()[2].is_empty(), "inserted row sits at oer+1");
5772        // Surrounding rows kept their content.
5773        assert!(!e.buffer_spans()[0].is_empty());
5774        assert!(!e.buffer_spans()[1].is_empty());
5775        assert!(!e.buffer_spans()[3].is_empty());
5776        assert!(!e.buffer_spans()[4].is_empty());
5777    }
5778
5779    #[test]
5780    fn delete_shrinks_buffer_spans_in_place() {
5781        let mut e = ed_with_spans(4);
5782        e.shift_syntax_spans_for_edits(&[edit_join_rows(1, 1)]);
5783        assert_eq!(
5784            e.buffer_spans().len(),
5785            3,
5786            "row-count shrank → spans rows must match"
5787        );
5788    }
5789
5790    #[test]
5791    fn same_row_edit_leaves_rows_untouched() {
5792        let mut e = ed_with_spans(3);
5793        let edit = ContentEdit {
5794            start_byte: 0,
5795            old_end_byte: 0,
5796            new_end_byte: 1,
5797            start_position: (1, 0),
5798            old_end_position: (1, 0),
5799            new_end_position: (1, 1),
5800        };
5801        e.shift_syntax_spans_for_edits(&[edit]);
5802        assert_eq!(e.buffer_spans().len(), 3);
5803        for row in 0..3 {
5804            assert!(
5805                !e.buffer_spans()[row].is_empty(),
5806                "row {row} should still hold its span"
5807            );
5808        }
5809    }
5810
5811    #[test]
5812    fn ordered_edits_apply_against_prior_state() {
5813        let mut e = ed_with_spans(3);
5814        // Two consecutive inserts: each adds a row.
5815        e.shift_syntax_spans_for_edits(&[
5816            edit_insert_newline_at(0, 1),
5817            edit_insert_newline_at(1, 1),
5818        ]);
5819        assert_eq!(e.buffer_spans().len(), 5);
5820    }
5821
5822    /// Build a buffer with `line_count` rows where row `i` has a span at
5823    /// column `i + 1` so the rows are independently identifiable after a
5824    /// shift (otherwise all spans look identical and can't tell which
5825    /// original row's spans landed at which post-shift index).
5826    fn ed_with_distinguishable_spans(line_count: usize) -> Editor<Buffer, DefaultHost> {
5827        let text = (0..line_count)
5828            .map(|i| format!("rowwwwwwwwww{i}"))
5829            .collect::<Vec<_>>()
5830            .join("\n");
5831        let buf = Buffer::from_str(&text);
5832        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
5833        let style = Style::default();
5834        let spans: Vec<Vec<(usize, usize, Style)>> = (0..line_count)
5835            .map(|i| vec![(i + 1, i + 2, style)])
5836            .collect();
5837        e.install_syntax_spans(spans);
5838        e
5839    }
5840
5841    /// Regression for off-by-one in `shift_syntax_spans_for_edits`.
5842    ///
5843    /// `P` (paste-before) at column 0 of row 0 inserts new lines BEFORE
5844    /// row 0. The pre-paste rows should shift down by N. The fix inserts
5845    /// empty rows at idx `start.row` (not `oer + 1`) when `start.col == 0`.
5846    ///
5847    /// Symptom before the fix: row 0's spans stayed at idx 0 after a
5848    /// 4-row `ggP`, but the file's row 0 was now the pasted content (no
5849    /// spans available yet). Display: pasted row 0 painted with the
5850    /// pre-paste row 0's spans (LUCKILY identical content in many cases)
5851    /// while the *shifted* pre-paste row 0 (now at file row 4) painted
5852    /// with the pre-paste row 1's spans — visible as the WRONG row
5853    /// showing the wrong-row colours.
5854    #[test]
5855    fn shift_for_paste_at_start_of_row_zero() {
5856        let mut e = ed_with_distinguishable_spans(7);
5857        // Snapshot: row i has a span at col (i+1, i+2).
5858        let pre = e.buffer_spans().to_vec();
5859        // P at (0, 0) inserting 4 lines.
5860        let edit = ContentEdit {
5861            start_byte: 0,
5862            old_end_byte: 0,
5863            new_end_byte: 4,
5864            start_position: (0, 0),
5865            old_end_position: (0, 0),
5866            new_end_position: (4, 0),
5867        };
5868        e.shift_syntax_spans_for_edits(&[edit]);
5869        assert_eq!(e.buffer_spans().len(), 11, "row count grew by 4");
5870        // Rows 0..4 are the new pasted lines — should be EMPTY placeholders.
5871        for row in 0..4 {
5872            assert!(
5873                e.buffer_spans()[row].is_empty(),
5874                "row {row} (new paste) must be empty placeholder, got {:?}",
5875                e.buffer_spans()[row]
5876            );
5877        }
5878        // Rows 4..11 are the original rows 0..7 shifted down by 4.
5879        for (orig_row, orig_spans) in pre.iter().enumerate() {
5880            let new_row = orig_row + 4;
5881            assert_eq!(
5882                &e.buffer_spans()[new_row],
5883                orig_spans,
5884                "original row {orig_row} should be at file row {new_row} after \
5885                 paste-before-row-0"
5886            );
5887        }
5888    }
5889
5890    /// Same idea for paste at start of a non-zero row: `2GP` inserts 3
5891    /// lines before row 2.
5892    #[test]
5893    fn shift_for_paste_at_start_of_middle_row() {
5894        let mut e = ed_with_distinguishable_spans(5);
5895        let pre = e.buffer_spans().to_vec();
5896        // Insert 3 lines at (2, 0).
5897        let edit = ContentEdit {
5898            start_byte: 0,
5899            old_end_byte: 0,
5900            new_end_byte: 3,
5901            start_position: (2, 0),
5902            old_end_position: (2, 0),
5903            new_end_position: (5, 0),
5904        };
5905        e.shift_syntax_spans_for_edits(&[edit]);
5906        assert_eq!(e.buffer_spans().len(), 8);
5907        // Rows 0..2 unchanged (before the insertion point).
5908        assert_eq!(e.buffer_spans()[0], pre[0]);
5909        assert_eq!(e.buffer_spans()[1], pre[1]);
5910        // Rows 2..5 are new pasted lines.
5911        for row in 2..5 {
5912            assert!(
5913                e.buffer_spans()[row].is_empty(),
5914                "row {row} must be empty placeholder"
5915            );
5916        }
5917        // Rows 5..8 are originals 2..5 shifted down by 3.
5918        for (orig_row, orig_spans) in pre.iter().enumerate().take(5).skip(2) {
5919            let new_row = orig_row + 3;
5920            assert_eq!(
5921                &e.buffer_spans()[new_row],
5922                orig_spans,
5923                "original row {orig_row} should land at file row {new_row}"
5924            );
5925        }
5926    }
5927
5928    /// Regression: pasting N rows at the beginning of the buffer used to
5929    /// run `Vec::insert(0, ...)` once per row → O(N²) memmove. samply
5930    /// showed this path eating 87 % of paste CPU on a 60 k-row paste.
5931    /// The splice rewrite is O(N).
5932    ///
5933    /// Asserting a hard wall-clock bound is brittle on slow CI, so we
5934    /// pick a budget the old code blows past by >10×: 60 k rows in
5935    /// under 200 ms even on a debug build. Old impl: ~3-5 seconds.
5936    #[test]
5937    fn shift_for_60k_row_paste_at_row_zero_is_under_200ms() {
5938        let mut e = ed_with_distinguishable_spans(8);
5939        let edit = ContentEdit {
5940            start_byte: 0,
5941            old_end_byte: 0,
5942            new_end_byte: 60_000,
5943            start_position: (0, 0),
5944            old_end_position: (0, 0),
5945            new_end_position: (60_000, 0),
5946        };
5947        let t = std::time::Instant::now();
5948        e.shift_syntax_spans_for_edits(&[edit]);
5949        let elapsed = t.elapsed();
5950        assert!(
5951            elapsed.as_millis() < 200,
5952            "60k-row shift took {elapsed:?}; budget is 200 ms (catches \
5953             reintroduction of the O(N²) per-row insert loop)"
5954        );
5955        assert_eq!(e.buffer_spans().len(), 60_008);
5956    }
5957
5958    /// Regression: `push_undo` used to clone every line into a
5959    /// `Vec<String>` (162 k heap allocations on a 162 k-row buffer per
5960    /// snapshot). Now stores an `Arc<String>` shared with
5961    /// `Buffer::content_joined`'s per-dirty_gen cache — a warm snapshot
5962    /// is an `Arc::clone` (one ptr bump).
5963    ///
5964    /// Test: snapshot a 60 k-row buffer 100 times. With the Arc impl
5965    /// this is essentially free (one join then 99 Arc::clones). The
5966    /// old `Vec<String>` impl required 60 k allocations per call =
5967    /// 6 M allocations, easily seconds even on release.
5968    #[test]
5969    fn push_undo_snapshot_arc_clone_is_under_100ms_for_100_snapshots() {
5970        use crate::types::{DefaultHost, Options};
5971        let text = "x\n".repeat(60_000);
5972        let buf = hjkl_buffer::Buffer::from_str(&text);
5973        let mut e = Editor::new(buf, DefaultHost::default(), Options::default());
5974        // Warm the cache: one join, subsequent snapshots Arc::clone it.
5975        e.push_undo();
5976        let t = std::time::Instant::now();
5977        for _ in 0..100 {
5978            e.push_undo();
5979        }
5980        let elapsed = t.elapsed();
5981        assert!(
5982            elapsed.as_millis() < 100,
5983            "100 snapshots of a 60k-row buffer took {elapsed:?}; budget \
5984             100 ms. Likely regressed to per-line cloning."
5985        );
5986    }
5987}
5988
5989#[cfg(test)]
5990mod insert_mode_scrolloff_tests {
5991    use super::*;
5992    use crate::types::{DefaultHost, Host, Options};
5993    use crate::vim::Mode;
5994    use hjkl_buffer::Buffer;
5995
5996    fn ed_with_lines(line_count: usize) -> Editor<Buffer, DefaultHost> {
5997        let text = (0..line_count)
5998            .map(|i| format!("row{i}"))
5999            .collect::<Vec<_>>()
6000            .join("\n");
6001        let buf = Buffer::from_str(&text);
6002        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
6003        // Viewport: 20 rows tall, starts at top.
6004        let vp = e.host_mut().viewport_mut();
6005        vp.width = 80;
6006        vp.height = 20;
6007        vp.top_row = 0;
6008        vp.top_col = 0;
6009        e.set_viewport_height(20);
6010        e.vim.mode = Mode::Insert;
6011        e
6012    }
6013
6014    /// Regression: holding Enter in insert mode used to scroll the cursor
6015    /// off the viewport because `insert_newline` (called from the app's
6016    /// `dispatch_insert_key`) bypasses the FSM `step` that runs
6017    /// `ensure_cursor_in_scrolloff`. The post-mutation helper now runs
6018    /// scrolloff for every insert primitive — the cursor must stay
6019    /// within `SCROLLOFF` rows of the bottom edge.
6020    #[test]
6021    fn insert_newline_keeps_cursor_in_scrolloff() {
6022        let mut e = ed_with_lines(200);
6023        // Park cursor at the bottom edge of the viewport (row 19).
6024        e.set_cursor_doc(19, 0);
6025        // Press Enter 50 times. Cursor moves down each newline; without
6026        // scrolloff the cursor would slide off the bottom of the
6027        // viewport at row 20+ and the user would type blind.
6028        for _ in 0..50 {
6029            e.insert_newline();
6030        }
6031        let (cursor_row, _) = e.cursor();
6032        let vp = e.host().viewport();
6033        let cursor_screen_row = cursor_row.saturating_sub(vp.top_row);
6034        let margin = Editor::<Buffer, DefaultHost>::SCROLLOFF.min(vp.height as usize - 1) / 2;
6035        let max_screen_row = vp.height as usize - 1 - margin;
6036        assert!(
6037            cursor_screen_row <= max_screen_row,
6038            "cursor screen row {cursor_screen_row} exceeded scrolloff bound {max_screen_row} \
6039             (cursor_row={cursor_row}, vp.top_row={vp_top}, vp.height={vp_h})",
6040            vp_top = vp.top_row,
6041            vp_h = vp.height,
6042        );
6043    }
6044
6045    /// Same check for `insert_arrow(Down)` — cursor-only motion that also
6046    /// must trigger scrolloff.
6047    #[test]
6048    fn insert_arrow_down_keeps_cursor_in_scrolloff() {
6049        let mut e = ed_with_lines(200);
6050        e.set_cursor_doc(19, 0);
6051        for _ in 0..50 {
6052            e.insert_arrow(vim::InsertDir::Down);
6053        }
6054        let (cursor_row, _) = e.cursor();
6055        let vp = e.host().viewport();
6056        let cursor_screen_row = cursor_row.saturating_sub(vp.top_row);
6057        let margin = Editor::<Buffer, DefaultHost>::SCROLLOFF.min(vp.height as usize - 1) / 2;
6058        let max_screen_row = vp.height as usize - 1 - margin;
6059        assert!(
6060            cursor_screen_row <= max_screen_row,
6061            "cursor screen row {cursor_screen_row} exceeded scrolloff bound {max_screen_row}"
6062        );
6063    }
6064}