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        // Screen rows ≠ doc rows only under soft-wrap (a doc row spans many
2890        // screen lines) or folds (a closed fold collapses many doc rows to
2891        // one); doc-row margin math drifts in those cases. Dispatch:
2892        //   • wrap            → the incremental screen-row walk.
2893        //   • folds, no wrap  → the O(height) fold-aware clamp below.
2894        //   • neither         → the fast O(1) doc-row math (every plain j/k/G).
2895        let wrapped = !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None);
2896        if wrapped {
2897            self.ensure_scrolloff_vertical(height, margin);
2898            return;
2899        }
2900        if !self.buffer.folds().is_empty() {
2901            self.ensure_scrolloff_folds_nowrap(height, margin);
2902            // Column-side (horizontal) scroll only — keep the fold-aware
2903            // top_row by snapshotting it across `ensure_visible`.
2904            let cursor = buf_cursor_pos(&self.buffer);
2905            let saved_top = self.host.viewport().top_row;
2906            self.host.viewport_mut().ensure_visible(cursor);
2907            self.host.viewport_mut().top_row = saved_top;
2908            return;
2909        }
2910        let cursor_row = buf_cursor_row(&self.buffer);
2911        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2912        let v = self.host.viewport_mut();
2913        // Top edge: cursor_row should sit at >= top_row + margin.
2914        if cursor_row < v.top_row + margin {
2915            v.top_row = cursor_row.saturating_sub(margin);
2916        }
2917        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2918        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2919        if cursor_row > v.top_row + max_bottom {
2920            v.top_row = cursor_row.saturating_sub(max_bottom);
2921        }
2922        // Clamp top_row so we never scroll past the buffer's bottom.
2923        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2924        if v.top_row > max_top {
2925            v.top_row = max_top;
2926        }
2927        // Column-side scroll (vim default `sidescrolloff = 0`).
2928        let cursor = buf_cursor_pos(&self.buffer);
2929        self.host.viewport_mut().ensure_visible(cursor);
2930    }
2931
2932    /// Fold-aware vertical scrolloff for `Wrap::None`, in **O(height)**.
2933    ///
2934    /// A closed fold collapses its body to one screen row, so the cursor's
2935    /// screen row is the count of *visible* rows above it — not the doc-row
2936    /// delta. Instead of re-walking that count on every candidate `top_row`
2937    /// (the incremental [`Self::ensure_scrolloff_vertical`], O(n²) on a big
2938    /// jump like `G` over a fold-heavy file), compute the valid `top_row`
2939    /// window directly: at most `height-1-margin` visible rows may sit above
2940    /// the cursor (bottom edge) and at least `margin` (top edge). Walk those
2941    /// two bounds up from the cursor via `prev_visible_row`, clamp the current
2942    /// `top_row` into the window, then clamp to `max_top_for_height` so the
2943    /// buffer's bottom never leaves blank rows. Each walk is bounded by
2944    /// `height`, so the whole thing is O(height) regardless of jump distance.
2945    fn ensure_scrolloff_folds_nowrap(&mut self, height: usize, margin: usize) {
2946        let cursor_row = buf_cursor_row(&self.buffer);
2947        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2948        // `top_lo`: the row `max_csr` visible rows above the cursor — `top_row`
2949        // must be >= this to keep the cursor within the bottom margin.
2950        let mut top_lo = cursor_row;
2951        for _ in 0..max_csr {
2952            match self.buffer.prev_visible_row(top_lo) {
2953                Some(p) => top_lo = p,
2954                None => break,
2955            }
2956        }
2957        // `top_hi`: the row `margin` visible rows above the cursor — `top_row`
2958        // must be <= this to keep the cursor below the top margin.
2959        let mut top_hi = cursor_row;
2960        for _ in 0..margin {
2961            match self.buffer.prev_visible_row(top_hi) {
2962                Some(p) => top_hi = p,
2963                None => break,
2964            }
2965        }
2966        // `max_csr >= margin` (margin is capped at (height-1)/2), so
2967        // `top_lo <= top_hi` and the clamp range is well-formed.
2968        let cur = self.host.viewport().top_row;
2969        let mut new_top = cur.clamp(top_lo, top_hi);
2970        let max_top = {
2971            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2972            crate::viewport_math::max_top_for_height(
2973                &self.buffer,
2974                &folds,
2975                self.host.viewport(),
2976                height,
2977            )
2978        };
2979        if new_top > max_top {
2980            new_top = max_top;
2981        }
2982        self.host.viewport_mut().top_row = new_top;
2983    }
2984
2985    /// Screen-row-aware vertical scrolloff. Walks `top_row` one visible
2986    /// doc row at a time so the cursor's *screen* row stays inside
2987    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2988    /// buffer's bottom never leaves blank rows below it.
2989    ///
2990    /// Correct under BOTH soft-wrap (a doc row spans many screen lines)
2991    /// and folds (a closed fold collapses many doc rows to one screen
2992    /// row): [`crate::viewport_math::cursor_screen_row_from`] counts
2993    /// visible/wrapped screen rows, so doc-row arithmetic can't drift the
2994    /// margin around a fold. Horizontal (column) scroll is the caller's
2995    /// job — this only moves `top_row`.
2996    fn ensure_scrolloff_vertical(&mut self, height: usize, margin: usize) {
2997        let cursor_row = buf_cursor_row(&self.buffer);
2998        // Step 1 — cursor above viewport: snap top to cursor row,
2999        // then we'll fix up the margin below.
3000        if cursor_row < self.host.viewport().top_row {
3001            let v = self.host.viewport_mut();
3002            v.top_row = cursor_row;
3003            v.top_col = 0;
3004        }
3005        // Step 2 — push top forward until cursor's screen row is
3006        // within the bottom margin (`csr <= height - 1 - margin`).
3007        // 0.0.33 (Patch C-γ): fold-iteration goes through the
3008        // [`crate::types::FoldProvider`] surface via
3009        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
3010        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
3011        // a `&Viewport` parameter; the host owns the viewport, so the
3012        // disjoint `(self.host, self.buffer)` borrows split cleanly.
3013        let max_csr = height.saturating_sub(1).saturating_sub(margin);
3014        loop {
3015            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
3016            let top = self.host.viewport().top_row;
3017            let csr = crate::viewport_math::cursor_screen_row_from(
3018                &self.buffer,
3019                &folds,
3020                self.host.viewport(),
3021                top,
3022            )
3023            .unwrap_or(0);
3024            if csr <= max_csr {
3025                break;
3026            }
3027            let row_count = buf_row_count(&self.buffer);
3028            let next = {
3029                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
3030                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
3031            };
3032            let Some(next) = next else {
3033                break;
3034            };
3035            // Don't walk past the cursor's row.
3036            if next > cursor_row {
3037                self.host.viewport_mut().top_row = cursor_row;
3038                break;
3039            }
3040            self.host.viewport_mut().top_row = next;
3041        }
3042        // Step 3 — pull top backward until cursor's screen row is
3043        // past the top margin (`csr >= margin`).
3044        loop {
3045            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
3046            let top = self.host.viewport().top_row;
3047            let csr = crate::viewport_math::cursor_screen_row_from(
3048                &self.buffer,
3049                &folds,
3050                self.host.viewport(),
3051                top,
3052            )
3053            .unwrap_or(0);
3054            if csr >= margin {
3055                break;
3056            }
3057            let prev = {
3058                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
3059                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
3060            };
3061            let Some(prev) = prev else {
3062                break;
3063            };
3064            self.host.viewport_mut().top_row = prev;
3065        }
3066        // Step 4 — clamp top so the buffer's bottom doesn't leave
3067        // blank rows below it. `max_top_for_height` walks segments
3068        // backward from the last row until it accumulates `height`
3069        // screen rows.
3070        let max_top = {
3071            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
3072            crate::viewport_math::max_top_for_height(
3073                &self.buffer,
3074                &folds,
3075                self.host.viewport(),
3076                height,
3077            )
3078        };
3079        if self.host.viewport().top_row > max_top {
3080            self.host.viewport_mut().top_row = max_top;
3081        }
3082        self.host.viewport_mut().top_col = 0;
3083    }
3084
3085    fn scroll_viewport(&mut self, delta: i16) {
3086        if delta == 0 {
3087            return;
3088        }
3089        // Bump the host viewport's top within bounds.
3090        let total_rows = buf_row_count(&self.buffer) as isize;
3091        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
3092        let cur_top = self.host.viewport().top_row as isize;
3093        let new_top = (cur_top + delta as isize)
3094            .max(0)
3095            .min((total_rows - 1).max(0)) as usize;
3096        self.host.viewport_mut().top_row = new_top;
3097        // Mirror to textarea so its viewport reads (still consumed by
3098        // a couple of helpers) stay accurate.
3099        let _ = cur_top;
3100        if height == 0 {
3101            return;
3102        }
3103        // Apply scrolloff: keep the cursor at least scrolloff rows
3104        // from the visible viewport edges.
3105        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
3106        let margin = self.settings.scrolloff.min(height / 2);
3107        let min_row = new_top + margin;
3108        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
3109        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
3110        if target_row != cursor_row {
3111            let line_len = buf_line(&self.buffer, target_row)
3112                .map(|l| l.chars().count())
3113                .unwrap_or(0);
3114            let target_col = cursor_col.min(line_len.saturating_sub(1));
3115            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
3116        }
3117    }
3118
3119    pub fn goto_line(&mut self, line: usize) {
3120        let row = line.saturating_sub(1);
3121        let max = buf_row_count(&self.buffer).saturating_sub(1);
3122        let target = row.min(max);
3123        // If the target row is hidden inside one or more closed folds, open
3124        // every fold that collapses it so the landing line is actually
3125        // visible — a jump to an unseen row is useless. `reveal_row` opens
3126        // all hiding folds (outer + nested) in one pass; `open_fold_at` /
3127        // `FoldOp::OpenAt` can't, because they only act on the first fold
3128        // containing the row and so can never reach a nested inner fold.
3129        self.buffer.reveal_row(target);
3130        buf_set_cursor_rc(&mut self.buffer, target, 0);
3131        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
3132        // the cursor lands off-screen and the user has to scroll
3133        // manually to see it.
3134        self.ensure_cursor_in_scrolloff();
3135    }
3136
3137    /// Scroll so the cursor row lands at the given viewport position:
3138    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
3139    /// Cursor stays on its absolute line; only the viewport moves.
3140    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
3141        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
3142        if height == 0 {
3143            return;
3144        }
3145        let cur_row = buf_cursor_row(&self.buffer);
3146        let cur_top = self.host.viewport().top_row;
3147        // Scrolloff awareness: `zt` lands the cursor at the top edge
3148        // of the viable area (top + margin), `zb` at the bottom edge
3149        // (top + height - 1 - margin). Match the cap used by
3150        // `ensure_cursor_in_scrolloff` so contradictory bounds are
3151        // impossible on tiny viewports.
3152        let margin = self.settings.scrolloff.min(height.saturating_sub(1) / 2);
3153        let new_top = match pos {
3154            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
3155            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
3156            CursorScrollTarget::Bottom => {
3157                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
3158            }
3159        };
3160        if new_top == cur_top {
3161            return;
3162        }
3163        self.host.viewport_mut().top_row = new_top;
3164    }
3165
3166    /// Jump the cursor to the given 1-based line/column, clamped to the document.
3167    pub fn jump_to(&mut self, line: usize, col: usize) {
3168        let r = line.saturating_sub(1);
3169        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
3170        let r = r.min(max_row);
3171        let line_len = buf_line(&self.buffer, r)
3172            .map(|l| l.chars().count())
3173            .unwrap_or(0);
3174        let c = col.saturating_sub(1).min(line_len);
3175        buf_set_cursor_rc(&mut self.buffer, r, c);
3176    }
3177
3178    // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
3179    //
3180    // These primitives operate on document (row, col) coordinates that the HOST
3181    // computes from its own layout knowledge (cell geometry for the TUI host,
3182    // pixel geometry for the future GUI host). The engine has no u16 terminal
3183    // assumption here — it just moves the cursor in doc-space.
3184
3185    /// Set the cursor to the given doc-space `(row, col)`, clamped to the
3186    /// document bounds. Hosts use this for programmatic cursor placement and
3187    /// as the building block for the mouse-click path.
3188    ///
3189    /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
3190    /// position); values beyond that are clamped to `char_count`.
3191    pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
3192        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
3193        let r = row.min(max_row);
3194        let line_len = buf_line(&self.buffer, r)
3195            .map(|l| l.chars().count())
3196            .unwrap_or(0);
3197        let c = col.min(line_len);
3198        buf_set_cursor_rc(&mut self.buffer, r, c);
3199    }
3200
3201    /// Handle a left-button click at doc-space `(row, col)`.
3202    ///
3203    /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
3204    /// parity for `undo_break_on_motion`), then moves the cursor. The host
3205    /// performs cell→doc or pixel→doc translation before calling this.
3206    ///
3207    /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
3208    /// cursor lives on chars and never on the implicit `\n` — `col` is
3209    /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
3210    /// allows the one-past-EOL insert position (`col == chars().count()`).
3211    ///
3212    /// Resets `sticky_col` to the clicked column so the next `j`/`k`
3213    /// motion uses the clicked column as the intended visual column
3214    /// (otherwise the cursor would snap back to the keyboard-tracked
3215    /// column on the first vertical motion after a click).
3216    pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
3217        if self.vim.is_visual() {
3218            self.vim.force_normal();
3219        }
3220        // Mouse-position click counts as a motion — break the active
3221        // insert-mode undo group when the toggle is on (vim parity).
3222        crate::vim::break_undo_group_in_insert(self);
3223
3224        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
3225        let r = row.min(max_row);
3226        let line_len = buf_line(&self.buffer, r)
3227            .map(|l| l.chars().count())
3228            .unwrap_or(0);
3229        let cap = if self.vim.current_mode == crate::VimMode::Insert {
3230            line_len
3231        } else {
3232            line_len.saturating_sub(1)
3233        };
3234        let c = col.min(cap);
3235        buf_set_cursor_rc(&mut self.buffer, r, c);
3236        self.sticky_col = Some(c);
3237    }
3238
3239    /// Begin a mouse-drag selection: anchor at the current cursor and enter
3240    /// Visual-char mode. Idempotent if already in Visual-char mode.
3241    pub fn mouse_begin_drag(&mut self) {
3242        if !self.vim.is_visual_char() {
3243            vim::enter_visual_char_bridge(self);
3244        }
3245    }
3246
3247    /// Extend an in-progress mouse drag to doc-space `(row, col)`.
3248    ///
3249    /// Moves the live cursor; the Visual anchor stays where
3250    /// [`Editor::mouse_begin_drag`] set it. Call after the host has
3251    /// translated the drag position to doc coordinates.
3252    pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
3253        self.set_cursor_doc(row, col);
3254    }
3255
3256    pub fn insert_str(&mut self, text: &str) {
3257        let pos = crate::types::Cursor::cursor(&self.buffer);
3258        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
3259        self.push_buffer_content_to_textarea();
3260        self.mark_content_dirty();
3261    }
3262
3263    pub fn accept_completion(&mut self, completion: &str) {
3264        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
3265        let cursor_pos = CursorTrait::cursor(&self.buffer);
3266        let cursor_row = cursor_pos.line as usize;
3267        let cursor_col = cursor_pos.col as usize;
3268        let line = buf_line(&self.buffer, cursor_row).unwrap_or_default();
3269        let chars: Vec<char> = line.chars().collect();
3270        let prefix_len = chars[..cursor_col.min(chars.len())]
3271            .iter()
3272            .rev()
3273            .take_while(|c| c.is_alphanumeric() || **c == '_')
3274            .count();
3275        if prefix_len > 0 {
3276            let start = Pos {
3277                line: cursor_row as u32,
3278                col: (cursor_col - prefix_len) as u32,
3279            };
3280            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
3281        }
3282        let cursor = CursorTrait::cursor(&self.buffer);
3283        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
3284        self.push_buffer_content_to_textarea();
3285        self.mark_content_dirty();
3286    }
3287
3288    /// Capture the buffer state for undo / redo.  Uses
3289    /// [`Query::content_joined`], which the `Buffer` impl caches as an
3290    /// `Arc<String>` against `dirty_gen` — so when LSP / git / syntax
3291    /// already joined this generation, the snapshot is an `Arc::clone`
3292    /// (one ptr bump). Previously this cloned every line into a
3293    /// `Vec<String>` (162 k allocations on a 162 k-row buffer) and the
3294    /// matching `restore` re-joined them — samply showed it at ~9 % of
3295    /// CPU on a big-paste session.
3296    pub(super) fn snapshot(&self) -> (ropey::Rope, (usize, usize)) {
3297        use crate::types::Query;
3298        let rc = buf_cursor_rc(&self.buffer);
3299        (Query::rope(&self.buffer), rc)
3300    }
3301
3302    /// Walk one step back through the undo history. Equivalent to the
3303    /// user pressing `u` in normal mode. Drains the most recent undo
3304    /// entry and pushes it onto the redo stack.
3305    pub fn undo(&mut self) {
3306        crate::vim::do_undo(self);
3307    }
3308
3309    /// Walk one step forward through the redo history. Equivalent to
3310    /// `<C-r>` in normal mode.
3311    pub fn redo(&mut self) {
3312        crate::vim::do_redo(self);
3313    }
3314
3315    /// Undo `n` steps. Returns the number of steps actually applied
3316    /// (bounded by undo stack size).
3317    pub fn earlier_by_steps(&mut self, n: usize) -> usize {
3318        let mut count = 0;
3319        for _ in 0..n {
3320            if self.undo_stack.is_empty() {
3321                break;
3322            }
3323            crate::vim::do_undo(self);
3324            count += 1;
3325        }
3326        count
3327    }
3328
3329    /// Redo `n` steps. Returns the number of steps actually applied
3330    /// (bounded by redo stack size).
3331    pub fn later_by_steps(&mut self, n: usize) -> usize {
3332        let mut count = 0;
3333        for _ in 0..n {
3334            if self.redo_stack.is_empty() {
3335                break;
3336            }
3337            crate::vim::do_redo(self);
3338            count += 1;
3339        }
3340        count
3341    }
3342
3343    /// Undo back until the next-to-pop entry's timestamp is at or before
3344    /// `target`. Entries whose timestamp is strictly greater than `target`
3345    /// are popped (undone). Returns the number of steps applied.
3346    ///
3347    /// Vim `:earlier Ns` semantics: `target = SystemTime::now() - N seconds`.
3348    pub fn earlier_by_time(&mut self, target: SystemTime) -> usize {
3349        let mut count = 0;
3350        loop {
3351            match self.undo_stack.last() {
3352                None => break,
3353                Some(entry) => {
3354                    if entry.timestamp <= target {
3355                        break;
3356                    }
3357                }
3358            }
3359            crate::vim::do_undo(self);
3360            count += 1;
3361        }
3362        count
3363    }
3364
3365    /// Redo forward while the next-to-pop redo entry's timestamp is at
3366    /// or before `target`. Returns the number of steps applied.
3367    ///
3368    /// Vim `:later Ns` semantics: `target = current_state_time + N seconds`.
3369    pub fn later_by_time(&mut self, target: SystemTime) -> usize {
3370        let mut count = 0;
3371        loop {
3372            match self.redo_stack.last() {
3373                None => break,
3374                Some(entry) => {
3375                    if entry.timestamp > target {
3376                        break;
3377                    }
3378                }
3379            }
3380            crate::vim::do_redo(self);
3381            count += 1;
3382        }
3383        count
3384    }
3385
3386    /// Snapshot current buffer state onto the undo stack and clear
3387    /// the redo stack. Bounded by `settings.undo_levels` — older
3388    /// entries pruned. Call before any group of buffer mutations the
3389    /// user might want to undo as a single step.
3390    pub fn push_undo(&mut self) {
3391        self.push_undo_at(SystemTime::now());
3392    }
3393
3394    /// Like [`push_undo`] but uses a caller-supplied timestamp. Used by
3395    /// tests that need deterministic time values without `sleep`.
3396    #[doc(hidden)]
3397    pub fn push_undo_at(&mut self, timestamp: SystemTime) {
3398        let (rope, cursor) = self.snapshot();
3399        self.undo_stack.push(UndoEntry {
3400            rope,
3401            cursor,
3402            timestamp,
3403        });
3404        self.cap_undo();
3405        self.redo_stack.clear();
3406    }
3407
3408    /// Trim the undo stack down to `settings.undo_levels`, dropping
3409    /// the oldest entries. `undo_levels == 0` is treated as
3410    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
3411    /// skipped — guarding with `> 0` is one line shorter than gating
3412    /// the cap path with an explicit zero-check above the call site).
3413    pub(crate) fn cap_undo(&mut self) {
3414        let cap = self.settings.undo_levels as usize;
3415        if cap > 0 && self.undo_stack.len() > cap {
3416            let diff = self.undo_stack.len() - cap;
3417            self.undo_stack.drain(..diff);
3418        }
3419    }
3420
3421    /// Test-only accessor for the undo stack length.
3422    #[doc(hidden)]
3423    pub fn undo_stack_len(&self) -> usize {
3424        self.undo_stack.len()
3425    }
3426
3427    /// Replace the buffer with `lines` joined by `\n` and set the
3428    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
3429    /// paths. Marks the editor dirty.
3430    ///
3431    /// Emits a single whole-buffer `ContentEdit` describing the
3432    /// transition so the syntax layer can apply it as an `InputEdit`
3433    /// on the retained tree and run an INCREMENTAL parse — tree-sitter
3434    /// reuses unchanged subtrees and `Tree::changed_ranges` reports
3435    /// just the bytes that differ, which lets the install path walk
3436    /// only the changed rows instead of the full viewport. Big undos
3437    /// that revert a large paste now refresh in ~1ms per affected
3438    /// row instead of a ~30ms full-viewport sync walk.
3439    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
3440        let text = lines.join("\n");
3441        self.restore_text(&text, cursor);
3442    }
3443
3444    /// Restore the buffer from a `ropey::Rope` snapshot. Used by undo /
3445    /// redo: snapshots are stored as `Rope` (O(1) Arc-clone via
3446    /// `Buffer::rope()`), so this avoids the full-document `to_string`
3447    /// materialization that the old `Arc<String>` snapshot path forced
3448    /// on every undo group boundary.
3449    ///
3450    /// Internally materializes the rope to a `String` for `restore_text`
3451    /// — paying the cost on the restore side instead of the snapshot
3452    /// side trades one ~3 MB build per undo for none-per-snapshot. Undo
3453    /// is user-initiated and rare; snapshots fire on every `i` / `o`.
3454    pub fn restore_rope(&mut self, rope: ropey::Rope, cursor: (usize, usize)) {
3455        let text = rope.to_string();
3456        self.restore_text(&text, cursor);
3457    }
3458
3459    fn restore_text(&mut self, text: &str, cursor: (usize, usize)) {
3460        // Diff the old rope (O(1) Arc-clone) against the incoming text
3461        // to emit a minimal ContentEdit — without it the syntax layer's
3462        // tree.edit() marks the whole document changed and tree-sitter
3463        // cold-parses on every undo.
3464        let old_rope = self.buffer.rope();
3465        let edit = minimal_content_edit_rope(&old_rope, text);
3466
3467        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
3468        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
3469
3470        // Bulk replace supersedes any prior queued edits.
3471        self.pending_content_edits.clear();
3472        self.pending_content_edits.push(edit);
3473        self.mark_content_dirty();
3474    }
3475
3476    /// Returns true if the key was consumed by the editor.
3477    /// Replace the char under the cursor with `ch`, `count` times. Matches
3478    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
3479    /// snapshot taken once at start. Promoted to public surface in 0.5.5
3480    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
3481    /// re-entering the FSM.
3482    pub fn replace_char_at(&mut self, ch: char, count: usize) {
3483        vim::replace_char(self, ch, count);
3484    }
3485
3486    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
3487    /// to the `count`-th occurrence of `ch` on the current line, respecting
3488    /// `forward` (direction) and `till` (stop one char before target).
3489    /// Records `last_find` so `;` / `,` repeat work.
3490    ///
3491    /// No-op if the target char isn't on the current line within range.
3492    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
3493    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
3494        vim::apply_find_char(self, ch, forward, till, count.max(1));
3495    }
3496
3497    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
3498    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
3499    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
3500    ///
3501    /// Promoted to public surface in 0.5.10 so hjkl-vim's
3502    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
3503    /// re-entering the engine FSM.
3504    pub fn after_g(&mut self, ch: char, count: usize) {
3505        vim::apply_after_g(self, ch, count);
3506    }
3507
3508    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
3509    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
3510    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
3511    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
3512    ///
3513    /// Promoted to public surface in 0.5.11 so hjkl-vim's
3514    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
3515    /// re-entering the engine FSM.
3516    pub fn after_z(&mut self, ch: char, count: usize) {
3517        vim::apply_after_z(self, ch, count);
3518    }
3519
3520    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
3521    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
3522    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
3523    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
3524    /// then calls `apply_op_with_motion`. `total_count` is already the product of
3525    /// the prefix count and any inner count accumulated by the reducer.
3526    ///
3527    /// No-op when `motion_key` does not map to a known motion (engine silently
3528    /// cancels the operator, matching vim's behaviour on unknown motions).
3529    ///
3530    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
3531    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
3532    /// re-entering the engine FSM.
3533    pub fn apply_op_motion(
3534        &mut self,
3535        op: crate::vim::Operator,
3536        motion_key: char,
3537        total_count: usize,
3538    ) {
3539        vim::apply_op_motion_key(self, op, motion_key, total_count);
3540    }
3541
3542    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
3543    /// `total_count` is the product of prefix count and inner count.
3544    ///
3545    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
3546    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
3547    /// re-entering the engine FSM.
3548    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
3549        vim::apply_op_double(self, op, total_count);
3550    }
3551
3552    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
3553    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
3554    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
3555    /// updates `last_change` when `op` is Change (for dot-repeat).
3556    ///
3557    /// `total_count` is the product of prefix count and any inner count
3558    /// accumulated by the reducer — already folded at transition time.
3559    ///
3560    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
3561    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
3562    /// re-entering the engine FSM. `handle_op_find_target` (used by the
3563    /// chord-init op path) delegates here to avoid logic duplication.
3564    pub fn apply_op_find(
3565        &mut self,
3566        op: crate::vim::Operator,
3567        ch: char,
3568        forward: bool,
3569        till: bool,
3570        total_count: usize,
3571    ) {
3572        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
3573    }
3574
3575    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
3576    /// Maps `ch` to a `TextObject` per the standard vim table, calls
3577    /// `apply_op_with_text_object`, and records `last_change` when `op` is
3578    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
3579    /// matching the engine FSM's behaviour on unrecognised text-object chars.
3580    ///
3581    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
3582    /// `apply_op_find` but is currently unused — text objects don't repeat in
3583    /// vim's current grammar. Kept for future-proofing.
3584    ///
3585    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
3586    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
3587    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
3588    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
3589    /// duplication.
3590    pub fn apply_op_text_obj(
3591        &mut self,
3592        op: crate::vim::Operator,
3593        ch: char,
3594        inner: bool,
3595        total_count: usize,
3596    ) {
3597        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
3598    }
3599
3600    /// Apply an operator over a g-chord motion or case-op linewise form
3601    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
3602    ///
3603    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
3604    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
3605    /// - Otherwise maps `ch` to a motion:
3606    ///   - `'g'` → `Motion::FileTop` (gg)
3607    ///   - `'e'` → `Motion::WordEndBack` (ge)
3608    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
3609    ///   - `'j'` → `Motion::ScreenDown` (gj)
3610    ///   - `'k'` → `Motion::ScreenUp` (gk)
3611    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
3612    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
3613    ///
3614    /// `total_count` is the already-folded product of prefix and inner counts.
3615    ///
3616    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
3617    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
3618    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
3619    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
3620    /// duplication.
3621    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
3622        vim::apply_op_g_inner(self, op, ch, total_count);
3623    }
3624
3625    // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
3626
3627    /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
3628    /// row and the motion's target row. Used by the app layer to compute the
3629    /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
3630    /// format job.
3631    ///
3632    /// Returns `None` when `motion_key` does not map to a known motion (same
3633    /// condition that makes `apply_op_motion` a no-op).
3634    ///
3635    /// The cursor is restored to its original position after the probe —
3636    /// the buffer content is not touched.
3637    pub fn range_for_op_motion(
3638        &mut self,
3639        motion_key: char,
3640        total_count: usize,
3641    ) -> Option<(usize, usize)> {
3642        let start = self.cursor();
3643        // Reuse the same logic as apply_op_motion_key but only read the
3644        // target row — we parse the motion, apply it to move the cursor,
3645        // then immediately restore.
3646        let input = crate::input::Input {
3647            key: crate::input::Key::Char(motion_key),
3648            ctrl: false,
3649            alt: false,
3650            shift: false,
3651        };
3652        let motion = vim::parse_motion(&input)?;
3653        // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
3654        let motion = match motion {
3655            vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
3656                Some((ch, forward, till)) => vim::Motion::Find {
3657                    ch,
3658                    forward: if reverse { !forward } else { forward },
3659                    till,
3660                },
3661                None => return None,
3662            },
3663            m => m,
3664        };
3665        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3666        let end = self.cursor();
3667        // Restore cursor.
3668        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3669        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3670        Some((r0, r1))
3671    }
3672
3673    /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
3674    /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
3675    /// linewise forms that don't map to a row range.
3676    ///
3677    /// The cursor is restored after the probe.
3678    pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
3679        let start = self.cursor();
3680        let motion = match ch {
3681            'g' => vim::Motion::FileTop,
3682            'e' => vim::Motion::WordEndBack,
3683            'E' => vim::Motion::BigWordEndBack,
3684            'j' => vim::Motion::ScreenDown,
3685            'k' => vim::Motion::ScreenUp,
3686            _ => return None,
3687        };
3688        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3689        let end = self.cursor();
3690        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3691        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3692        Some((r0, r1))
3693    }
3694
3695    /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
3696    /// matched region. Returns `None` when `ch` is not a known text-object
3697    /// kind or the text object could not be resolved (e.g. no enclosing bracket).
3698    ///
3699    /// The buffer is not mutated.
3700    pub fn range_for_op_text_obj(
3701        &self,
3702        ch: char,
3703        inner: bool,
3704        total_count: usize,
3705    ) -> Option<(usize, usize)> {
3706        let obj = match ch {
3707            'w' => vim::TextObject::Word { big: false },
3708            'W' => vim::TextObject::Word { big: true },
3709            '"' | '\'' | '`' => vim::TextObject::Quote(ch),
3710            '(' | ')' | 'b' => vim::TextObject::Bracket('('),
3711            '[' | ']' => vim::TextObject::Bracket('['),
3712            '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
3713            '<' | '>' => vim::TextObject::Bracket('<'),
3714            'p' => vim::TextObject::Paragraph,
3715            't' => vim::TextObject::XmlTag,
3716            's' => vim::TextObject::Sentence,
3717            _ => return None,
3718        };
3719        let (start, end, _kind) = vim::text_object_range(self, obj, inner, total_count.max(1))?;
3720        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3721        Some((r0, r1))
3722    }
3723
3724    // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
3725    //
3726    // These do not consume input — the caller (hjkl-vim's visual-mode operator
3727    // path, chunk 4e) has already resolved the range from the visual selection
3728    // before calling in. Normal-mode op dispatch continues to use
3729    // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
3730
3731    /// Delete the region `[start, end)` and stash the removed text in
3732    /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
3733    /// select named registers.
3734    ///
3735    /// Pure range-mutation primitive — does not consume input. Called by
3736    /// hjkl-vim's visual-mode operator path which has already resolved the range
3737    /// from the visual selection.
3738    ///
3739    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3740    /// grammar migration (kryptic-sh/hjkl#70).
3741    pub fn delete_range(
3742        &mut self,
3743        start: (usize, usize),
3744        end: (usize, usize),
3745        kind: crate::vim::RangeKind,
3746        register: char,
3747    ) {
3748        vim::delete_range_bridge(self, start, end, kind, register);
3749    }
3750
3751    /// Yank (copy) the region `[start, end)` into `register` without mutating
3752    /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
3753    /// register; `'a'`–`'z'` select named registers.
3754    ///
3755    /// Pure range-mutation primitive — does not consume input. Called by
3756    /// hjkl-vim's visual-mode operator path which has already resolved the range
3757    /// from the visual selection.
3758    ///
3759    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3760    /// grammar migration (kryptic-sh/hjkl#70).
3761    pub fn yank_range(
3762        &mut self,
3763        start: (usize, usize),
3764        end: (usize, usize),
3765        kind: crate::vim::RangeKind,
3766        register: char,
3767    ) {
3768        vim::yank_range_bridge(self, start, end, kind, register);
3769    }
3770
3771    /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
3772    /// operator). The deleted text is stashed in `register`. On return the
3773    /// editor is in Insert mode; the caller must not issue further normal-mode
3774    /// ops until the insert session ends.
3775    ///
3776    /// Pure range-mutation primitive — does not consume input. Called by
3777    /// hjkl-vim's visual-mode operator path which has already resolved the range
3778    /// from the visual selection.
3779    ///
3780    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3781    /// grammar migration (kryptic-sh/hjkl#70).
3782    pub fn change_range(
3783        &mut self,
3784        start: (usize, usize),
3785        end: (usize, usize),
3786        kind: crate::vim::RangeKind,
3787        register: char,
3788    ) {
3789        vim::change_range_bridge(self, start, end, kind, register);
3790    }
3791
3792    /// Indent (`count > 0`) or outdent (`count < 0`) the row span
3793    /// `[start.0, end.0]`. Column components are ignored — indent is always
3794    /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
3795    /// this call; pass `0` to use the current editor setting. `count == 0` is a
3796    /// no-op.
3797    ///
3798    /// Pure range-mutation primitive — does not consume input. Called by
3799    /// hjkl-vim's visual-mode operator path which has already resolved the range
3800    /// from the visual selection.
3801    ///
3802    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3803    /// grammar migration (kryptic-sh/hjkl#70).
3804    pub fn indent_range(
3805        &mut self,
3806        start: (usize, usize),
3807        end: (usize, usize),
3808        count: i32,
3809        shiftwidth: u32,
3810    ) {
3811        vim::indent_range_bridge(self, start, end, count, shiftwidth);
3812    }
3813
3814    /// Apply a case transformation (`Operator::Uppercase` /
3815    /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3816    /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3817    /// Yanks registers are left untouched — vim's case operators do not write
3818    /// to registers.
3819    ///
3820    /// Pure range-mutation primitive — does not consume input. Called by
3821    /// hjkl-vim's visual-mode operator path which has already resolved the range
3822    /// from the visual selection.
3823    ///
3824    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3825    /// grammar migration (kryptic-sh/hjkl#70).
3826    pub fn case_range(
3827        &mut self,
3828        start: (usize, usize),
3829        end: (usize, usize),
3830        kind: crate::vim::RangeKind,
3831        op: crate::vim::Operator,
3832    ) {
3833        vim::case_range_bridge(self, start, end, kind, op);
3834    }
3835
3836    // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3837    //
3838    // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3839    // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3840    // Ragged-edge handling (short lines not reaching `right_col`) matches the
3841    // engine FSM's `apply_block_operator` path — short lines lose only the
3842    // chars that exist.
3843    //
3844    // `register` is the target register; `'"'` selects the unnamed register.
3845
3846    /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3847    /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3848    /// bounds at the visual (display) column level. Ragged-edge handling
3849    /// matches engine FSM's VisualBlock op behavior — short lines that don't
3850    /// reach `right_col` lose only the chars that exist.
3851    ///
3852    /// `register` honors the user's pending register selection.
3853    ///
3854    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3855    pub fn delete_block(
3856        &mut self,
3857        top_row: usize,
3858        bot_row: usize,
3859        left_col: usize,
3860        right_col: usize,
3861        register: char,
3862    ) {
3863        vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3864    }
3865
3866    /// Yank a rectangular VisualBlock selection into `register` without
3867    /// mutating the buffer. `'"'` selects the unnamed register.
3868    ///
3869    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3870    pub fn yank_block(
3871        &mut self,
3872        top_row: usize,
3873        bot_row: usize,
3874        left_col: usize,
3875        right_col: usize,
3876        register: char,
3877    ) {
3878        vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3879    }
3880
3881    /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3882    /// operator). The deleted text is stashed in `register`. Mode is Insert
3883    /// on return; the caller must not issue further normal-mode ops until the
3884    /// insert session ends.
3885    ///
3886    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3887    pub fn change_block(
3888        &mut self,
3889        top_row: usize,
3890        bot_row: usize,
3891        left_col: usize,
3892        right_col: usize,
3893        register: char,
3894    ) {
3895        vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3896    }
3897
3898    /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3899    /// Column bounds are ignored — vim's block indent is always linewise.
3900    /// `count == 0` is a no-op.
3901    ///
3902    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3903    pub fn indent_block(
3904        &mut self,
3905        top_row: usize,
3906        bot_row: usize,
3907        _left_col: usize,
3908        _right_col: usize,
3909        count: i32,
3910    ) {
3911        vim::indent_block_bridge(self, top_row, bot_row, count);
3912    }
3913
3914    /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3915    /// Column components are ignored — auto-indent is always linewise.
3916    ///
3917    /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3918    /// row 0 to compute the correct depth at `start.0`, then for each line in
3919    /// the target range strips existing leading whitespace and prepends
3920    /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3921    /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3922    /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3923    /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3924    ///
3925    /// After the operation the cursor lands on the first non-whitespace
3926    /// character of `start_row` (vim parity for `==`).
3927    ///
3928    /// **v1 limitation**: the bracket scan does not detect brackets inside
3929    /// string literals or comments. Code such as `let s = "{";` will increment
3930    /// the depth counter even though the brace is not a structural opener.
3931    /// Tree-sitter / LSP indentation is deferred to a follow-up.
3932    pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3933        vim::auto_indent_range_bridge(self, start, end);
3934    }
3935
3936    /// Drain the row range set by the most recent auto-indent operation.
3937    ///
3938    /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3939    /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3940    /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3941    /// uses this to arm a brief visual flash over the reindented rows.
3942    pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3943        self.last_indent_range.take()
3944    }
3945
3946    /// Filter rows `top_row..=bot_row` through an external shell command.
3947    ///
3948    /// Spawns `sh -c "<command>"` (or `cmd /C "<command>"` on Windows), pipes
3949    /// the selected lines (joined by `\n`) to stdin, and waits up to
3950    /// `timeout_secs` seconds (default 10) for the process to finish.
3951    ///
3952    /// On success: the rows are replaced with stdout. No trailing-newline trim.
3953    /// On non-zero exit, spawn failure, or timeout: returns `Err(stderr_or_msg)`
3954    /// without mutating the buffer.
3955    ///
3956    /// `top_row` and `bot_row` are clamped to the buffer's valid row range.
3957    pub fn filter_range(
3958        &mut self,
3959        top_row: usize,
3960        bot_row: usize,
3961        command: &str,
3962        timeout_secs: Option<u64>,
3963    ) -> Result<(), String> {
3964        use std::io::Write;
3965        use std::process::{Command, Stdio};
3966        use std::thread;
3967        use std::time::Instant;
3968
3969        let timeout = std::time::Duration::from_secs(timeout_secs.unwrap_or(10));
3970        let rope = crate::types::Query::rope(self.buffer());
3971        let line_count = rope.len_lines();
3972        let top = top_row.min(line_count.saturating_sub(1));
3973        let bot = bot_row.min(line_count.saturating_sub(1));
3974        let (top, bot) = (top.min(bot), top.max(bot));
3975        let input_text = crate::vim::rope_row_range_str(&rope, top, bot);
3976        // Materialized for the splice-back after the command succeeds.
3977        let lines = crate::vim::rope_to_lines_vec(&rope);
3978
3979        tracing::debug!(
3980            top_row = top,
3981            bot_row = bot,
3982            command = command,
3983            "filter_range: spawning shell command"
3984        );
3985
3986        #[cfg(not(windows))]
3987        let mut child = Command::new("sh")
3988            .args(["-c", command])
3989            .stdin(Stdio::piped())
3990            .stdout(Stdio::piped())
3991            .stderr(Stdio::piped())
3992            .spawn()
3993            .map_err(|e| format!("spawn failed: {e}"))?;
3994
3995        #[cfg(windows)]
3996        let mut child = Command::new("cmd")
3997            .args(["/C", command])
3998            .stdin(Stdio::piped())
3999            .stdout(Stdio::piped())
4000            .stderr(Stdio::piped())
4001            .spawn()
4002            .map_err(|e| format!("spawn failed: {e}"))?;
4003
4004        // Write stdin on a thread to avoid deadlock when output > pipe buffer.
4005        let mut stdin = child.stdin.take().ok_or("no stdin handle")?;
4006        let input_bytes = input_text.into_bytes();
4007        thread::spawn(move || {
4008            let _ = stdin.write_all(&input_bytes);
4009            // stdin drops here, signalling EOF to the child.
4010        });
4011
4012        // Drain stdout/stderr on separate threads so the child's pipes don't
4013        // fill and deadlock the child. Keep `child` here so we can kill it on
4014        // timeout.
4015        let mut stdout_pipe = child.stdout.take().ok_or("no stdout handle")?;
4016        let mut stderr_pipe = child.stderr.take().ok_or("no stderr handle")?;
4017        let stdout_thread = thread::spawn(move || {
4018            let mut buf = Vec::new();
4019            let _ = std::io::Read::read_to_end(&mut stdout_pipe, &mut buf);
4020            buf
4021        });
4022        let stderr_thread = thread::spawn(move || {
4023            let mut buf = Vec::new();
4024            let _ = std::io::Read::read_to_end(&mut stderr_pipe, &mut buf);
4025            buf
4026        });
4027
4028        // Poll try_wait until exit or timeout. On timeout: SIGKILL the child
4029        // (std Child::kill sends SIGKILL on Unix / TerminateProcess on Windows).
4030        // A proper TERM→KILL escalation would need nix/libc; skip for v1.
4031        let start = Instant::now();
4032        let status = loop {
4033            match child.try_wait() {
4034                Ok(Some(status)) => break status,
4035                Ok(None) => {
4036                    if start.elapsed() >= timeout {
4037                        tracing::debug!(command, "filter_range: timeout — killing child");
4038                        let _ = child.kill();
4039                        let _ = child.wait(); // reap so the OS can free resources
4040                        return Err(format!("command timed out after {}s", timeout.as_secs()));
4041                    }
4042                    thread::sleep(std::time::Duration::from_millis(20));
4043                }
4044                Err(e) => return Err(format!("wait failed: {e}")),
4045            }
4046        };
4047
4048        let stdout_bytes = stdout_thread.join().unwrap_or_default();
4049        let stderr_bytes = stderr_thread.join().unwrap_or_default();
4050
4051        if !status.success() {
4052            let stderr = String::from_utf8_lossy(&stderr_bytes).into_owned();
4053            tracing::debug!(
4054                command,
4055                exit_code = ?status.code(),
4056                "filter_range: command exited with non-zero status"
4057            );
4058            return Err(if stderr.is_empty() {
4059                format!("command exited with status {}", status.code().unwrap_or(-1))
4060            } else {
4061                stderr
4062            });
4063        }
4064
4065        let stdout = String::from_utf8_lossy(&stdout_bytes).into_owned();
4066        tracing::debug!(
4067            command,
4068            stdout_bytes = stdout_bytes.len(),
4069            "filter_range: command succeeded, replacing rows"
4070        );
4071
4072        // Replace the row range with the stdout lines.
4073        let mut all_lines = lines;
4074        let new_lines: Vec<String> = stdout.lines().map(|l| l.to_owned()).collect();
4075        // If stdout ended with a newline, stdout.lines() drops the trailing empty
4076        // entry — this preserves vim's "no trailing-newline trim" spec because
4077        // a trailing '\n' from the command means the last replacement line is the
4078        // line BEFORE the newline, not an empty line after it.
4079        let after = all_lines.split_off(bot + 1);
4080        all_lines.truncate(top);
4081        all_lines.extend(new_lines);
4082        all_lines.extend(after);
4083
4084        self.push_undo();
4085        self.restore(all_lines, (top, 0));
4086        // Leave mode as Normal after a successful filter operation (vim parity).
4087        self.force_normal();
4088
4089        Ok(())
4090    }
4091
4092    // ─── Comment toggle (#187) ───────────────────────────────────────────────
4093
4094    /// Toggle line comments on rows `top_row..=bot_row` (0-based, inclusive).
4095    ///
4096    /// **Algorithm** (vim-commentary parity):
4097    ///
4098    /// 1. Determine the comment marker(s) for the active filetype.
4099    ///    Priority: `settings.commentstring` (`:set commentstring=…`) → per-filetype
4100    ///    default from `hjkl_lang::comment::commentstring_for_lang` → no-op.
4101    /// 2. Scan non-blank lines.  If every non-blank line is already commented →
4102    ///    strip the comment marker from each.  Otherwise → add it to all non-blank
4103    ///    lines.
4104    /// 3. Blank / whitespace-only lines are skipped (no marker added or removed).
4105    /// 4. The marker is inserted AFTER the leading whitespace (indent-preserving).
4106    /// 5. The entire operation is a single undo step.
4107    ///
4108    /// For block-comment languages (HTML, CSS) each line is individually wrapped
4109    /// as `start text end` (per-line block style, not one multi-line block).
4110    ///
4111    /// `top_row` and `bot_row` are clamped to the buffer's valid row range.
4112    pub fn toggle_comment_range(&mut self, top_row: usize, bot_row: usize) {
4113        use hjkl_lang::comment::commentstring_for_lang;
4114
4115        let lang = self.settings.filetype.clone();
4116
4117        // Resolve the comment markers.
4118        // If `settings.commentstring` is set (non-empty) parse `start %s end`
4119        // from it; otherwise fall back to the filetype table.
4120        let (start, end) = if !self.settings.commentstring.is_empty() {
4121            let cs = &self.settings.commentstring;
4122            if let Some(idx) = cs.find("%s") {
4123                let s = cs[..idx].trim_end().to_string();
4124                let e_raw = cs[idx + 2..].trim_start();
4125                let e: Option<String> = if e_raw.is_empty() {
4126                    None
4127                } else {
4128                    Some(e_raw.to_string())
4129                };
4130                (s, e)
4131            } else {
4132                // No %s placeholder — treat the whole string as start marker.
4133                (cs.clone(), None)
4134            }
4135        } else {
4136            match commentstring_for_lang(&lang) {
4137                Some((s, e)) => (s.to_string(), e.map(|v| v.to_string())),
4138                None => return, // no known comment syntax → no-op
4139            }
4140        };
4141
4142        let row_count = buf_row_count(&self.buffer);
4143        let top = top_row.min(row_count.saturating_sub(1));
4144        let bot = bot_row.min(row_count.saturating_sub(1));
4145
4146        // Collect all lines in the range.
4147        let lines: Vec<String> = (top..=bot)
4148            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
4149            .collect();
4150
4151        // Check whether every non-blank line is already commented.
4152        let all_commented = lines.iter().all(|line| {
4153            let trimmed = line.trim_start();
4154            if trimmed.is_empty() {
4155                return true; // blank lines don't count against "all commented"
4156            }
4157            if let Some(ref end_marker) = end {
4158                // Block style: line starts with start and ends with end.
4159                trimmed.starts_with(start.as_str())
4160                    && line.trim_end().ends_with(end_marker.as_str())
4161            } else {
4162                trimmed.starts_with(start.as_str())
4163            }
4164        });
4165
4166        let mut new_lines: Vec<String> = Vec::with_capacity(lines.len());
4167        for line in &lines {
4168            let trimmed = line.trim_start();
4169            if trimmed.is_empty() {
4170                // Blank line — leave as-is.
4171                new_lines.push(line.clone());
4172                continue;
4173            }
4174            let indent_len = line.len() - trimmed.len();
4175            let indent = &line[..indent_len];
4176
4177            if all_commented {
4178                // Uncomment: strip exactly one occurrence of start (+ optional space).
4179                if let Some(after_start) = trimmed.strip_prefix(start.as_str()) {
4180                    // Strip one leading space after the marker if present.
4181                    let after_space = after_start.strip_prefix(' ').unwrap_or(after_start);
4182                    // For block style also strip the trailing end marker.
4183                    let text = if let Some(ref end_marker) = end {
4184                        after_space
4185                            .trim_end()
4186                            .strip_suffix(end_marker.as_str())
4187                            .map(|s| s.trim_end())
4188                            .unwrap_or(after_space)
4189                    } else {
4190                        after_space
4191                    };
4192                    new_lines.push(format!("{indent}{text}"));
4193                } else {
4194                    new_lines.push(line.clone());
4195                }
4196            } else {
4197                // Comment: insert marker after indent.
4198                let commented = if let Some(ref end_marker) = end {
4199                    format!("{indent}{start} {trimmed} {end_marker}")
4200                } else {
4201                    format!("{indent}{start} {trimmed}")
4202                };
4203                new_lines.push(commented);
4204            }
4205        }
4206
4207        // Replace the row range in the buffer — single undo step.
4208        self.push_undo();
4209        let row_count_after = buf_row_count(&self.buffer);
4210        let all_before: Vec<String> = (0..top)
4211            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
4212            .collect();
4213        let all_after: Vec<String> = ((bot + 1)..row_count_after)
4214            .map(|r| buf_line(&self.buffer, r).unwrap_or_default())
4215            .collect();
4216        let mut all: Vec<String> = all_before;
4217        all.extend(new_lines);
4218        all.extend(all_after);
4219        self.restore(all, (top, 0));
4220    }
4221
4222    // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
4223    //
4224    // Pure functions — no cursor mutation, no mode change, no register write.
4225    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
4226    // the existing `word_text_object` private resolver in vim.rs.
4227    //
4228    // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
4229    // before invoking a range-mutation primitive (`delete_range`, etc.).
4230    //
4231    // Return value: `Some((start, end))` where both positions are `(row, col)`
4232    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
4233    // on), matching the convention used by `delete_range` / `yank_range` / etc.
4234    // Returns `None` when the cursor is on an empty line or the resolver cannot
4235    // find a word boundary.
4236
4237    /// Resolve the range of `iw` (inner word) at the current cursor position.
4238    ///
4239    /// An inner word is the contiguous run of keyword characters (or punctuation
4240    /// characters if the cursor is on punctuation) under the cursor, without any
4241    /// surrounding whitespace. Whitespace-only positions return `None`.
4242    ///
4243    /// Pure function — does not move the cursor or change any editor state.
4244    /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
4245    /// invoking a range-mutation primitive (`delete_range`, etc.).
4246    ///
4247    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
4248    /// migration (kryptic-sh/hjkl#70).
4249    pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
4250        vim::text_object_inner_word_bridge(self)
4251    }
4252
4253    /// Resolve the range of `aw` (around word) at the current cursor position.
4254    ///
4255    /// Like `iw` but extends the range to include trailing whitespace after the
4256    /// word. If no trailing whitespace exists, leading whitespace before the word
4257    /// is absorbed instead (vim `:help text-objects` behaviour).
4258    ///
4259    /// Pure function — does not move the cursor or change any editor state.
4260    ///
4261    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
4262    /// migration (kryptic-sh/hjkl#70).
4263    pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
4264        vim::text_object_around_word_bridge(self)
4265    }
4266
4267    /// Resolve the range of `iW` (inner WORD) at the current cursor position.
4268    ///
4269    /// A WORD is any contiguous run of non-whitespace characters — punctuation
4270    /// is not treated as a word boundary. Returns the span of the WORD under the
4271    /// cursor, without surrounding whitespace.
4272    ///
4273    /// Pure function — does not move the cursor or change any editor state.
4274    ///
4275    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
4276    /// migration (kryptic-sh/hjkl#70).
4277    pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
4278        vim::text_object_inner_big_word_bridge(self)
4279    }
4280
4281    /// Resolve the range of `aW` (around WORD) at the current cursor position.
4282    ///
4283    /// Like `iW` but extends the range to include trailing whitespace after the
4284    /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
4285    /// is absorbed instead.
4286    ///
4287    /// Pure function — does not move the cursor or change any editor state.
4288    ///
4289    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
4290    /// migration (kryptic-sh/hjkl#70).
4291    pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
4292        vim::text_object_around_big_word_bridge(self)
4293    }
4294
4295    // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
4296    //
4297    // Pure functions — no cursor mutation, no mode change, no register write.
4298    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
4299    // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
4300    // in vim.rs.
4301    //
4302    // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
4303    // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
4304    // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
4305    // hjkl-vim grammar layer normalises close→open before calling these methods.
4306    //
4307    // Return value: `Some((start, end))` where both positions are `(row, col)`
4308    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
4309    // on), matching the convention used by `delete_range` / `yank_range` / etc.
4310    // `bracket_text_object` internally distinguishes Linewise vs Exclusive
4311    // ranges for multi-line pairs; that tag is stripped here — callers receive
4312    // the same flat shape as all other text-object resolvers.
4313
4314    /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
4315    ///
4316    /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
4317    /// cursor's line contains fewer than two occurrences of `quote`, or when no
4318    /// matching pair can be found around or ahead of the cursor.
4319    ///
4320    /// Inner range excludes the quote characters themselves.
4321    ///
4322    /// Pure function — no cursor mutation.
4323    ///
4324    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
4325    /// migration (kryptic-sh/hjkl#70).
4326    pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
4327        vim::text_object_inner_quote_bridge(self, quote)
4328    }
4329
4330    /// Resolve the range of `a<quote>` (around quote) at the cursor position.
4331    ///
4332    /// Like `i<quote>` but includes the quote characters themselves plus
4333    /// surrounding whitespace on one side: trailing whitespace after the closing
4334    /// quote if any exists; otherwise leading whitespace before the opening
4335    /// quote. This matches vim `:help text-objects` behaviour.
4336    ///
4337    /// Pure function — no cursor mutation.
4338    ///
4339    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
4340    /// migration (kryptic-sh/hjkl#70).
4341    pub fn text_object_around_quote(
4342        &self,
4343        quote: char,
4344    ) -> Option<((usize, usize), (usize, usize))> {
4345        vim::text_object_around_quote_bridge(self, quote)
4346    }
4347
4348    /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
4349    ///
4350    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
4351    /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
4352    /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
4353    /// calling this method. Returns `None` when no enclosing pair is found.
4354    ///
4355    /// The cursor may be anywhere inside the pair or on a bracket character
4356    /// itself. When not inside any pair the resolver falls back to a forward
4357    /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
4358    ///
4359    /// Inner range excludes the bracket characters. Multi-line pairs are
4360    /// supported; the returned range spans the full content between the
4361    /// brackets.
4362    ///
4363    /// Pure function — no cursor mutation.
4364    ///
4365    /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
4366    /// handled here.
4367    ///
4368    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
4369    /// migration (kryptic-sh/hjkl#70).
4370    pub fn text_object_inner_bracket(
4371        &self,
4372        open: char,
4373    ) -> Option<((usize, usize), (usize, usize))> {
4374        vim::text_object_inner_bracket_bridge(self, open)
4375    }
4376
4377    /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
4378    ///
4379    /// Like `i<bracket>` but includes the bracket characters themselves.
4380    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
4381    ///
4382    /// Pure function — no cursor mutation.
4383    ///
4384    /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
4385    ///
4386    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
4387    /// migration (kryptic-sh/hjkl#70).
4388    pub fn text_object_around_bracket(
4389        &self,
4390        open: char,
4391    ) -> Option<((usize, usize), (usize, usize))> {
4392        vim::text_object_around_bracket_bridge(self, open)
4393    }
4394
4395    // ── Sentence text objects (is / as) ───────────────────────────────────
4396
4397    /// Resolve `is` (inner sentence) at the cursor position.
4398    ///
4399    /// Returns the range of the current sentence, excluding trailing
4400    /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
4401    /// `?` / `!` followed by whitespace or end-of-paragraph).
4402    ///
4403    /// Pure function — no cursor mutation.
4404    ///
4405    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4406    /// grammar migration (kryptic-sh/hjkl#70).
4407    pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
4408        vim::text_object_inner_sentence_bridge(self)
4409    }
4410
4411    /// Resolve `as` (around sentence) at the cursor position.
4412    ///
4413    /// Like `is` but includes trailing whitespace after the sentence
4414    /// terminator.
4415    ///
4416    /// Pure function — no cursor mutation.
4417    ///
4418    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4419    /// grammar migration (kryptic-sh/hjkl#70).
4420    pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
4421        vim::text_object_around_sentence_bridge(self)
4422    }
4423
4424    // ── Paragraph text objects (ip / ap) ──────────────────────────────────
4425
4426    /// Resolve `ip` (inner paragraph) at the cursor position.
4427    ///
4428    /// A paragraph is a block of non-blank lines bounded by blank lines or
4429    /// buffer edges. Returns `None` when the cursor is on a blank line.
4430    ///
4431    /// Pure function — no cursor mutation.
4432    ///
4433    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4434    /// grammar migration (kryptic-sh/hjkl#70).
4435    pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
4436        vim::text_object_inner_paragraph_bridge(self)
4437    }
4438
4439    /// Resolve `ap` (around paragraph) at the cursor position.
4440    ///
4441    /// Like `ip` but includes one trailing blank line when present.
4442    ///
4443    /// Pure function — no cursor mutation.
4444    ///
4445    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4446    /// grammar migration (kryptic-sh/hjkl#70).
4447    pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
4448        vim::text_object_around_paragraph_bridge(self)
4449    }
4450
4451    // ── Tag text objects (it / at) ────────────────────────────────────────
4452
4453    /// Resolve `it` (inner tag) at the cursor position.
4454    ///
4455    /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
4456    /// inner content between the open and close tags (excluding the tags
4457    /// themselves).
4458    ///
4459    /// Pure function — no cursor mutation.
4460    ///
4461    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4462    /// grammar migration (kryptic-sh/hjkl#70).
4463    pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
4464        vim::text_object_inner_tag_bridge(self)
4465    }
4466
4467    /// Resolve `at` (around tag) at the cursor position.
4468    ///
4469    /// Like `it` but includes the open and close tag delimiters themselves.
4470    ///
4471    /// Pure function — no cursor mutation.
4472    ///
4473    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
4474    /// grammar migration (kryptic-sh/hjkl#70).
4475    pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
4476        vim::text_object_around_tag_bridge(self)
4477    }
4478
4479    /// Execute a named cursor motion `kind` repeated `count` times.
4480    ///
4481    /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
4482    /// motion primitives, bypassing the engine FSM. Identical cursor semantics
4483    /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
4484    /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
4485    /// used by the FSM arms.
4486    ///
4487    /// Introduced in 0.6.1 as the host entry point for Phase 3a of
4488    /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
4489    /// calls this method rather than re-entering the engine FSM.
4490    ///
4491    /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
4492    /// intact for macro-replay coverage (macros re-feed raw keys through the
4493    /// FSM). This method is the keymap / controller path only.
4494    pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
4495        vim::apply_motion_kind(self, kind, count);
4496    }
4497
4498    /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
4499    /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
4500    /// chars are silently ignored (no-op), matching the engine FSM's
4501    /// `handle_select_register` behaviour.
4502    ///
4503    /// Promoted to the public surface in 0.5.17 so the hjkl-vim
4504    /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
4505    /// without re-entering the engine FSM. `handle_select_register` (engine FSM
4506    /// path for macro-replay / defensive coverage) delegates here to avoid
4507    /// logic duplication.
4508    pub fn set_pending_register(&mut self, reg: char) {
4509        if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
4510            self.vim.pending_register = Some(reg);
4511        }
4512        // Invalid chars silently no-op (matches engine FSM behavior).
4513    }
4514
4515    /// Record a mark named `ch` at the current cursor position.
4516    ///
4517    /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
4518    /// rules). Invalid chars are silently ignored (no-op), matching the engine
4519    /// FSM's `handle_set_mark` behaviour.
4520    ///
4521    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4522    /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
4523    /// without re-entering the engine FSM. `handle_set_mark` delegates here.
4524    pub fn set_mark_at_cursor(&mut self, ch: char) {
4525        vim::set_mark_at_cursor(self, ch);
4526    }
4527
4528    /// `.` dot-repeat: replay the last buffered change at the current cursor.
4529    /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
4530    /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
4531    ///
4532    /// Storage of `LastChange` stays inside engine for now; Phase 5c of
4533    /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
4534    /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
4535    /// FSM `.` arm stays for macro-replay defensive coverage.
4536    pub fn replay_last_change(&mut self, count: usize) {
4537        vim::replay_last_change(self, count);
4538    }
4539
4540    /// Jump to the mark named `ch`, linewise (row only; col snaps to first
4541    /// non-blank). Pushes the pre-jump position onto the jumplist if the
4542    /// cursor actually moved.
4543    ///
4544    /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
4545    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
4546    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
4547    /// are silently ignored (no-op), matching the engine FSM's
4548    /// `handle_goto_mark` behaviour.
4549    ///
4550    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4551    /// `PendingState::GotoMarkLine` reducer can dispatch
4552    /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
4553    pub fn goto_mark_line(&mut self, ch: char) {
4554        vim::goto_mark(self, ch, true);
4555    }
4556
4557    /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
4558    /// pre-jump position onto the jumplist if the cursor actually moved.
4559    ///
4560    /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
4561    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
4562    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
4563    /// are silently ignored (no-op), matching the engine FSM's
4564    /// `handle_goto_mark` behaviour.
4565    ///
4566    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
4567    /// `PendingState::GotoMarkChar` reducer can dispatch
4568    /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
4569    pub fn goto_mark_char(&mut self, ch: char) {
4570        vim::goto_mark(self, ch, false);
4571    }
4572
4573    /// Jump to the mark named `ch`, linewise. For uppercase marks (`'A'`–`'Z'`)
4574    /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
4575    /// app can switch slots before positioning the cursor. Returns
4576    /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
4577    /// `MarkJump::Unset` when the mark is not set.
4578    pub fn try_goto_mark_line(&mut self, ch: char) -> MarkJump {
4579        vim::try_goto_mark(self, ch, true)
4580    }
4581
4582    /// Jump to the mark named `ch`, charwise. For uppercase marks (`'A'`–`'Z'`)
4583    /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
4584    /// app can switch slots before positioning the cursor. Returns
4585    /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
4586    /// `MarkJump::Unset` when the mark is not set.
4587    pub fn try_goto_mark_char(&mut self, ch: char) -> MarkJump {
4588        vim::try_goto_mark(self, ch, false)
4589    }
4590
4591    // ── Macro controller API (Phase 5b) ──────────────────────────────────────
4592
4593    /// Begin recording keystrokes into register `reg`. The caller (app) is
4594    /// responsible for stopping the recording via `stop_macro_record` when the
4595    /// user presses bare `q`.
4596    ///
4597    /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
4598    ///   recording by pre-seeding `recording_keys` with the decoded text of the
4599    ///   matching lowercase register, matching vim's capital-register append
4600    ///   semantics.
4601    /// - Lowercase `reg` clears `recording_keys` (fresh recording).
4602    /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
4603    ///
4604    /// Promoted to the public surface in Phase 5b so the app's
4605    /// `route_chord_key` can start a recording without re-entering the engine
4606    /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
4607    /// defensive coverage) continues to use the same logic via delegation.
4608    pub fn start_macro_record(&mut self, reg: char) {
4609        if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
4610            return;
4611        }
4612        self.vim.recording_macro = Some(reg);
4613        if reg.is_ascii_uppercase() {
4614            // Seed recording_keys with the existing lowercase register's text
4615            // decoded back to inputs so capital-register append continues from
4616            // where the previous recording left off.
4617            let lower = reg.to_ascii_lowercase();
4618            let text = self
4619                .registers
4620                .read(lower)
4621                .map(|s| s.text.clone())
4622                .unwrap_or_default();
4623            self.vim.recording_keys = crate::input::decode_macro(&text);
4624        } else {
4625            self.vim.recording_keys.clear();
4626        }
4627    }
4628
4629    /// Finalize the active recording: encode `recording_keys` as text and write
4630    /// to the matching (lowercase) named register. Clears both `recording_macro`
4631    /// and `recording_keys`. No-ops if no recording is active.
4632    ///
4633    /// Promoted to the public surface in Phase 5b so the app's `QChord` action
4634    /// can stop a recording when the user presses bare `q` without re-entering
4635    /// the engine FSM.
4636    pub fn stop_macro_record(&mut self) {
4637        let Some(reg) = self.vim.recording_macro.take() else {
4638            return;
4639        };
4640        let keys = std::mem::take(&mut self.vim.recording_keys);
4641        let text = crate::input::encode_macro(&keys);
4642        self.set_named_register_text(reg.to_ascii_lowercase(), text);
4643    }
4644
4645    /// Returns `true` while a `q{reg}` recording is in progress.
4646    /// Hosts use this to show a "recording @r" status indicator and to decide
4647    /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
4648    /// chord.
4649    pub fn is_recording_macro(&self) -> bool {
4650        self.vim.recording_macro.is_some()
4651    }
4652
4653    /// Returns `true` while a macro is being replayed. The app sets this flag
4654    /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
4655    /// re-feed loop so the recorder hook can skip double-capture.
4656    pub fn is_replaying_macro(&self) -> bool {
4657        self.vim.replaying_macro
4658    }
4659
4660    /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
4661    /// prepare for replay, returning the inputs the app should re-feed through
4662    /// `route_chord_key`.
4663    ///
4664    /// Resolves `reg`:
4665    /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
4666    /// - Any other char → lowercase it, read the register, decode.
4667    ///
4668    /// Side-effects:
4669    /// - Sets `vim.last_macro` to the resolved register.
4670    /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
4671    ///   replay. The app calls `end_macro_replay` after the loop finishes.
4672    ///
4673    /// Returns an empty vec (and no side-effects for `'@'`) if the register is
4674    /// unset or empty.
4675    pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
4676        let resolved = if reg == '@' {
4677            match self.vim.last_macro {
4678                Some(r) => r,
4679                None => return vec![],
4680            }
4681        } else {
4682            reg.to_ascii_lowercase()
4683        };
4684        let text = match self.registers.read(resolved) {
4685            Some(slot) if !slot.text.is_empty() => slot.text.clone(),
4686            _ => return vec![],
4687        };
4688        let keys = crate::input::decode_macro(&text);
4689        self.vim.last_macro = Some(resolved);
4690        self.vim.replaying_macro = true;
4691        // Multiply by count (minimum 1).
4692        keys.repeat(count.max(1))
4693    }
4694
4695    /// Clear the `replaying_macro` flag. Called by the app after the
4696    /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
4697    pub fn end_macro_replay(&mut self) {
4698        self.vim.replaying_macro = false;
4699    }
4700
4701    /// Append `input` to the active recording (`recording_keys`) if and only
4702    /// if a recording is in progress AND we are not currently replaying.
4703    /// Called by the app's `route_chord_key` recorder hook so that user
4704    /// keystrokes captured through the app-level chord path are recorded
4705    /// (rather than relying solely on the engine FSM's in-step hook).
4706    pub fn record_input(&mut self, input: crate::input::Input) {
4707        if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
4708            self.vim.recording_keys.push(input);
4709        }
4710    }
4711
4712    // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
4713    //
4714    // Each method is the publicly callable form of one insert-mode action.
4715    // All logic lives in the corresponding `vim::*_bridge` free function;
4716    // these methods are thin delegators so the public surface stays on `Editor`.
4717    //
4718    // Invariants (enforced by the bridge fns):
4719    //   - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
4720    //   - Navigation keys call `break_undo_group_in_insert` when the FSM did.
4721    //   - `push_buffer_cursor_to_textarea` is called after every mutation
4722    //     (currently a no-op, kept for migration hygiene).
4723
4724    /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
4725    /// the cursor instead of inserting; at end-of-line, always appends. With
4726    /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
4727    /// dedent on an otherwise-whitespace line.
4728    ///
4729    /// Callers must ensure the editor is in Insert or Replace mode before
4730    /// calling this method.
4731    pub fn insert_char(&mut self, ch: char) {
4732        if vim::insert_char_bridge(self, ch) {
4733            self.after_insert_mutation();
4734        }
4735    }
4736
4737    /// Insert a newline at the cursor, applying autoindent / smartindent to
4738    /// prefix the new line with the appropriate leading whitespace.
4739    ///
4740    /// Callers must ensure the editor is in Insert mode before calling.
4741    pub fn insert_newline(&mut self) {
4742        if vim::insert_newline_bridge(self) {
4743            self.after_insert_mutation();
4744        }
4745    }
4746
4747    /// Common post-mutation sync for the `insert_*` primitives. The vim
4748    /// FSM's `step` runs `ensure_cursor_in_scrolloff` at the end of every
4749    /// normal/visual motion; insert-mode primitives bypass `step` and
4750    /// must self-correct or the cursor scrolls off the viewport (held
4751    /// Enter, multi-line backspace at BOL, arrow keys at edge, etc.).
4752    ///
4753    /// Marks the content dirty, widens the insert row's autoindent
4754    /// tracking, and re-checks scrolloff.
4755    fn after_insert_mutation(&mut self) {
4756        self.mark_content_dirty();
4757        let (row, _) = self.cursor();
4758        self.vim.widen_insert_row(row);
4759        self.ensure_cursor_in_scrolloff();
4760    }
4761
4762    /// Like `after_insert_mutation` but for cursor-only insert ops that
4763    /// don't change content (arrows, Home/End, PageUp/Down). Skips the
4764    /// dirty mark.
4765    fn after_insert_motion(&mut self) {
4766        let (row, _) = self.cursor();
4767        self.vim.widen_insert_row(row);
4768        self.ensure_cursor_in_scrolloff();
4769    }
4770
4771    /// Insert a tab character (or spaces up to the next `softtabstop` boundary
4772    /// when `expandtab` is set).
4773    ///
4774    /// Callers must ensure the editor is in Insert mode before calling.
4775    pub fn insert_tab(&mut self) {
4776        if vim::insert_tab_bridge(self) {
4777            self.after_insert_mutation();
4778        }
4779    }
4780
4781    /// Delete the character before the cursor (Backspace). With `softtabstop`
4782    /// active, deletes the entire soft-tab run at an aligned boundary. Joins
4783    /// with the previous line when at column 0.
4784    ///
4785    /// Callers must ensure the editor is in Insert mode before calling.
4786    pub fn insert_backspace(&mut self) {
4787        if vim::insert_backspace_bridge(self) {
4788            self.after_insert_mutation();
4789        }
4790    }
4791
4792    /// Delete the character under the cursor (Delete key). Joins with the
4793    /// next line when at end-of-line.
4794    ///
4795    /// Callers must ensure the editor is in Insert mode before calling.
4796    pub fn insert_delete(&mut self) {
4797        if vim::insert_delete_bridge(self) {
4798            self.after_insert_mutation();
4799        }
4800    }
4801
4802    /// Move the cursor one step in `dir` (arrow key), breaking the undo group
4803    /// per `undo_break_on_motion`.
4804    ///
4805    /// Callers must ensure the editor is in Insert mode before calling.
4806    pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
4807        vim::insert_arrow_bridge(self, dir);
4808        self.after_insert_motion();
4809    }
4810
4811    /// Move the cursor to the start of the current line (Home key), breaking
4812    /// the undo group.
4813    ///
4814    /// Callers must ensure the editor is in Insert mode before calling.
4815    pub fn insert_home(&mut self) {
4816        vim::insert_home_bridge(self);
4817        self.after_insert_motion();
4818    }
4819
4820    /// Move the cursor to the end of the current line (End key), breaking the
4821    /// undo group.
4822    ///
4823    /// Callers must ensure the editor is in Insert mode before calling.
4824    pub fn insert_end(&mut self) {
4825        vim::insert_end_bridge(self);
4826        self.after_insert_motion();
4827    }
4828
4829    /// Scroll up one full viewport height (PageUp), moving the cursor with it.
4830    /// `viewport_h` is the current viewport height in rows; pass
4831    /// `self.viewport_height_value()` if the stored value is current.
4832    ///
4833    /// Callers must ensure the editor is in Insert mode before calling.
4834    pub fn insert_pageup(&mut self, viewport_h: u16) {
4835        vim::insert_pageup_bridge(self, viewport_h);
4836        self.after_insert_motion();
4837    }
4838
4839    /// Scroll down one full viewport height (PageDown), moving the cursor with
4840    /// it. `viewport_h` is the current viewport height in rows.
4841    ///
4842    /// Callers must ensure the editor is in Insert mode before calling.
4843    pub fn insert_pagedown(&mut self, viewport_h: u16) {
4844        vim::insert_pagedown_bridge(self, viewport_h);
4845        self.after_insert_motion();
4846    }
4847
4848    /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
4849    /// At column 0, joins with the previous line (vim `b`-motion semantics).
4850    ///
4851    /// Callers must ensure the editor is in Insert mode before calling.
4852    pub fn insert_ctrl_w(&mut self) {
4853        if vim::insert_ctrl_w_bridge(self) {
4854            self.after_insert_mutation();
4855        }
4856    }
4857
4858    /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
4859    /// No-op when already at column 0.
4860    ///
4861    /// Callers must ensure the editor is in Insert mode before calling.
4862    pub fn insert_ctrl_u(&mut self) {
4863        if vim::insert_ctrl_u_bridge(self) {
4864            self.after_insert_mutation();
4865        }
4866    }
4867
4868    /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
4869    /// insert mode. Joins with the previous line when at col 0.
4870    ///
4871    /// Callers must ensure the editor is in Insert mode before calling.
4872    pub fn insert_ctrl_h(&mut self) {
4873        if vim::insert_ctrl_h_bridge(self) {
4874            self.after_insert_mutation();
4875        }
4876    }
4877
4878    /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
4879    /// complete normal-mode command, then return to insert automatically.
4880    ///
4881    /// Callers must ensure the editor is in Insert mode before calling.
4882    pub fn insert_ctrl_o_arm(&mut self) {
4883        vim::insert_ctrl_o_bridge(self);
4884    }
4885
4886    /// Arm the register-paste selector (`Ctrl-R`). The next call to
4887    /// `insert_paste_register(reg)` will insert the register contents.
4888    /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
4889    /// the armed state and paste register `c`.
4890    ///
4891    /// Callers must ensure the editor is in Insert mode before calling.
4892    pub fn insert_ctrl_r_arm(&mut self) {
4893        vim::insert_ctrl_r_bridge(self);
4894    }
4895
4896    /// Indent the current line by one `shiftwidth` and shift the cursor right
4897    /// by the same amount (`Ctrl-T`).
4898    ///
4899    /// Callers must ensure the editor is in Insert mode before calling.
4900    pub fn insert_ctrl_t(&mut self) {
4901        let mutated = vim::insert_ctrl_t_bridge(self);
4902        if mutated {
4903            self.mark_content_dirty();
4904            let (row, _) = self.cursor();
4905            self.vim.widen_insert_row(row);
4906        }
4907    }
4908
4909    /// Outdent the current line by up to one `shiftwidth` and shift the cursor
4910    /// left by the amount stripped (`Ctrl-D`).
4911    ///
4912    /// Callers must ensure the editor is in Insert mode before calling.
4913    pub fn insert_ctrl_d(&mut self) {
4914        let mutated = vim::insert_ctrl_d_bridge(self);
4915        if mutated {
4916            self.mark_content_dirty();
4917            let (row, _) = self.cursor();
4918            self.vim.widen_insert_row(row);
4919        }
4920    }
4921
4922    /// Paste the contents of register `reg` at the cursor (the commit arm of
4923    /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
4924    ///
4925    /// Callers must ensure the editor is in Insert mode before calling.
4926    pub fn insert_paste_register(&mut self, reg: char) {
4927        vim::insert_paste_register_bridge(self, reg);
4928        let (row, _) = self.cursor();
4929        self.vim.widen_insert_row(row);
4930    }
4931
4932    /// Exit insert mode to Normal: finish the insert session, step the cursor
4933    /// one cell left (vim convention on Esc), record the `gi` target position,
4934    /// and update the sticky column.
4935    ///
4936    /// Callers must ensure the editor is in Insert mode before calling.
4937    pub fn leave_insert_to_normal(&mut self) {
4938        vim::leave_insert_to_normal_bridge(self);
4939    }
4940
4941    // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
4942    //
4943    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4944    // `vim.rs` following the same pattern as Phase 6.1. The FSM's
4945    // `handle_normal_only` now calls the same bridges so both paths are
4946    // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
4947
4948    /// `i` — transition to Insert mode at the current cursor position.
4949    /// `count` is stored in the insert session and replayed by dot-repeat
4950    /// as a repeat count on the inserted text.
4951    pub fn enter_insert_i(&mut self, count: usize) {
4952        vim::enter_insert_i_bridge(self, count);
4953    }
4954
4955    /// `I` — move to the first non-blank character on the line, then
4956    /// transition to Insert mode. `count` is stored for dot-repeat.
4957    pub fn enter_insert_shift_i(&mut self, count: usize) {
4958        vim::enter_insert_shift_i_bridge(self, count);
4959    }
4960
4961    /// `a` — advance the cursor one cell past the current position, then
4962    /// transition to Insert mode (append). `count` is stored for dot-repeat.
4963    pub fn enter_insert_a(&mut self, count: usize) {
4964        vim::enter_insert_a_bridge(self, count);
4965    }
4966
4967    /// `A` — move the cursor to the end of the line, then transition to
4968    /// Insert mode (append at end). `count` is stored for dot-repeat.
4969    pub fn enter_insert_shift_a(&mut self, count: usize) {
4970        vim::enter_insert_shift_a_bridge(self, count);
4971    }
4972
4973    /// `o` — open a new line below the current line with smart-indent, then
4974    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4975    pub fn open_line_below(&mut self, count: usize) {
4976        vim::open_line_below_bridge(self, count);
4977    }
4978
4979    /// `O` — open a new line above the current line with smart-indent, then
4980    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4981    pub fn open_line_above(&mut self, count: usize) {
4982        vim::open_line_above_bridge(self, count);
4983    }
4984
4985    /// `R` — enter Replace mode: subsequent typed characters overstrike the
4986    /// cell under the cursor rather than inserting. `count` is for replay.
4987    pub fn enter_replace_mode(&mut self, count: usize) {
4988        vim::enter_replace_mode_bridge(self, count);
4989    }
4990
4991    /// `x` — delete `count` characters forward from the cursor and write them
4992    /// to the unnamed register. No-op on an empty line. Records for `.`.
4993    pub fn delete_char_forward(&mut self, count: usize) {
4994        vim::delete_char_forward_bridge(self, count);
4995    }
4996
4997    /// `X` — delete `count` characters backward from the cursor and write
4998    /// them to the unnamed register. No-op at column 0. Records for `.`.
4999    pub fn delete_char_backward(&mut self, count: usize) {
5000        vim::delete_char_backward_bridge(self, count);
5001    }
5002
5003    /// `s` — substitute `count` characters: delete them (writing to the
5004    /// unnamed register) then enter Insert mode. Equivalent to `cl`.
5005    /// Records as `OpMotion { Change, Right }` for dot-repeat.
5006    pub fn substitute_char(&mut self, count: usize) {
5007        vim::substitute_char_bridge(self, count);
5008    }
5009
5010    /// `S` — substitute the current line: wipe its contents (writing to the
5011    /// unnamed register) then enter Insert mode. Equivalent to `cc`.
5012    /// Records as `LineOp { Change }` for dot-repeat.
5013    pub fn substitute_line(&mut self, count: usize) {
5014        vim::substitute_line_bridge(self, count);
5015    }
5016
5017    /// `D` — delete from the cursor to end-of-line, writing to the unnamed
5018    /// register. The cursor parks on the new last character. Records for `.`.
5019    pub fn delete_to_eol(&mut self) {
5020        vim::delete_to_eol_bridge(self);
5021    }
5022
5023    /// `C` — change from the cursor to end-of-line: delete to EOL then enter
5024    /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
5025    /// (the insert session records `DeleteToEol` on exit, like `c` motions).
5026    pub fn change_to_eol(&mut self) {
5027        vim::change_to_eol_bridge(self);
5028    }
5029
5030    /// `Y` — yank from the cursor to end-of-line into the unnamed register.
5031    /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
5032    pub fn yank_to_eol(&mut self, count: usize) {
5033        vim::yank_to_eol_bridge(self, count);
5034    }
5035
5036    /// `J` — join `count` lines (default 2) onto the current line, inserting
5037    /// a single space between each non-empty pair. Records for dot-repeat.
5038    pub fn join_line(&mut self, count: usize) {
5039        vim::join_line_bridge(self, count);
5040    }
5041
5042    /// `~` — toggle the case of `count` characters from the cursor, advancing
5043    /// right after each toggle. Records `ToggleCase` for dot-repeat.
5044    pub fn toggle_case_at_cursor(&mut self, count: usize) {
5045        vim::toggle_case_at_cursor_bridge(self, count);
5046    }
5047
5048    /// `p` — paste the unnamed register (or the register selected via `"r`)
5049    /// after the cursor. Linewise content opens a new line below; charwise
5050    /// content is inserted inline. Records `Paste { before: false }` for `.`.
5051    pub fn paste_after(&mut self, count: usize) {
5052        vim::paste_after_bridge(self, count);
5053    }
5054
5055    /// `P` — paste the unnamed register (or the `"r` register) before the
5056    /// cursor. Linewise content opens a new line above; charwise is inline.
5057    /// Records `Paste { before: true }` for dot-repeat.
5058    pub fn paste_before(&mut self, count: usize) {
5059        vim::paste_before_bridge(self, count);
5060    }
5061
5062    /// `<C-o>` — jump back `count` entries in the jumplist, saving the
5063    /// current position on the forward stack so `<C-i>` can return.
5064    pub fn jump_back(&mut self, count: usize) {
5065        vim::jump_back_bridge(self, count);
5066    }
5067
5068    /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
5069    /// saving the current position on the backward stack.
5070    pub fn jump_forward(&mut self, count: usize) {
5071        vim::jump_forward_bridge(self, count);
5072    }
5073
5074    /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
5075    /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
5076    /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
5077    pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
5078        vim::scroll_full_page_bridge(self, dir, count);
5079    }
5080
5081    /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
5082    /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
5083    pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
5084        vim::scroll_half_page_bridge(self, dir, count);
5085    }
5086
5087    /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
5088    /// the cursor (cursor is clamped to the new visible region if necessary).
5089    /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
5090    pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
5091        vim::scroll_line_bridge(self, dir, count);
5092    }
5093
5094    /// `n` — repeat the last `/` or `?` search `count` times in its original
5095    /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
5096    pub fn search_repeat(&mut self, forward: bool, count: usize) {
5097        vim::search_repeat_bridge(self, forward, count);
5098    }
5099
5100    /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
5101    /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
5102    /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
5103    pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
5104        vim::word_search_bridge(self, forward, whole_word, count);
5105    }
5106
5107    // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
5108    //
5109    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
5110    // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
5111    // and these wrappers write `current_mode` so `vim_mode()` returns correct
5112    // values regardless of which path performed the transition.
5113    // See kryptic-sh/hjkl#89 for the full promotion plan.
5114
5115    /// `v` from Normal — enter charwise Visual mode, anchoring the selection
5116    /// at the current cursor position.
5117    pub fn enter_visual_char(&mut self) {
5118        vim::enter_visual_char_bridge(self);
5119    }
5120
5121    /// `V` from Normal — enter linewise Visual mode, anchoring on the current
5122    /// line. Motions extend the selection by whole lines.
5123    pub fn enter_visual_line(&mut self) {
5124        vim::enter_visual_line_bridge(self);
5125    }
5126
5127    /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
5128    /// rectangle whose corners are the anchor and the live cursor.
5129    pub fn enter_visual_block(&mut self) {
5130        vim::enter_visual_block_bridge(self);
5131    }
5132
5133    /// Esc from any visual mode — set `<` / `>` marks, stash the selection
5134    /// for `gv` re-entry, then return to Normal mode.
5135    pub fn exit_visual_to_normal(&mut self) {
5136        vim::exit_visual_to_normal_bridge(self);
5137    }
5138
5139    /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
5140    /// so the user can extend the other end of the selection. Does NOT
5141    /// mutate the selection range; only the active endpoint changes.
5142    pub fn visual_o_toggle(&mut self) {
5143        vim::visual_o_toggle_bridge(self);
5144    }
5145
5146    /// `gv` — restore the last visual selection (mode + anchor + cursor
5147    /// position). No-op when no visual selection has been exited yet.
5148    pub fn reenter_last_visual(&mut self) {
5149        vim::reenter_last_visual_bridge(self);
5150    }
5151
5152    /// Direct mode-transition entry point. Sets both the internal FSM mode
5153    /// and the stable `current_mode` field read by [`Editor::vim_mode`].
5154    ///
5155    /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
5156    /// …) which also set up required bookkeeping (anchors, sessions, …).
5157    /// Use `set_mode` only when you need a raw mode flip without side-effects.
5158    pub fn set_mode(&mut self, mode: VimMode) {
5159        vim::set_mode_bridge(self, mode);
5160    }
5161}
5162
5163// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
5164//
5165// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
5166// `Editor` accessors and mutators defined in this block. Each method gets a
5167// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
5168// rather than individual getters + setters (e.g. `accumulate_count_digit`).
5169
5170/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
5171///
5172/// Treat as opaque — construct by calling `begin_step` and pass the
5173/// returned value directly into `end_step` without modification.
5174/// The fields capture per-step pre-dispatch state that the epilogue
5175/// needs to run its invariants correctly.
5176pub struct StepBookkeeping {
5177    /// True when the pending chord before this step was a macro-chord
5178    /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
5179    /// keys so that only the *payload* keys enter `recording_keys`.
5180    pub pending_was_macro_chord: bool,
5181    /// True when the mode was Insert *before* the FSM body ran. Used by
5182    /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
5183    /// back into Insert.
5184    pub was_insert: bool,
5185    /// Pre-dispatch visual snapshot. When the FSM body transitions out of
5186    /// a visual mode the epilogue uses this to set the `<`/`>` marks and
5187    /// store `last_visual` for `gv`.
5188    pub pre_visual_snapshot: Option<vim::LastVisual>,
5189}
5190
5191impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
5192    // ── Pending chord ─────────────────────────────────────────────────────────
5193
5194    /// Return a clone of the current pending chord state.
5195    pub fn pending(&self) -> vim::Pending {
5196        self.vim.pending.clone()
5197    }
5198
5199    /// Overwrite the pending chord state.
5200    pub fn set_pending(&mut self, p: vim::Pending) {
5201        self.vim.pending = p;
5202    }
5203
5204    /// Atomically take the pending chord, replacing it with `Pending::None`.
5205    pub fn take_pending(&mut self) -> vim::Pending {
5206        std::mem::take(&mut self.vim.pending)
5207    }
5208
5209    // ── Count prefix ──────────────────────────────────────────────────────────
5210
5211    /// Return the raw digit-prefix count (`0` = no prefix typed yet).
5212    pub fn count(&self) -> usize {
5213        self.vim.count
5214    }
5215
5216    /// Overwrite the digit-prefix count directly.
5217    pub fn set_count(&mut self, c: usize) {
5218        self.vim.count = c;
5219    }
5220
5221    /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
5222    pub fn accumulate_count_digit(&mut self, digit: usize) {
5223        self.vim.count = self.vim.count.saturating_mul(10) + digit;
5224    }
5225
5226    /// Reset the count prefix to zero (no pending count).
5227    pub fn reset_count(&mut self) {
5228        self.vim.count = 0;
5229    }
5230
5231    /// Consume the count and return it; resets to zero. Returns `1` when no
5232    /// prefix was typed (mirrors `take_count` in vim.rs).
5233    pub fn take_count(&mut self) -> usize {
5234        if self.vim.count > 0 {
5235            let n = self.vim.count;
5236            self.vim.count = 0;
5237            n
5238        } else {
5239            1
5240        }
5241    }
5242
5243    // ── Internal FSM mode ─────────────────────────────────────────────────────
5244
5245    /// Return the FSM-internal mode (Normal / Insert / Visual / …).
5246    pub fn fsm_mode(&self) -> vim::Mode {
5247        self.vim.mode
5248    }
5249
5250    /// Overwrite the FSM-internal mode without side-effects. Prefer the
5251    /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
5252    pub fn set_fsm_mode(&mut self, m: vim::Mode) {
5253        self.vim.mode = m;
5254        self.vim.current_mode = self.vim.public_mode();
5255    }
5256
5257    // ── Replaying flag ────────────────────────────────────────────────────────
5258
5259    /// `true` while the `.` dot-repeat replay is running.
5260    pub fn is_replaying(&self) -> bool {
5261        self.vim.replaying
5262    }
5263
5264    /// Set or clear the dot-replay flag.
5265    pub fn set_replaying(&mut self, v: bool) {
5266        self.vim.replaying = v;
5267    }
5268
5269    // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
5270
5271    /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
5272    /// to Insert after the next complete command.
5273    pub fn is_one_shot_normal(&self) -> bool {
5274        self.vim.one_shot_normal
5275    }
5276
5277    /// Set or clear the Ctrl-o one-shot-normal flag.
5278    pub fn set_one_shot_normal(&mut self, v: bool) {
5279        self.vim.one_shot_normal = v;
5280    }
5281
5282    // ── Last find (f/F/t/T target) ────────────────────────────────────────────
5283
5284    /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
5285    /// `None` before any find command was executed.
5286    pub fn last_find(&self) -> Option<(char, bool, bool)> {
5287        self.vim.last_find
5288    }
5289
5290    /// Overwrite the stored last-find target.
5291    pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
5292        self.vim.last_find = target;
5293    }
5294
5295    // ── Sneak motion ──────────────────────────────────────────────────────────
5296
5297    /// Perform a vim-sneak style two-char digraph jump. Scans the buffer
5298    /// from the current cursor for the `count`-th occurrence of `c1+c2`.
5299    /// `forward=true` searches ahead; `forward=false` searches backward.
5300    /// Respects `Settings::motion_sneak` — callers (hjkl-vim FSM) should
5301    /// already gate on the setting; this method always executes the sneak.
5302    pub fn sneak(&mut self, c1: char, c2: char, forward: bool, count: usize) {
5303        vim::apply_sneak(self, c1, c2, forward, count.max(1));
5304    }
5305
5306    /// Apply an operator over a sneak digraph range. Charwise exclusive —
5307    /// deletes from cursor up to (not including) the first char of the match.
5308    pub fn apply_op_sneak(
5309        &mut self,
5310        op: vim::Operator,
5311        c1: char,
5312        c2: char,
5313        forward: bool,
5314        total_count: usize,
5315    ) {
5316        vim::apply_op_sneak(self, op, c1, c2, forward, total_count);
5317    }
5318
5319    /// Return the last sneak digraph and direction stored after a sneak motion.
5320    /// `Some(((c1, c2), forward))` when a sneak has been performed this session;
5321    /// `None` before any sneak. Used by `;`/`,` repeat and tests.
5322    pub fn last_sneak(&self) -> Option<((char, char), bool)> {
5323        self.vim.last_sneak
5324    }
5325
5326    // ── Last change (dot-repeat payload) ─────────────────────────────────────
5327
5328    /// Return a clone of the last recorded mutating change, or `None` before
5329    /// any change has been made.
5330    pub fn last_change(&self) -> Option<vim::LastChange> {
5331        self.vim.last_change.clone()
5332    }
5333
5334    /// Overwrite the stored last-change record.
5335    pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
5336        self.vim.last_change = lc;
5337    }
5338
5339    /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
5340    /// field after the insert session completes).
5341    pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
5342        self.vim.last_change.as_mut()
5343    }
5344
5345    // ── Insert session ────────────────────────────────────────────────────────
5346
5347    /// Borrow the active insert session, or `None` when not in Insert mode.
5348    pub fn insert_session(&self) -> Option<&vim::InsertSession> {
5349        self.vim.insert_session.as_ref()
5350    }
5351
5352    /// Borrow the active insert session mutably.
5353    pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
5354        self.vim.insert_session.as_mut()
5355    }
5356
5357    /// Atomically take the insert session out, leaving `None`.
5358    pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
5359        self.vim.insert_session.take()
5360    }
5361
5362    /// Install a new insert session, replacing any existing one.
5363    pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
5364        self.vim.insert_session = s;
5365    }
5366
5367    // ── Visual anchors ────────────────────────────────────────────────────────
5368
5369    /// Return the charwise Visual-mode anchor `(row, col)`.
5370    pub fn visual_anchor(&self) -> (usize, usize) {
5371        self.vim.visual_anchor
5372    }
5373
5374    /// Overwrite the charwise Visual-mode anchor.
5375    pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
5376        self.vim.visual_anchor = anchor;
5377    }
5378
5379    /// Return the VisualLine anchor row.
5380    pub fn visual_line_anchor(&self) -> usize {
5381        self.vim.visual_line_anchor
5382    }
5383
5384    /// Overwrite the VisualLine anchor row.
5385    pub fn set_visual_line_anchor(&mut self, row: usize) {
5386        self.vim.visual_line_anchor = row;
5387    }
5388
5389    /// Return the VisualBlock anchor `(row, col)`.
5390    pub fn block_anchor(&self) -> (usize, usize) {
5391        self.vim.block_anchor
5392    }
5393
5394    /// Overwrite the VisualBlock anchor.
5395    pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
5396        self.vim.block_anchor = anchor;
5397    }
5398
5399    /// Return the VisualBlock virtual column used to survive j/k row clamping.
5400    pub fn block_vcol(&self) -> usize {
5401        self.vim.block_vcol
5402    }
5403
5404    /// Overwrite the VisualBlock virtual column.
5405    pub fn set_block_vcol(&mut self, vcol: usize) {
5406        self.vim.block_vcol = vcol;
5407    }
5408
5409    // ── Yank linewise flag ────────────────────────────────────────────────────
5410
5411    /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
5412    pub fn yank_linewise(&self) -> bool {
5413        self.vim.yank_linewise
5414    }
5415
5416    /// Set or clear the linewise-yank flag.
5417    pub fn set_yank_linewise(&mut self, v: bool) {
5418        self.vim.yank_linewise = v;
5419    }
5420
5421    // ── Pending register selector ─────────────────────────────────────────────
5422    // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
5423    // Only the mutators are new here.
5424
5425    /// Overwrite the pending register selector (Phase 6.6b mutator companion to
5426    /// the existing `pending_register()` getter).
5427    pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
5428        self.vim.pending_register = reg;
5429    }
5430
5431    /// Atomically take the pending register, returning `None` afterward.
5432    pub fn take_pending_register_raw(&mut self) -> Option<char> {
5433        self.vim.pending_register.take()
5434    }
5435
5436    // ── Macro recording ───────────────────────────────────────────────────────
5437
5438    /// Return the register currently being recorded into, or `None`.
5439    pub fn recording_macro(&self) -> Option<char> {
5440        self.vim.recording_macro
5441    }
5442
5443    /// Overwrite the recording-macro target register.
5444    pub fn set_recording_macro(&mut self, reg: Option<char>) {
5445        self.vim.recording_macro = reg;
5446    }
5447
5448    /// Append one input to the in-progress macro recording buffer.
5449    pub fn push_recording_key(&mut self, input: crate::input::Input) {
5450        self.vim.recording_keys.push(input);
5451    }
5452
5453    /// Atomically take the recorded key sequence, leaving an empty vec.
5454    pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
5455        std::mem::take(&mut self.vim.recording_keys)
5456    }
5457
5458    /// Overwrite the recording-keys buffer (e.g. to seed from a register).
5459    pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
5460        self.vim.recording_keys = keys;
5461    }
5462
5463    /// Return the number of keys currently in the recording buffer.
5464    /// Useful for integration tests that verify macro-recording bookkeeping
5465    /// without draining the buffer via [`take_recording_keys`].
5466    pub fn recording_keys_len(&self) -> usize {
5467        self.vim.recording_keys.len()
5468    }
5469
5470    // ── Macro replay flag ─────────────────────────────────────────────────────
5471
5472    /// `true` while `@reg` macro replay is running (suppresses re-recording).
5473    pub fn is_replaying_macro_raw(&self) -> bool {
5474        self.vim.replaying_macro
5475    }
5476
5477    /// Set or clear the macro-replay-in-progress flag.
5478    pub fn set_replaying_macro_raw(&mut self, v: bool) {
5479        self.vim.replaying_macro = v;
5480    }
5481
5482    // ── Last macro register ───────────────────────────────────────────────────
5483
5484    /// Return the register of the most recently played macro (`@@` source).
5485    pub fn last_macro(&self) -> Option<char> {
5486        self.vim.last_macro
5487    }
5488
5489    /// Overwrite the last-played-macro register.
5490    pub fn set_last_macro(&mut self, reg: Option<char>) {
5491        self.vim.last_macro = reg;
5492    }
5493
5494    // ── Last insert position ──────────────────────────────────────────────────
5495
5496    /// Return the cursor position when Insert mode was last exited (for `gi`).
5497    pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
5498        self.vim.last_insert_pos
5499    }
5500
5501    /// Overwrite the stored last-insert position.
5502    pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
5503        self.vim.last_insert_pos = pos;
5504    }
5505
5506    // ── Last visual selection ─────────────────────────────────────────────────
5507
5508    /// Return the saved visual selection snapshot for `gv`, or `None`.
5509    pub fn last_visual(&self) -> Option<vim::LastVisual> {
5510        self.vim.last_visual
5511    }
5512
5513    /// Overwrite the saved visual selection snapshot.
5514    pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
5515        self.vim.last_visual = snap;
5516    }
5517
5518    // ── Viewport-pinned flag ──────────────────────────────────────────────────
5519
5520    /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
5521    /// the end-of-step scrolloff pass).
5522    pub fn viewport_pinned(&self) -> bool {
5523        self.vim.viewport_pinned
5524    }
5525
5526    /// Set or clear the viewport-pinned flag.
5527    pub fn set_viewport_pinned(&mut self, v: bool) {
5528        self.vim.viewport_pinned = v;
5529    }
5530
5531    // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
5532
5533    /// `true` while waiting for the register-name key after `Ctrl-R` in
5534    /// Insert mode.
5535    pub fn insert_pending_register(&self) -> bool {
5536        self.vim.insert_pending_register
5537    }
5538
5539    /// Set or clear the `Ctrl-R` register-wait flag.
5540    pub fn set_insert_pending_register(&mut self, v: bool) {
5541        self.vim.insert_pending_register = v;
5542    }
5543
5544    // ── Change-mark start ─────────────────────────────────────────────────────
5545
5546    /// Return the stashed `[` mark start for a Change operation, or `None`.
5547    pub fn change_mark_start(&self) -> Option<(usize, usize)> {
5548        self.vim.change_mark_start
5549    }
5550
5551    /// Atomically take the change-mark start, leaving `None`.
5552    pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
5553        self.vim.change_mark_start.take()
5554    }
5555
5556    /// Overwrite the change-mark start.
5557    pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
5558        self.vim.change_mark_start = pos;
5559    }
5560
5561    // ── Timeout tracking ──────────────────────────────────────────────────────
5562
5563    /// Return the wall-clock `Instant` of the last keystroke.
5564    pub fn last_input_at(&self) -> Option<std::time::Instant> {
5565        self.vim.last_input_at
5566    }
5567
5568    /// Overwrite the wall-clock last-input timestamp.
5569    pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
5570        self.vim.last_input_at = t;
5571    }
5572
5573    /// Return the `Host::now()` duration at the last keystroke.
5574    pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
5575        self.vim.last_input_host_at
5576    }
5577
5578    /// Overwrite the host-clock last-input timestamp.
5579    pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
5580        self.vim.last_input_host_at = d;
5581    }
5582
5583    // ── Search prompt ──────────────────────────────────────────────────────────
5584
5585    /// Borrow the live search prompt, or `None` when not in search-prompt mode.
5586    pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
5587        self.vim.search_prompt.as_ref()
5588    }
5589
5590    /// Borrow the live search prompt mutably.
5591    pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
5592        self.vim.search_prompt.as_mut()
5593    }
5594
5595    /// Atomically take the search prompt, leaving `None`.
5596    pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
5597        self.vim.search_prompt.take()
5598    }
5599
5600    /// Install a new search prompt (entering search-prompt mode).
5601    pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
5602        self.vim.search_prompt = prompt;
5603    }
5604
5605    // ── Last search pattern / direction ───────────────────────────────────────
5606    // Note: `last_search_forward()` getter already exists at line ~1909.
5607    // `set_last_search()` combined mutator exists at line ~1918.
5608    // Only new / complementary accessors are added here.
5609
5610    /// Return the most recently committed search pattern, or `None`.
5611    pub fn last_search_pattern(&self) -> Option<&str> {
5612        self.vim.last_search.as_deref()
5613    }
5614
5615    /// Overwrite the stored last-search pattern without changing direction
5616    /// (use the existing `set_last_search` for the combined update).
5617    pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
5618        self.vim.last_search = pattern;
5619    }
5620
5621    /// Overwrite only the last-search direction flag.
5622    pub fn set_last_search_forward_only(&mut self, forward: bool) {
5623        self.vim.last_search_forward = forward;
5624    }
5625
5626    // ── Search history ────────────────────────────────────────────────────────
5627
5628    /// Borrow the committed search-pattern history (oldest first).
5629    pub fn search_history(&self) -> &[String] {
5630        &self.vim.search_history
5631    }
5632
5633    /// Borrow the search history mutably (e.g. to push a new entry).
5634    pub fn search_history_mut(&mut self) -> &mut Vec<String> {
5635        &mut self.vim.search_history
5636    }
5637
5638    /// Return the current search-history navigation cursor index.
5639    pub fn search_history_cursor(&self) -> Option<usize> {
5640        self.vim.search_history_cursor
5641    }
5642
5643    /// Overwrite the search-history navigation cursor.
5644    pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
5645        self.vim.search_history_cursor = idx;
5646    }
5647
5648    // ── Jump lists ────────────────────────────────────────────────────────────
5649
5650    /// Borrow the back half of the jump list (entries Ctrl-o pops from).
5651    pub fn jump_back_list(&self) -> &[(usize, usize)] {
5652        &self.vim.jump_back
5653    }
5654
5655    /// Borrow the back jump list mutably (push / pop).
5656    pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
5657        &mut self.vim.jump_back
5658    }
5659
5660    /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
5661    pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
5662        &self.vim.jump_fwd
5663    }
5664
5665    /// Borrow the forward jump list mutably (push / pop / clear).
5666    pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
5667        &mut self.vim.jump_fwd
5668    }
5669
5670    // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
5671    //
5672    // `push_search_pattern`, `push_jump`, `record_search_history`, and
5673    // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
5674    // search-prompt and normal-mode FSM can call them via the public API.
5675
5676    /// Compile `pattern` into a regex and install it as the active search
5677    /// pattern. Respects `:set ignorecase` / `:set smartcase` and inline
5678    /// `\c`/`\C` overrides. An empty or invalid pattern clears the highlight
5679    /// without raising an error.
5680    pub fn push_search_pattern(&mut self, pattern: &str) {
5681        let compiled = if pattern.is_empty() {
5682            None
5683        } else {
5684            use crate::search::{CaseMode, resolve_case_mode};
5685            let base =
5686                CaseMode::from_options(self.settings().ignore_case, self.settings().smartcase);
5687            let (stripped, mode) = resolve_case_mode(pattern, base);
5688            let src = if mode == CaseMode::Insensitive {
5689                format!("(?i){stripped}")
5690            } else {
5691                stripped
5692            };
5693            regex::Regex::new(&src).ok()
5694        };
5695        let wrap = self.settings().wrapscan;
5696        self.set_search_pattern(compiled);
5697        self.search_state_mut().wrap_around = wrap;
5698    }
5699
5700    /// Record a pre-jump cursor position onto the back jumplist. Called
5701    /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
5702    /// committed `/` or `?`, …). Branching off the history clears the
5703    /// forward half, matching vim's "redo-is-lost" semantics.
5704    pub fn push_jump(&mut self, from: (usize, usize)) {
5705        self.vim.jump_back.push(from);
5706        if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
5707            self.vim.jump_back.remove(0);
5708        }
5709        self.vim.jump_fwd.clear();
5710    }
5711
5712    /// Push `pattern` onto the committed search history. Skips if the
5713    /// most recent entry already matches (consecutive dedupe) and trims
5714    /// the oldest entries beyond the history cap.
5715    pub fn record_search_history(&mut self, pattern: &str) {
5716        if pattern.is_empty() {
5717            return;
5718        }
5719        if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
5720            return;
5721        }
5722        self.vim.search_history.push(pattern.to_string());
5723        let len = self.vim.search_history.len();
5724        if len > vim::SEARCH_HISTORY_MAX {
5725            self.vim
5726                .search_history
5727                .drain(0..len - vim::SEARCH_HISTORY_MAX);
5728        }
5729    }
5730
5731    /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
5732    /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
5733    /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
5734    /// active search prompt.
5735    pub fn walk_search_history(&mut self, dir: isize) {
5736        if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
5737            return;
5738        }
5739        let len = self.vim.search_history.len();
5740        let next_idx = match (self.vim.search_history_cursor, dir) {
5741            (None, -1) => Some(len - 1),
5742            (None, 1) => return,
5743            (Some(i), -1) => i.checked_sub(1),
5744            (Some(i), 1) if i + 1 < len => Some(i + 1),
5745            _ => None,
5746        };
5747        let Some(idx) = next_idx else {
5748            return;
5749        };
5750        self.vim.search_history_cursor = Some(idx);
5751        let text = self.vim.search_history[idx].clone();
5752        if let Some(prompt) = self.vim.search_prompt.as_mut() {
5753            prompt.cursor = text.chars().count();
5754            prompt.text = text.clone();
5755        }
5756        self.push_search_pattern(&text);
5757    }
5758
5759    // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
5760    //
5761    // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
5762    // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
5763
5764    /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
5765    ///
5766    /// Call this at the start of every step; pass the returned
5767    /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
5768    ///
5769    /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
5770    /// Returns `Err(consumed)` when the prelude itself handled the input
5771    /// (macro-stop chord); in that case skip the FSM body and do NOT call
5772    /// `end_step` — the macro-stop path is a true short-circuit with no
5773    /// epilogue needed.
5774    ///
5775    /// This method does NOT handle the search-prompt intercept — callers
5776    /// must check `search_prompt_state().is_some()` before calling `begin_step`
5777    /// and dispatch to the search-prompt FSM body directly.
5778    pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
5779        use crate::input::Key;
5780        use vim::{Mode, Pending};
5781        // ── Timestamps ───────────────────────────────────────────────────────
5782        // Phase 7f: sync buffer before motion handlers see it.
5783        self.sync_buffer_content_from_textarea();
5784        // `:set timeoutlen` chord-timeout handling.
5785        let now = std::time::Instant::now();
5786        let host_now = self.host.now();
5787        let timed_out = match self.vim.last_input_host_at {
5788            Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
5789            None => false,
5790        };
5791        if timed_out {
5792            let chord_in_flight = !matches!(self.vim.pending, Pending::None)
5793                || self.vim.count != 0
5794                || self.vim.pending_register.is_some()
5795                || self.vim.insert_pending_register;
5796            if chord_in_flight {
5797                self.vim.clear_pending_prefix();
5798            }
5799        }
5800        self.vim.last_input_at = Some(now);
5801        self.vim.last_input_host_at = Some(host_now);
5802        // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
5803        if self.vim.recording_macro.is_some()
5804            && !self.vim.replaying_macro
5805            && matches!(self.vim.pending, Pending::None)
5806            && self.vim.mode != Mode::Insert
5807            && input.key == Key::Char('q')
5808            && !input.ctrl
5809            && !input.alt
5810        {
5811            let reg = self.vim.recording_macro.take().unwrap();
5812            let keys = std::mem::take(&mut self.vim.recording_keys);
5813            let text = crate::input::encode_macro(&keys);
5814            self.set_named_register_text(reg.to_ascii_lowercase(), text);
5815            return Err(true);
5816        }
5817        // ── Snapshots for epilogue ────────────────────────────────────────────
5818        let pending_was_macro_chord = matches!(
5819            self.vim.pending,
5820            Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
5821        );
5822        let was_insert = self.vim.mode == Mode::Insert;
5823        let pre_visual_snapshot = match self.vim.mode {
5824            Mode::Visual => Some(vim::LastVisual {
5825                mode: Mode::Visual,
5826                anchor: self.vim.visual_anchor,
5827                cursor: self.cursor(),
5828                block_vcol: 0,
5829            }),
5830            Mode::VisualLine => Some(vim::LastVisual {
5831                mode: Mode::VisualLine,
5832                anchor: (self.vim.visual_line_anchor, 0),
5833                cursor: self.cursor(),
5834                block_vcol: 0,
5835            }),
5836            Mode::VisualBlock => Some(vim::LastVisual {
5837                mode: Mode::VisualBlock,
5838                anchor: self.vim.block_anchor,
5839                cursor: self.cursor(),
5840                block_vcol: self.vim.block_vcol,
5841            }),
5842            _ => None,
5843        };
5844        Ok(StepBookkeeping {
5845            pending_was_macro_chord,
5846            was_insert,
5847            pre_visual_snapshot,
5848        })
5849    }
5850
5851    /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
5852    ///
5853    /// `input` is the same input that was passed to `begin_step`.
5854    /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
5855    /// `consumed` is the return value of the FSM body; this method returns
5856    /// it after running all epilogue invariants.
5857    ///
5858    /// Must NOT be called when `begin_step` returned `Err(...)`.
5859    pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
5860        use crate::input::Key;
5861        use vim::{Mode, Pending};
5862        let StepBookkeeping {
5863            pending_was_macro_chord,
5864            was_insert,
5865            pre_visual_snapshot,
5866        } = bk;
5867        // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
5868        if let Some(snap) = pre_visual_snapshot
5869            && !matches!(
5870                self.vim.mode,
5871                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
5872            )
5873        {
5874            let (lo, hi) = match snap.mode {
5875                Mode::Visual => {
5876                    if snap.anchor <= snap.cursor {
5877                        (snap.anchor, snap.cursor)
5878                    } else {
5879                        (snap.cursor, snap.anchor)
5880                    }
5881                }
5882                Mode::VisualLine => {
5883                    let r_lo = snap.anchor.0.min(snap.cursor.0);
5884                    let r_hi = snap.anchor.0.max(snap.cursor.0);
5885                    let vl_rope = self.buffer().rope();
5886                    let r_hi_clamped = r_hi.min(vl_rope.len_lines().saturating_sub(1));
5887                    let last_col = hjkl_buffer::rope_line_str(&vl_rope, r_hi_clamped)
5888                        .chars()
5889                        .count()
5890                        .saturating_sub(1);
5891                    ((r_lo, 0), (r_hi, last_col))
5892                }
5893                Mode::VisualBlock => {
5894                    let (r1, c1) = snap.anchor;
5895                    let (r2, c2) = snap.cursor;
5896                    ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
5897                }
5898                _ => {
5899                    if snap.anchor <= snap.cursor {
5900                        (snap.anchor, snap.cursor)
5901                    } else {
5902                        (snap.cursor, snap.anchor)
5903                    }
5904                }
5905            };
5906            self.set_mark('<', lo);
5907            self.set_mark('>', hi);
5908            self.vim.last_visual = Some(snap);
5909        }
5910        // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
5911        if !was_insert
5912            && self.vim.one_shot_normal
5913            && self.vim.mode == Mode::Normal
5914            && matches!(self.vim.pending, Pending::None)
5915        {
5916            self.vim.one_shot_normal = false;
5917            self.vim.mode = Mode::Insert;
5918        }
5919        // ── Content + viewport sync ───────────────────────────────────────────
5920        self.sync_buffer_content_from_textarea();
5921        if !self.vim.viewport_pinned {
5922            self.ensure_cursor_in_scrolloff();
5923        }
5924        self.vim.viewport_pinned = false;
5925        // ── Recorder hook ─────────────────────────────────────────────────────
5926        if self.vim.recording_macro.is_some()
5927            && !self.vim.replaying_macro
5928            && input.key != Key::Char('q')
5929            && !pending_was_macro_chord
5930        {
5931            self.vim.recording_keys.push(input);
5932        }
5933        // ── Phase 6.3: current_mode sync ─────────────────────────────────────
5934        self.vim.current_mode = self.vim.public_mode();
5935        // BLAME is a Normal-only read-only view; any transition out of Normal
5936        // (a keyboard mode switch, etc.) implicitly leaves it.
5937        vim::drop_blame_if_left_normal(self);
5938        consumed
5939    }
5940
5941    // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
5942
5943    /// `true` when the editor is in any visual mode (Visual / VisualLine /
5944    /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
5945    pub fn is_visual(&self) -> bool {
5946        matches!(
5947            self.vim.mode,
5948            vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
5949        )
5950    }
5951
5952    /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
5953    /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
5954    /// virtual column, which survives j/k clamping to shorter rows).
5955    ///
5956    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
5957    /// extents needed for VisualBlock `I` / `A` / `r` without accessing
5958    /// engine-private helpers.
5959    pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
5960        let (ar, ac) = self.vim.block_anchor;
5961        let (cr, _) = self.cursor();
5962        let cc = self.vim.block_vcol;
5963        let top = ar.min(cr);
5964        let bot = ar.max(cr);
5965        let left = ac.min(cc);
5966        let right = ac.max(cc);
5967        (top, bot, left, right)
5968    }
5969
5970    /// Return the character count (code-point count) of line `row`, or `0`
5971    /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
5972    /// I / A column computations.
5973    pub fn line_char_count(&self, row: usize) -> usize {
5974        buf_line_chars(&self.buffer, row)
5975    }
5976
5977    /// Apply operator over `motion` with `count` repetitions. The full
5978    /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
5979    ///
5980    /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
5981    /// relocated `handle_after_op` can call it directly with a parsed `Motion`
5982    /// without re-entering the engine FSM.
5983    pub fn apply_op_with_motion_direct(
5984        &mut self,
5985        op: crate::vim::Operator,
5986        motion: &crate::vim::Motion,
5987        count: usize,
5988    ) {
5989        vim::apply_op_with_motion(self, op, motion, count);
5990    }
5991
5992    /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
5993    /// `delta = 1` increments; `delta = -1` decrements; larger deltas
5994    /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
5995    /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
5996    pub fn adjust_number(&mut self, delta: i64) {
5997        vim::adjust_number(self, delta);
5998    }
5999
6000    /// Open the `/` or `?` search prompt. `forward = true` for `/`,
6001    /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
6002    /// dispatch `/` and `?` without re-entering the engine FSM.
6003    pub fn enter_search(&mut self, forward: bool) {
6004        vim::enter_search(self, forward);
6005    }
6006
6007    /// Enter Insert mode at the left edge of a VisualBlock selection for
6008    /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
6009    /// then begins an insert session with `InsertReason::BlockEdge`.
6010    ///
6011    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
6012    /// VisualBlock `I` command without accessing engine-private helpers.
6013    pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
6014        self.jump_cursor(top, col);
6015        self.vim.mode = vim::Mode::Normal;
6016        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
6017    }
6018
6019    /// Enter Insert mode at the right edge of a VisualBlock selection for
6020    /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
6021    /// then begins an insert session with `InsertReason::BlockEdge`.
6022    ///
6023    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
6024    /// VisualBlock `A` command without accessing engine-private helpers.
6025    pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
6026        self.jump_cursor(top, col);
6027        self.vim.mode = vim::Mode::Normal;
6028        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
6029    }
6030
6031    /// Execute a motion (cursor movement), push to the jumplist for big jumps,
6032    /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
6033    /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
6034    pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
6035        vim::execute_motion(self, motion, count);
6036    }
6037
6038    /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
6039    /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
6040    /// non-h/l motions leave it alone so the intended column survives clamping
6041    /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
6042    pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
6043        vim::update_block_vcol(self, motion);
6044    }
6045
6046    /// Apply `op` over the current visual selection (char-wise, linewise, or
6047    /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
6048    /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
6049    pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
6050        vim::apply_visual_operator(self, op);
6051    }
6052
6053    /// Replace each character cell in the current VisualBlock selection with
6054    /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
6055    /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
6056    pub fn replace_block_char(&mut self, ch: char) {
6057        vim::block_replace(self, ch);
6058    }
6059
6060    /// Extend the current visual selection to cover the text object identified
6061    /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
6062    /// via `text_object_range`, then updates the visual anchor and cursor.
6063    ///
6064    /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
6065    /// in `hjkl-vim::normal::handle_visual_text_obj`.
6066    pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
6067        use crate::vim::{Mode, TextObject};
6068        let obj = match ch {
6069            'w' => TextObject::Word { big: false },
6070            'W' => TextObject::Word { big: true },
6071            '"' | '\'' | '`' => TextObject::Quote(ch),
6072            '(' | ')' | 'b' => TextObject::Bracket('('),
6073            '[' | ']' => TextObject::Bracket('['),
6074            '{' | '}' | 'B' => TextObject::Bracket('{'),
6075            '<' | '>' => TextObject::Bracket('<'),
6076            'p' => TextObject::Paragraph,
6077            't' => TextObject::XmlTag,
6078            's' => TextObject::Sentence,
6079            _ => return,
6080        };
6081        let Some((start, end, kind)) = vim::text_object_range(self, obj, inner, 1) else {
6082            return;
6083        };
6084        match kind {
6085            crate::vim::RangeKind::Linewise => {
6086                self.vim.visual_line_anchor = start.0;
6087                self.vim.mode = Mode::VisualLine;
6088                self.vim.current_mode = VimMode::VisualLine;
6089                self.jump_cursor(end.0, 0);
6090            }
6091            _ => {
6092                self.vim.mode = Mode::Visual;
6093                self.vim.current_mode = VimMode::Visual;
6094                self.vim.visual_anchor = (start.0, start.1);
6095                let (er, ec) = vim::retreat_one(self, end);
6096                self.jump_cursor(er, ec);
6097            }
6098        }
6099    }
6100}
6101
6102/// Visual column of the character at `char_col` in `line`, treating `\t`
6103/// as expansion to the next `tab_width` stop and every other char as
6104/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
6105/// the cursor math elsewhere also assumes single-cell chars.
6106fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
6107    let mut visual = 0usize;
6108    for (i, ch) in line.chars().enumerate() {
6109        if i >= char_col {
6110            break;
6111        }
6112        if ch == '\t' {
6113            visual += tab_width - (visual % tab_width);
6114        } else {
6115            visual += 1;
6116        }
6117    }
6118    visual
6119}
6120
6121#[cfg(test)]
6122mod shift_syntax_spans_tests {
6123    use super::*;
6124    use crate::types::{ContentEdit, DefaultHost, Options, Style};
6125    use hjkl_buffer::Buffer;
6126
6127    fn ed_with_spans(line_count: usize) -> Editor<Buffer, DefaultHost> {
6128        let text = (0..line_count)
6129            .map(|i| format!("row{i}"))
6130            .collect::<Vec<_>>()
6131            .join("\n");
6132        let buf = Buffer::from_str(&text);
6133        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
6134        // Synthesize span rows so we can detect which survive a shift.
6135        // Use a distinct fg colour per row so spans are identifiable.
6136        let style = Style::default();
6137        let spans: Vec<Vec<(usize, usize, Style)>> =
6138            (0..line_count).map(|_| vec![(0, 1, style)]).collect();
6139        e.install_syntax_spans(spans);
6140        e
6141    }
6142
6143    fn edit_insert_newline_at(row: u32, col: u32) -> ContentEdit {
6144        // Pressing Enter: zero-width insertion that produces one new row.
6145        ContentEdit {
6146            start_byte: 0,
6147            old_end_byte: 0,
6148            new_end_byte: 1,
6149            start_position: (row, col),
6150            old_end_position: (row, col),
6151            new_end_position: (row + 1, 0),
6152        }
6153    }
6154
6155    fn edit_join_rows(row: u32, col: u32) -> ContentEdit {
6156        // Backspace at start of `row+1`: removes the newline, joining the
6157        // two rows. old_end is on `row+1`, new_end on `row`.
6158        ContentEdit {
6159            start_byte: 0,
6160            old_end_byte: 1,
6161            new_end_byte: 0,
6162            start_position: (row, col),
6163            old_end_position: (row + 1, 0),
6164            new_end_position: (row, col),
6165        }
6166    }
6167
6168    #[test]
6169    fn insert_grows_buffer_spans_in_place() {
6170        let mut e = ed_with_spans(4);
6171        // Newline at row 1 → buffer grew by one row.
6172        e.shift_syntax_spans_for_edits(&[edit_insert_newline_at(1, 1)]);
6173        assert_eq!(
6174            e.buffer_spans().len(),
6175            5,
6176            "row-count grew → spans rows must match"
6177        );
6178        // The empty row should be at index 2 (right after the split point).
6179        assert!(e.buffer_spans()[2].is_empty(), "inserted row sits at oer+1");
6180        // Surrounding rows kept their content.
6181        assert!(!e.buffer_spans()[0].is_empty());
6182        assert!(!e.buffer_spans()[1].is_empty());
6183        assert!(!e.buffer_spans()[3].is_empty());
6184        assert!(!e.buffer_spans()[4].is_empty());
6185    }
6186
6187    #[test]
6188    fn delete_shrinks_buffer_spans_in_place() {
6189        let mut e = ed_with_spans(4);
6190        e.shift_syntax_spans_for_edits(&[edit_join_rows(1, 1)]);
6191        assert_eq!(
6192            e.buffer_spans().len(),
6193            3,
6194            "row-count shrank → spans rows must match"
6195        );
6196    }
6197
6198    #[test]
6199    fn same_row_edit_leaves_rows_untouched() {
6200        let mut e = ed_with_spans(3);
6201        let edit = ContentEdit {
6202            start_byte: 0,
6203            old_end_byte: 0,
6204            new_end_byte: 1,
6205            start_position: (1, 0),
6206            old_end_position: (1, 0),
6207            new_end_position: (1, 1),
6208        };
6209        e.shift_syntax_spans_for_edits(&[edit]);
6210        assert_eq!(e.buffer_spans().len(), 3);
6211        for row in 0..3 {
6212            assert!(
6213                !e.buffer_spans()[row].is_empty(),
6214                "row {row} should still hold its span"
6215            );
6216        }
6217    }
6218
6219    #[test]
6220    fn ordered_edits_apply_against_prior_state() {
6221        let mut e = ed_with_spans(3);
6222        // Two consecutive inserts: each adds a row.
6223        e.shift_syntax_spans_for_edits(&[
6224            edit_insert_newline_at(0, 1),
6225            edit_insert_newline_at(1, 1),
6226        ]);
6227        assert_eq!(e.buffer_spans().len(), 5);
6228    }
6229
6230    /// Build a buffer with `line_count` rows where row `i` has a span at
6231    /// column `i + 1` so the rows are independently identifiable after a
6232    /// shift (otherwise all spans look identical and can't tell which
6233    /// original row's spans landed at which post-shift index).
6234    fn ed_with_distinguishable_spans(line_count: usize) -> Editor<Buffer, DefaultHost> {
6235        let text = (0..line_count)
6236            .map(|i| format!("rowwwwwwwwww{i}"))
6237            .collect::<Vec<_>>()
6238            .join("\n");
6239        let buf = Buffer::from_str(&text);
6240        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
6241        let style = Style::default();
6242        let spans: Vec<Vec<(usize, usize, Style)>> = (0..line_count)
6243            .map(|i| vec![(i + 1, i + 2, style)])
6244            .collect();
6245        e.install_syntax_spans(spans);
6246        e
6247    }
6248
6249    /// Regression for off-by-one in `shift_syntax_spans_for_edits`.
6250    ///
6251    /// `P` (paste-before) at column 0 of row 0 inserts new lines BEFORE
6252    /// row 0. The pre-paste rows should shift down by N. The fix inserts
6253    /// empty rows at idx `start.row` (not `oer + 1`) when `start.col == 0`.
6254    ///
6255    /// Symptom before the fix: row 0's spans stayed at idx 0 after a
6256    /// 4-row `ggP`, but the file's row 0 was now the pasted content (no
6257    /// spans available yet). Display: pasted row 0 painted with the
6258    /// pre-paste row 0's spans (LUCKILY identical content in many cases)
6259    /// while the *shifted* pre-paste row 0 (now at file row 4) painted
6260    /// with the pre-paste row 1's spans — visible as the WRONG row
6261    /// showing the wrong-row colours.
6262    #[test]
6263    fn shift_for_paste_at_start_of_row_zero() {
6264        let mut e = ed_with_distinguishable_spans(7);
6265        // Snapshot: row i has a span at col (i+1, i+2).
6266        let pre = e.buffer_spans().to_vec();
6267        // P at (0, 0) inserting 4 lines.
6268        let edit = ContentEdit {
6269            start_byte: 0,
6270            old_end_byte: 0,
6271            new_end_byte: 4,
6272            start_position: (0, 0),
6273            old_end_position: (0, 0),
6274            new_end_position: (4, 0),
6275        };
6276        e.shift_syntax_spans_for_edits(&[edit]);
6277        assert_eq!(e.buffer_spans().len(), 11, "row count grew by 4");
6278        // Rows 0..4 are the new pasted lines — should be EMPTY placeholders.
6279        for row in 0..4 {
6280            assert!(
6281                e.buffer_spans()[row].is_empty(),
6282                "row {row} (new paste) must be empty placeholder, got {:?}",
6283                e.buffer_spans()[row]
6284            );
6285        }
6286        // Rows 4..11 are the original rows 0..7 shifted down by 4.
6287        for (orig_row, orig_spans) in pre.iter().enumerate() {
6288            let new_row = orig_row + 4;
6289            assert_eq!(
6290                &e.buffer_spans()[new_row],
6291                orig_spans,
6292                "original row {orig_row} should be at file row {new_row} after \
6293                 paste-before-row-0"
6294            );
6295        }
6296    }
6297
6298    /// Same idea for paste at start of a non-zero row: `2GP` inserts 3
6299    /// lines before row 2.
6300    #[test]
6301    fn shift_for_paste_at_start_of_middle_row() {
6302        let mut e = ed_with_distinguishable_spans(5);
6303        let pre = e.buffer_spans().to_vec();
6304        // Insert 3 lines at (2, 0).
6305        let edit = ContentEdit {
6306            start_byte: 0,
6307            old_end_byte: 0,
6308            new_end_byte: 3,
6309            start_position: (2, 0),
6310            old_end_position: (2, 0),
6311            new_end_position: (5, 0),
6312        };
6313        e.shift_syntax_spans_for_edits(&[edit]);
6314        assert_eq!(e.buffer_spans().len(), 8);
6315        // Rows 0..2 unchanged (before the insertion point).
6316        assert_eq!(e.buffer_spans()[0], pre[0]);
6317        assert_eq!(e.buffer_spans()[1], pre[1]);
6318        // Rows 2..5 are new pasted lines.
6319        for row in 2..5 {
6320            assert!(
6321                e.buffer_spans()[row].is_empty(),
6322                "row {row} must be empty placeholder"
6323            );
6324        }
6325        // Rows 5..8 are originals 2..5 shifted down by 3.
6326        for (orig_row, orig_spans) in pre.iter().enumerate().take(5).skip(2) {
6327            let new_row = orig_row + 3;
6328            assert_eq!(
6329                &e.buffer_spans()[new_row],
6330                orig_spans,
6331                "original row {orig_row} should land at file row {new_row}"
6332            );
6333        }
6334    }
6335
6336    /// Regression: pasting N rows at the beginning of the buffer used to
6337    /// run `Vec::insert(0, ...)` once per row → O(N²) memmove. samply
6338    /// showed this path eating 87 % of paste CPU on a 60 k-row paste.
6339    /// The splice rewrite is O(N).
6340    ///
6341    /// Asserting a hard wall-clock bound is brittle on slow CI, so we
6342    /// pick a budget the old code blows past by >10×: 60 k rows in
6343    /// under 200 ms even on a debug build. Old impl: ~3-5 seconds.
6344    #[test]
6345    fn shift_for_60k_row_paste_at_row_zero_is_under_200ms() {
6346        let mut e = ed_with_distinguishable_spans(8);
6347        let edit = ContentEdit {
6348            start_byte: 0,
6349            old_end_byte: 0,
6350            new_end_byte: 60_000,
6351            start_position: (0, 0),
6352            old_end_position: (0, 0),
6353            new_end_position: (60_000, 0),
6354        };
6355        let t = std::time::Instant::now();
6356        e.shift_syntax_spans_for_edits(&[edit]);
6357        let elapsed = t.elapsed();
6358        assert!(
6359            elapsed.as_millis() < 200,
6360            "60k-row shift took {elapsed:?}; budget is 200 ms (catches \
6361             reintroduction of the O(N²) per-row insert loop)"
6362        );
6363        assert_eq!(e.buffer_spans().len(), 60_008);
6364    }
6365
6366    /// Regression: `push_undo` used to clone every line into a
6367    /// `Vec<String>` (162 k heap allocations on a 162 k-row buffer per
6368    /// snapshot). Now stores an `Arc<String>` shared with
6369    /// `Buffer::content_joined`'s per-dirty_gen cache — a warm snapshot
6370    /// is an `Arc::clone` (one ptr bump).
6371    ///
6372    /// Test: snapshot a 60 k-row buffer 100 times. With the Arc impl
6373    /// this is essentially free (one join then 99 Arc::clones). The
6374    /// old `Vec<String>` impl required 60 k allocations per call =
6375    /// 6 M allocations, easily seconds even on release.
6376    #[test]
6377    fn push_undo_snapshot_arc_clone_is_under_100ms_for_100_snapshots() {
6378        use crate::types::{DefaultHost, Options};
6379        let text = "x\n".repeat(60_000);
6380        let buf = hjkl_buffer::Buffer::from_str(&text);
6381        let mut e = Editor::new(buf, DefaultHost::default(), Options::default());
6382        // Warm the cache: one join, subsequent snapshots Arc::clone it.
6383        e.push_undo();
6384        let t = std::time::Instant::now();
6385        for _ in 0..100 {
6386            e.push_undo();
6387        }
6388        let elapsed = t.elapsed();
6389        assert!(
6390            elapsed.as_millis() < 100,
6391            "100 snapshots of a 60k-row buffer took {elapsed:?}; budget \
6392             100 ms. Likely regressed to per-line cloning."
6393        );
6394    }
6395}
6396
6397#[cfg(test)]
6398mod earlier_later_tests {
6399    use super::*;
6400    use crate::types::{DefaultHost, Options};
6401    use hjkl_buffer::Buffer;
6402    use std::time::{Duration, SystemTime};
6403
6404    fn make_ed(content: &str) -> Editor<Buffer, DefaultHost> {
6405        let buf = Buffer::from_str(content);
6406        Editor::new(buf, DefaultHost::default(), Options::default())
6407    }
6408
6409    // ── step-based ───────────────────────────────────────────────────────────
6410
6411    #[test]
6412    fn earlier_by_steps_n_undoes_n_changes() {
6413        let mut ed = make_ed("hello");
6414        ed.push_undo(); // snap 1
6415        ed.push_undo(); // snap 2
6416        ed.push_undo(); // snap 3
6417        assert_eq!(ed.undo_stack_len(), 3);
6418        let applied = ed.earlier_by_steps(2);
6419        assert_eq!(applied, 2);
6420        assert_eq!(ed.undo_stack_len(), 1);
6421    }
6422
6423    #[test]
6424    fn earlier_by_steps_caps_at_stack_size() {
6425        let mut ed = make_ed("hello");
6426        ed.push_undo(); // snap 1
6427        // Ask for 10 but only 1 available.
6428        let applied = ed.earlier_by_steps(10);
6429        assert_eq!(applied, 1);
6430        assert_eq!(ed.undo_stack_len(), 0);
6431    }
6432
6433    #[test]
6434    fn later_by_steps_n_redoes_n_changes() {
6435        let mut ed = make_ed("hello");
6436        ed.push_undo(); // snap 1
6437        ed.push_undo(); // snap 2
6438        ed.push_undo(); // snap 3
6439        // Undo all 3 so they're on redo stack.
6440        ed.earlier_by_steps(3);
6441        assert_eq!(ed.undo_stack_len(), 0);
6442        let applied = ed.later_by_steps(2);
6443        assert_eq!(applied, 2);
6444        assert_eq!(ed.undo_stack_len(), 2);
6445    }
6446
6447    #[test]
6448    fn later_by_steps_caps_at_redo_stack_size() {
6449        let mut ed = make_ed("hello");
6450        ed.push_undo(); // snap 1
6451        ed.earlier_by_steps(1); // moves to redo
6452        let applied = ed.later_by_steps(99);
6453        assert_eq!(applied, 1);
6454    }
6455
6456    // ── time-based ───────────────────────────────────────────────────────────
6457
6458    fn epoch_plus(secs: u64) -> SystemTime {
6459        SystemTime::UNIX_EPOCH + Duration::from_secs(secs)
6460    }
6461
6462    #[test]
6463    fn earlier_by_time_stops_at_target_boundary() {
6464        let mut ed = make_ed("hello");
6465        // Push 3 entries at t-30s, t-20s, t-10s (relative to epoch).
6466        ed.push_undo_at(epoch_plus(30));
6467        ed.push_undo_at(epoch_plus(40));
6468        ed.push_undo_at(epoch_plus(50));
6469        // Redo stack is empty; undo has 3 entries.
6470        // target = epoch+35 → should undo entries at t=50 and t=40, stop at t=30
6471        let target = epoch_plus(35);
6472        let applied = ed.earlier_by_time(target);
6473        assert_eq!(applied, 2, "should undo t=50 and t=40; stop at t=30");
6474        assert_eq!(ed.undo_stack_len(), 1, "t=30 entry remains");
6475    }
6476
6477    #[test]
6478    fn earlier_by_time_empty_stack_returns_zero() {
6479        let mut ed = make_ed("hello");
6480        let applied = ed.earlier_by_time(epoch_plus(999));
6481        assert_eq!(applied, 0);
6482        assert_eq!(ed.undo_stack_len(), 0);
6483    }
6484
6485    #[test]
6486    fn later_by_time_target_in_future_redoes_all() {
6487        let mut ed = make_ed("hello");
6488        ed.push_undo_at(epoch_plus(10));
6489        ed.push_undo_at(epoch_plus(20));
6490        // Undo both → they move to redo stack with their timestamps preserved.
6491        ed.earlier_by_steps(2);
6492        // target far in future: should redo all.
6493        let applied = ed.later_by_time(epoch_plus(9999));
6494        assert_eq!(applied, 2);
6495        assert_eq!(ed.undo_stack_len(), 2);
6496    }
6497}
6498
6499#[cfg(test)]
6500mod insert_mode_scrolloff_tests {
6501    use super::*;
6502    use crate::types::{DefaultHost, Host, Options};
6503    use crate::vim::Mode;
6504    use hjkl_buffer::Buffer;
6505
6506    fn ed_with_lines(line_count: usize) -> Editor<Buffer, DefaultHost> {
6507        let text = (0..line_count)
6508            .map(|i| format!("row{i}"))
6509            .collect::<Vec<_>>()
6510            .join("\n");
6511        let buf = Buffer::from_str(&text);
6512        let mut e = Editor::new(buf, DefaultHost::new(), Options::default());
6513        // Viewport: 20 rows tall, starts at top.
6514        let vp = e.host_mut().viewport_mut();
6515        vp.width = 80;
6516        vp.height = 20;
6517        vp.top_row = 0;
6518        vp.top_col = 0;
6519        e.set_viewport_height(20);
6520        e.vim.mode = Mode::Insert;
6521        e
6522    }
6523
6524    /// Regression: holding Enter in insert mode used to scroll the cursor
6525    /// off the viewport because `insert_newline` (called from the app's
6526    /// `dispatch_insert_key`) bypasses the FSM `step` that runs
6527    /// `ensure_cursor_in_scrolloff`. The post-mutation helper now runs
6528    /// scrolloff for every insert primitive — the cursor must stay
6529    /// within `scrolloff` rows of the bottom edge.
6530    #[test]
6531    fn insert_newline_keeps_cursor_in_scrolloff() {
6532        let mut e = ed_with_lines(200);
6533        // Park cursor at the bottom edge of the viewport (row 19).
6534        e.set_cursor_doc(19, 0);
6535        // Press Enter 50 times. Cursor moves down each newline; without
6536        // scrolloff the cursor would slide off the bottom of the
6537        // viewport at row 20+ and the user would type blind.
6538        for _ in 0..50 {
6539            e.insert_newline();
6540        }
6541        let (cursor_row, _) = e.cursor();
6542        let vp = e.host().viewport();
6543        let cursor_screen_row = cursor_row.saturating_sub(vp.top_row);
6544        let scrolloff = e.settings().scrolloff;
6545        let margin = scrolloff.min(vp.height as usize - 1) / 2;
6546        let max_screen_row = vp.height as usize - 1 - margin;
6547        assert!(
6548            cursor_screen_row <= max_screen_row,
6549            "cursor screen row {cursor_screen_row} exceeded scrolloff bound {max_screen_row} \
6550             (cursor_row={cursor_row}, vp.top_row={vp_top}, vp.height={vp_h})",
6551            vp_top = vp.top_row,
6552            vp_h = vp.height,
6553        );
6554    }
6555
6556    /// Same check for `insert_arrow(Down)` — cursor-only motion that also
6557    /// must trigger scrolloff.
6558    #[test]
6559    fn insert_arrow_down_keeps_cursor_in_scrolloff() {
6560        let mut e = ed_with_lines(200);
6561        e.set_cursor_doc(19, 0);
6562        for _ in 0..50 {
6563            e.insert_arrow(vim::InsertDir::Down);
6564        }
6565        let (cursor_row, _) = e.cursor();
6566        let vp = e.host().viewport();
6567        let cursor_screen_row = cursor_row.saturating_sub(vp.top_row);
6568        let scrolloff = e.settings().scrolloff;
6569        let margin = scrolloff.min(vp.height as usize - 1) / 2;
6570        let max_screen_row = vp.height as usize - 1 - margin;
6571        assert!(
6572            cursor_screen_row <= max_screen_row,
6573            "cursor screen row {cursor_screen_row} exceeded scrolloff bound {max_screen_row}"
6574        );
6575    }
6576
6577    /// Scrolloff must be measured in SCREEN rows, not doc rows: a closed
6578    /// fold between `top_row` and the cursor collapses its hidden body to
6579    /// one screen row. The old doc-row arithmetic left the cursor only a
6580    /// few SCREEN rows below the top (it scrolled as if the fold's hidden
6581    /// rows still occupied screen space), violating the top margin. The
6582    /// fold-aware path keeps the cursor's screen row inside
6583    /// `[margin, height - 1 - margin]`.
6584    #[test]
6585    fn scrolloff_is_fold_aware_screen_rows() {
6586        let mut e = ed_with_lines(200);
6587        // Close a fold whose body sits between the viewport top and the
6588        // cursor: rows 11..=25 are hidden (15 doc rows collapse to 0).
6589        e.buffer_mut().add_fold(10, 25, true);
6590        // Jump below the fold. The doc-based viewport pre-scroll parks the
6591        // cursor near the *top* of the screen because the fold ate the
6592        // space above it; scrolloff must pull `top_row` back so the cursor
6593        // is at least `margin` screen rows from the top.
6594        e.set_cursor_doc(30, 0);
6595        e.ensure_cursor_in_scrolloff();
6596
6597        let vp = e.host().viewport();
6598        let (cursor_row, _) = e.cursor();
6599        // Fold-aware cursor screen row = count of VISIBLE rows in
6600        // [top_row, cursor_row).
6601        let screen_row = (vp.top_row..cursor_row)
6602            .filter(|&r| !e.buffer().is_row_hidden(r))
6603            .count();
6604        let height = vp.height as usize;
6605        let margin = e.settings().scrolloff.min(height.saturating_sub(1) / 2);
6606        let bottom_bound = height - 1 - margin;
6607        // Old (doc-row) code produced screen_row = 4 here — below the top
6608        // margin of 5. The fix keeps it within the screen-row band.
6609        assert!(
6610            screen_row >= margin,
6611            "cursor screen row {screen_row} is inside the top margin {margin} \
6612             (top_row={top}, cursor_row={cursor_row})",
6613            top = vp.top_row,
6614        );
6615        assert!(
6616            screen_row <= bottom_bound,
6617            "cursor screen row {screen_row} exceeds bottom bound {bottom_bound}"
6618        );
6619        // Cursor itself must never be on a hidden row.
6620        assert!(!e.buffer().is_row_hidden(cursor_row));
6621    }
6622
6623    /// A `G`-style jump to the bottom of a fold-heavy buffer must land the
6624    /// cursor on the last screen row (bottom-margin clamp) and keep it
6625    /// visible — exercising the O(height) fold-aware path that replaced the
6626    /// O(n²) per-step walk that made `G` laggy on real (open-fold) source.
6627    #[test]
6628    fn scrolloff_fold_big_jump_lands_at_bottom() {
6629        let mut e = ed_with_lines(400);
6630        // Several OPEN auto-fold-style ranges scattered through the file —
6631        // open folds don't hide rows but still route through the fold path
6632        // (this is the real Rust-file case: many folds, all open).
6633        for start in (0..390).step_by(10) {
6634            e.buffer_mut().add_fold(start, start + 5, false);
6635        }
6636        // Jump to the last line from the top.
6637        e.set_cursor_doc(399, 0);
6638        e.ensure_cursor_in_scrolloff();
6639
6640        let vp = e.host().viewport();
6641        let (cursor_row, _) = e.cursor();
6642        let height = vp.height as usize;
6643        let screen_row = (vp.top_row..cursor_row)
6644            .filter(|&r| !e.buffer().is_row_hidden(r))
6645            .count();
6646        // Cursor visible and at the bottom (all folds open → screen == doc rows,
6647        // so the bottom row sits at height-1).
6648        assert!(
6649            screen_row < height,
6650            "cursor off-screen: screen_row={screen_row}"
6651        );
6652        assert_eq!(
6653            screen_row,
6654            height - 1,
6655            "G should bottom-align the cursor (top_row={}, cursor={cursor_row})",
6656            vp.top_row,
6657        );
6658    }
6659
6660    /// Perf guard: scrolloff on a fold-heavy buffer must be O(height), not
6661    /// O(n²) in the jump distance. A `G`-to-bottom jump over 50 k rows with a
6662    /// fold every 10 lines must finish well under budget. The old
6663    /// re-walk-per-step path was ~50k × ~50k line reads = seconds-to-minutes
6664    /// even in debug; the O(height) path is microseconds. Budget is generous
6665    /// (200 ms) so it never false-fails on slow CI but still catches a
6666    /// reintroduced per-step rescan, which would blow past it by orders of
6667    /// magnitude.
6668    #[test]
6669    fn scrolloff_fold_big_jump_is_under_200ms() {
6670        let mut e = ed_with_lines(50_000);
6671        for start in (0..49_990).step_by(10) {
6672            e.buffer_mut().add_fold(start, start + 5, false);
6673        }
6674        e.set_cursor_doc(49_999, 0);
6675        let t = std::time::Instant::now();
6676        e.ensure_cursor_in_scrolloff();
6677        let elapsed = t.elapsed();
6678        assert!(
6679            elapsed.as_millis() < 200,
6680            "fold-heavy G-to-bottom took {elapsed:?}; budget 200 ms (catches \
6681             reintroduction of the O(n²) per-step screen-row rescan)"
6682        );
6683    }
6684}
6685
6686#[cfg(test)]
6687mod blame_view_mode_tests {
6688    use super::*;
6689    use crate::types::{DefaultHost, Options};
6690    use hjkl_buffer::Buffer;
6691
6692    fn make_ed(content: &str) -> Editor<Buffer, DefaultHost> {
6693        let buf = Buffer::from_str(content);
6694        Editor::new(buf, DefaultHost::default(), Options::default())
6695    }
6696
6697    #[test]
6698    fn enter_blame_sets_view_in_normal() {
6699        let mut ed = make_ed("hello\nworld");
6700        assert!(!ed.is_blame());
6701        assert_eq!(ed.view_mode(), crate::ViewMode::Normal);
6702        ed.enter_blame();
6703        assert!(ed.is_blame());
6704        assert_eq!(ed.view_mode(), crate::ViewMode::Blame);
6705    }
6706
6707    #[test]
6708    fn exit_blame_clears_view() {
6709        let mut ed = make_ed("hello");
6710        ed.enter_blame();
6711        ed.exit_blame();
6712        assert!(!ed.is_blame());
6713        assert_eq!(ed.view_mode(), crate::ViewMode::Normal);
6714    }
6715
6716    #[test]
6717    fn enter_blame_is_noop_outside_normal() {
6718        let mut ed = make_ed("hello");
6719        ed.set_mode(VimMode::Insert);
6720        ed.enter_blame();
6721        assert!(!ed.is_blame(), "BLAME is Normal-only");
6722        assert_eq!(ed.view_mode(), crate::ViewMode::Normal);
6723    }
6724
6725    #[test]
6726    fn entering_visual_drops_blame() {
6727        let mut ed = make_ed("hello\nworld");
6728        ed.enter_blame();
6729        assert!(ed.is_blame());
6730        // Mouse drag and keyboard `v` both funnel through this.
6731        ed.enter_visual_char();
6732        assert!(!ed.is_blame());
6733        assert_eq!(ed.view_mode(), crate::ViewMode::Normal);
6734        // Returning to Normal must NOT resurrect the overlay.
6735        ed.exit_visual_to_normal();
6736        assert!(!ed.is_blame());
6737    }
6738
6739    #[test]
6740    fn entering_insert_drops_blame() {
6741        let mut ed = make_ed("hello");
6742        ed.enter_blame();
6743        ed.enter_insert_i(1);
6744        assert!(!ed.is_blame());
6745        ed.leave_insert_to_normal();
6746        assert!(
6747            !ed.is_blame(),
6748            "overlay must not resurrect on Esc-to-Normal"
6749        );
6750    }
6751
6752    #[test]
6753    fn is_blame_masked_while_in_visual() {
6754        // Even before the overlay flag is dropped, is_blame() is masked on the
6755        // input mode so the renderer never frames blame outside Normal.
6756        let mut ed = make_ed("hello");
6757        ed.enter_blame();
6758        ed.set_mode(VimMode::Visual);
6759        assert!(!ed.is_blame());
6760    }
6761
6762    #[test]
6763    fn mutation_blocked_while_blame() {
6764        let mut ed = make_ed("hello");
6765        ed.enter_blame();
6766        let result = ed.mutate_edit(hjkl_buffer::Edit::InsertStr {
6767            at: hjkl_buffer::Position::new(0, 0),
6768            text: "XXX".to_string(),
6769        });
6770        // BLAME swallows the edit and hands back a self-inverse no-op.
6771        assert!(
6772            matches!(result, hjkl_buffer::Edit::InsertStr { ref text, .. } if text.is_empty()),
6773            "edit must be swallowed while BLAME is active"
6774        );
6775    }
6776}