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