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    /// True when the user's pending register selector is `+` or `*`.
1246    /// the host peeks this so it can refresh `sync_clipboard_register`
1247    /// only when a clipboard read is actually about to happen.
1248    pub fn pending_register_is_clipboard(&self) -> bool {
1249        matches!(self.vim.pending_register, Some('+') | Some('*'))
1250    }
1251
1252    /// Register currently being recorded into via `q{reg}`. `None` when
1253    /// no recording is active. Hosts use this to surface a "recording @r"
1254    /// indicator in the status line.
1255    pub fn recording_register(&self) -> Option<char> {
1256        self.vim.recording_macro
1257    }
1258
1259    /// Pending repeat count the user has typed but not yet resolved
1260    /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1261    /// Hosts surface this in a "showcmd" area.
1262    pub fn pending_count(&self) -> Option<u32> {
1263        self.vim.pending_count_val()
1264    }
1265
1266    /// The operator character for any in-flight operator that is waiting
1267    /// for a motion (e.g. `d` after the user types `d` but before a
1268    /// motion). Returns `None` when no operator is pending.
1269    pub fn pending_op(&self) -> Option<char> {
1270        self.vim.pending_op_char()
1271    }
1272
1273    /// `true` when the engine is in any pending chord state — waiting for
1274    /// the next key to complete a command (e.g. `r<char>` replace,
1275    /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1276    /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1277    /// register selection `"<reg>`, macro recording target, etc).
1278    ///
1279    /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1280    /// and forward keys directly to the engine so in-flight commands can
1281    /// complete without the host eating their continuation keys.
1282    pub fn is_chord_pending(&self) -> bool {
1283        self.vim.is_chord_pending()
1284    }
1285
1286    /// Read-only view of the jump-back list (positions pushed on "big"
1287    /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1288    #[allow(clippy::type_complexity)]
1289    pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1290        (&self.vim.jump_back, &self.vim.jump_fwd)
1291    }
1292
1293    /// Read-only view of the change list (positions of recent edits) plus
1294    /// the current walk cursor. Newest entry is at the back.
1295    pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1296        (&self.vim.change_list, self.vim.change_list_cursor)
1297    }
1298
1299    /// Replace the unnamed register without touching any other slot.
1300    /// For host-driven imports (e.g. system clipboard); operator
1301    /// code uses [`record_yank`] / [`record_delete`].
1302    pub fn set_yank(&mut self, text: impl Into<String>) {
1303        let text = text.into();
1304        let linewise = self.vim.yank_linewise;
1305        self.registers.unnamed = crate::registers::Slot { text, linewise };
1306    }
1307
1308    /// Record a yank into `"` and `"0`, plus the named target if the
1309    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1310    /// paste path.
1311    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1312        self.vim.yank_linewise = linewise;
1313        let target = self.vim.pending_register.take();
1314        self.registers.record_yank(text, linewise, target);
1315    }
1316
1317    /// Direct write to a named register slot — bypasses the unnamed
1318    /// `"` and `"0` updates that `record_yank` does. Used by the
1319    /// macro recorder so finishing a `q{reg}` recording doesn't
1320    /// pollute the user's last yank.
1321    pub(crate) fn set_named_register_text(&mut self, reg: char, text: String) {
1322        if let Some(slot) = match reg {
1323            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1324            'A'..='Z' => {
1325                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1326            }
1327            _ => None,
1328        } {
1329            slot.text = text;
1330            slot.linewise = false;
1331        }
1332    }
1333
1334    /// Record a delete / change into `"` and the `"1`–`"9` ring.
1335    /// Honours the active named-register prefix.
1336    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1337        self.vim.yank_linewise = linewise;
1338        let target = self.vim.pending_register.take();
1339        self.registers.record_delete(text, linewise, target);
1340    }
1341
1342    /// Install styled syntax spans using the engine-native
1343    /// [`crate::types::Style`]. Always available, regardless of the
1344    /// `ratatui` feature. Hosts depending on ratatui can use the
1345    /// ratatui-flavoured [`Editor::install_ratatui_syntax_spans`].
1346    ///
1347    /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1348    /// 0.1.0 freeze the unprefixed name is the universally-available
1349    /// engine-native variant ("engine never imports ratatui").
1350    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1351        let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1352            .map(|r| buf_line(&self.buffer, r).map(str::len).unwrap_or(0))
1353            .collect();
1354        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1355        #[cfg(feature = "ratatui")]
1356        let mut ratatui_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>> =
1357            Vec::with_capacity(spans.len());
1358        for (row, row_spans) in spans.iter().enumerate() {
1359            let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1360            let mut translated = Vec::with_capacity(row_spans.len());
1361            #[cfg(feature = "ratatui")]
1362            let mut translated_r = Vec::with_capacity(row_spans.len());
1363            for (start, end, style) in row_spans {
1364                let end_clamped = (*end).min(line_len);
1365                if end_clamped <= *start {
1366                    continue;
1367                }
1368                let id = self.intern_style(*style);
1369                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1370                #[cfg(feature = "ratatui")]
1371                translated_r.push((*start, end_clamped, engine_style_to_ratatui(*style)));
1372            }
1373            by_row.push(translated);
1374            #[cfg(feature = "ratatui")]
1375            ratatui_spans.push(translated_r);
1376        }
1377        self.buffer_spans = by_row;
1378        #[cfg(feature = "ratatui")]
1379        {
1380            self.styled_spans = ratatui_spans;
1381        }
1382    }
1383
1384    /// Intern a `ratatui::style::Style` and return the opaque id used
1385    /// in `hjkl_buffer::Span::style`. The ratatui-flavoured variant of
1386    /// [`Editor::intern_style`]. Linear-scan dedup — the table grows
1387    /// only as new tree-sitter token kinds appear, so it stays tiny.
1388    /// Behind the `ratatui` feature.
1389    ///
1390    /// Renamed from `intern_style` in 0.0.32 — at 0.1.0 freeze the
1391    /// unprefixed name belongs to the engine-native variant.
1392    #[cfg(feature = "ratatui")]
1393    pub fn intern_ratatui_style(&mut self, style: ratatui::style::Style) -> u32 {
1394        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1395            return idx as u32;
1396        }
1397        self.style_table.push(style);
1398        (self.style_table.len() - 1) as u32
1399    }
1400
1401    /// Read-only view of the style table — id `i` → `style_table[i]`.
1402    /// The render path passes a closure backed by this slice as the
1403    /// `StyleResolver` for `BufferView`. Behind the `ratatui` feature.
1404    #[cfg(feature = "ratatui")]
1405    pub fn style_table(&self) -> &[ratatui::style::Style] {
1406        &self.style_table
1407    }
1408
1409    /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1410    /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1411    /// per draw frame.
1412    ///
1413    /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1414    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1415    /// caches spans; they live on the engine and route through the
1416    /// `Host::syntax_highlights` pipeline.
1417    pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1418        &self.buffer_spans
1419    }
1420
1421    /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1422    /// With the `ratatui` feature on, the id matches the one
1423    /// [`Editor::intern_ratatui_style`] would return for the equivalent
1424    /// `ratatui::Style` (both share the underlying table). With it off,
1425    /// the engine keeps a parallel `crate::types::Style`-keyed table
1426    /// — ids are still stable per-editor.
1427    ///
1428    /// Hosts that don't depend on ratatui (buffr, future GUI shells)
1429    /// reach this method to populate the table during syntax span
1430    /// installation.
1431    ///
1432    /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1433    /// the unprefixed name is the universally-available engine-native
1434    /// variant.
1435    pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1436        #[cfg(feature = "ratatui")]
1437        {
1438            let r = engine_style_to_ratatui(style);
1439            self.intern_ratatui_style(r)
1440        }
1441        #[cfg(not(feature = "ratatui"))]
1442        {
1443            if let Some(idx) = self.engine_style_table.iter().position(|s| *s == style) {
1444                return idx as u32;
1445            }
1446            self.engine_style_table.push(style);
1447            (self.engine_style_table.len() - 1) as u32
1448        }
1449    }
1450
1451    /// Look up an interned style by id and return it as a SPEC
1452    /// [`crate::types::Style`]. Returns `None` for ids past the end
1453    /// of the table.
1454    pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1455        #[cfg(feature = "ratatui")]
1456        {
1457            let r = self.style_table.get(id as usize).copied()?;
1458            Some(ratatui_style_to_engine(r))
1459        }
1460        #[cfg(not(feature = "ratatui"))]
1461        {
1462            self.engine_style_table.get(id as usize).copied()
1463        }
1464    }
1465
1466    /// Historical reverse-sync hook from when the textarea mirrored
1467    /// the buffer. Now that Buffer is the cursor authority this is a
1468    /// no-op; call sites can remain in place during the migration.
1469    pub(crate) fn push_buffer_cursor_to_textarea(&mut self) {}
1470
1471    /// Force the host viewport's top row without touching the
1472    /// cursor. Used by tests that simulate a scroll without the
1473    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1474    /// apply.
1475    ///
1476    /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1477    /// instead of the (now-deleted) `Buffer::viewport_mut`.
1478    pub fn set_viewport_top(&mut self, row: usize) {
1479        let last = buf_row_count(&self.buffer).saturating_sub(1);
1480        let target = row.min(last);
1481        self.host.viewport_mut().top_row = target;
1482    }
1483
1484    /// Set the cursor to `(row, col)`, clamped to the buffer's
1485    /// content. Hosts use this for goto-line, jump-to-mark, and
1486    /// programmatic cursor placement.
1487    pub fn jump_cursor(&mut self, row: usize, col: usize) {
1488        buf_set_cursor_rc(&mut self.buffer, row, col);
1489    }
1490
1491    /// `(row, col)` cursor read sourced from the migration buffer.
1492    /// Equivalent to `self.textarea.cursor()` when the two are in
1493    /// sync — which is the steady state during Phase 7f because
1494    /// every step opens with `sync_buffer_content_from_textarea` and
1495    /// every ported motion pushes the result back. Prefer this over
1496    /// `self.textarea.cursor()` so call sites keep working unchanged
1497    /// once the textarea field is ripped.
1498    pub fn cursor(&self) -> (usize, usize) {
1499        buf_cursor_rc(&self.buffer)
1500    }
1501
1502    /// Drain any pending LSP intent raised by the last key. Returns
1503    /// `None` when no intent is armed.
1504    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1505        self.pending_lsp.take()
1506    }
1507
1508    /// Drain every [`crate::types::FoldOp`] raised since the last
1509    /// call. Hosts that mirror the engine's fold storage (or that
1510    /// project folds onto a separate fold tree, LSP folding ranges,
1511    /// …) drain this each step and dispatch as their own
1512    /// [`crate::types::Host::Intent`] requires.
1513    ///
1514    /// The engine has already applied every op locally against the
1515    /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1516    /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1517    /// don't track folds independently can ignore the queue
1518    /// (or simply never call this drain).
1519    ///
1520    /// Introduced in 0.0.38 (Patch C-δ.4).
1521    pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1522        std::mem::take(&mut self.pending_fold_ops)
1523    }
1524
1525    /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1526    /// surface: queue it for host observation (drained by
1527    /// [`Editor::take_fold_ops`]) and apply it locally against the
1528    /// in-tree buffer fold storage via
1529    /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1530    /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1531    /// invalidation) route every fold mutation through this method.
1532    ///
1533    /// Introduced in 0.0.38 (Patch C-δ.4).
1534    pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1535        use crate::types::FoldProvider;
1536        self.pending_fold_ops.push(op);
1537        let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1538        provider.apply(op);
1539    }
1540
1541    /// Refresh the host viewport's height from the cached
1542    /// `viewport_height_value()`. Called from the per-step
1543    /// boilerplate; was the textarea → buffer mirror before Phase 7f
1544    /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1545    /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1546    /// `Host::viewport_mut`.
1547    pub(crate) fn sync_buffer_from_textarea(&mut self) {
1548        let height = self.viewport_height_value();
1549        self.host.viewport_mut().height = height;
1550    }
1551
1552    /// Was the full textarea → buffer content sync. Buffer is the
1553    /// content authority now; this remains as a no-op so the per-step
1554    /// call sites don't have to be ripped in the same patch.
1555    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1556        self.sync_buffer_from_textarea();
1557    }
1558
1559    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1560    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1561    /// the cursor without going through the vim engine's motion
1562    /// machinery, where push_jump fires automatically.
1563    pub fn record_jump(&mut self, pos: (usize, usize)) {
1564        const JUMPLIST_MAX: usize = 100;
1565        self.vim.jump_back.push(pos);
1566        if self.vim.jump_back.len() > JUMPLIST_MAX {
1567            self.vim.jump_back.remove(0);
1568        }
1569        self.vim.jump_fwd.clear();
1570    }
1571
1572    /// Host apps call this each draw with the current text area height so
1573    /// scroll helpers can clamp the cursor without recomputing layout.
1574    pub fn set_viewport_height(&self, height: u16) {
1575        self.viewport_height.store(height, Ordering::Relaxed);
1576    }
1577
1578    /// Last height published by `set_viewport_height` (in rows).
1579    pub fn viewport_height_value(&self) -> u16 {
1580        self.viewport_height.load(Ordering::Relaxed)
1581    }
1582
1583    /// Apply `edit` against the buffer and return the inverse so the
1584    /// host can push it onto an undo stack. Side effects: dirty
1585    /// flag, change-list ring, mark / jump-list shifts, change_log
1586    /// append, fold invalidation around the touched rows.
1587    ///
1588    /// The primary edit funnel — both FSM operators and ex commands
1589    /// route mutations through here so the side effects fire
1590    /// uniformly.
1591    pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1592        // `:set readonly` short-circuits every mutation funnel: no
1593        // buffer change, no dirty flag, no undo entry, no change-log
1594        // emission. We swallow the requested `edit` and hand back a
1595        // self-inverse no-op (`InsertStr` of an empty string at the
1596        // current cursor) so callers that push the return value onto
1597        // an undo stack still get a structurally valid round trip.
1598        if self.settings.readonly {
1599            let _ = edit;
1600            return hjkl_buffer::Edit::InsertStr {
1601                at: buf_cursor_pos(&self.buffer),
1602                text: String::new(),
1603            };
1604        }
1605        let pre_row = buf_cursor_row(&self.buffer);
1606        let pre_rows = buf_row_count(&self.buffer);
1607        // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1608        // Vim's `:h '.` says "the position where the last change was made",
1609        // meaning the change-start, not the post-insert cursor. We snap it
1610        // here before `apply_buffer_edit` moves the cursor.
1611        let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1612        // Map the underlying buffer edit to a SPEC EditOp for
1613        // change-log emission before consuming it. Coarse — see
1614        // change_log field doc on the struct.
1615        self.change_log.extend(edit_to_editops(&edit));
1616        // Compute ContentEdit fan-out from the pre-edit buffer state.
1617        // Done before `apply_buffer_edit` consumes `edit` so we can
1618        // inspect the operation's fields and the buffer's pre-edit row
1619        // bytes (needed for byte_of_row / col_byte conversion). Edits
1620        // are pushed onto `pending_content_edits` for host drain.
1621        let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1622        self.pending_content_edits.extend(content_edits);
1623        // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1624        // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1625        // the 0.0.42 plan — see that fn's doc comment). The free fn
1626        // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1627        // no longer carries a `self.buffer.<inherent>` hop.
1628        let inverse = apply_buffer_edit(&mut self.buffer, edit);
1629        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1630        // Drop any folds the edit's range overlapped — vim opens the
1631        // surrounding fold automatically when you edit inside it. The
1632        // approximation here invalidates folds covering either the
1633        // pre-edit cursor row or the post-edit cursor row, which
1634        // catches the common single-line / multi-line edit shapes.
1635        let lo = pre_row.min(pos_row);
1636        let hi = pre_row.max(pos_row);
1637        self.apply_fold_op(crate::types::FoldOp::Invalidate {
1638            start_row: lo,
1639            end_row: hi,
1640        });
1641        // Dot mark records the PRE-edit position (change start), matching
1642        // vim's `:h '.` semantics. Previously this stored the post-edit
1643        // cursor, which diverged from nvim on `iX<Esc>j`.
1644        self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1645        // Append to the change-list ring (skip when the cursor sits on
1646        // the same cell as the last entry — back-to-back keystrokes on
1647        // one column shouldn't pollute the ring). A new edit while
1648        // walking the ring trims the forward half, vim style.
1649        let entry = (pos_row, pos_col);
1650        if self.vim.change_list.last() != Some(&entry) {
1651            if let Some(idx) = self.vim.change_list_cursor.take() {
1652                self.vim.change_list.truncate(idx + 1);
1653            }
1654            self.vim.change_list.push(entry);
1655            let len = self.vim.change_list.len();
1656            if len > crate::vim::CHANGE_LIST_MAX {
1657                self.vim
1658                    .change_list
1659                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1660            }
1661        }
1662        self.vim.change_list_cursor = None;
1663        // Shift / drop marks + jump-list entries to track the row
1664        // delta the edit produced. Without this, every line-changing
1665        // edit silently invalidates `'a`-style positions.
1666        let post_rows = buf_row_count(&self.buffer);
1667        let delta = post_rows as isize - pre_rows as isize;
1668        if delta != 0 {
1669            self.shift_marks_after_edit(pre_row, delta);
1670        }
1671        self.push_buffer_content_to_textarea();
1672        self.mark_content_dirty();
1673        inverse
1674    }
1675
1676    /// Migrate user marks + jumplist entries when an edit at row
1677    /// `edit_start` changes the buffer's row count by `delta` (positive
1678    /// for inserts, negative for deletes). Marks tied to a deleted row
1679    /// are dropped; marks past the affected band shift by `delta`.
1680    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1681        if delta == 0 {
1682            return;
1683        }
1684        // Deleted-row band (only meaningful for delta < 0). Inclusive
1685        // start, exclusive end.
1686        let drop_end = if delta < 0 {
1687            edit_start.saturating_add((-delta) as usize)
1688        } else {
1689            edit_start
1690        };
1691        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1692
1693        // 0.0.36: lowercase + uppercase marks share the unified
1694        // `marks` map; one pass migrates both.
1695        let mut to_drop: Vec<char> = Vec::new();
1696        for (c, (row, _col)) in self.marks.iter_mut() {
1697            if (edit_start..drop_end).contains(row) {
1698                to_drop.push(*c);
1699            } else if *row >= shift_threshold {
1700                *row = ((*row as isize) + delta).max(0) as usize;
1701            }
1702        }
1703        for c in to_drop {
1704            self.marks.remove(&c);
1705        }
1706
1707        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1708            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1709            for (row, _) in entries.iter_mut() {
1710                if *row >= shift_threshold {
1711                    *row = ((*row as isize) + delta).max(0) as usize;
1712                }
1713            }
1714        };
1715        shift_jumps(&mut self.vim.jump_back);
1716        shift_jumps(&mut self.vim.jump_fwd);
1717    }
1718
1719    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1720    /// the textarea from the buffer's lines + cursor, preserving yank
1721    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1722    /// textarea field disappears at the end of Phase 7f anyway.
1723    /// No-op since Buffer is the content authority. Retained as a
1724    /// shim so call sites in `mutate_edit` and friends don't have to
1725    /// be ripped in lockstep with the field removal.
1726    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1727
1728    /// Single choke-point for "the buffer just changed". Sets the
1729    /// dirty flag and drops the cached `content_arc` snapshot so
1730    /// subsequent reads rebuild from the live textarea. Callers
1731    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1732    /// path) must invoke this to keep the cache honest.
1733    pub fn mark_content_dirty(&mut self) {
1734        self.content_dirty = true;
1735        self.cached_content = None;
1736    }
1737
1738    /// Returns true if content changed since the last call, then clears the flag.
1739    pub fn take_dirty(&mut self) -> bool {
1740        let dirty = self.content_dirty;
1741        self.content_dirty = false;
1742        dirty
1743    }
1744
1745    /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1746    /// the last call. Each entry corresponds to a single buffer
1747    /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1748    /// fan out to one entry per row touched.
1749    ///
1750    /// Hosts call this each frame (after [`Editor::take_content_reset`])
1751    /// to fan edits into a tree-sitter parser via `Tree::edit`.
1752    pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1753        std::mem::take(&mut self.pending_content_edits)
1754    }
1755
1756    /// Returns `true` if a bulk buffer replacement happened since the
1757    /// last call (e.g. `set_content` / `restore` / undo restore), then
1758    /// clears the flag. When this returns `true`, hosts should drop
1759    /// any retained syntax tree before consuming
1760    /// [`Editor::take_content_edits`].
1761    pub fn take_content_reset(&mut self) -> bool {
1762        let r = self.pending_content_reset;
1763        self.pending_content_reset = false;
1764        r
1765    }
1766
1767    /// Pull-model coarse change observation. If content changed since
1768    /// the last call, returns `Some(Arc<String>)` with the new content
1769    /// and clears the dirty flag; otherwise returns `None`.
1770    ///
1771    /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1772    /// the character level) should diff against their own previous
1773    /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1774    /// once every edit path inside the engine is instrumented; this
1775    /// coarse form covers the pull-model use case in the meantime.
1776    pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1777        if !self.content_dirty {
1778            return None;
1779        }
1780        let arc = self.content_arc();
1781        self.content_dirty = false;
1782        Some(arc)
1783    }
1784
1785    /// Returns the cursor's row within the visible textarea (0-based), updating
1786    /// the stored viewport top so subsequent calls remain accurate.
1787    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1788        let cursor = buf_cursor_row(&self.buffer);
1789        let top = self.host.viewport().top_row;
1790        cursor.saturating_sub(top).min(height as usize - 1) as u16
1791    }
1792
1793    /// Returns the cursor's screen position `(x, y)` for the textarea
1794    /// described by `(area_x, area_y, area_width, area_height)`.
1795    /// Accounts for line-number gutter and viewport scroll. Returns
1796    /// `None` if the cursor is outside the visible viewport. Always
1797    /// available (engine-native; no ratatui dependency).
1798    ///
1799    /// Renamed from `cursor_screen_pos_xywh` in 0.0.32 — the
1800    /// ratatui-flavoured `Rect` variant is now
1801    /// [`Editor::cursor_screen_pos_in_rect`] (cfg `ratatui`).
1802    pub fn cursor_screen_pos(
1803        &self,
1804        area_x: u16,
1805        area_y: u16,
1806        area_width: u16,
1807        area_height: u16,
1808    ) -> Option<(u16, u16)> {
1809        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1810        let v = self.host.viewport();
1811        if pos_row < v.top_row || pos_col < v.top_col {
1812            return None;
1813        }
1814        let lnum_width = if self.settings.number || self.settings.relativenumber {
1815            let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1816            needed.max(self.settings.numberwidth) as u16
1817        } else {
1818            0
1819        };
1820        let dy = (pos_row - v.top_row) as u16;
1821        // Convert char column to visual column so cursor lands on the
1822        // correct cell when the line contains tabs (which the renderer
1823        // expands to TAB_WIDTH stops). Tab width must match the renderer.
1824        let line = self.buffer.line(pos_row).unwrap_or("");
1825        let tab_width = if v.tab_width == 0 {
1826            4
1827        } else {
1828            v.tab_width as usize
1829        };
1830        let visual_pos = visual_col_for_char(line, pos_col, tab_width);
1831        let visual_top = visual_col_for_char(line, v.top_col, tab_width);
1832        let dx = (visual_pos - visual_top) as u16;
1833        if dy >= area_height || dx + lnum_width >= area_width {
1834            return None;
1835        }
1836        Some((area_x + lnum_width + dx, area_y + dy))
1837    }
1838
1839    /// Ratatui [`Rect`]-flavoured wrapper around
1840    /// [`Editor::cursor_screen_pos`]. Behind the `ratatui` feature.
1841    ///
1842    /// Renamed from `cursor_screen_pos` in 0.0.32 — the unprefixed
1843    /// name now belongs to the engine-native variant.
1844    #[cfg(feature = "ratatui")]
1845    pub fn cursor_screen_pos_in_rect(&self, area: Rect) -> Option<(u16, u16)> {
1846        self.cursor_screen_pos(area.x, area.y, area.width, area.height)
1847    }
1848
1849    pub fn vim_mode(&self) -> VimMode {
1850        self.vim.public_mode()
1851    }
1852
1853    /// Bounds of the active visual-block rectangle as
1854    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1855    /// `None` when we're not in VisualBlock mode.
1856    /// Read-only view of the live `/` or `?` prompt. `None` outside
1857    /// search-prompt mode.
1858    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1859        self.vim.search_prompt.as_ref()
1860    }
1861
1862    /// Most recent committed search pattern (persists across `n` / `N`
1863    /// and across prompt exits). `None` before the first search.
1864    pub fn last_search(&self) -> Option<&str> {
1865        self.vim.last_search.as_deref()
1866    }
1867
1868    /// Whether the last committed search was a forward `/` (`true`) or
1869    /// a backward `?` (`false`). `n` and `N` consult this to honour the
1870    /// direction the user committed.
1871    pub fn last_search_forward(&self) -> bool {
1872        self.vim.last_search_forward
1873    }
1874
1875    /// Set the most recent committed search text + direction. Used by
1876    /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1877    /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1878    /// most recent commit with the right direction. Pass `None` /
1879    /// `true` to clear.
1880    pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1881        self.vim.last_search = text;
1882        self.vim.last_search_forward = forward;
1883    }
1884
1885    /// Start/end `(row, col)` of the active char-wise Visual selection
1886    /// (inclusive on both ends, positionally ordered). `None` when not
1887    /// in Visual mode.
1888    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1889        if self.vim_mode() != VimMode::Visual {
1890            return None;
1891        }
1892        let anchor = self.vim.visual_anchor;
1893        let cursor = self.cursor();
1894        let (start, end) = if anchor <= cursor {
1895            (anchor, cursor)
1896        } else {
1897            (cursor, anchor)
1898        };
1899        Some((start, end))
1900    }
1901
1902    /// Top/bottom rows of the active VisualLine selection (inclusive).
1903    /// `None` when we're not in VisualLine mode.
1904    pub fn line_highlight(&self) -> Option<(usize, usize)> {
1905        if self.vim_mode() != VimMode::VisualLine {
1906            return None;
1907        }
1908        let anchor = self.vim.visual_line_anchor;
1909        let cursor = buf_cursor_row(&self.buffer);
1910        Some((anchor.min(cursor), anchor.max(cursor)))
1911    }
1912
1913    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
1914        if self.vim_mode() != VimMode::VisualBlock {
1915            return None;
1916        }
1917        let (ar, ac) = self.vim.block_anchor;
1918        let cr = buf_cursor_row(&self.buffer);
1919        let cc = self.vim.block_vcol;
1920        let top = ar.min(cr);
1921        let bot = ar.max(cr);
1922        let left = ac.min(cc);
1923        let right = ac.max(cc);
1924        Some((top, bot, left, right))
1925    }
1926
1927    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
1928    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
1929    /// straight to `BufferView` once render flips off textarea
1930    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
1931    /// switch).
1932    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
1933        use hjkl_buffer::{Position, Selection};
1934        match self.vim_mode() {
1935            VimMode::Visual => {
1936                let (ar, ac) = self.vim.visual_anchor;
1937                let head = buf_cursor_pos(&self.buffer);
1938                Some(Selection::Char {
1939                    anchor: Position::new(ar, ac),
1940                    head,
1941                })
1942            }
1943            VimMode::VisualLine => {
1944                let anchor_row = self.vim.visual_line_anchor;
1945                let head_row = buf_cursor_row(&self.buffer);
1946                Some(Selection::Line {
1947                    anchor_row,
1948                    head_row,
1949                })
1950            }
1951            VimMode::VisualBlock => {
1952                let (ar, ac) = self.vim.block_anchor;
1953                let cr = buf_cursor_row(&self.buffer);
1954                let cc = self.vim.block_vcol;
1955                Some(Selection::Block {
1956                    anchor: Position::new(ar, ac),
1957                    head: Position::new(cr, cc),
1958                })
1959            }
1960            _ => None,
1961        }
1962    }
1963
1964    /// Force back to normal mode (used when dismissing completions etc.)
1965    pub fn force_normal(&mut self) {
1966        self.vim.force_normal();
1967    }
1968
1969    pub fn content(&self) -> String {
1970        let n = buf_row_count(&self.buffer);
1971        let mut s = String::new();
1972        for r in 0..n {
1973            if r > 0 {
1974                s.push('\n');
1975            }
1976            s.push_str(crate::types::Query::line(&self.buffer, r as u32));
1977        }
1978        s.push('\n');
1979        s
1980    }
1981
1982    /// Same logical output as [`content`], but returns a cached
1983    /// `Arc<String>` so back-to-back reads within an un-mutated window
1984    /// are ref-count bumps instead of multi-MB joins. The cache is
1985    /// invalidated by every [`mark_content_dirty`] call.
1986    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
1987        if let Some(arc) = &self.cached_content {
1988            return std::sync::Arc::clone(arc);
1989        }
1990        let arc = std::sync::Arc::new(self.content());
1991        self.cached_content = Some(std::sync::Arc::clone(&arc));
1992        arc
1993    }
1994
1995    pub fn set_content(&mut self, text: &str) {
1996        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1997        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1998            lines.pop();
1999        }
2000        if lines.is_empty() {
2001            lines.push(String::new());
2002        }
2003        let _ = lines;
2004        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2005        self.undo_stack.clear();
2006        self.redo_stack.clear();
2007        // Whole-buffer replace supersedes any queued ContentEdits.
2008        self.pending_content_edits.clear();
2009        self.pending_content_reset = true;
2010        self.mark_content_dirty();
2011    }
2012
2013    /// Feed an SPEC [`crate::PlannedInput`] into the engine.
2014    ///
2015    /// Bridge for hosts that don't carry crossterm — buffr's CEF
2016    /// shell, future GUI frontends. Converts directly to the engine's
2017    /// internal [`Input`] type and dispatches through the vim FSM,
2018    /// bypassing crossterm entirely so this entry point is always
2019    /// available regardless of the `crossterm` feature.
2020    ///
2021    /// `Input::Mouse`, `Input::Paste`, `Input::FocusGained`,
2022    /// `Input::FocusLost`, and `Input::Resize` currently fall through
2023    /// without effect — the legacy FSM doesn't dispatch them. They're
2024    /// accepted so the host can pump them into the engine without
2025    /// special-casing.
2026    ///
2027    /// Returns `true` when the keystroke was consumed.
2028    pub fn feed_input(&mut self, input: crate::PlannedInput) -> bool {
2029        use crate::{PlannedInput, SpecialKey};
2030        let (key, mods) = match input {
2031            PlannedInput::Char(c, m) => (Key::Char(c), m),
2032            PlannedInput::Key(k, m) => {
2033                let key = match k {
2034                    SpecialKey::Esc => Key::Esc,
2035                    SpecialKey::Enter => Key::Enter,
2036                    SpecialKey::Backspace => Key::Backspace,
2037                    SpecialKey::Tab => Key::Tab,
2038                    // Engine's internal `Key` doesn't model BackTab as a
2039                    // distinct variant — fall through to the FSM as
2040                    // shift+Tab, matching crossterm semantics.
2041                    SpecialKey::BackTab => Key::Tab,
2042                    SpecialKey::Up => Key::Up,
2043                    SpecialKey::Down => Key::Down,
2044                    SpecialKey::Left => Key::Left,
2045                    SpecialKey::Right => Key::Right,
2046                    SpecialKey::Home => Key::Home,
2047                    SpecialKey::End => Key::End,
2048                    SpecialKey::PageUp => Key::PageUp,
2049                    SpecialKey::PageDown => Key::PageDown,
2050                    // Engine's `Key` has no Insert / F(n) — drop to Null
2051                    // (FSM ignores it) which matches the crossterm path
2052                    // (`crossterm_to_input` mapped these to Null too).
2053                    SpecialKey::Insert => Key::Null,
2054                    SpecialKey::Delete => Key::Delete,
2055                    SpecialKey::F(_) => Key::Null,
2056                };
2057                let m = if matches!(k, SpecialKey::BackTab) {
2058                    crate::Modifiers { shift: true, ..m }
2059                } else {
2060                    m
2061                };
2062                (key, m)
2063            }
2064            // Variants the legacy FSM doesn't consume yet.
2065            PlannedInput::Mouse(_)
2066            | PlannedInput::Paste(_)
2067            | PlannedInput::FocusGained
2068            | PlannedInput::FocusLost
2069            | PlannedInput::Resize(_, _) => return false,
2070        };
2071        if key == Key::Null {
2072            return false;
2073        }
2074        let event = Input {
2075            key,
2076            ctrl: mods.ctrl,
2077            alt: mods.alt,
2078            shift: mods.shift,
2079        };
2080        let consumed = vim::step(self, event);
2081        self.emit_cursor_shape_if_changed();
2082        consumed
2083    }
2084
2085    /// Drain the pending change log produced by buffer mutations.
2086    ///
2087    /// Returns a `Vec<EditOp>` covering edits applied since the last
2088    /// call. Empty when no edits ran. Pull-model, complementary to
2089    /// [`Editor::take_content_change`] which gives back the new full
2090    /// content.
2091    ///
2092    /// Mapping coverage:
2093    /// - InsertChar / InsertStr → exact `EditOp` with empty range +
2094    ///   replacement.
2095    /// - DeleteRange (`Char` kind) → exact range + empty replacement.
2096    /// - Replace → exact range + new replacement.
2097    /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
2098    ///   InsertBlock, DeleteBlockChunks → best-effort placeholder
2099    ///   covering the touched range. Hosts wanting per-cell deltas
2100    ///   should diff their own `lines()` snapshot.
2101    pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
2102        std::mem::take(&mut self.change_log)
2103    }
2104
2105    /// Read the engine's current settings as a SPEC
2106    /// [`crate::types::Options`].
2107    ///
2108    /// Bridges between the legacy [`Settings`] (which carries fewer
2109    /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
2110    /// not present in `Settings` fall back to vim defaults (e.g.,
2111    /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
2112    /// Once trait extraction lands, this becomes the canonical config
2113    /// reader and `Settings` retires.
2114    pub fn current_options(&self) -> crate::types::Options {
2115        crate::types::Options {
2116            shiftwidth: self.settings.shiftwidth as u32,
2117            tabstop: self.settings.tabstop as u32,
2118            softtabstop: self.settings.softtabstop as u32,
2119            textwidth: self.settings.textwidth as u32,
2120            expandtab: self.settings.expandtab,
2121            ignorecase: self.settings.ignore_case,
2122            smartcase: self.settings.smartcase,
2123            wrapscan: self.settings.wrapscan,
2124            wrap: match self.settings.wrap {
2125                hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
2126                hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
2127                hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
2128            },
2129            readonly: self.settings.readonly,
2130            autoindent: self.settings.autoindent,
2131            smartindent: self.settings.smartindent,
2132            undo_levels: self.settings.undo_levels,
2133            undo_break_on_motion: self.settings.undo_break_on_motion,
2134            iskeyword: self.settings.iskeyword.clone(),
2135            timeout_len: self.settings.timeout_len,
2136            ..crate::types::Options::default()
2137        }
2138    }
2139
2140    /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
2141    /// Only the fields backed by today's [`Settings`] take effect;
2142    /// remaining options become live once trait extraction wires them
2143    /// through.
2144    pub fn apply_options(&mut self, opts: &crate::types::Options) {
2145        self.settings.shiftwidth = opts.shiftwidth as usize;
2146        self.settings.tabstop = opts.tabstop as usize;
2147        self.settings.softtabstop = opts.softtabstop as usize;
2148        self.settings.textwidth = opts.textwidth as usize;
2149        self.settings.expandtab = opts.expandtab;
2150        self.settings.ignore_case = opts.ignorecase;
2151        self.settings.smartcase = opts.smartcase;
2152        self.settings.wrapscan = opts.wrapscan;
2153        self.settings.wrap = match opts.wrap {
2154            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
2155            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
2156            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
2157        };
2158        self.settings.readonly = opts.readonly;
2159        self.settings.autoindent = opts.autoindent;
2160        self.settings.smartindent = opts.smartindent;
2161        self.settings.undo_levels = opts.undo_levels;
2162        self.settings.undo_break_on_motion = opts.undo_break_on_motion;
2163        self.set_iskeyword(opts.iskeyword.clone());
2164        self.settings.timeout_len = opts.timeout_len;
2165        self.settings.number = opts.number;
2166        self.settings.relativenumber = opts.relativenumber;
2167        self.settings.numberwidth = opts.numberwidth;
2168        self.settings.cursorline = opts.cursorline;
2169        self.settings.cursorcolumn = opts.cursorcolumn;
2170        self.settings.signcolumn = opts.signcolumn;
2171        self.settings.foldcolumn = opts.foldcolumn;
2172        self.settings.colorcolumn = opts.colorcolumn.clone();
2173    }
2174
2175    /// Active visual selection as a SPEC [`crate::types::Highlight`]
2176    /// with [`crate::types::HighlightKind::Selection`].
2177    ///
2178    /// Returns `None` when the editor isn't in a Visual mode.
2179    /// Visual-line and visual-block selections collapse to the
2180    /// bounding char range of the selection — the SPEC `Selection`
2181    /// kind doesn't carry sub-line info today; hosts that need full
2182    /// line / block geometry continue to read [`buffer_selection`]
2183    /// (the legacy [`hjkl_buffer::Selection`] shape).
2184    pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2185        use crate::types::{Highlight, HighlightKind, Pos};
2186        let sel = self.buffer_selection()?;
2187        let (start, end) = match sel {
2188            hjkl_buffer::Selection::Char { anchor, head } => {
2189                let a = (anchor.row, anchor.col);
2190                let h = (head.row, head.col);
2191                if a <= h { (a, h) } else { (h, a) }
2192            }
2193            hjkl_buffer::Selection::Line {
2194                anchor_row,
2195                head_row,
2196            } => {
2197                let (top, bot) = if anchor_row <= head_row {
2198                    (anchor_row, head_row)
2199                } else {
2200                    (head_row, anchor_row)
2201                };
2202                let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2203                ((top, 0), (bot, last_col))
2204            }
2205            hjkl_buffer::Selection::Block { anchor, head } => {
2206                let (top, bot) = if anchor.row <= head.row {
2207                    (anchor.row, head.row)
2208                } else {
2209                    (head.row, anchor.row)
2210                };
2211                let (left, right) = if anchor.col <= head.col {
2212                    (anchor.col, head.col)
2213                } else {
2214                    (head.col, anchor.col)
2215                };
2216                ((top, left), (bot, right))
2217            }
2218        };
2219        Some(Highlight {
2220            range: Pos {
2221                line: start.0 as u32,
2222                col: start.1 as u32,
2223            }..Pos {
2224                line: end.0 as u32,
2225                col: end.1 as u32,
2226            },
2227            kind: HighlightKind::Selection,
2228        })
2229    }
2230
2231    /// SPEC-typed highlights for `line`.
2232    ///
2233    /// Two emission modes:
2234    ///
2235    /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2236    ///   `Editor::search_prompt` is `Some`. Live-preview matches of
2237    ///   the in-flight pattern surface as
2238    ///   [`crate::types::HighlightKind::IncSearch`].
2239    /// - **SearchMatch**: the prompt has been committed (or absent)
2240    ///   and the buffer's armed pattern is non-empty. Matches surface
2241    ///   as [`crate::types::HighlightKind::SearchMatch`].
2242    ///
2243    /// Selection / MatchParen / Syntax(id) variants land once the
2244    /// trait extraction routes the FSM's selection set + the host's
2245    /// syntax pipeline through the [`crate::types::Host`] trait.
2246    ///
2247    /// Returns an empty vec when there is nothing to highlight or
2248    /// `line` is out of bounds.
2249    pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2250        use crate::types::{Highlight, HighlightKind, Pos};
2251        let row = line as usize;
2252        if row >= buf_row_count(&self.buffer) {
2253            return Vec::new();
2254        }
2255
2256        // Live preview while the prompt is open beats the committed
2257        // pattern.
2258        if let Some(prompt) = self.search_prompt() {
2259            if prompt.text.is_empty() {
2260                return Vec::new();
2261            }
2262            let Ok(re) = regex::Regex::new(&prompt.text) else {
2263                return Vec::new();
2264            };
2265            let Some(haystack) = buf_line(&self.buffer, row) else {
2266                return Vec::new();
2267            };
2268            return re
2269                .find_iter(haystack)
2270                .map(|m| Highlight {
2271                    range: Pos {
2272                        line,
2273                        col: m.start() as u32,
2274                    }..Pos {
2275                        line,
2276                        col: m.end() as u32,
2277                    },
2278                    kind: HighlightKind::IncSearch,
2279                })
2280                .collect();
2281        }
2282
2283        if self.search_state.pattern.is_none() {
2284            return Vec::new();
2285        }
2286        let dgen = crate::types::Query::dirty_gen(&self.buffer);
2287        crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2288            .into_iter()
2289            .map(|(start, end)| Highlight {
2290                range: Pos {
2291                    line,
2292                    col: start as u32,
2293                }..Pos {
2294                    line,
2295                    col: end as u32,
2296                },
2297                kind: HighlightKind::SearchMatch,
2298            })
2299            .collect()
2300    }
2301
2302    /// Build the engine's [`crate::types::RenderFrame`] for the
2303    /// current state. Hosts call this once per redraw and diff
2304    /// across frames.
2305    ///
2306    /// Coarse today — covers mode + cursor + cursor shape + viewport
2307    /// top + line count. SPEC-target fields (selections, highlights,
2308    /// command line, search prompt, status line) land once trait
2309    /// extraction routes them through `SelectionSet` and the
2310    /// `Highlight` pipeline.
2311    pub fn render_frame(&self) -> crate::types::RenderFrame {
2312        use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2313        let (cursor_row, cursor_col) = self.cursor();
2314        let (mode, shape) = match self.vim_mode() {
2315            crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2316            crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2317            crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2318            crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2319            crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2320        };
2321        RenderFrame {
2322            mode,
2323            cursor_row: cursor_row as u32,
2324            cursor_col: cursor_col as u32,
2325            cursor_shape: shape,
2326            viewport_top: self.host.viewport().top_row as u32,
2327            line_count: crate::types::Query::line_count(&self.buffer),
2328        }
2329    }
2330
2331    /// Capture the editor's coarse state into a serde-friendly
2332    /// [`crate::types::EditorSnapshot`].
2333    ///
2334    /// Today's snapshot covers mode, cursor, lines, viewport top.
2335    /// Registers, marks, jump list, undo tree, and full options arrive
2336    /// once phase 5 trait extraction lands the generic
2337    /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2338    /// stays stable; only the snapshot's internal fields grow.
2339    ///
2340    /// Distinct from the internal `snapshot` used by undo (which
2341    /// returns `(Vec<String>, (usize, usize))`); host-facing
2342    /// persistence goes through this one.
2343    pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2344        use crate::types::{EditorSnapshot, SnapshotMode};
2345        let mode = match self.vim_mode() {
2346            crate::VimMode::Normal => SnapshotMode::Normal,
2347            crate::VimMode::Insert => SnapshotMode::Insert,
2348            crate::VimMode::Visual => SnapshotMode::Visual,
2349            crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2350            crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2351        };
2352        let cursor = self.cursor();
2353        let cursor = (cursor.0 as u32, cursor.1 as u32);
2354        let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2355        let viewport_top = self.host.viewport().top_row as u32;
2356        let marks = self
2357            .marks
2358            .iter()
2359            .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2360            .collect();
2361        EditorSnapshot {
2362            version: EditorSnapshot::VERSION,
2363            mode,
2364            cursor,
2365            lines,
2366            viewport_top,
2367            registers: self.registers.clone(),
2368            marks,
2369        }
2370    }
2371
2372    /// Restore editor state from an [`EditorSnapshot`]. Returns
2373    /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2374    /// `version` doesn't match [`EditorSnapshot::VERSION`].
2375    ///
2376    /// Mode is best-effort: `SnapshotMode` only round-trips the
2377    /// status-line summary, not the full FSM state. Visual / Insert
2378    /// mode entry happens through synthetic key dispatch when needed.
2379    pub fn restore_snapshot(
2380        &mut self,
2381        snap: crate::types::EditorSnapshot,
2382    ) -> Result<(), crate::EngineError> {
2383        use crate::types::EditorSnapshot;
2384        if snap.version != EditorSnapshot::VERSION {
2385            return Err(crate::EngineError::SnapshotVersion(
2386                snap.version,
2387                EditorSnapshot::VERSION,
2388            ));
2389        }
2390        let text = snap.lines.join("\n");
2391        self.set_content(&text);
2392        self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2393        self.host.viewport_mut().top_row = snap.viewport_top as usize;
2394        self.registers = snap.registers;
2395        self.marks = snap
2396            .marks
2397            .into_iter()
2398            .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2399            .collect();
2400        Ok(())
2401    }
2402
2403    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2404    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2405    /// shape their payload.
2406    pub fn seed_yank(&mut self, text: String) {
2407        let linewise = text.ends_with('\n');
2408        self.vim.yank_linewise = linewise;
2409        self.registers.unnamed = crate::registers::Slot { text, linewise };
2410    }
2411
2412    /// Scroll the viewport down by `rows`. The cursor stays on its
2413    /// absolute line (vim convention) unless the scroll would take it
2414    /// off-screen — in that case it's clamped to the first row still
2415    /// visible.
2416    pub fn scroll_down(&mut self, rows: i16) {
2417        self.scroll_viewport(rows);
2418    }
2419
2420    /// Scroll the viewport up by `rows`. Cursor stays unless it would
2421    /// fall off the bottom of the new viewport, then clamp to the
2422    /// bottom-most visible row.
2423    pub fn scroll_up(&mut self, rows: i16) {
2424        self.scroll_viewport(-rows);
2425    }
2426
2427    /// Vim's `scrolloff` default — keep the cursor at least this many
2428    /// rows away from the top / bottom edge of the viewport while
2429    /// scrolling. Collapses to `height / 2` for tiny viewports.
2430    const SCROLLOFF: usize = 5;
2431
2432    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2433    /// rows from each edge. Replaces the bare
2434    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2435    /// don't park the cursor on the very last visible row.
2436    pub fn ensure_cursor_in_scrolloff(&mut self) {
2437        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2438        if height == 0 {
2439            // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2440            // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2441            // Disjoint-field borrow split: `self.buffer` (immutable via
2442            // `folds` snapshot + cursor) and `self.host` (mutable
2443            // viewport ref) live on distinct struct fields, so one
2444            // statement satisfies the borrow checker.
2445            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2446            crate::viewport_math::ensure_cursor_visible(
2447                &self.buffer,
2448                &folds,
2449                self.host.viewport_mut(),
2450            );
2451            return;
2452        }
2453        // Cap margin at (height - 1) / 2 so the upper + lower bands
2454        // can't overlap on tiny windows (margin=5 + height=10 would
2455        // otherwise produce contradictory clamp ranges).
2456        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2457        // Soft-wrap path: scrolloff math runs in *screen rows*, not
2458        // doc rows, since a wrapped doc row spans many visual lines.
2459        if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2460            self.ensure_scrolloff_wrap(height, margin);
2461            return;
2462        }
2463        let cursor_row = buf_cursor_row(&self.buffer);
2464        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2465        let v = self.host.viewport_mut();
2466        // Top edge: cursor_row should sit at >= top_row + margin.
2467        if cursor_row < v.top_row + margin {
2468            v.top_row = cursor_row.saturating_sub(margin);
2469        }
2470        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2471        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2472        if cursor_row > v.top_row + max_bottom {
2473            v.top_row = cursor_row.saturating_sub(max_bottom);
2474        }
2475        // Clamp top_row so we never scroll past the buffer's bottom.
2476        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2477        if v.top_row > max_top {
2478            v.top_row = max_top;
2479        }
2480        // Defer to Buffer for column-side scroll (no scrolloff for
2481        // horizontal scrolling — vim default `sidescrolloff = 0`).
2482        let cursor = buf_cursor_pos(&self.buffer);
2483        self.host.viewport_mut().ensure_visible(cursor);
2484    }
2485
2486    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2487    /// at a time so the cursor's *screen* row stays inside
2488    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2489    /// buffer's bottom never leaves blank rows below it.
2490    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2491        let cursor_row = buf_cursor_row(&self.buffer);
2492        // Step 1 — cursor above viewport: snap top to cursor row,
2493        // then we'll fix up the margin below.
2494        if cursor_row < self.host.viewport().top_row {
2495            let v = self.host.viewport_mut();
2496            v.top_row = cursor_row;
2497            v.top_col = 0;
2498        }
2499        // Step 2 — push top forward until cursor's screen row is
2500        // within the bottom margin (`csr <= height - 1 - margin`).
2501        // 0.0.33 (Patch C-γ): fold-iteration goes through the
2502        // [`crate::types::FoldProvider`] surface via
2503        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2504        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2505        // a `&Viewport` parameter; the host owns the viewport, so the
2506        // disjoint `(self.host, self.buffer)` borrows split cleanly.
2507        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2508        loop {
2509            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2510            let csr =
2511                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2512                    .unwrap_or(0);
2513            if csr <= max_csr {
2514                break;
2515            }
2516            let top = self.host.viewport().top_row;
2517            let row_count = buf_row_count(&self.buffer);
2518            let next = {
2519                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2520                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2521            };
2522            let Some(next) = next else {
2523                break;
2524            };
2525            // Don't walk past the cursor's row.
2526            if next > cursor_row {
2527                self.host.viewport_mut().top_row = cursor_row;
2528                break;
2529            }
2530            self.host.viewport_mut().top_row = next;
2531        }
2532        // Step 3 — pull top backward until cursor's screen row is
2533        // past the top margin (`csr >= margin`).
2534        loop {
2535            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2536            let csr =
2537                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2538                    .unwrap_or(0);
2539            if csr >= margin {
2540                break;
2541            }
2542            let top = self.host.viewport().top_row;
2543            let prev = {
2544                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2545                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2546            };
2547            let Some(prev) = prev else {
2548                break;
2549            };
2550            self.host.viewport_mut().top_row = prev;
2551        }
2552        // Step 4 — clamp top so the buffer's bottom doesn't leave
2553        // blank rows below it. `max_top_for_height` walks segments
2554        // backward from the last row until it accumulates `height`
2555        // screen rows.
2556        let max_top = {
2557            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2558            crate::viewport_math::max_top_for_height(
2559                &self.buffer,
2560                &folds,
2561                self.host.viewport(),
2562                height,
2563            )
2564        };
2565        if self.host.viewport().top_row > max_top {
2566            self.host.viewport_mut().top_row = max_top;
2567        }
2568        self.host.viewport_mut().top_col = 0;
2569    }
2570
2571    fn scroll_viewport(&mut self, delta: i16) {
2572        if delta == 0 {
2573            return;
2574        }
2575        // Bump the host viewport's top within bounds.
2576        let total_rows = buf_row_count(&self.buffer) as isize;
2577        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2578        let cur_top = self.host.viewport().top_row as isize;
2579        let new_top = (cur_top + delta as isize)
2580            .max(0)
2581            .min((total_rows - 1).max(0)) as usize;
2582        self.host.viewport_mut().top_row = new_top;
2583        // Mirror to textarea so its viewport reads (still consumed by
2584        // a couple of helpers) stay accurate.
2585        let _ = cur_top;
2586        if height == 0 {
2587            return;
2588        }
2589        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2590        // from the visible viewport edges.
2591        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2592        let margin = Self::SCROLLOFF.min(height / 2);
2593        let min_row = new_top + margin;
2594        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2595        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2596        if target_row != cursor_row {
2597            let line_len = buf_line(&self.buffer, target_row)
2598                .map(|l| l.chars().count())
2599                .unwrap_or(0);
2600            let target_col = cursor_col.min(line_len.saturating_sub(1));
2601            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2602        }
2603    }
2604
2605    pub fn goto_line(&mut self, line: usize) {
2606        let row = line.saturating_sub(1);
2607        let max = buf_row_count(&self.buffer).saturating_sub(1);
2608        let target = row.min(max);
2609        buf_set_cursor_rc(&mut self.buffer, target, 0);
2610        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2611        // the cursor lands off-screen and the user has to scroll
2612        // manually to see it.
2613        self.ensure_cursor_in_scrolloff();
2614    }
2615
2616    /// Scroll so the cursor row lands at the given viewport position:
2617    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2618    /// Cursor stays on its absolute line; only the viewport moves.
2619    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2620        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2621        if height == 0 {
2622            return;
2623        }
2624        let cur_row = buf_cursor_row(&self.buffer);
2625        let cur_top = self.host.viewport().top_row;
2626        // Scrolloff awareness: `zt` lands the cursor at the top edge
2627        // of the viable area (top + margin), `zb` at the bottom edge
2628        // (top + height - 1 - margin). Match the cap used by
2629        // `ensure_cursor_in_scrolloff` so contradictory bounds are
2630        // impossible on tiny viewports.
2631        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2632        let new_top = match pos {
2633            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2634            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2635            CursorScrollTarget::Bottom => {
2636                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2637            }
2638        };
2639        if new_top == cur_top {
2640            return;
2641        }
2642        self.host.viewport_mut().top_row = new_top;
2643    }
2644
2645    /// Translate a terminal mouse position into a (row, col) inside
2646    /// the document. The outer editor area is described by `(area_x,
2647    /// area_y, area_width)` (height is unused). 1-row tab bar at the
2648    /// top, then the textarea with 1 cell of horizontal pane padding
2649    /// on each side. Clicks past the line's last character clamp to
2650    /// the last char (Normal-mode invariant) — never past it.
2651    /// Char-counted, not byte-counted.
2652    ///
2653    /// Ratatui-free; [`Editor::mouse_to_doc_pos`] (behind the
2654    /// `ratatui` feature) is a thin `Rect`-flavoured wrapper.
2655    fn mouse_to_doc_pos_xy(&self, area_x: u16, area_y: u16, col: u16, row: u16) -> (usize, usize) {
2656        let n = buf_row_count(&self.buffer);
2657        let inner_top = area_y.saturating_add(1); // tab bar row
2658        let lnum_width = if self.settings.number || self.settings.relativenumber {
2659            let needed = n.to_string().len() + 1;
2660            needed.max(self.settings.numberwidth) as u16
2661        } else {
2662            0
2663        };
2664        let content_x = area_x.saturating_add(1).saturating_add(lnum_width);
2665        let rel_row = row.saturating_sub(inner_top) as usize;
2666        let top = self.host.viewport().top_row;
2667        let doc_row = (top + rel_row).min(n.saturating_sub(1));
2668        let rel_col = col.saturating_sub(content_x) as usize;
2669        let line_chars = buf_line(&self.buffer, doc_row)
2670            .map(|l| l.chars().count())
2671            .unwrap_or(0);
2672        let last_col = line_chars.saturating_sub(1);
2673        (doc_row, rel_col.min(last_col))
2674    }
2675
2676    /// Jump the cursor to the given 1-based line/column, clamped to the document.
2677    pub fn jump_to(&mut self, line: usize, col: usize) {
2678        let r = line.saturating_sub(1);
2679        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2680        let r = r.min(max_row);
2681        let line_len = buf_line(&self.buffer, r)
2682            .map(|l| l.chars().count())
2683            .unwrap_or(0);
2684        let c = col.saturating_sub(1).min(line_len);
2685        buf_set_cursor_rc(&mut self.buffer, r, c);
2686    }
2687
2688    /// Jump cursor to the terminal-space mouse position; exits Visual
2689    /// modes if active. Engine-native coordinate flavour — pass the
2690    /// outer editor rect's `(x, y)` plus the click `(col, row)`.
2691    /// Always available (no ratatui dependency).
2692    ///
2693    /// Renamed from `mouse_click_xy` in 0.0.32 — at 0.1.0 freeze the
2694    /// unprefixed name belongs to the universally-available variant.
2695    pub fn mouse_click(&mut self, area_x: u16, area_y: u16, col: u16, row: u16) {
2696        if self.vim.is_visual() {
2697            self.vim.force_normal();
2698        }
2699        // Mouse-position click counts as a motion — break the active
2700        // insert-mode undo group when the toggle is on (vim parity).
2701        crate::vim::break_undo_group_in_insert(self);
2702        let (r, c) = self.mouse_to_doc_pos_xy(area_x, area_y, col, row);
2703        buf_set_cursor_rc(&mut self.buffer, r, c);
2704    }
2705
2706    /// Ratatui [`Rect`]-flavoured wrapper around
2707    /// [`Editor::mouse_click`]. Behind the `ratatui` feature.
2708    ///
2709    /// Renamed from `mouse_click` in 0.0.32 — the unprefixed name now
2710    /// belongs to the engine-native variant.
2711    #[cfg(feature = "ratatui")]
2712    pub fn mouse_click_in_rect(&mut self, area: Rect, col: u16, row: u16) {
2713        self.mouse_click(area.x, area.y, col, row);
2714    }
2715
2716    /// Begin a mouse-drag selection: anchor at current cursor and enter Visual mode.
2717    pub fn mouse_begin_drag(&mut self) {
2718        if !self.vim.is_visual_char() {
2719            let cursor = self.cursor();
2720            self.vim.enter_visual(cursor);
2721        }
2722    }
2723
2724    /// Extend an in-progress mouse drag to the given terminal-space
2725    /// position. Engine-native coordinate flavour. Always available.
2726    ///
2727    /// Renamed from `mouse_extend_drag_xy` in 0.0.32 — at 0.1.0 freeze
2728    /// the unprefixed name belongs to the universally-available variant.
2729    pub fn mouse_extend_drag(&mut self, area_x: u16, area_y: u16, col: u16, row: u16) {
2730        let (r, c) = self.mouse_to_doc_pos_xy(area_x, area_y, col, row);
2731        buf_set_cursor_rc(&mut self.buffer, r, c);
2732    }
2733
2734    /// Ratatui [`Rect`]-flavoured wrapper around
2735    /// [`Editor::mouse_extend_drag`]. Behind the `ratatui` feature.
2736    ///
2737    /// Renamed from `mouse_extend_drag` in 0.0.32 — the unprefixed
2738    /// name now belongs to the engine-native variant.
2739    #[cfg(feature = "ratatui")]
2740    pub fn mouse_extend_drag_in_rect(&mut self, area: Rect, col: u16, row: u16) {
2741        self.mouse_extend_drag(area.x, area.y, col, row);
2742    }
2743
2744    pub fn insert_str(&mut self, text: &str) {
2745        let pos = crate::types::Cursor::cursor(&self.buffer);
2746        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2747        self.push_buffer_content_to_textarea();
2748        self.mark_content_dirty();
2749    }
2750
2751    pub fn accept_completion(&mut self, completion: &str) {
2752        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2753        let cursor_pos = CursorTrait::cursor(&self.buffer);
2754        let cursor_row = cursor_pos.line as usize;
2755        let cursor_col = cursor_pos.col as usize;
2756        let line = buf_line(&self.buffer, cursor_row).unwrap_or("").to_string();
2757        let chars: Vec<char> = line.chars().collect();
2758        let prefix_len = chars[..cursor_col.min(chars.len())]
2759            .iter()
2760            .rev()
2761            .take_while(|c| c.is_alphanumeric() || **c == '_')
2762            .count();
2763        if prefix_len > 0 {
2764            let start = Pos {
2765                line: cursor_row as u32,
2766                col: (cursor_col - prefix_len) as u32,
2767            };
2768            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2769        }
2770        let cursor = CursorTrait::cursor(&self.buffer);
2771        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2772        self.push_buffer_content_to_textarea();
2773        self.mark_content_dirty();
2774    }
2775
2776    pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2777        let rc = buf_cursor_rc(&self.buffer);
2778        (buf_lines_to_vec(&self.buffer), rc)
2779    }
2780
2781    /// Walk one step back through the undo history. Equivalent to the
2782    /// user pressing `u` in normal mode. Drains the most recent undo
2783    /// entry and pushes it onto the redo stack.
2784    pub fn undo(&mut self) {
2785        crate::vim::do_undo(self);
2786    }
2787
2788    /// Walk one step forward through the redo history. Equivalent to
2789    /// `<C-r>` in normal mode.
2790    pub fn redo(&mut self) {
2791        crate::vim::do_redo(self);
2792    }
2793
2794    /// Snapshot current buffer state onto the undo stack and clear
2795    /// the redo stack. Bounded by `settings.undo_levels` — older
2796    /// entries pruned. Call before any group of buffer mutations the
2797    /// user might want to undo as a single step.
2798    pub fn push_undo(&mut self) {
2799        let snap = self.snapshot();
2800        self.undo_stack.push(snap);
2801        self.cap_undo();
2802        self.redo_stack.clear();
2803    }
2804
2805    /// Trim the undo stack down to `settings.undo_levels`, dropping
2806    /// the oldest entries. `undo_levels == 0` is treated as
2807    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2808    /// skipped — guarding with `> 0` is one line shorter than gating
2809    /// the cap path with an explicit zero-check above the call site).
2810    pub(crate) fn cap_undo(&mut self) {
2811        let cap = self.settings.undo_levels as usize;
2812        if cap > 0 && self.undo_stack.len() > cap {
2813            let diff = self.undo_stack.len() - cap;
2814            self.undo_stack.drain(..diff);
2815        }
2816    }
2817
2818    /// Test-only accessor for the undo stack length.
2819    #[doc(hidden)]
2820    pub fn undo_stack_len(&self) -> usize {
2821        self.undo_stack.len()
2822    }
2823
2824    /// Replace the buffer with `lines` joined by `\n` and set the
2825    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2826    /// paths. Marks the editor dirty.
2827    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2828        let text = lines.join("\n");
2829        crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2830        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2831        // Bulk replace — supersedes any queued ContentEdits.
2832        self.pending_content_edits.clear();
2833        self.pending_content_reset = true;
2834        self.mark_content_dirty();
2835    }
2836
2837    /// Returns true if the key was consumed by the editor.
2838    /// Replace the char under the cursor with `ch`, `count` times. Matches
2839    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2840    /// snapshot taken once at start. Promoted to public surface in 0.5.5
2841    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2842    /// re-entering the FSM.
2843    pub fn replace_char_at(&mut self, ch: char, count: usize) {
2844        vim::replace_char(self, ch, count);
2845    }
2846
2847    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2848    /// to the `count`-th occurrence of `ch` on the current line, respecting
2849    /// `forward` (direction) and `till` (stop one char before target).
2850    /// Records `last_find` so `;` / `,` repeat work.
2851    ///
2852    /// No-op if the target char isn't on the current line within range.
2853    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2854    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2855        vim::apply_find_char(self, ch, forward, till, count.max(1));
2856    }
2857
2858    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2859    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2860    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2861    ///
2862    /// Promoted to public surface in 0.5.10 so hjkl-vim's
2863    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2864    /// re-entering the engine FSM.
2865    pub fn after_g(&mut self, ch: char, count: usize) {
2866        vim::apply_after_g(self, ch, count);
2867    }
2868
2869    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2870    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2871    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2872    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2873    ///
2874    /// Promoted to public surface in 0.5.11 so hjkl-vim's
2875    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2876    /// re-entering the engine FSM.
2877    pub fn after_z(&mut self, ch: char, count: usize) {
2878        vim::apply_after_z(self, ch, count);
2879    }
2880
2881    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2882    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2883    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2884    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2885    /// then calls `apply_op_with_motion`. `total_count` is already the product of
2886    /// the prefix count and any inner count accumulated by the reducer.
2887    ///
2888    /// No-op when `motion_key` does not map to a known motion (engine silently
2889    /// cancels the operator, matching vim's behaviour on unknown motions).
2890    ///
2891    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2892    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2893    /// re-entering the engine FSM.
2894    pub fn apply_op_motion(
2895        &mut self,
2896        op: crate::vim::Operator,
2897        motion_key: char,
2898        total_count: usize,
2899    ) {
2900        vim::apply_op_motion_key(self, op, motion_key, total_count);
2901    }
2902
2903    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2904    /// `total_count` is the product of prefix count and inner count.
2905    ///
2906    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2907    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2908    /// re-entering the engine FSM.
2909    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2910        vim::apply_op_double(self, op, total_count);
2911    }
2912
2913    /// Set `Pending::OpTextObj { op, count1, inner }` — i.e. the engine is now
2914    /// waiting for the text-object character (`w`, `(`, `"`, `p`, `t`, `s`).
2915    /// The next key is routed through the engine FSM (via `is_chord_pending()`
2916    /// bypass in the host) which handles `OpTextObj`.
2917    ///
2918    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2919    /// `PendingState::AfterOp` reducer can dispatch `EnterOpTextObj` without
2920    /// re-entering the engine FSM.
2921    pub fn enter_op_text_obj(&mut self, op: crate::vim::Operator, count1: usize, inner: bool) {
2922        vim::enter_op_text_obj(self, op, count1, inner);
2923    }
2924
2925    /// Set `Pending::OpG { op, count1 }` — engine waiting for the `g`-second
2926    /// char (`g` for `dgg`, etc.). The next key is routed through the engine FSM.
2927    ///
2928    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2929    /// `PendingState::AfterOp` reducer can dispatch `EnterOpG` without
2930    /// re-entering the engine FSM.
2931    pub fn enter_op_g(&mut self, op: crate::vim::Operator, count1: usize) {
2932        vim::enter_op_g(self, op, count1);
2933    }
2934
2935    /// Set `Pending::OpFind { op, count1, forward, till }` — engine waiting for
2936    /// the find-target character. The next key is routed through the engine FSM.
2937    ///
2938    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2939    /// `PendingState::AfterOp` reducer can dispatch `EnterOpFind` without
2940    /// re-entering the engine FSM.
2941    ///
2942    /// Still used by the chord-init op path (gu/gU/g~/gq + f/F/t/T via engine
2943    /// FSM). The reducer path uses `apply_op_find` instead.
2944    pub fn enter_op_find(
2945        &mut self,
2946        op: crate::vim::Operator,
2947        count1: usize,
2948        forward: bool,
2949        till: bool,
2950    ) {
2951        vim::enter_op_find(self, op, count1, forward, till);
2952    }
2953
2954    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2955    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2956    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2957    /// updates `last_change` when `op` is Change (for dot-repeat).
2958    ///
2959    /// `total_count` is the product of prefix count and any inner count
2960    /// accumulated by the reducer — already folded at transition time.
2961    ///
2962    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2963    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2964    /// re-entering the engine FSM. `handle_op_find_target` (used by the
2965    /// chord-init op path) delegates here to avoid logic duplication.
2966    pub fn apply_op_find(
2967        &mut self,
2968        op: crate::vim::Operator,
2969        ch: char,
2970        forward: bool,
2971        till: bool,
2972        total_count: usize,
2973    ) {
2974        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2975    }
2976
2977    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2978    /// Maps `ch` to a `TextObject` per the standard vim table, calls
2979    /// `apply_op_with_text_object`, and records `last_change` when `op` is
2980    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2981    /// matching the engine FSM's behaviour on unrecognised text-object chars.
2982    ///
2983    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2984    /// `apply_op_find` but is currently unused — text objects don't repeat in
2985    /// vim's current grammar. Kept for future-proofing.
2986    ///
2987    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
2988    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
2989    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
2990    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
2991    /// duplication.
2992    pub fn apply_op_text_obj(
2993        &mut self,
2994        op: crate::vim::Operator,
2995        ch: char,
2996        inner: bool,
2997        total_count: usize,
2998    ) {
2999        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
3000    }
3001
3002    /// Apply an operator over a g-chord motion or case-op linewise form
3003    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
3004    ///
3005    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
3006    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
3007    /// - Otherwise maps `ch` to a motion:
3008    ///   - `'g'` → `Motion::FileTop` (gg)
3009    ///   - `'e'` → `Motion::WordEndBack` (ge)
3010    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
3011    ///   - `'j'` → `Motion::ScreenDown` (gj)
3012    ///   - `'k'` → `Motion::ScreenUp` (gk)
3013    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
3014    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
3015    ///
3016    /// `total_count` is the already-folded product of prefix and inner counts.
3017    ///
3018    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
3019    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
3020    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
3021    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
3022    /// duplication.
3023    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
3024        vim::apply_op_g_inner(self, op, ch, total_count);
3025    }
3026
3027    #[cfg(feature = "crossterm")]
3028    pub fn handle_key(&mut self, key: KeyEvent) -> bool {
3029        let input = crossterm_to_input(key);
3030        if input.key == Key::Null {
3031            return false;
3032        }
3033        let consumed = vim::step(self, input);
3034        self.emit_cursor_shape_if_changed();
3035        consumed
3036    }
3037}
3038
3039/// Visual column of the character at `char_col` in `line`, treating `\t`
3040/// as expansion to the next `tab_width` stop and every other char as
3041/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
3042/// the cursor math elsewhere also assumes single-cell chars.
3043fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
3044    let mut visual = 0usize;
3045    for (i, ch) in line.chars().enumerate() {
3046        if i >= char_col {
3047            break;
3048        }
3049        if ch == '\t' {
3050            visual += tab_width - (visual % tab_width);
3051        } else {
3052            visual += 1;
3053        }
3054    }
3055    visual
3056}
3057
3058#[cfg(feature = "crossterm")]
3059impl From<KeyEvent> for Input {
3060    fn from(key: KeyEvent) -> Self {
3061        let k = match key.code {
3062            KeyCode::Char(c) => Key::Char(c),
3063            KeyCode::Backspace => Key::Backspace,
3064            KeyCode::Delete => Key::Delete,
3065            KeyCode::Enter => Key::Enter,
3066            KeyCode::Left => Key::Left,
3067            KeyCode::Right => Key::Right,
3068            KeyCode::Up => Key::Up,
3069            KeyCode::Down => Key::Down,
3070            KeyCode::Home => Key::Home,
3071            KeyCode::End => Key::End,
3072            KeyCode::Tab => Key::Tab,
3073            KeyCode::Esc => Key::Esc,
3074            _ => Key::Null,
3075        };
3076        Input {
3077            key: k,
3078            ctrl: key.modifiers.contains(KeyModifiers::CONTROL),
3079            alt: key.modifiers.contains(KeyModifiers::ALT),
3080            shift: key.modifiers.contains(KeyModifiers::SHIFT),
3081        }
3082    }
3083}
3084
3085/// Crossterm `KeyEvent` → engine `Input`. Thin wrapper that delegates
3086/// to the [`From`] impl above; kept as a free fn for the in-tree
3087/// callers in the legacy ratatui-coupled paths.
3088#[cfg(feature = "crossterm")]
3089pub(super) fn crossterm_to_input(key: KeyEvent) -> Input {
3090    Input::from(key)
3091}
3092
3093#[cfg(all(test, feature = "crossterm", feature = "ratatui"))]
3094mod tests {
3095    use super::*;
3096    use crate::types::Host;
3097    use crossterm::event::KeyEvent;
3098
3099    fn key(code: KeyCode) -> KeyEvent {
3100        KeyEvent::new(code, KeyModifiers::NONE)
3101    }
3102    fn shift_key(code: KeyCode) -> KeyEvent {
3103        KeyEvent::new(code, KeyModifiers::SHIFT)
3104    }
3105    fn ctrl_key(code: KeyCode) -> KeyEvent {
3106        KeyEvent::new(code, KeyModifiers::CONTROL)
3107    }
3108
3109    #[test]
3110    fn vim_normal_to_insert() {
3111        let mut e = Editor::new(
3112            hjkl_buffer::Buffer::new(),
3113            crate::types::DefaultHost::new(),
3114            crate::types::Options::default(),
3115        );
3116        e.handle_key(key(KeyCode::Char('i')));
3117        assert_eq!(e.vim_mode(), VimMode::Insert);
3118    }
3119
3120    #[test]
3121    fn with_options_constructs_from_spec_options() {
3122        // 0.0.33 (Patch C-γ): SPEC-shaped constructor preview.
3123        // Build with custom Options + DefaultHost; confirm the
3124        // settings translation honours the SPEC field names.
3125        let opts = crate::types::Options {
3126            shiftwidth: 4,
3127            tabstop: 4,
3128            expandtab: true,
3129            iskeyword: "@,a-z".to_string(),
3130            wrap: crate::types::WrapMode::Word,
3131            ..crate::types::Options::default()
3132        };
3133        let mut e = Editor::new(
3134            hjkl_buffer::Buffer::new(),
3135            crate::types::DefaultHost::new(),
3136            opts,
3137        );
3138        assert_eq!(e.settings().shiftwidth, 4);
3139        assert_eq!(e.settings().tabstop, 4);
3140        assert!(e.settings().expandtab);
3141        assert_eq!(e.settings().iskeyword, "@,a-z");
3142        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Word);
3143        // Confirm input plumbing still works.
3144        e.handle_key(key(KeyCode::Char('i')));
3145        assert_eq!(e.vim_mode(), VimMode::Insert);
3146    }
3147
3148    #[test]
3149    fn feed_input_char_routes_through_handle_key() {
3150        use crate::{Modifiers, PlannedInput};
3151        let mut e = Editor::new(
3152            hjkl_buffer::Buffer::new(),
3153            crate::types::DefaultHost::new(),
3154            crate::types::Options::default(),
3155        );
3156        e.set_content("abc");
3157        // `i` enters insert mode via SPEC input.
3158        e.feed_input(PlannedInput::Char('i', Modifiers::default()));
3159        assert_eq!(e.vim_mode(), VimMode::Insert);
3160        // Type 'X' via SPEC input.
3161        e.feed_input(PlannedInput::Char('X', Modifiers::default()));
3162        assert!(e.content().contains('X'));
3163    }
3164
3165    #[test]
3166    fn feed_input_special_key_routes() {
3167        use crate::{Modifiers, PlannedInput, SpecialKey};
3168        let mut e = Editor::new(
3169            hjkl_buffer::Buffer::new(),
3170            crate::types::DefaultHost::new(),
3171            crate::types::Options::default(),
3172        );
3173        e.set_content("abc");
3174        e.feed_input(PlannedInput::Char('i', Modifiers::default()));
3175        assert_eq!(e.vim_mode(), VimMode::Insert);
3176        e.feed_input(PlannedInput::Key(SpecialKey::Esc, Modifiers::default()));
3177        assert_eq!(e.vim_mode(), VimMode::Normal);
3178    }
3179
3180    #[test]
3181    fn feed_input_mouse_paste_focus_resize_no_op() {
3182        use crate::{MouseEvent, MouseKind, PlannedInput, Pos};
3183        let mut e = Editor::new(
3184            hjkl_buffer::Buffer::new(),
3185            crate::types::DefaultHost::new(),
3186            crate::types::Options::default(),
3187        );
3188        e.set_content("abc");
3189        let mode_before = e.vim_mode();
3190        let consumed = e.feed_input(PlannedInput::Mouse(MouseEvent {
3191            kind: MouseKind::Press,
3192            pos: Pos::new(0, 0),
3193            mods: Default::default(),
3194        }));
3195        assert!(!consumed);
3196        assert_eq!(e.vim_mode(), mode_before);
3197        assert!(!e.feed_input(PlannedInput::Paste("xx".into())));
3198        assert!(!e.feed_input(PlannedInput::FocusGained));
3199        assert!(!e.feed_input(PlannedInput::FocusLost));
3200        assert!(!e.feed_input(PlannedInput::Resize(80, 24)));
3201    }
3202
3203    #[test]
3204    fn intern_style_dedups_engine_native_styles() {
3205        use crate::types::{Attrs, Color, Style};
3206        let mut e = Editor::new(
3207            hjkl_buffer::Buffer::new(),
3208            crate::types::DefaultHost::new(),
3209            crate::types::Options::default(),
3210        );
3211        let s = Style {
3212            fg: Some(Color(255, 0, 0)),
3213            bg: None,
3214            attrs: Attrs::BOLD,
3215        };
3216        let id_a = e.intern_style(s);
3217        // Re-interning the same engine style returns the same id.
3218        let id_b = e.intern_style(s);
3219        assert_eq!(id_a, id_b);
3220        // Engine accessor returns the same style back.
3221        let back = e.engine_style_at(id_a).expect("interned");
3222        assert_eq!(back, s);
3223    }
3224
3225    #[test]
3226    fn engine_style_at_out_of_range_returns_none() {
3227        let e = Editor::new(
3228            hjkl_buffer::Buffer::new(),
3229            crate::types::DefaultHost::new(),
3230            crate::types::Options::default(),
3231        );
3232        assert!(e.engine_style_at(99).is_none());
3233    }
3234
3235    #[test]
3236    fn take_changes_emits_per_row_for_block_insert() {
3237        // Visual-block insert (`Ctrl-V` then `I` then text then Esc)
3238        // produces an InsertBlock buffer edit with one chunk per
3239        // selected row. take_changes should surface N EditOps,
3240        // not a single placeholder.
3241        let mut e = Editor::new(
3242            hjkl_buffer::Buffer::new(),
3243            crate::types::DefaultHost::new(),
3244            crate::types::Options::default(),
3245        );
3246        e.set_content("aaa\nbbb\nccc\nddd");
3247        // Place cursor at (0, 0), enter visual-block, extend down 2.
3248        e.handle_key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
3249        e.handle_key(key(KeyCode::Char('j')));
3250        e.handle_key(key(KeyCode::Char('j')));
3251        // `I` to enter insert mode at the block left edge.
3252        e.handle_key(shift_key(KeyCode::Char('I')));
3253        e.handle_key(key(KeyCode::Char('X')));
3254        e.handle_key(key(KeyCode::Esc));
3255
3256        let changes = e.take_changes();
3257        // Expect at least 3 entries — one per row in the 3-row block.
3258        // Vim's block-I inserts on Esc; the cleanup may add more
3259        // EditOps for cursor sync, hence >= rather than ==.
3260        assert!(
3261            changes.len() >= 3,
3262            "expected >=3 EditOps for 3-row block insert, got {}: {changes:?}",
3263            changes.len()
3264        );
3265    }
3266
3267    #[test]
3268    fn take_changes_drains_after_insert() {
3269        let mut e = Editor::new(
3270            hjkl_buffer::Buffer::new(),
3271            crate::types::DefaultHost::new(),
3272            crate::types::Options::default(),
3273        );
3274        e.set_content("abc");
3275        // Empty initially.
3276        assert!(e.take_changes().is_empty());
3277        // Type a char in insert mode.
3278        e.handle_key(key(KeyCode::Char('i')));
3279        e.handle_key(key(KeyCode::Char('X')));
3280        let changes = e.take_changes();
3281        assert!(
3282            !changes.is_empty(),
3283            "insert mode keystroke should produce a change"
3284        );
3285        // Drained — second call empty.
3286        assert!(e.take_changes().is_empty());
3287    }
3288
3289    #[test]
3290    fn options_bridge_roundtrip() {
3291        let mut e = Editor::new(
3292            hjkl_buffer::Buffer::new(),
3293            crate::types::DefaultHost::new(),
3294            crate::types::Options::default(),
3295        );
3296        let opts = e.current_options();
3297        // 0.2.0: defaults flipped to modern editor norms — 4-space soft tabs.
3298        assert_eq!(opts.shiftwidth, 4);
3299        assert_eq!(opts.tabstop, 4);
3300
3301        let new_opts = crate::types::Options {
3302            shiftwidth: 4,
3303            tabstop: 2,
3304            ignorecase: true,
3305            ..crate::types::Options::default()
3306        };
3307        e.apply_options(&new_opts);
3308
3309        let after = e.current_options();
3310        assert_eq!(after.shiftwidth, 4);
3311        assert_eq!(after.tabstop, 2);
3312        assert!(after.ignorecase);
3313    }
3314
3315    #[test]
3316    fn selection_highlight_none_in_normal() {
3317        let mut e = Editor::new(
3318            hjkl_buffer::Buffer::new(),
3319            crate::types::DefaultHost::new(),
3320            crate::types::Options::default(),
3321        );
3322        e.set_content("hello");
3323        assert!(e.selection_highlight().is_none());
3324    }
3325
3326    #[test]
3327    fn selection_highlight_some_in_visual() {
3328        use crate::types::HighlightKind;
3329        let mut e = Editor::new(
3330            hjkl_buffer::Buffer::new(),
3331            crate::types::DefaultHost::new(),
3332            crate::types::Options::default(),
3333        );
3334        e.set_content("hello world");
3335        e.handle_key(key(KeyCode::Char('v')));
3336        e.handle_key(key(KeyCode::Char('l')));
3337        e.handle_key(key(KeyCode::Char('l')));
3338        let h = e
3339            .selection_highlight()
3340            .expect("visual mode should produce a highlight");
3341        assert_eq!(h.kind, HighlightKind::Selection);
3342        assert_eq!(h.range.start.line, 0);
3343        assert_eq!(h.range.end.line, 0);
3344    }
3345
3346    #[test]
3347    fn highlights_emit_incsearch_during_active_prompt() {
3348        use crate::types::HighlightKind;
3349        let mut e = Editor::new(
3350            hjkl_buffer::Buffer::new(),
3351            crate::types::DefaultHost::new(),
3352            crate::types::Options::default(),
3353        );
3354        e.set_content("foo bar foo\nbaz\n");
3355        // Open the `/` prompt and type `f` `o` `o`.
3356        e.handle_key(key(KeyCode::Char('/')));
3357        e.handle_key(key(KeyCode::Char('f')));
3358        e.handle_key(key(KeyCode::Char('o')));
3359        e.handle_key(key(KeyCode::Char('o')));
3360        // Prompt should be active.
3361        assert!(e.search_prompt().is_some());
3362        let hs = e.highlights_for_line(0);
3363        assert_eq!(hs.len(), 2);
3364        for h in &hs {
3365            assert_eq!(h.kind, HighlightKind::IncSearch);
3366        }
3367    }
3368
3369    #[test]
3370    fn highlights_empty_for_blank_prompt() {
3371        let mut e = Editor::new(
3372            hjkl_buffer::Buffer::new(),
3373            crate::types::DefaultHost::new(),
3374            crate::types::Options::default(),
3375        );
3376        e.set_content("foo");
3377        e.handle_key(key(KeyCode::Char('/')));
3378        // Nothing typed yet — prompt active but text empty.
3379        assert!(e.search_prompt().is_some());
3380        assert!(e.highlights_for_line(0).is_empty());
3381    }
3382
3383    #[test]
3384    fn highlights_emit_search_matches() {
3385        use crate::types::HighlightKind;
3386        let mut e = Editor::new(
3387            hjkl_buffer::Buffer::new(),
3388            crate::types::DefaultHost::new(),
3389            crate::types::Options::default(),
3390        );
3391        e.set_content("foo bar foo\nbaz qux\n");
3392        // 0.0.35: arm via the engine search state. The buffer
3393        // accessor still works (deprecated) but new code goes
3394        // through Editor.
3395        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
3396        let hs = e.highlights_for_line(0);
3397        assert_eq!(hs.len(), 2);
3398        for h in &hs {
3399            assert_eq!(h.kind, HighlightKind::SearchMatch);
3400            assert_eq!(h.range.start.line, 0);
3401            assert_eq!(h.range.end.line, 0);
3402        }
3403    }
3404
3405    #[test]
3406    fn highlights_empty_without_pattern() {
3407        let mut e = Editor::new(
3408            hjkl_buffer::Buffer::new(),
3409            crate::types::DefaultHost::new(),
3410            crate::types::Options::default(),
3411        );
3412        e.set_content("foo bar");
3413        assert!(e.highlights_for_line(0).is_empty());
3414    }
3415
3416    #[test]
3417    fn highlights_empty_for_out_of_range_line() {
3418        let mut e = Editor::new(
3419            hjkl_buffer::Buffer::new(),
3420            crate::types::DefaultHost::new(),
3421            crate::types::Options::default(),
3422        );
3423        e.set_content("foo");
3424        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
3425        assert!(e.highlights_for_line(99).is_empty());
3426    }
3427
3428    #[test]
3429    fn render_frame_reflects_mode_and_cursor() {
3430        use crate::types::{CursorShape, SnapshotMode};
3431        let mut e = Editor::new(
3432            hjkl_buffer::Buffer::new(),
3433            crate::types::DefaultHost::new(),
3434            crate::types::Options::default(),
3435        );
3436        e.set_content("alpha\nbeta");
3437        let f = e.render_frame();
3438        assert_eq!(f.mode, SnapshotMode::Normal);
3439        assert_eq!(f.cursor_shape, CursorShape::Block);
3440        assert_eq!(f.line_count, 2);
3441
3442        e.handle_key(key(KeyCode::Char('i')));
3443        let f = e.render_frame();
3444        assert_eq!(f.mode, SnapshotMode::Insert);
3445        assert_eq!(f.cursor_shape, CursorShape::Bar);
3446    }
3447
3448    #[test]
3449    fn snapshot_roundtrips_through_restore() {
3450        use crate::types::SnapshotMode;
3451        let mut e = Editor::new(
3452            hjkl_buffer::Buffer::new(),
3453            crate::types::DefaultHost::new(),
3454            crate::types::Options::default(),
3455        );
3456        e.set_content("alpha\nbeta\ngamma");
3457        e.jump_cursor(2, 3);
3458        let snap = e.take_snapshot();
3459        assert_eq!(snap.mode, SnapshotMode::Normal);
3460        assert_eq!(snap.cursor, (2, 3));
3461        assert_eq!(snap.lines.len(), 3);
3462
3463        let mut other = Editor::new(
3464            hjkl_buffer::Buffer::new(),
3465            crate::types::DefaultHost::new(),
3466            crate::types::Options::default(),
3467        );
3468        other.restore_snapshot(snap).expect("restore");
3469        assert_eq!(other.cursor(), (2, 3));
3470        assert_eq!(other.buffer().lines().len(), 3);
3471    }
3472
3473    #[test]
3474    fn restore_snapshot_rejects_version_mismatch() {
3475        let mut e = Editor::new(
3476            hjkl_buffer::Buffer::new(),
3477            crate::types::DefaultHost::new(),
3478            crate::types::Options::default(),
3479        );
3480        let mut snap = e.take_snapshot();
3481        snap.version = 9999;
3482        match e.restore_snapshot(snap) {
3483            Err(crate::EngineError::SnapshotVersion(got, want)) => {
3484                assert_eq!(got, 9999);
3485                assert_eq!(want, crate::types::EditorSnapshot::VERSION);
3486            }
3487            other => panic!("expected SnapshotVersion err, got {other:?}"),
3488        }
3489    }
3490
3491    #[test]
3492    fn take_content_change_returns_some_on_first_dirty() {
3493        let mut e = Editor::new(
3494            hjkl_buffer::Buffer::new(),
3495            crate::types::DefaultHost::new(),
3496            crate::types::Options::default(),
3497        );
3498        e.set_content("hello");
3499        let first = e.take_content_change();
3500        assert!(first.is_some());
3501        let second = e.take_content_change();
3502        assert!(second.is_none());
3503    }
3504
3505    #[test]
3506    fn take_content_change_none_until_mutation() {
3507        let mut e = Editor::new(
3508            hjkl_buffer::Buffer::new(),
3509            crate::types::DefaultHost::new(),
3510            crate::types::Options::default(),
3511        );
3512        e.set_content("hello");
3513        // drain
3514        e.take_content_change();
3515        assert!(e.take_content_change().is_none());
3516        // mutate via insert mode
3517        e.handle_key(key(KeyCode::Char('i')));
3518        e.handle_key(key(KeyCode::Char('x')));
3519        let after = e.take_content_change();
3520        assert!(after.is_some());
3521        assert!(after.unwrap().contains('x'));
3522    }
3523
3524    #[test]
3525    fn vim_insert_to_normal() {
3526        let mut e = Editor::new(
3527            hjkl_buffer::Buffer::new(),
3528            crate::types::DefaultHost::new(),
3529            crate::types::Options::default(),
3530        );
3531        e.handle_key(key(KeyCode::Char('i')));
3532        e.handle_key(key(KeyCode::Esc));
3533        assert_eq!(e.vim_mode(), VimMode::Normal);
3534    }
3535
3536    #[test]
3537    fn vim_normal_to_visual() {
3538        let mut e = Editor::new(
3539            hjkl_buffer::Buffer::new(),
3540            crate::types::DefaultHost::new(),
3541            crate::types::Options::default(),
3542        );
3543        e.handle_key(key(KeyCode::Char('v')));
3544        assert_eq!(e.vim_mode(), VimMode::Visual);
3545    }
3546
3547    #[test]
3548    fn vim_visual_to_normal() {
3549        let mut e = Editor::new(
3550            hjkl_buffer::Buffer::new(),
3551            crate::types::DefaultHost::new(),
3552            crate::types::Options::default(),
3553        );
3554        e.handle_key(key(KeyCode::Char('v')));
3555        e.handle_key(key(KeyCode::Esc));
3556        assert_eq!(e.vim_mode(), VimMode::Normal);
3557    }
3558
3559    #[test]
3560    fn vim_shift_i_moves_to_first_non_whitespace() {
3561        let mut e = Editor::new(
3562            hjkl_buffer::Buffer::new(),
3563            crate::types::DefaultHost::new(),
3564            crate::types::Options::default(),
3565        );
3566        e.set_content("   hello");
3567        e.jump_cursor(0, 8);
3568        e.handle_key(shift_key(KeyCode::Char('I')));
3569        assert_eq!(e.vim_mode(), VimMode::Insert);
3570        assert_eq!(e.cursor(), (0, 3));
3571    }
3572
3573    #[test]
3574    fn vim_shift_a_moves_to_end_and_insert() {
3575        let mut e = Editor::new(
3576            hjkl_buffer::Buffer::new(),
3577            crate::types::DefaultHost::new(),
3578            crate::types::Options::default(),
3579        );
3580        e.set_content("hello");
3581        e.handle_key(shift_key(KeyCode::Char('A')));
3582        assert_eq!(e.vim_mode(), VimMode::Insert);
3583        assert_eq!(e.cursor().1, 5);
3584    }
3585
3586    #[test]
3587    fn count_10j_moves_down_10() {
3588        let mut e = Editor::new(
3589            hjkl_buffer::Buffer::new(),
3590            crate::types::DefaultHost::new(),
3591            crate::types::Options::default(),
3592        );
3593        e.set_content(
3594            (0..20)
3595                .map(|i| format!("line{i}"))
3596                .collect::<Vec<_>>()
3597                .join("\n")
3598                .as_str(),
3599        );
3600        for d in "10".chars() {
3601            e.handle_key(key(KeyCode::Char(d)));
3602        }
3603        e.handle_key(key(KeyCode::Char('j')));
3604        assert_eq!(e.cursor().0, 10);
3605    }
3606
3607    #[test]
3608    fn count_o_repeats_insert_on_esc() {
3609        let mut e = Editor::new(
3610            hjkl_buffer::Buffer::new(),
3611            crate::types::DefaultHost::new(),
3612            crate::types::Options::default(),
3613        );
3614        e.set_content("hello");
3615        for d in "3".chars() {
3616            e.handle_key(key(KeyCode::Char(d)));
3617        }
3618        e.handle_key(key(KeyCode::Char('o')));
3619        assert_eq!(e.vim_mode(), VimMode::Insert);
3620        for c in "world".chars() {
3621            e.handle_key(key(KeyCode::Char(c)));
3622        }
3623        e.handle_key(key(KeyCode::Esc));
3624        assert_eq!(e.vim_mode(), VimMode::Normal);
3625        assert_eq!(e.buffer().lines().len(), 4);
3626        assert!(e.buffer().lines().iter().skip(1).all(|l| l == "world"));
3627    }
3628
3629    #[test]
3630    fn count_i_repeats_text_on_esc() {
3631        let mut e = Editor::new(
3632            hjkl_buffer::Buffer::new(),
3633            crate::types::DefaultHost::new(),
3634            crate::types::Options::default(),
3635        );
3636        e.set_content("");
3637        for d in "3".chars() {
3638            e.handle_key(key(KeyCode::Char(d)));
3639        }
3640        e.handle_key(key(KeyCode::Char('i')));
3641        for c in "ab".chars() {
3642            e.handle_key(key(KeyCode::Char(c)));
3643        }
3644        e.handle_key(key(KeyCode::Esc));
3645        assert_eq!(e.vim_mode(), VimMode::Normal);
3646        assert_eq!(e.buffer().lines()[0], "ababab");
3647    }
3648
3649    #[test]
3650    fn vim_shift_o_opens_line_above() {
3651        let mut e = Editor::new(
3652            hjkl_buffer::Buffer::new(),
3653            crate::types::DefaultHost::new(),
3654            crate::types::Options::default(),
3655        );
3656        e.set_content("hello");
3657        e.handle_key(shift_key(KeyCode::Char('O')));
3658        assert_eq!(e.vim_mode(), VimMode::Insert);
3659        assert_eq!(e.cursor(), (0, 0));
3660        assert_eq!(e.buffer().lines().len(), 2);
3661    }
3662
3663    #[test]
3664    fn vim_gg_goes_to_top() {
3665        let mut e = Editor::new(
3666            hjkl_buffer::Buffer::new(),
3667            crate::types::DefaultHost::new(),
3668            crate::types::Options::default(),
3669        );
3670        e.set_content("a\nb\nc");
3671        e.jump_cursor(2, 0);
3672        e.handle_key(key(KeyCode::Char('g')));
3673        e.handle_key(key(KeyCode::Char('g')));
3674        assert_eq!(e.cursor().0, 0);
3675    }
3676
3677    #[test]
3678    fn vim_shift_g_goes_to_bottom() {
3679        let mut e = Editor::new(
3680            hjkl_buffer::Buffer::new(),
3681            crate::types::DefaultHost::new(),
3682            crate::types::Options::default(),
3683        );
3684        e.set_content("a\nb\nc");
3685        e.handle_key(shift_key(KeyCode::Char('G')));
3686        assert_eq!(e.cursor().0, 2);
3687    }
3688
3689    #[test]
3690    fn vim_dd_deletes_line() {
3691        let mut e = Editor::new(
3692            hjkl_buffer::Buffer::new(),
3693            crate::types::DefaultHost::new(),
3694            crate::types::Options::default(),
3695        );
3696        e.set_content("first\nsecond");
3697        e.handle_key(key(KeyCode::Char('d')));
3698        e.handle_key(key(KeyCode::Char('d')));
3699        assert_eq!(e.buffer().lines().len(), 1);
3700        assert_eq!(e.buffer().lines()[0], "second");
3701    }
3702
3703    #[test]
3704    fn vim_dw_deletes_word() {
3705        let mut e = Editor::new(
3706            hjkl_buffer::Buffer::new(),
3707            crate::types::DefaultHost::new(),
3708            crate::types::Options::default(),
3709        );
3710        e.set_content("hello world");
3711        e.handle_key(key(KeyCode::Char('d')));
3712        e.handle_key(key(KeyCode::Char('w')));
3713        assert_eq!(e.vim_mode(), VimMode::Normal);
3714        assert!(!e.buffer().lines()[0].starts_with("hello"));
3715    }
3716
3717    #[test]
3718    fn vim_yy_yanks_line() {
3719        let mut e = Editor::new(
3720            hjkl_buffer::Buffer::new(),
3721            crate::types::DefaultHost::new(),
3722            crate::types::Options::default(),
3723        );
3724        e.set_content("hello\nworld");
3725        e.handle_key(key(KeyCode::Char('y')));
3726        e.handle_key(key(KeyCode::Char('y')));
3727        assert!(e.last_yank.as_deref().unwrap_or("").starts_with("hello"));
3728    }
3729
3730    #[test]
3731    fn vim_yy_does_not_move_cursor() {
3732        let mut e = Editor::new(
3733            hjkl_buffer::Buffer::new(),
3734            crate::types::DefaultHost::new(),
3735            crate::types::Options::default(),
3736        );
3737        e.set_content("first\nsecond\nthird");
3738        e.jump_cursor(1, 0);
3739        let before = e.cursor();
3740        e.handle_key(key(KeyCode::Char('y')));
3741        e.handle_key(key(KeyCode::Char('y')));
3742        assert_eq!(e.cursor(), before);
3743        assert_eq!(e.vim_mode(), VimMode::Normal);
3744    }
3745
3746    #[test]
3747    fn vim_yw_yanks_word() {
3748        let mut e = Editor::new(
3749            hjkl_buffer::Buffer::new(),
3750            crate::types::DefaultHost::new(),
3751            crate::types::Options::default(),
3752        );
3753        e.set_content("hello world");
3754        e.handle_key(key(KeyCode::Char('y')));
3755        e.handle_key(key(KeyCode::Char('w')));
3756        assert_eq!(e.vim_mode(), VimMode::Normal);
3757        assert!(e.last_yank.is_some());
3758    }
3759
3760    #[test]
3761    fn vim_cc_changes_line() {
3762        let mut e = Editor::new(
3763            hjkl_buffer::Buffer::new(),
3764            crate::types::DefaultHost::new(),
3765            crate::types::Options::default(),
3766        );
3767        e.set_content("hello\nworld");
3768        e.handle_key(key(KeyCode::Char('c')));
3769        e.handle_key(key(KeyCode::Char('c')));
3770        assert_eq!(e.vim_mode(), VimMode::Insert);
3771    }
3772
3773    #[test]
3774    fn vim_u_undoes_insert_session_as_chunk() {
3775        let mut e = Editor::new(
3776            hjkl_buffer::Buffer::new(),
3777            crate::types::DefaultHost::new(),
3778            crate::types::Options::default(),
3779        );
3780        e.set_content("hello");
3781        e.handle_key(key(KeyCode::Char('i')));
3782        e.handle_key(key(KeyCode::Enter));
3783        e.handle_key(key(KeyCode::Enter));
3784        e.handle_key(key(KeyCode::Esc));
3785        assert_eq!(e.buffer().lines().len(), 3);
3786        e.handle_key(key(KeyCode::Char('u')));
3787        assert_eq!(e.buffer().lines().len(), 1);
3788        assert_eq!(e.buffer().lines()[0], "hello");
3789    }
3790
3791    #[test]
3792    fn vim_undo_redo_roundtrip() {
3793        let mut e = Editor::new(
3794            hjkl_buffer::Buffer::new(),
3795            crate::types::DefaultHost::new(),
3796            crate::types::Options::default(),
3797        );
3798        e.set_content("hello");
3799        e.handle_key(key(KeyCode::Char('i')));
3800        for c in "world".chars() {
3801            e.handle_key(key(KeyCode::Char(c)));
3802        }
3803        e.handle_key(key(KeyCode::Esc));
3804        let after = e.buffer().lines()[0].clone();
3805        e.handle_key(key(KeyCode::Char('u')));
3806        assert_eq!(e.buffer().lines()[0], "hello");
3807        e.handle_key(ctrl_key(KeyCode::Char('r')));
3808        assert_eq!(e.buffer().lines()[0], after);
3809    }
3810
3811    #[test]
3812    fn vim_u_undoes_dd() {
3813        let mut e = Editor::new(
3814            hjkl_buffer::Buffer::new(),
3815            crate::types::DefaultHost::new(),
3816            crate::types::Options::default(),
3817        );
3818        e.set_content("first\nsecond");
3819        e.handle_key(key(KeyCode::Char('d')));
3820        e.handle_key(key(KeyCode::Char('d')));
3821        assert_eq!(e.buffer().lines().len(), 1);
3822        e.handle_key(key(KeyCode::Char('u')));
3823        assert_eq!(e.buffer().lines().len(), 2);
3824        assert_eq!(e.buffer().lines()[0], "first");
3825    }
3826
3827    #[test]
3828    fn vim_ctrl_r_redoes() {
3829        let mut e = Editor::new(
3830            hjkl_buffer::Buffer::new(),
3831            crate::types::DefaultHost::new(),
3832            crate::types::Options::default(),
3833        );
3834        e.set_content("hello");
3835        e.handle_key(ctrl_key(KeyCode::Char('r')));
3836    }
3837
3838    #[test]
3839    fn vim_r_replaces_char() {
3840        let mut e = Editor::new(
3841            hjkl_buffer::Buffer::new(),
3842            crate::types::DefaultHost::new(),
3843            crate::types::Options::default(),
3844        );
3845        e.set_content("hello");
3846        e.handle_key(key(KeyCode::Char('r')));
3847        e.handle_key(key(KeyCode::Char('x')));
3848        assert_eq!(e.buffer().lines()[0].chars().next(), Some('x'));
3849    }
3850
3851    #[test]
3852    fn vim_tilde_toggles_case() {
3853        let mut e = Editor::new(
3854            hjkl_buffer::Buffer::new(),
3855            crate::types::DefaultHost::new(),
3856            crate::types::Options::default(),
3857        );
3858        e.set_content("hello");
3859        e.handle_key(key(KeyCode::Char('~')));
3860        assert_eq!(e.buffer().lines()[0].chars().next(), Some('H'));
3861    }
3862
3863    #[test]
3864    fn vim_visual_d_cuts() {
3865        let mut e = Editor::new(
3866            hjkl_buffer::Buffer::new(),
3867            crate::types::DefaultHost::new(),
3868            crate::types::Options::default(),
3869        );
3870        e.set_content("hello");
3871        e.handle_key(key(KeyCode::Char('v')));
3872        e.handle_key(key(KeyCode::Char('l')));
3873        e.handle_key(key(KeyCode::Char('l')));
3874        e.handle_key(key(KeyCode::Char('d')));
3875        assert_eq!(e.vim_mode(), VimMode::Normal);
3876        assert!(e.last_yank.is_some());
3877    }
3878
3879    #[test]
3880    fn vim_visual_c_enters_insert() {
3881        let mut e = Editor::new(
3882            hjkl_buffer::Buffer::new(),
3883            crate::types::DefaultHost::new(),
3884            crate::types::Options::default(),
3885        );
3886        e.set_content("hello");
3887        e.handle_key(key(KeyCode::Char('v')));
3888        e.handle_key(key(KeyCode::Char('l')));
3889        e.handle_key(key(KeyCode::Char('c')));
3890        assert_eq!(e.vim_mode(), VimMode::Insert);
3891    }
3892
3893    #[test]
3894    fn vim_normal_unknown_key_consumed() {
3895        let mut e = Editor::new(
3896            hjkl_buffer::Buffer::new(),
3897            crate::types::DefaultHost::new(),
3898            crate::types::Options::default(),
3899        );
3900        // Unknown keys are consumed (swallowed) rather than returning false.
3901        let consumed = e.handle_key(key(KeyCode::Char('z')));
3902        assert!(consumed);
3903    }
3904
3905    #[test]
3906    fn force_normal_clears_operator() {
3907        let mut e = Editor::new(
3908            hjkl_buffer::Buffer::new(),
3909            crate::types::DefaultHost::new(),
3910            crate::types::Options::default(),
3911        );
3912        e.handle_key(key(KeyCode::Char('d')));
3913        e.force_normal();
3914        assert_eq!(e.vim_mode(), VimMode::Normal);
3915    }
3916
3917    fn many_lines(n: usize) -> String {
3918        (0..n)
3919            .map(|i| format!("line{i}"))
3920            .collect::<Vec<_>>()
3921            .join("\n")
3922    }
3923
3924    fn prime_viewport<H: Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, height: u16) {
3925        e.set_viewport_height(height);
3926    }
3927
3928    #[test]
3929    fn zz_centers_cursor_in_viewport() {
3930        let mut e = Editor::new(
3931            hjkl_buffer::Buffer::new(),
3932            crate::types::DefaultHost::new(),
3933            crate::types::Options::default(),
3934        );
3935        e.set_content(&many_lines(100));
3936        prime_viewport(&mut e, 20);
3937        e.jump_cursor(50, 0);
3938        e.handle_key(key(KeyCode::Char('z')));
3939        e.handle_key(key(KeyCode::Char('z')));
3940        assert_eq!(e.host().viewport().top_row, 40);
3941        assert_eq!(e.cursor().0, 50);
3942    }
3943
3944    #[test]
3945    fn zt_puts_cursor_at_viewport_top_with_scrolloff() {
3946        let mut e = Editor::new(
3947            hjkl_buffer::Buffer::new(),
3948            crate::types::DefaultHost::new(),
3949            crate::types::Options::default(),
3950        );
3951        e.set_content(&many_lines(100));
3952        prime_viewport(&mut e, 20);
3953        e.jump_cursor(50, 0);
3954        e.handle_key(key(KeyCode::Char('z')));
3955        e.handle_key(key(KeyCode::Char('t')));
3956        // Cursor lands at top of viable area = top + SCROLLOFF (5).
3957        // Viewport top therefore sits at cursor - 5.
3958        assert_eq!(e.host().viewport().top_row, 45);
3959        assert_eq!(e.cursor().0, 50);
3960    }
3961
3962    #[test]
3963    fn ctrl_a_increments_number_at_cursor() {
3964        let mut e = Editor::new(
3965            hjkl_buffer::Buffer::new(),
3966            crate::types::DefaultHost::new(),
3967            crate::types::Options::default(),
3968        );
3969        e.set_content("x = 41");
3970        e.handle_key(ctrl_key(KeyCode::Char('a')));
3971        assert_eq!(e.buffer().lines()[0], "x = 42");
3972        assert_eq!(e.cursor(), (0, 5));
3973    }
3974
3975    #[test]
3976    fn ctrl_a_finds_number_to_right_of_cursor() {
3977        let mut e = Editor::new(
3978            hjkl_buffer::Buffer::new(),
3979            crate::types::DefaultHost::new(),
3980            crate::types::Options::default(),
3981        );
3982        e.set_content("foo 99 bar");
3983        e.handle_key(ctrl_key(KeyCode::Char('a')));
3984        assert_eq!(e.buffer().lines()[0], "foo 100 bar");
3985        assert_eq!(e.cursor(), (0, 6));
3986    }
3987
3988    #[test]
3989    fn ctrl_a_with_count_adds_count() {
3990        let mut e = Editor::new(
3991            hjkl_buffer::Buffer::new(),
3992            crate::types::DefaultHost::new(),
3993            crate::types::Options::default(),
3994        );
3995        e.set_content("x = 10");
3996        for d in "5".chars() {
3997            e.handle_key(key(KeyCode::Char(d)));
3998        }
3999        e.handle_key(ctrl_key(KeyCode::Char('a')));
4000        assert_eq!(e.buffer().lines()[0], "x = 15");
4001    }
4002
4003    #[test]
4004    fn ctrl_x_decrements_number() {
4005        let mut e = Editor::new(
4006            hjkl_buffer::Buffer::new(),
4007            crate::types::DefaultHost::new(),
4008            crate::types::Options::default(),
4009        );
4010        e.set_content("n=5");
4011        e.handle_key(ctrl_key(KeyCode::Char('x')));
4012        assert_eq!(e.buffer().lines()[0], "n=4");
4013    }
4014
4015    #[test]
4016    fn ctrl_x_crosses_zero_into_negative() {
4017        let mut e = Editor::new(
4018            hjkl_buffer::Buffer::new(),
4019            crate::types::DefaultHost::new(),
4020            crate::types::Options::default(),
4021        );
4022        e.set_content("v=0");
4023        e.handle_key(ctrl_key(KeyCode::Char('x')));
4024        assert_eq!(e.buffer().lines()[0], "v=-1");
4025    }
4026
4027    #[test]
4028    fn ctrl_a_on_negative_number_increments_toward_zero() {
4029        let mut e = Editor::new(
4030            hjkl_buffer::Buffer::new(),
4031            crate::types::DefaultHost::new(),
4032            crate::types::Options::default(),
4033        );
4034        e.set_content("a = -5");
4035        e.handle_key(ctrl_key(KeyCode::Char('a')));
4036        assert_eq!(e.buffer().lines()[0], "a = -4");
4037    }
4038
4039    #[test]
4040    fn ctrl_a_noop_when_no_digit_on_line() {
4041        let mut e = Editor::new(
4042            hjkl_buffer::Buffer::new(),
4043            crate::types::DefaultHost::new(),
4044            crate::types::Options::default(),
4045        );
4046        e.set_content("no digits here");
4047        e.handle_key(ctrl_key(KeyCode::Char('a')));
4048        assert_eq!(e.buffer().lines()[0], "no digits here");
4049    }
4050
4051    #[test]
4052    fn zb_puts_cursor_at_viewport_bottom_with_scrolloff() {
4053        let mut e = Editor::new(
4054            hjkl_buffer::Buffer::new(),
4055            crate::types::DefaultHost::new(),
4056            crate::types::Options::default(),
4057        );
4058        e.set_content(&many_lines(100));
4059        prime_viewport(&mut e, 20);
4060        e.jump_cursor(50, 0);
4061        e.handle_key(key(KeyCode::Char('z')));
4062        e.handle_key(key(KeyCode::Char('b')));
4063        // Cursor lands at bottom of viable area = top + height - 1 -
4064        // SCROLLOFF. For height 20, scrolloff 5: cursor at top + 14,
4065        // so top = cursor - 14 = 36.
4066        assert_eq!(e.host().viewport().top_row, 36);
4067        assert_eq!(e.cursor().0, 50);
4068    }
4069
4070    /// Contract that the TUI drain relies on: `set_content` flags the
4071    /// editor dirty (so the next `take_dirty` call reports the change),
4072    /// and a second `take_dirty` returns `false` after consumption. The
4073    /// TUI drains this flag after every programmatic content load so
4074    /// opening a tab doesn't get mistaken for a user edit and mark the
4075    /// tab dirty (which would then trigger the quit-prompt on `:q`).
4076    #[test]
4077    fn set_content_dirties_then_take_dirty_clears() {
4078        let mut e = Editor::new(
4079            hjkl_buffer::Buffer::new(),
4080            crate::types::DefaultHost::new(),
4081            crate::types::Options::default(),
4082        );
4083        e.set_content("hello");
4084        assert!(
4085            e.take_dirty(),
4086            "set_content should leave content_dirty=true"
4087        );
4088        assert!(!e.take_dirty(), "take_dirty should clear the flag");
4089    }
4090
4091    #[test]
4092    fn content_arc_returns_same_arc_until_mutation() {
4093        let mut e = Editor::new(
4094            hjkl_buffer::Buffer::new(),
4095            crate::types::DefaultHost::new(),
4096            crate::types::Options::default(),
4097        );
4098        e.set_content("hello");
4099        let a = e.content_arc();
4100        let b = e.content_arc();
4101        assert!(
4102            std::sync::Arc::ptr_eq(&a, &b),
4103            "repeated content_arc() should hit the cache"
4104        );
4105
4106        // Any mutation must invalidate the cache.
4107        e.handle_key(key(KeyCode::Char('i')));
4108        e.handle_key(key(KeyCode::Char('!')));
4109        let c = e.content_arc();
4110        assert!(
4111            !std::sync::Arc::ptr_eq(&a, &c),
4112            "mutation should invalidate content_arc() cache"
4113        );
4114        assert!(c.contains('!'));
4115    }
4116
4117    #[test]
4118    fn content_arc_cache_invalidated_by_set_content() {
4119        let mut e = Editor::new(
4120            hjkl_buffer::Buffer::new(),
4121            crate::types::DefaultHost::new(),
4122            crate::types::Options::default(),
4123        );
4124        e.set_content("one");
4125        let a = e.content_arc();
4126        e.set_content("two");
4127        let b = e.content_arc();
4128        assert!(!std::sync::Arc::ptr_eq(&a, &b));
4129        assert!(b.starts_with("two"));
4130    }
4131
4132    /// Click past the last char of a line should land the cursor on
4133    /// the line's last char (Normal mode), not one past it. The
4134    /// previous bug clamped to the line's BYTE length and used `>=`
4135    /// past-end, so clicking deep into the trailing space parked the
4136    /// cursor at `chars().count()` — past where Normal mode lives.
4137    #[test]
4138    fn mouse_click_past_eol_lands_on_last_char() {
4139        let mut e = Editor::new(
4140            hjkl_buffer::Buffer::new(),
4141            crate::types::DefaultHost::new(),
4142            crate::types::Options::default(),
4143        );
4144        e.set_content("hello");
4145        // Outer editor area: x=0, y=0, width=80. mouse_to_doc_pos
4146        // reserves row 0 for the tab bar and adds gutter padding,
4147        // so click row 1, way past the line end.
4148        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4149        e.mouse_click_in_rect(area, 78, 1);
4150        assert_eq!(e.cursor(), (0, 4));
4151    }
4152
4153    #[test]
4154    fn mouse_click_past_eol_handles_multibyte_line() {
4155        let mut e = Editor::new(
4156            hjkl_buffer::Buffer::new(),
4157            crate::types::DefaultHost::new(),
4158            crate::types::Options::default(),
4159        );
4160        // 5 chars, 6 bytes — old code's `String::len()` clamp was
4161        // wrong here.
4162        e.set_content("héllo");
4163        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4164        e.mouse_click_in_rect(area, 78, 1);
4165        assert_eq!(e.cursor(), (0, 4));
4166    }
4167
4168    #[test]
4169    fn mouse_click_inside_line_lands_on_clicked_char() {
4170        let mut e = Editor::new(
4171            hjkl_buffer::Buffer::new(),
4172            crate::types::DefaultHost::new(),
4173            crate::types::Options::default(),
4174        );
4175        e.set_content("hello world");
4176        // Gutter width = max(numberwidth=4, digits+1=2) = 4 cells, plus
4177        // 1 cell of pane padding (area_x.saturating_add(1)) = 5 total offset.
4178        // Click col 5 → char 0; click col 7 → char 2.
4179        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4180        e.mouse_click_in_rect(area, 5, 1);
4181        assert_eq!(e.cursor(), (0, 0));
4182        e.mouse_click_in_rect(area, 7, 1);
4183        assert_eq!(e.cursor(), (0, 2));
4184    }
4185
4186    /// Vim parity: a mouse-position click during insert mode counts
4187    /// as a motion and breaks the active undo group (when
4188    /// `undo_break_on_motion` is on, the default). After clicking and
4189    /// typing more chars, `u` should reverse only the post-click run.
4190    #[test]
4191    fn mouse_click_breaks_insert_undo_group_when_undobreak_on() {
4192        let mut e = Editor::new(
4193            hjkl_buffer::Buffer::new(),
4194            crate::types::DefaultHost::new(),
4195            crate::types::Options::default(),
4196        );
4197        e.set_content("hello world");
4198        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4199        // Default settings.undo_break_on_motion = true.
4200        assert!(e.settings().undo_break_on_motion);
4201        // Enter insert mode and type "AAA" before the line content.
4202        e.handle_key(key(KeyCode::Char('i')));
4203        e.handle_key(key(KeyCode::Char('A')));
4204        e.handle_key(key(KeyCode::Char('A')));
4205        e.handle_key(key(KeyCode::Char('A')));
4206        // Mouse click somewhere else on the line (still insert mode).
4207        e.mouse_click_in_rect(area, 10, 1);
4208        // Type more chars at the new cursor position.
4209        e.handle_key(key(KeyCode::Char('B')));
4210        e.handle_key(key(KeyCode::Char('B')));
4211        e.handle_key(key(KeyCode::Char('B')));
4212        // Leave insert and undo once.
4213        e.handle_key(key(KeyCode::Esc));
4214        e.handle_key(key(KeyCode::Char('u')));
4215        let line = e.buffer().line(0).unwrap_or("").to_string();
4216        assert!(
4217            line.contains("AAA"),
4218            "AAA must survive undo (separate group): {line:?}"
4219        );
4220        assert!(
4221            !line.contains("BBB"),
4222            "BBB must be undone (post-click group): {line:?}"
4223        );
4224    }
4225
4226    /// With `:set noundobreak`, the entire insert run — including
4227    /// chars typed before AND after a mouse click — should collapse
4228    /// into one undo group, so `u` clears everything.
4229    #[test]
4230    fn mouse_click_keeps_one_undo_group_when_undobreak_off() {
4231        let mut e = Editor::new(
4232            hjkl_buffer::Buffer::new(),
4233            crate::types::DefaultHost::new(),
4234            crate::types::Options::default(),
4235        );
4236        e.set_content("hello world");
4237        e.settings_mut().undo_break_on_motion = false;
4238        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4239        e.handle_key(key(KeyCode::Char('i')));
4240        e.handle_key(key(KeyCode::Char('A')));
4241        e.handle_key(key(KeyCode::Char('A')));
4242        e.mouse_click_in_rect(area, 10, 1);
4243        e.handle_key(key(KeyCode::Char('B')));
4244        e.handle_key(key(KeyCode::Char('B')));
4245        e.handle_key(key(KeyCode::Esc));
4246        e.handle_key(key(KeyCode::Char('u')));
4247        let line = e.buffer().line(0).unwrap_or("").to_string();
4248        assert!(
4249            !line.contains("AA") && !line.contains("BB"),
4250            "with undobreak off, single `u` must reverse whole insert: {line:?}"
4251        );
4252        assert_eq!(line, "hello world");
4253    }
4254
4255    // ── Patch B (0.0.29): Host trait wired into Editor ──
4256
4257    #[test]
4258    fn host_clipboard_round_trip_via_default_host() {
4259        // DefaultHost stores write_clipboard in-memory; read_clipboard
4260        // returns the most recent payload.
4261        let mut e = Editor::new(
4262            hjkl_buffer::Buffer::new(),
4263            crate::types::DefaultHost::new(),
4264            crate::types::Options::default(),
4265        );
4266        e.host_mut().write_clipboard("payload".to_string());
4267        assert_eq!(e.host_mut().read_clipboard().as_deref(), Some("payload"));
4268    }
4269
4270    #[test]
4271    fn host_records_clipboard_on_yank() {
4272        // `yy` on a single-line buffer must drive `Host::write_clipboard`
4273        // (the new Patch B side-channel) in addition to the legacy
4274        // `last_yank` mirror.
4275        let mut e = Editor::new(
4276            hjkl_buffer::Buffer::new(),
4277            crate::types::DefaultHost::new(),
4278            crate::types::Options::default(),
4279        );
4280        e.set_content("hello\n");
4281        e.handle_key(key(KeyCode::Char('y')));
4282        e.handle_key(key(KeyCode::Char('y')));
4283        // Clipboard cache holds the linewise yank.
4284        let clip = e.host_mut().read_clipboard();
4285        assert!(
4286            clip.as_deref().unwrap_or("").starts_with("hello"),
4287            "host clipboard should carry the yank: {clip:?}"
4288        );
4289        // Legacy mirror still populated for 0.0.28-era hosts.
4290        assert!(e.last_yank.as_deref().unwrap_or("").starts_with("hello"));
4291    }
4292
4293    #[test]
4294    fn host_cursor_shape_via_shared_recorder() {
4295        // Recording host backed by a leaked `Mutex` so the test can
4296        // inspect the emit sequence after the editor has consumed the
4297        // host. (Host: Send rules out Rc/RefCell.)
4298        let shapes_ptr: &'static std::sync::Mutex<Vec<crate::types::CursorShape>> =
4299            Box::leak(Box::new(std::sync::Mutex::new(Vec::new())));
4300        struct LeakHost {
4301            shapes: &'static std::sync::Mutex<Vec<crate::types::CursorShape>>,
4302            viewport: crate::types::Viewport,
4303        }
4304        impl crate::types::Host for LeakHost {
4305            type Intent = ();
4306            fn write_clipboard(&mut self, _: String) {}
4307            fn read_clipboard(&mut self) -> Option<String> {
4308                None
4309            }
4310            fn now(&self) -> core::time::Duration {
4311                core::time::Duration::ZERO
4312            }
4313            fn prompt_search(&mut self) -> Option<String> {
4314                None
4315            }
4316            fn emit_cursor_shape(&mut self, s: crate::types::CursorShape) {
4317                self.shapes.lock().unwrap().push(s);
4318            }
4319            fn viewport(&self) -> &crate::types::Viewport {
4320                &self.viewport
4321            }
4322            fn viewport_mut(&mut self) -> &mut crate::types::Viewport {
4323                &mut self.viewport
4324            }
4325            fn emit_intent(&mut self, _: Self::Intent) {}
4326        }
4327        let mut e = Editor::new(
4328            hjkl_buffer::Buffer::new(),
4329            LeakHost {
4330                shapes: shapes_ptr,
4331                viewport: crate::types::Viewport::default(),
4332            },
4333            crate::types::Options::default(),
4334        );
4335        e.set_content("abc");
4336        // Normal → Insert: Bar emit.
4337        e.handle_key(key(KeyCode::Char('i')));
4338        // Insert → Normal: Block emit.
4339        e.handle_key(key(KeyCode::Esc));
4340        let shapes = shapes_ptr.lock().unwrap().clone();
4341        assert_eq!(
4342            shapes,
4343            vec![
4344                crate::types::CursorShape::Bar,
4345                crate::types::CursorShape::Block,
4346            ],
4347            "host should observe Insert(Bar) → Normal(Block) transitions"
4348        );
4349    }
4350
4351    #[test]
4352    fn host_now_drives_chord_timeout_deterministically() {
4353        // Custom host whose `now()` is host-controlled; we drive it
4354        // forward by `timeout_len + 1ms` between the first `g` and
4355        // the second so the chord-timeout fires regardless of
4356        // wall-clock progress.
4357        let now_ptr: &'static std::sync::Mutex<core::time::Duration> =
4358            Box::leak(Box::new(std::sync::Mutex::new(core::time::Duration::ZERO)));
4359        struct ClockHost {
4360            now: &'static std::sync::Mutex<core::time::Duration>,
4361            viewport: crate::types::Viewport,
4362        }
4363        impl crate::types::Host for ClockHost {
4364            type Intent = ();
4365            fn write_clipboard(&mut self, _: String) {}
4366            fn read_clipboard(&mut self) -> Option<String> {
4367                None
4368            }
4369            fn now(&self) -> core::time::Duration {
4370                *self.now.lock().unwrap()
4371            }
4372            fn prompt_search(&mut self) -> Option<String> {
4373                None
4374            }
4375            fn emit_cursor_shape(&mut self, _: crate::types::CursorShape) {}
4376            fn viewport(&self) -> &crate::types::Viewport {
4377                &self.viewport
4378            }
4379            fn viewport_mut(&mut self) -> &mut crate::types::Viewport {
4380                &mut self.viewport
4381            }
4382            fn emit_intent(&mut self, _: Self::Intent) {}
4383        }
4384        let mut e = Editor::new(
4385            hjkl_buffer::Buffer::new(),
4386            ClockHost {
4387                now: now_ptr,
4388                viewport: crate::types::Viewport::default(),
4389            },
4390            crate::types::Options::default(),
4391        );
4392        e.set_content("a\nb\nc\n");
4393        e.jump_cursor(2, 0);
4394        // First `g` — host time = 0ms, lands in g-pending.
4395        e.handle_key(key(KeyCode::Char('g')));
4396        // Advance host time well past timeout_len (default 1000ms).
4397        *now_ptr.lock().unwrap() = core::time::Duration::from_secs(60);
4398        // Second `g` — chord-timeout fires; bare `g` re-dispatches and
4399        // does nothing on its own. Cursor must NOT have jumped to row 0.
4400        e.handle_key(key(KeyCode::Char('g')));
4401        assert_eq!(
4402            e.cursor().0,
4403            2,
4404            "Host::now() must drive `:set timeoutlen` deterministically"
4405        );
4406    }
4407
4408    // ── ContentEdit emission ─────────────────────────────────────────
4409
4410    fn fresh_editor(initial: &str) -> Editor {
4411        let buffer = hjkl_buffer::Buffer::from_str(initial);
4412        Editor::new(
4413            buffer,
4414            crate::types::DefaultHost::new(),
4415            crate::types::Options::default(),
4416        )
4417    }
4418
4419    #[test]
4420    fn content_edit_insert_char_at_origin() {
4421        let mut e = fresh_editor("");
4422        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
4423            at: hjkl_buffer::Position::new(0, 0),
4424            ch: 'a',
4425        });
4426        let edits = e.take_content_edits();
4427        assert_eq!(edits.len(), 1);
4428        let ce = &edits[0];
4429        assert_eq!(ce.start_byte, 0);
4430        assert_eq!(ce.old_end_byte, 0);
4431        assert_eq!(ce.new_end_byte, 1);
4432        assert_eq!(ce.start_position, (0, 0));
4433        assert_eq!(ce.old_end_position, (0, 0));
4434        assert_eq!(ce.new_end_position, (0, 1));
4435    }
4436
4437    #[test]
4438    fn content_edit_insert_str_multiline() {
4439        // Buffer "x\ny" — insert "ab\ncd" at end of row 0.
4440        let mut e = fresh_editor("x\ny");
4441        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertStr {
4442            at: hjkl_buffer::Position::new(0, 1),
4443            text: "ab\ncd".into(),
4444        });
4445        let edits = e.take_content_edits();
4446        assert_eq!(edits.len(), 1);
4447        let ce = &edits[0];
4448        assert_eq!(ce.start_byte, 1);
4449        assert_eq!(ce.old_end_byte, 1);
4450        assert_eq!(ce.new_end_byte, 1 + 5);
4451        assert_eq!(ce.start_position, (0, 1));
4452        // Insertion contains one '\n', so row+1, col = bytes after last '\n' = 2.
4453        assert_eq!(ce.new_end_position, (1, 2));
4454    }
4455
4456    #[test]
4457    fn content_edit_delete_range_charwise() {
4458        // "abcdef" — delete chars 1..4 ("bcd").
4459        let mut e = fresh_editor("abcdef");
4460        let _ = e.mutate_edit(hjkl_buffer::Edit::DeleteRange {
4461            start: hjkl_buffer::Position::new(0, 1),
4462            end: hjkl_buffer::Position::new(0, 4),
4463            kind: hjkl_buffer::MotionKind::Char,
4464        });
4465        let edits = e.take_content_edits();
4466        assert_eq!(edits.len(), 1);
4467        let ce = &edits[0];
4468        assert_eq!(ce.start_byte, 1);
4469        assert_eq!(ce.old_end_byte, 4);
4470        assert_eq!(ce.new_end_byte, 1);
4471        assert!(ce.old_end_byte > ce.new_end_byte);
4472    }
4473
4474    #[test]
4475    fn content_edit_set_content_resets() {
4476        let mut e = fresh_editor("foo");
4477        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
4478            at: hjkl_buffer::Position::new(0, 0),
4479            ch: 'X',
4480        });
4481        // set_content should clear queued edits and raise the reset
4482        // flag on the next take_content_reset.
4483        e.set_content("brand new");
4484        assert!(e.take_content_reset());
4485        // Subsequent call clears the flag.
4486        assert!(!e.take_content_reset());
4487        // Edits cleared on reset.
4488        assert!(e.take_content_edits().is_empty());
4489    }
4490
4491    #[test]
4492    fn content_edit_multiple_replaces_in_order() {
4493        // Three Replace edits applied left-to-right (mimics the
4494        // substitute path's per-match Replace fan-out). Verify each
4495        // mutation queues exactly one ContentEdit and they're drained
4496        // in source-order with structurally valid byte spans.
4497        let mut e = fresh_editor("xax xbx xcx");
4498        let _ = e.take_content_edits();
4499        let _ = e.take_content_reset();
4500        // Replace each "x" with "yy", left to right. After each replace,
4501        // the next match's char-col shifts by +1 (since "yy" is 1 char
4502        // longer than "x" but they're both ASCII so byte = char here).
4503        let positions = [(0usize, 0usize), (0, 4), (0, 8)];
4504        for (row, col) in positions {
4505            let _ = e.mutate_edit(hjkl_buffer::Edit::Replace {
4506                start: hjkl_buffer::Position::new(row, col),
4507                end: hjkl_buffer::Position::new(row, col + 1),
4508                with: "yy".into(),
4509            });
4510        }
4511        let edits = e.take_content_edits();
4512        assert_eq!(edits.len(), 3);
4513        for ce in &edits {
4514            assert!(ce.start_byte <= ce.old_end_byte);
4515            assert!(ce.start_byte <= ce.new_end_byte);
4516        }
4517        // Document order.
4518        for w in edits.windows(2) {
4519            assert!(w[0].start_byte <= w[1].start_byte);
4520        }
4521    }
4522
4523    #[test]
4524    fn replace_char_at_replaces_single_char_under_cursor() {
4525        // Matches vim's `rx` semantics: replace char under cursor.
4526        let mut e = fresh_editor("abc");
4527        e.jump_cursor(0, 1); // cursor on 'b'
4528        e.replace_char_at('X', 1);
4529        let got = e.content();
4530        let got = got.trim_end_matches('\n');
4531        assert_eq!(
4532            got, "aXc",
4533            "replace_char_at(X, 1) must replace 'b' with 'X'"
4534        );
4535        // Cursor stays on the replaced char.
4536        assert_eq!(e.cursor(), (0, 1));
4537    }
4538
4539    #[test]
4540    fn replace_char_at_count_replaces_multiple_chars() {
4541        // `3rx` in vim replaces 3 chars starting at cursor.
4542        let mut e = fresh_editor("abcde");
4543        e.jump_cursor(0, 0);
4544        e.replace_char_at('Z', 3);
4545        let got = e.content();
4546        let got = got.trim_end_matches('\n');
4547        assert_eq!(
4548            got, "ZZZde",
4549            "replace_char_at(Z, 3) must replace first 3 chars"
4550        );
4551    }
4552
4553    #[test]
4554    fn find_char_method_moves_to_target() {
4555        // buffer "abcabc", cursor (0,0), f<c> → cursor (0,2).
4556        let mut e = fresh_editor("abcabc");
4557        e.jump_cursor(0, 0);
4558        e.find_char('c', true, false, 1);
4559        assert_eq!(
4560            e.cursor(),
4561            (0, 2),
4562            "find_char('c', forward=true, till=false, count=1) must land on 'c' at col 2"
4563        );
4564    }
4565
4566    // ── after_g unit tests (Phase 2b-ii) ────────────────────────────────────
4567
4568    #[test]
4569    fn after_g_gg_jumps_to_top() {
4570        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
4571        let mut e = fresh_editor(&content);
4572        e.jump_cursor(15, 0);
4573        e.after_g('g', 1);
4574        assert_eq!(e.cursor().0, 0, "gg must move cursor to row 0");
4575    }
4576
4577    #[test]
4578    fn after_g_gg_with_count_jumps_line() {
4579        // 5gg → row 4 (0-indexed).
4580        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
4581        let mut e = fresh_editor(&content);
4582        e.jump_cursor(0, 0);
4583        e.after_g('g', 5);
4584        assert_eq!(e.cursor().0, 4, "5gg must land on row 4");
4585    }
4586
4587    #[test]
4588    fn after_g_gv_restores_last_visual() {
4589        // Enter visual, move right, exit, then gv re-enters.
4590        let mut e = fresh_editor("hello world\n");
4591        // Enter char-visual at col 0, move to col 3, then exit.
4592        e.handle_key(key(KeyCode::Char('v')));
4593        e.handle_key(key(KeyCode::Char('l')));
4594        e.handle_key(key(KeyCode::Char('l')));
4595        e.handle_key(key(KeyCode::Char('l')));
4596        e.handle_key(key(KeyCode::Esc));
4597        assert_eq!(e.vim_mode(), VimMode::Normal, "should be Normal after Esc");
4598        // gv via after_g.
4599        e.after_g('v', 1);
4600        assert_eq!(
4601            e.vim_mode(),
4602            VimMode::Visual,
4603            "gv must re-enter Visual mode"
4604        );
4605    }
4606
4607    #[test]
4608    fn after_g_gj_moves_down() {
4609        let mut e = fresh_editor("line0\nline1\nline2\n");
4610        e.jump_cursor(0, 0);
4611        e.after_g('j', 1);
4612        assert_eq!(e.cursor().0, 1, "gj must move down one display row");
4613    }
4614
4615    #[test]
4616    fn after_g_gu_sets_operator_pending() {
4617        // gU enters operator-pending with Uppercase op; next key applies it.
4618        let mut e = fresh_editor("hello\n");
4619        e.after_g('U', 1);
4620        // The engine should now be chord-pending (Pending::Op set).
4621        assert!(
4622            e.is_chord_pending(),
4623            "gU must set engine chord-pending (Pending::Op)"
4624        );
4625    }
4626
4627    #[test]
4628    fn after_g_g_star_searches_forward_non_whole_word() {
4629        // g* on word "foo" in "foobar" should find the match.
4630        let mut e = fresh_editor("foo foobar\n");
4631        e.jump_cursor(0, 0); // cursor on 'f' of "foo"
4632        e.after_g('*', 1);
4633        // After g* the cursor should have moved (ScreenDown motion is
4634        // not applicable here; WordAtCursor forward moves to next match).
4635        // At minimum: no panic and mode stays Normal.
4636        assert_eq!(e.vim_mode(), VimMode::Normal, "g* must stay in Normal mode");
4637    }
4638}