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