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