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    pub fn enter_op_find(
2942        &mut self,
2943        op: crate::vim::Operator,
2944        count1: usize,
2945        forward: bool,
2946        till: bool,
2947    ) {
2948        vim::enter_op_find(self, op, count1, forward, till);
2949    }
2950
2951    #[cfg(feature = "crossterm")]
2952    pub fn handle_key(&mut self, key: KeyEvent) -> bool {
2953        let input = crossterm_to_input(key);
2954        if input.key == Key::Null {
2955            return false;
2956        }
2957        let consumed = vim::step(self, input);
2958        self.emit_cursor_shape_if_changed();
2959        consumed
2960    }
2961}
2962
2963/// Visual column of the character at `char_col` in `line`, treating `\t`
2964/// as expansion to the next `tab_width` stop and every other char as
2965/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
2966/// the cursor math elsewhere also assumes single-cell chars.
2967fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
2968    let mut visual = 0usize;
2969    for (i, ch) in line.chars().enumerate() {
2970        if i >= char_col {
2971            break;
2972        }
2973        if ch == '\t' {
2974            visual += tab_width - (visual % tab_width);
2975        } else {
2976            visual += 1;
2977        }
2978    }
2979    visual
2980}
2981
2982#[cfg(feature = "crossterm")]
2983impl From<KeyEvent> for Input {
2984    fn from(key: KeyEvent) -> Self {
2985        let k = match key.code {
2986            KeyCode::Char(c) => Key::Char(c),
2987            KeyCode::Backspace => Key::Backspace,
2988            KeyCode::Delete => Key::Delete,
2989            KeyCode::Enter => Key::Enter,
2990            KeyCode::Left => Key::Left,
2991            KeyCode::Right => Key::Right,
2992            KeyCode::Up => Key::Up,
2993            KeyCode::Down => Key::Down,
2994            KeyCode::Home => Key::Home,
2995            KeyCode::End => Key::End,
2996            KeyCode::Tab => Key::Tab,
2997            KeyCode::Esc => Key::Esc,
2998            _ => Key::Null,
2999        };
3000        Input {
3001            key: k,
3002            ctrl: key.modifiers.contains(KeyModifiers::CONTROL),
3003            alt: key.modifiers.contains(KeyModifiers::ALT),
3004            shift: key.modifiers.contains(KeyModifiers::SHIFT),
3005        }
3006    }
3007}
3008
3009/// Crossterm `KeyEvent` → engine `Input`. Thin wrapper that delegates
3010/// to the [`From`] impl above; kept as a free fn for the in-tree
3011/// callers in the legacy ratatui-coupled paths.
3012#[cfg(feature = "crossterm")]
3013pub(super) fn crossterm_to_input(key: KeyEvent) -> Input {
3014    Input::from(key)
3015}
3016
3017#[cfg(all(test, feature = "crossterm", feature = "ratatui"))]
3018mod tests {
3019    use super::*;
3020    use crate::types::Host;
3021    use crossterm::event::KeyEvent;
3022
3023    fn key(code: KeyCode) -> KeyEvent {
3024        KeyEvent::new(code, KeyModifiers::NONE)
3025    }
3026    fn shift_key(code: KeyCode) -> KeyEvent {
3027        KeyEvent::new(code, KeyModifiers::SHIFT)
3028    }
3029    fn ctrl_key(code: KeyCode) -> KeyEvent {
3030        KeyEvent::new(code, KeyModifiers::CONTROL)
3031    }
3032
3033    #[test]
3034    fn vim_normal_to_insert() {
3035        let mut e = Editor::new(
3036            hjkl_buffer::Buffer::new(),
3037            crate::types::DefaultHost::new(),
3038            crate::types::Options::default(),
3039        );
3040        e.handle_key(key(KeyCode::Char('i')));
3041        assert_eq!(e.vim_mode(), VimMode::Insert);
3042    }
3043
3044    #[test]
3045    fn with_options_constructs_from_spec_options() {
3046        // 0.0.33 (Patch C-γ): SPEC-shaped constructor preview.
3047        // Build with custom Options + DefaultHost; confirm the
3048        // settings translation honours the SPEC field names.
3049        let opts = crate::types::Options {
3050            shiftwidth: 4,
3051            tabstop: 4,
3052            expandtab: true,
3053            iskeyword: "@,a-z".to_string(),
3054            wrap: crate::types::WrapMode::Word,
3055            ..crate::types::Options::default()
3056        };
3057        let mut e = Editor::new(
3058            hjkl_buffer::Buffer::new(),
3059            crate::types::DefaultHost::new(),
3060            opts,
3061        );
3062        assert_eq!(e.settings().shiftwidth, 4);
3063        assert_eq!(e.settings().tabstop, 4);
3064        assert!(e.settings().expandtab);
3065        assert_eq!(e.settings().iskeyword, "@,a-z");
3066        assert_eq!(e.settings().wrap, hjkl_buffer::Wrap::Word);
3067        // Confirm input plumbing still works.
3068        e.handle_key(key(KeyCode::Char('i')));
3069        assert_eq!(e.vim_mode(), VimMode::Insert);
3070    }
3071
3072    #[test]
3073    fn feed_input_char_routes_through_handle_key() {
3074        use crate::{Modifiers, PlannedInput};
3075        let mut e = Editor::new(
3076            hjkl_buffer::Buffer::new(),
3077            crate::types::DefaultHost::new(),
3078            crate::types::Options::default(),
3079        );
3080        e.set_content("abc");
3081        // `i` enters insert mode via SPEC input.
3082        e.feed_input(PlannedInput::Char('i', Modifiers::default()));
3083        assert_eq!(e.vim_mode(), VimMode::Insert);
3084        // Type 'X' via SPEC input.
3085        e.feed_input(PlannedInput::Char('X', Modifiers::default()));
3086        assert!(e.content().contains('X'));
3087    }
3088
3089    #[test]
3090    fn feed_input_special_key_routes() {
3091        use crate::{Modifiers, PlannedInput, SpecialKey};
3092        let mut e = Editor::new(
3093            hjkl_buffer::Buffer::new(),
3094            crate::types::DefaultHost::new(),
3095            crate::types::Options::default(),
3096        );
3097        e.set_content("abc");
3098        e.feed_input(PlannedInput::Char('i', Modifiers::default()));
3099        assert_eq!(e.vim_mode(), VimMode::Insert);
3100        e.feed_input(PlannedInput::Key(SpecialKey::Esc, Modifiers::default()));
3101        assert_eq!(e.vim_mode(), VimMode::Normal);
3102    }
3103
3104    #[test]
3105    fn feed_input_mouse_paste_focus_resize_no_op() {
3106        use crate::{MouseEvent, MouseKind, PlannedInput, Pos};
3107        let mut e = Editor::new(
3108            hjkl_buffer::Buffer::new(),
3109            crate::types::DefaultHost::new(),
3110            crate::types::Options::default(),
3111        );
3112        e.set_content("abc");
3113        let mode_before = e.vim_mode();
3114        let consumed = e.feed_input(PlannedInput::Mouse(MouseEvent {
3115            kind: MouseKind::Press,
3116            pos: Pos::new(0, 0),
3117            mods: Default::default(),
3118        }));
3119        assert!(!consumed);
3120        assert_eq!(e.vim_mode(), mode_before);
3121        assert!(!e.feed_input(PlannedInput::Paste("xx".into())));
3122        assert!(!e.feed_input(PlannedInput::FocusGained));
3123        assert!(!e.feed_input(PlannedInput::FocusLost));
3124        assert!(!e.feed_input(PlannedInput::Resize(80, 24)));
3125    }
3126
3127    #[test]
3128    fn intern_style_dedups_engine_native_styles() {
3129        use crate::types::{Attrs, Color, Style};
3130        let mut e = Editor::new(
3131            hjkl_buffer::Buffer::new(),
3132            crate::types::DefaultHost::new(),
3133            crate::types::Options::default(),
3134        );
3135        let s = Style {
3136            fg: Some(Color(255, 0, 0)),
3137            bg: None,
3138            attrs: Attrs::BOLD,
3139        };
3140        let id_a = e.intern_style(s);
3141        // Re-interning the same engine style returns the same id.
3142        let id_b = e.intern_style(s);
3143        assert_eq!(id_a, id_b);
3144        // Engine accessor returns the same style back.
3145        let back = e.engine_style_at(id_a).expect("interned");
3146        assert_eq!(back, s);
3147    }
3148
3149    #[test]
3150    fn engine_style_at_out_of_range_returns_none() {
3151        let e = Editor::new(
3152            hjkl_buffer::Buffer::new(),
3153            crate::types::DefaultHost::new(),
3154            crate::types::Options::default(),
3155        );
3156        assert!(e.engine_style_at(99).is_none());
3157    }
3158
3159    #[test]
3160    fn take_changes_emits_per_row_for_block_insert() {
3161        // Visual-block insert (`Ctrl-V` then `I` then text then Esc)
3162        // produces an InsertBlock buffer edit with one chunk per
3163        // selected row. take_changes should surface N EditOps,
3164        // not a single placeholder.
3165        let mut e = Editor::new(
3166            hjkl_buffer::Buffer::new(),
3167            crate::types::DefaultHost::new(),
3168            crate::types::Options::default(),
3169        );
3170        e.set_content("aaa\nbbb\nccc\nddd");
3171        // Place cursor at (0, 0), enter visual-block, extend down 2.
3172        e.handle_key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
3173        e.handle_key(key(KeyCode::Char('j')));
3174        e.handle_key(key(KeyCode::Char('j')));
3175        // `I` to enter insert mode at the block left edge.
3176        e.handle_key(shift_key(KeyCode::Char('I')));
3177        e.handle_key(key(KeyCode::Char('X')));
3178        e.handle_key(key(KeyCode::Esc));
3179
3180        let changes = e.take_changes();
3181        // Expect at least 3 entries — one per row in the 3-row block.
3182        // Vim's block-I inserts on Esc; the cleanup may add more
3183        // EditOps for cursor sync, hence >= rather than ==.
3184        assert!(
3185            changes.len() >= 3,
3186            "expected >=3 EditOps for 3-row block insert, got {}: {changes:?}",
3187            changes.len()
3188        );
3189    }
3190
3191    #[test]
3192    fn take_changes_drains_after_insert() {
3193        let mut e = Editor::new(
3194            hjkl_buffer::Buffer::new(),
3195            crate::types::DefaultHost::new(),
3196            crate::types::Options::default(),
3197        );
3198        e.set_content("abc");
3199        // Empty initially.
3200        assert!(e.take_changes().is_empty());
3201        // Type a char in insert mode.
3202        e.handle_key(key(KeyCode::Char('i')));
3203        e.handle_key(key(KeyCode::Char('X')));
3204        let changes = e.take_changes();
3205        assert!(
3206            !changes.is_empty(),
3207            "insert mode keystroke should produce a change"
3208        );
3209        // Drained — second call empty.
3210        assert!(e.take_changes().is_empty());
3211    }
3212
3213    #[test]
3214    fn options_bridge_roundtrip() {
3215        let mut e = Editor::new(
3216            hjkl_buffer::Buffer::new(),
3217            crate::types::DefaultHost::new(),
3218            crate::types::Options::default(),
3219        );
3220        let opts = e.current_options();
3221        // 0.2.0: defaults flipped to modern editor norms — 4-space soft tabs.
3222        assert_eq!(opts.shiftwidth, 4);
3223        assert_eq!(opts.tabstop, 4);
3224
3225        let new_opts = crate::types::Options {
3226            shiftwidth: 4,
3227            tabstop: 2,
3228            ignorecase: true,
3229            ..crate::types::Options::default()
3230        };
3231        e.apply_options(&new_opts);
3232
3233        let after = e.current_options();
3234        assert_eq!(after.shiftwidth, 4);
3235        assert_eq!(after.tabstop, 2);
3236        assert!(after.ignorecase);
3237    }
3238
3239    #[test]
3240    fn selection_highlight_none_in_normal() {
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("hello");
3247        assert!(e.selection_highlight().is_none());
3248    }
3249
3250    #[test]
3251    fn selection_highlight_some_in_visual() {
3252        use crate::types::HighlightKind;
3253        let mut e = Editor::new(
3254            hjkl_buffer::Buffer::new(),
3255            crate::types::DefaultHost::new(),
3256            crate::types::Options::default(),
3257        );
3258        e.set_content("hello world");
3259        e.handle_key(key(KeyCode::Char('v')));
3260        e.handle_key(key(KeyCode::Char('l')));
3261        e.handle_key(key(KeyCode::Char('l')));
3262        let h = e
3263            .selection_highlight()
3264            .expect("visual mode should produce a highlight");
3265        assert_eq!(h.kind, HighlightKind::Selection);
3266        assert_eq!(h.range.start.line, 0);
3267        assert_eq!(h.range.end.line, 0);
3268    }
3269
3270    #[test]
3271    fn highlights_emit_incsearch_during_active_prompt() {
3272        use crate::types::HighlightKind;
3273        let mut e = Editor::new(
3274            hjkl_buffer::Buffer::new(),
3275            crate::types::DefaultHost::new(),
3276            crate::types::Options::default(),
3277        );
3278        e.set_content("foo bar foo\nbaz\n");
3279        // Open the `/` prompt and type `f` `o` `o`.
3280        e.handle_key(key(KeyCode::Char('/')));
3281        e.handle_key(key(KeyCode::Char('f')));
3282        e.handle_key(key(KeyCode::Char('o')));
3283        e.handle_key(key(KeyCode::Char('o')));
3284        // Prompt should be active.
3285        assert!(e.search_prompt().is_some());
3286        let hs = e.highlights_for_line(0);
3287        assert_eq!(hs.len(), 2);
3288        for h in &hs {
3289            assert_eq!(h.kind, HighlightKind::IncSearch);
3290        }
3291    }
3292
3293    #[test]
3294    fn highlights_empty_for_blank_prompt() {
3295        let mut e = Editor::new(
3296            hjkl_buffer::Buffer::new(),
3297            crate::types::DefaultHost::new(),
3298            crate::types::Options::default(),
3299        );
3300        e.set_content("foo");
3301        e.handle_key(key(KeyCode::Char('/')));
3302        // Nothing typed yet — prompt active but text empty.
3303        assert!(e.search_prompt().is_some());
3304        assert!(e.highlights_for_line(0).is_empty());
3305    }
3306
3307    #[test]
3308    fn highlights_emit_search_matches() {
3309        use crate::types::HighlightKind;
3310        let mut e = Editor::new(
3311            hjkl_buffer::Buffer::new(),
3312            crate::types::DefaultHost::new(),
3313            crate::types::Options::default(),
3314        );
3315        e.set_content("foo bar foo\nbaz qux\n");
3316        // 0.0.35: arm via the engine search state. The buffer
3317        // accessor still works (deprecated) but new code goes
3318        // through Editor.
3319        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
3320        let hs = e.highlights_for_line(0);
3321        assert_eq!(hs.len(), 2);
3322        for h in &hs {
3323            assert_eq!(h.kind, HighlightKind::SearchMatch);
3324            assert_eq!(h.range.start.line, 0);
3325            assert_eq!(h.range.end.line, 0);
3326        }
3327    }
3328
3329    #[test]
3330    fn highlights_empty_without_pattern() {
3331        let mut e = Editor::new(
3332            hjkl_buffer::Buffer::new(),
3333            crate::types::DefaultHost::new(),
3334            crate::types::Options::default(),
3335        );
3336        e.set_content("foo bar");
3337        assert!(e.highlights_for_line(0).is_empty());
3338    }
3339
3340    #[test]
3341    fn highlights_empty_for_out_of_range_line() {
3342        let mut e = Editor::new(
3343            hjkl_buffer::Buffer::new(),
3344            crate::types::DefaultHost::new(),
3345            crate::types::Options::default(),
3346        );
3347        e.set_content("foo");
3348        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
3349        assert!(e.highlights_for_line(99).is_empty());
3350    }
3351
3352    #[test]
3353    fn render_frame_reflects_mode_and_cursor() {
3354        use crate::types::{CursorShape, SnapshotMode};
3355        let mut e = Editor::new(
3356            hjkl_buffer::Buffer::new(),
3357            crate::types::DefaultHost::new(),
3358            crate::types::Options::default(),
3359        );
3360        e.set_content("alpha\nbeta");
3361        let f = e.render_frame();
3362        assert_eq!(f.mode, SnapshotMode::Normal);
3363        assert_eq!(f.cursor_shape, CursorShape::Block);
3364        assert_eq!(f.line_count, 2);
3365
3366        e.handle_key(key(KeyCode::Char('i')));
3367        let f = e.render_frame();
3368        assert_eq!(f.mode, SnapshotMode::Insert);
3369        assert_eq!(f.cursor_shape, CursorShape::Bar);
3370    }
3371
3372    #[test]
3373    fn snapshot_roundtrips_through_restore() {
3374        use crate::types::SnapshotMode;
3375        let mut e = Editor::new(
3376            hjkl_buffer::Buffer::new(),
3377            crate::types::DefaultHost::new(),
3378            crate::types::Options::default(),
3379        );
3380        e.set_content("alpha\nbeta\ngamma");
3381        e.jump_cursor(2, 3);
3382        let snap = e.take_snapshot();
3383        assert_eq!(snap.mode, SnapshotMode::Normal);
3384        assert_eq!(snap.cursor, (2, 3));
3385        assert_eq!(snap.lines.len(), 3);
3386
3387        let mut other = Editor::new(
3388            hjkl_buffer::Buffer::new(),
3389            crate::types::DefaultHost::new(),
3390            crate::types::Options::default(),
3391        );
3392        other.restore_snapshot(snap).expect("restore");
3393        assert_eq!(other.cursor(), (2, 3));
3394        assert_eq!(other.buffer().lines().len(), 3);
3395    }
3396
3397    #[test]
3398    fn restore_snapshot_rejects_version_mismatch() {
3399        let mut e = Editor::new(
3400            hjkl_buffer::Buffer::new(),
3401            crate::types::DefaultHost::new(),
3402            crate::types::Options::default(),
3403        );
3404        let mut snap = e.take_snapshot();
3405        snap.version = 9999;
3406        match e.restore_snapshot(snap) {
3407            Err(crate::EngineError::SnapshotVersion(got, want)) => {
3408                assert_eq!(got, 9999);
3409                assert_eq!(want, crate::types::EditorSnapshot::VERSION);
3410            }
3411            other => panic!("expected SnapshotVersion err, got {other:?}"),
3412        }
3413    }
3414
3415    #[test]
3416    fn take_content_change_returns_some_on_first_dirty() {
3417        let mut e = Editor::new(
3418            hjkl_buffer::Buffer::new(),
3419            crate::types::DefaultHost::new(),
3420            crate::types::Options::default(),
3421        );
3422        e.set_content("hello");
3423        let first = e.take_content_change();
3424        assert!(first.is_some());
3425        let second = e.take_content_change();
3426        assert!(second.is_none());
3427    }
3428
3429    #[test]
3430    fn take_content_change_none_until_mutation() {
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("hello");
3437        // drain
3438        e.take_content_change();
3439        assert!(e.take_content_change().is_none());
3440        // mutate via insert mode
3441        e.handle_key(key(KeyCode::Char('i')));
3442        e.handle_key(key(KeyCode::Char('x')));
3443        let after = e.take_content_change();
3444        assert!(after.is_some());
3445        assert!(after.unwrap().contains('x'));
3446    }
3447
3448    #[test]
3449    fn vim_insert_to_normal() {
3450        let mut e = Editor::new(
3451            hjkl_buffer::Buffer::new(),
3452            crate::types::DefaultHost::new(),
3453            crate::types::Options::default(),
3454        );
3455        e.handle_key(key(KeyCode::Char('i')));
3456        e.handle_key(key(KeyCode::Esc));
3457        assert_eq!(e.vim_mode(), VimMode::Normal);
3458    }
3459
3460    #[test]
3461    fn vim_normal_to_visual() {
3462        let mut e = Editor::new(
3463            hjkl_buffer::Buffer::new(),
3464            crate::types::DefaultHost::new(),
3465            crate::types::Options::default(),
3466        );
3467        e.handle_key(key(KeyCode::Char('v')));
3468        assert_eq!(e.vim_mode(), VimMode::Visual);
3469    }
3470
3471    #[test]
3472    fn vim_visual_to_normal() {
3473        let mut e = Editor::new(
3474            hjkl_buffer::Buffer::new(),
3475            crate::types::DefaultHost::new(),
3476            crate::types::Options::default(),
3477        );
3478        e.handle_key(key(KeyCode::Char('v')));
3479        e.handle_key(key(KeyCode::Esc));
3480        assert_eq!(e.vim_mode(), VimMode::Normal);
3481    }
3482
3483    #[test]
3484    fn vim_shift_i_moves_to_first_non_whitespace() {
3485        let mut e = Editor::new(
3486            hjkl_buffer::Buffer::new(),
3487            crate::types::DefaultHost::new(),
3488            crate::types::Options::default(),
3489        );
3490        e.set_content("   hello");
3491        e.jump_cursor(0, 8);
3492        e.handle_key(shift_key(KeyCode::Char('I')));
3493        assert_eq!(e.vim_mode(), VimMode::Insert);
3494        assert_eq!(e.cursor(), (0, 3));
3495    }
3496
3497    #[test]
3498    fn vim_shift_a_moves_to_end_and_insert() {
3499        let mut e = Editor::new(
3500            hjkl_buffer::Buffer::new(),
3501            crate::types::DefaultHost::new(),
3502            crate::types::Options::default(),
3503        );
3504        e.set_content("hello");
3505        e.handle_key(shift_key(KeyCode::Char('A')));
3506        assert_eq!(e.vim_mode(), VimMode::Insert);
3507        assert_eq!(e.cursor().1, 5);
3508    }
3509
3510    #[test]
3511    fn count_10j_moves_down_10() {
3512        let mut e = Editor::new(
3513            hjkl_buffer::Buffer::new(),
3514            crate::types::DefaultHost::new(),
3515            crate::types::Options::default(),
3516        );
3517        e.set_content(
3518            (0..20)
3519                .map(|i| format!("line{i}"))
3520                .collect::<Vec<_>>()
3521                .join("\n")
3522                .as_str(),
3523        );
3524        for d in "10".chars() {
3525            e.handle_key(key(KeyCode::Char(d)));
3526        }
3527        e.handle_key(key(KeyCode::Char('j')));
3528        assert_eq!(e.cursor().0, 10);
3529    }
3530
3531    #[test]
3532    fn count_o_repeats_insert_on_esc() {
3533        let mut e = Editor::new(
3534            hjkl_buffer::Buffer::new(),
3535            crate::types::DefaultHost::new(),
3536            crate::types::Options::default(),
3537        );
3538        e.set_content("hello");
3539        for d in "3".chars() {
3540            e.handle_key(key(KeyCode::Char(d)));
3541        }
3542        e.handle_key(key(KeyCode::Char('o')));
3543        assert_eq!(e.vim_mode(), VimMode::Insert);
3544        for c in "world".chars() {
3545            e.handle_key(key(KeyCode::Char(c)));
3546        }
3547        e.handle_key(key(KeyCode::Esc));
3548        assert_eq!(e.vim_mode(), VimMode::Normal);
3549        assert_eq!(e.buffer().lines().len(), 4);
3550        assert!(e.buffer().lines().iter().skip(1).all(|l| l == "world"));
3551    }
3552
3553    #[test]
3554    fn count_i_repeats_text_on_esc() {
3555        let mut e = Editor::new(
3556            hjkl_buffer::Buffer::new(),
3557            crate::types::DefaultHost::new(),
3558            crate::types::Options::default(),
3559        );
3560        e.set_content("");
3561        for d in "3".chars() {
3562            e.handle_key(key(KeyCode::Char(d)));
3563        }
3564        e.handle_key(key(KeyCode::Char('i')));
3565        for c in "ab".chars() {
3566            e.handle_key(key(KeyCode::Char(c)));
3567        }
3568        e.handle_key(key(KeyCode::Esc));
3569        assert_eq!(e.vim_mode(), VimMode::Normal);
3570        assert_eq!(e.buffer().lines()[0], "ababab");
3571    }
3572
3573    #[test]
3574    fn vim_shift_o_opens_line_above() {
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('O')));
3582        assert_eq!(e.vim_mode(), VimMode::Insert);
3583        assert_eq!(e.cursor(), (0, 0));
3584        assert_eq!(e.buffer().lines().len(), 2);
3585    }
3586
3587    #[test]
3588    fn vim_gg_goes_to_top() {
3589        let mut e = Editor::new(
3590            hjkl_buffer::Buffer::new(),
3591            crate::types::DefaultHost::new(),
3592            crate::types::Options::default(),
3593        );
3594        e.set_content("a\nb\nc");
3595        e.jump_cursor(2, 0);
3596        e.handle_key(key(KeyCode::Char('g')));
3597        e.handle_key(key(KeyCode::Char('g')));
3598        assert_eq!(e.cursor().0, 0);
3599    }
3600
3601    #[test]
3602    fn vim_shift_g_goes_to_bottom() {
3603        let mut e = Editor::new(
3604            hjkl_buffer::Buffer::new(),
3605            crate::types::DefaultHost::new(),
3606            crate::types::Options::default(),
3607        );
3608        e.set_content("a\nb\nc");
3609        e.handle_key(shift_key(KeyCode::Char('G')));
3610        assert_eq!(e.cursor().0, 2);
3611    }
3612
3613    #[test]
3614    fn vim_dd_deletes_line() {
3615        let mut e = Editor::new(
3616            hjkl_buffer::Buffer::new(),
3617            crate::types::DefaultHost::new(),
3618            crate::types::Options::default(),
3619        );
3620        e.set_content("first\nsecond");
3621        e.handle_key(key(KeyCode::Char('d')));
3622        e.handle_key(key(KeyCode::Char('d')));
3623        assert_eq!(e.buffer().lines().len(), 1);
3624        assert_eq!(e.buffer().lines()[0], "second");
3625    }
3626
3627    #[test]
3628    fn vim_dw_deletes_word() {
3629        let mut e = Editor::new(
3630            hjkl_buffer::Buffer::new(),
3631            crate::types::DefaultHost::new(),
3632            crate::types::Options::default(),
3633        );
3634        e.set_content("hello world");
3635        e.handle_key(key(KeyCode::Char('d')));
3636        e.handle_key(key(KeyCode::Char('w')));
3637        assert_eq!(e.vim_mode(), VimMode::Normal);
3638        assert!(!e.buffer().lines()[0].starts_with("hello"));
3639    }
3640
3641    #[test]
3642    fn vim_yy_yanks_line() {
3643        let mut e = Editor::new(
3644            hjkl_buffer::Buffer::new(),
3645            crate::types::DefaultHost::new(),
3646            crate::types::Options::default(),
3647        );
3648        e.set_content("hello\nworld");
3649        e.handle_key(key(KeyCode::Char('y')));
3650        e.handle_key(key(KeyCode::Char('y')));
3651        assert!(e.last_yank.as_deref().unwrap_or("").starts_with("hello"));
3652    }
3653
3654    #[test]
3655    fn vim_yy_does_not_move_cursor() {
3656        let mut e = Editor::new(
3657            hjkl_buffer::Buffer::new(),
3658            crate::types::DefaultHost::new(),
3659            crate::types::Options::default(),
3660        );
3661        e.set_content("first\nsecond\nthird");
3662        e.jump_cursor(1, 0);
3663        let before = e.cursor();
3664        e.handle_key(key(KeyCode::Char('y')));
3665        e.handle_key(key(KeyCode::Char('y')));
3666        assert_eq!(e.cursor(), before);
3667        assert_eq!(e.vim_mode(), VimMode::Normal);
3668    }
3669
3670    #[test]
3671    fn vim_yw_yanks_word() {
3672        let mut e = Editor::new(
3673            hjkl_buffer::Buffer::new(),
3674            crate::types::DefaultHost::new(),
3675            crate::types::Options::default(),
3676        );
3677        e.set_content("hello world");
3678        e.handle_key(key(KeyCode::Char('y')));
3679        e.handle_key(key(KeyCode::Char('w')));
3680        assert_eq!(e.vim_mode(), VimMode::Normal);
3681        assert!(e.last_yank.is_some());
3682    }
3683
3684    #[test]
3685    fn vim_cc_changes_line() {
3686        let mut e = Editor::new(
3687            hjkl_buffer::Buffer::new(),
3688            crate::types::DefaultHost::new(),
3689            crate::types::Options::default(),
3690        );
3691        e.set_content("hello\nworld");
3692        e.handle_key(key(KeyCode::Char('c')));
3693        e.handle_key(key(KeyCode::Char('c')));
3694        assert_eq!(e.vim_mode(), VimMode::Insert);
3695    }
3696
3697    #[test]
3698    fn vim_u_undoes_insert_session_as_chunk() {
3699        let mut e = Editor::new(
3700            hjkl_buffer::Buffer::new(),
3701            crate::types::DefaultHost::new(),
3702            crate::types::Options::default(),
3703        );
3704        e.set_content("hello");
3705        e.handle_key(key(KeyCode::Char('i')));
3706        e.handle_key(key(KeyCode::Enter));
3707        e.handle_key(key(KeyCode::Enter));
3708        e.handle_key(key(KeyCode::Esc));
3709        assert_eq!(e.buffer().lines().len(), 3);
3710        e.handle_key(key(KeyCode::Char('u')));
3711        assert_eq!(e.buffer().lines().len(), 1);
3712        assert_eq!(e.buffer().lines()[0], "hello");
3713    }
3714
3715    #[test]
3716    fn vim_undo_redo_roundtrip() {
3717        let mut e = Editor::new(
3718            hjkl_buffer::Buffer::new(),
3719            crate::types::DefaultHost::new(),
3720            crate::types::Options::default(),
3721        );
3722        e.set_content("hello");
3723        e.handle_key(key(KeyCode::Char('i')));
3724        for c in "world".chars() {
3725            e.handle_key(key(KeyCode::Char(c)));
3726        }
3727        e.handle_key(key(KeyCode::Esc));
3728        let after = e.buffer().lines()[0].clone();
3729        e.handle_key(key(KeyCode::Char('u')));
3730        assert_eq!(e.buffer().lines()[0], "hello");
3731        e.handle_key(ctrl_key(KeyCode::Char('r')));
3732        assert_eq!(e.buffer().lines()[0], after);
3733    }
3734
3735    #[test]
3736    fn vim_u_undoes_dd() {
3737        let mut e = Editor::new(
3738            hjkl_buffer::Buffer::new(),
3739            crate::types::DefaultHost::new(),
3740            crate::types::Options::default(),
3741        );
3742        e.set_content("first\nsecond");
3743        e.handle_key(key(KeyCode::Char('d')));
3744        e.handle_key(key(KeyCode::Char('d')));
3745        assert_eq!(e.buffer().lines().len(), 1);
3746        e.handle_key(key(KeyCode::Char('u')));
3747        assert_eq!(e.buffer().lines().len(), 2);
3748        assert_eq!(e.buffer().lines()[0], "first");
3749    }
3750
3751    #[test]
3752    fn vim_ctrl_r_redoes() {
3753        let mut e = Editor::new(
3754            hjkl_buffer::Buffer::new(),
3755            crate::types::DefaultHost::new(),
3756            crate::types::Options::default(),
3757        );
3758        e.set_content("hello");
3759        e.handle_key(ctrl_key(KeyCode::Char('r')));
3760    }
3761
3762    #[test]
3763    fn vim_r_replaces_char() {
3764        let mut e = Editor::new(
3765            hjkl_buffer::Buffer::new(),
3766            crate::types::DefaultHost::new(),
3767            crate::types::Options::default(),
3768        );
3769        e.set_content("hello");
3770        e.handle_key(key(KeyCode::Char('r')));
3771        e.handle_key(key(KeyCode::Char('x')));
3772        assert_eq!(e.buffer().lines()[0].chars().next(), Some('x'));
3773    }
3774
3775    #[test]
3776    fn vim_tilde_toggles_case() {
3777        let mut e = Editor::new(
3778            hjkl_buffer::Buffer::new(),
3779            crate::types::DefaultHost::new(),
3780            crate::types::Options::default(),
3781        );
3782        e.set_content("hello");
3783        e.handle_key(key(KeyCode::Char('~')));
3784        assert_eq!(e.buffer().lines()[0].chars().next(), Some('H'));
3785    }
3786
3787    #[test]
3788    fn vim_visual_d_cuts() {
3789        let mut e = Editor::new(
3790            hjkl_buffer::Buffer::new(),
3791            crate::types::DefaultHost::new(),
3792            crate::types::Options::default(),
3793        );
3794        e.set_content("hello");
3795        e.handle_key(key(KeyCode::Char('v')));
3796        e.handle_key(key(KeyCode::Char('l')));
3797        e.handle_key(key(KeyCode::Char('l')));
3798        e.handle_key(key(KeyCode::Char('d')));
3799        assert_eq!(e.vim_mode(), VimMode::Normal);
3800        assert!(e.last_yank.is_some());
3801    }
3802
3803    #[test]
3804    fn vim_visual_c_enters_insert() {
3805        let mut e = Editor::new(
3806            hjkl_buffer::Buffer::new(),
3807            crate::types::DefaultHost::new(),
3808            crate::types::Options::default(),
3809        );
3810        e.set_content("hello");
3811        e.handle_key(key(KeyCode::Char('v')));
3812        e.handle_key(key(KeyCode::Char('l')));
3813        e.handle_key(key(KeyCode::Char('c')));
3814        assert_eq!(e.vim_mode(), VimMode::Insert);
3815    }
3816
3817    #[test]
3818    fn vim_normal_unknown_key_consumed() {
3819        let mut e = Editor::new(
3820            hjkl_buffer::Buffer::new(),
3821            crate::types::DefaultHost::new(),
3822            crate::types::Options::default(),
3823        );
3824        // Unknown keys are consumed (swallowed) rather than returning false.
3825        let consumed = e.handle_key(key(KeyCode::Char('z')));
3826        assert!(consumed);
3827    }
3828
3829    #[test]
3830    fn force_normal_clears_operator() {
3831        let mut e = Editor::new(
3832            hjkl_buffer::Buffer::new(),
3833            crate::types::DefaultHost::new(),
3834            crate::types::Options::default(),
3835        );
3836        e.handle_key(key(KeyCode::Char('d')));
3837        e.force_normal();
3838        assert_eq!(e.vim_mode(), VimMode::Normal);
3839    }
3840
3841    fn many_lines(n: usize) -> String {
3842        (0..n)
3843            .map(|i| format!("line{i}"))
3844            .collect::<Vec<_>>()
3845            .join("\n")
3846    }
3847
3848    fn prime_viewport<H: Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, height: u16) {
3849        e.set_viewport_height(height);
3850    }
3851
3852    #[test]
3853    fn zz_centers_cursor_in_viewport() {
3854        let mut e = Editor::new(
3855            hjkl_buffer::Buffer::new(),
3856            crate::types::DefaultHost::new(),
3857            crate::types::Options::default(),
3858        );
3859        e.set_content(&many_lines(100));
3860        prime_viewport(&mut e, 20);
3861        e.jump_cursor(50, 0);
3862        e.handle_key(key(KeyCode::Char('z')));
3863        e.handle_key(key(KeyCode::Char('z')));
3864        assert_eq!(e.host().viewport().top_row, 40);
3865        assert_eq!(e.cursor().0, 50);
3866    }
3867
3868    #[test]
3869    fn zt_puts_cursor_at_viewport_top_with_scrolloff() {
3870        let mut e = Editor::new(
3871            hjkl_buffer::Buffer::new(),
3872            crate::types::DefaultHost::new(),
3873            crate::types::Options::default(),
3874        );
3875        e.set_content(&many_lines(100));
3876        prime_viewport(&mut e, 20);
3877        e.jump_cursor(50, 0);
3878        e.handle_key(key(KeyCode::Char('z')));
3879        e.handle_key(key(KeyCode::Char('t')));
3880        // Cursor lands at top of viable area = top + SCROLLOFF (5).
3881        // Viewport top therefore sits at cursor - 5.
3882        assert_eq!(e.host().viewport().top_row, 45);
3883        assert_eq!(e.cursor().0, 50);
3884    }
3885
3886    #[test]
3887    fn ctrl_a_increments_number_at_cursor() {
3888        let mut e = Editor::new(
3889            hjkl_buffer::Buffer::new(),
3890            crate::types::DefaultHost::new(),
3891            crate::types::Options::default(),
3892        );
3893        e.set_content("x = 41");
3894        e.handle_key(ctrl_key(KeyCode::Char('a')));
3895        assert_eq!(e.buffer().lines()[0], "x = 42");
3896        assert_eq!(e.cursor(), (0, 5));
3897    }
3898
3899    #[test]
3900    fn ctrl_a_finds_number_to_right_of_cursor() {
3901        let mut e = Editor::new(
3902            hjkl_buffer::Buffer::new(),
3903            crate::types::DefaultHost::new(),
3904            crate::types::Options::default(),
3905        );
3906        e.set_content("foo 99 bar");
3907        e.handle_key(ctrl_key(KeyCode::Char('a')));
3908        assert_eq!(e.buffer().lines()[0], "foo 100 bar");
3909        assert_eq!(e.cursor(), (0, 6));
3910    }
3911
3912    #[test]
3913    fn ctrl_a_with_count_adds_count() {
3914        let mut e = Editor::new(
3915            hjkl_buffer::Buffer::new(),
3916            crate::types::DefaultHost::new(),
3917            crate::types::Options::default(),
3918        );
3919        e.set_content("x = 10");
3920        for d in "5".chars() {
3921            e.handle_key(key(KeyCode::Char(d)));
3922        }
3923        e.handle_key(ctrl_key(KeyCode::Char('a')));
3924        assert_eq!(e.buffer().lines()[0], "x = 15");
3925    }
3926
3927    #[test]
3928    fn ctrl_x_decrements_number() {
3929        let mut e = Editor::new(
3930            hjkl_buffer::Buffer::new(),
3931            crate::types::DefaultHost::new(),
3932            crate::types::Options::default(),
3933        );
3934        e.set_content("n=5");
3935        e.handle_key(ctrl_key(KeyCode::Char('x')));
3936        assert_eq!(e.buffer().lines()[0], "n=4");
3937    }
3938
3939    #[test]
3940    fn ctrl_x_crosses_zero_into_negative() {
3941        let mut e = Editor::new(
3942            hjkl_buffer::Buffer::new(),
3943            crate::types::DefaultHost::new(),
3944            crate::types::Options::default(),
3945        );
3946        e.set_content("v=0");
3947        e.handle_key(ctrl_key(KeyCode::Char('x')));
3948        assert_eq!(e.buffer().lines()[0], "v=-1");
3949    }
3950
3951    #[test]
3952    fn ctrl_a_on_negative_number_increments_toward_zero() {
3953        let mut e = Editor::new(
3954            hjkl_buffer::Buffer::new(),
3955            crate::types::DefaultHost::new(),
3956            crate::types::Options::default(),
3957        );
3958        e.set_content("a = -5");
3959        e.handle_key(ctrl_key(KeyCode::Char('a')));
3960        assert_eq!(e.buffer().lines()[0], "a = -4");
3961    }
3962
3963    #[test]
3964    fn ctrl_a_noop_when_no_digit_on_line() {
3965        let mut e = Editor::new(
3966            hjkl_buffer::Buffer::new(),
3967            crate::types::DefaultHost::new(),
3968            crate::types::Options::default(),
3969        );
3970        e.set_content("no digits here");
3971        e.handle_key(ctrl_key(KeyCode::Char('a')));
3972        assert_eq!(e.buffer().lines()[0], "no digits here");
3973    }
3974
3975    #[test]
3976    fn zb_puts_cursor_at_viewport_bottom_with_scrolloff() {
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(&many_lines(100));
3983        prime_viewport(&mut e, 20);
3984        e.jump_cursor(50, 0);
3985        e.handle_key(key(KeyCode::Char('z')));
3986        e.handle_key(key(KeyCode::Char('b')));
3987        // Cursor lands at bottom of viable area = top + height - 1 -
3988        // SCROLLOFF. For height 20, scrolloff 5: cursor at top + 14,
3989        // so top = cursor - 14 = 36.
3990        assert_eq!(e.host().viewport().top_row, 36);
3991        assert_eq!(e.cursor().0, 50);
3992    }
3993
3994    /// Contract that the TUI drain relies on: `set_content` flags the
3995    /// editor dirty (so the next `take_dirty` call reports the change),
3996    /// and a second `take_dirty` returns `false` after consumption. The
3997    /// TUI drains this flag after every programmatic content load so
3998    /// opening a tab doesn't get mistaken for a user edit and mark the
3999    /// tab dirty (which would then trigger the quit-prompt on `:q`).
4000    #[test]
4001    fn set_content_dirties_then_take_dirty_clears() {
4002        let mut e = Editor::new(
4003            hjkl_buffer::Buffer::new(),
4004            crate::types::DefaultHost::new(),
4005            crate::types::Options::default(),
4006        );
4007        e.set_content("hello");
4008        assert!(
4009            e.take_dirty(),
4010            "set_content should leave content_dirty=true"
4011        );
4012        assert!(!e.take_dirty(), "take_dirty should clear the flag");
4013    }
4014
4015    #[test]
4016    fn content_arc_returns_same_arc_until_mutation() {
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("hello");
4023        let a = e.content_arc();
4024        let b = e.content_arc();
4025        assert!(
4026            std::sync::Arc::ptr_eq(&a, &b),
4027            "repeated content_arc() should hit the cache"
4028        );
4029
4030        // Any mutation must invalidate the cache.
4031        e.handle_key(key(KeyCode::Char('i')));
4032        e.handle_key(key(KeyCode::Char('!')));
4033        let c = e.content_arc();
4034        assert!(
4035            !std::sync::Arc::ptr_eq(&a, &c),
4036            "mutation should invalidate content_arc() cache"
4037        );
4038        assert!(c.contains('!'));
4039    }
4040
4041    #[test]
4042    fn content_arc_cache_invalidated_by_set_content() {
4043        let mut e = Editor::new(
4044            hjkl_buffer::Buffer::new(),
4045            crate::types::DefaultHost::new(),
4046            crate::types::Options::default(),
4047        );
4048        e.set_content("one");
4049        let a = e.content_arc();
4050        e.set_content("two");
4051        let b = e.content_arc();
4052        assert!(!std::sync::Arc::ptr_eq(&a, &b));
4053        assert!(b.starts_with("two"));
4054    }
4055
4056    /// Click past the last char of a line should land the cursor on
4057    /// the line's last char (Normal mode), not one past it. The
4058    /// previous bug clamped to the line's BYTE length and used `>=`
4059    /// past-end, so clicking deep into the trailing space parked the
4060    /// cursor at `chars().count()` — past where Normal mode lives.
4061    #[test]
4062    fn mouse_click_past_eol_lands_on_last_char() {
4063        let mut e = Editor::new(
4064            hjkl_buffer::Buffer::new(),
4065            crate::types::DefaultHost::new(),
4066            crate::types::Options::default(),
4067        );
4068        e.set_content("hello");
4069        // Outer editor area: x=0, y=0, width=80. mouse_to_doc_pos
4070        // reserves row 0 for the tab bar and adds gutter padding,
4071        // so click row 1, way past the line end.
4072        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4073        e.mouse_click_in_rect(area, 78, 1);
4074        assert_eq!(e.cursor(), (0, 4));
4075    }
4076
4077    #[test]
4078    fn mouse_click_past_eol_handles_multibyte_line() {
4079        let mut e = Editor::new(
4080            hjkl_buffer::Buffer::new(),
4081            crate::types::DefaultHost::new(),
4082            crate::types::Options::default(),
4083        );
4084        // 5 chars, 6 bytes — old code's `String::len()` clamp was
4085        // wrong here.
4086        e.set_content("héllo");
4087        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4088        e.mouse_click_in_rect(area, 78, 1);
4089        assert_eq!(e.cursor(), (0, 4));
4090    }
4091
4092    #[test]
4093    fn mouse_click_inside_line_lands_on_clicked_char() {
4094        let mut e = Editor::new(
4095            hjkl_buffer::Buffer::new(),
4096            crate::types::DefaultHost::new(),
4097            crate::types::Options::default(),
4098        );
4099        e.set_content("hello world");
4100        // Gutter width = max(numberwidth=4, digits+1=2) = 4 cells, plus
4101        // 1 cell of pane padding (area_x.saturating_add(1)) = 5 total offset.
4102        // Click col 5 → char 0; click col 7 → char 2.
4103        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4104        e.mouse_click_in_rect(area, 5, 1);
4105        assert_eq!(e.cursor(), (0, 0));
4106        e.mouse_click_in_rect(area, 7, 1);
4107        assert_eq!(e.cursor(), (0, 2));
4108    }
4109
4110    /// Vim parity: a mouse-position click during insert mode counts
4111    /// as a motion and breaks the active undo group (when
4112    /// `undo_break_on_motion` is on, the default). After clicking and
4113    /// typing more chars, `u` should reverse only the post-click run.
4114    #[test]
4115    fn mouse_click_breaks_insert_undo_group_when_undobreak_on() {
4116        let mut e = Editor::new(
4117            hjkl_buffer::Buffer::new(),
4118            crate::types::DefaultHost::new(),
4119            crate::types::Options::default(),
4120        );
4121        e.set_content("hello world");
4122        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4123        // Default settings.undo_break_on_motion = true.
4124        assert!(e.settings().undo_break_on_motion);
4125        // Enter insert mode and type "AAA" before the line content.
4126        e.handle_key(key(KeyCode::Char('i')));
4127        e.handle_key(key(KeyCode::Char('A')));
4128        e.handle_key(key(KeyCode::Char('A')));
4129        e.handle_key(key(KeyCode::Char('A')));
4130        // Mouse click somewhere else on the line (still insert mode).
4131        e.mouse_click_in_rect(area, 10, 1);
4132        // Type more chars at the new cursor position.
4133        e.handle_key(key(KeyCode::Char('B')));
4134        e.handle_key(key(KeyCode::Char('B')));
4135        e.handle_key(key(KeyCode::Char('B')));
4136        // Leave insert and undo once.
4137        e.handle_key(key(KeyCode::Esc));
4138        e.handle_key(key(KeyCode::Char('u')));
4139        let line = e.buffer().line(0).unwrap_or("").to_string();
4140        assert!(
4141            line.contains("AAA"),
4142            "AAA must survive undo (separate group): {line:?}"
4143        );
4144        assert!(
4145            !line.contains("BBB"),
4146            "BBB must be undone (post-click group): {line:?}"
4147        );
4148    }
4149
4150    /// With `:set noundobreak`, the entire insert run — including
4151    /// chars typed before AND after a mouse click — should collapse
4152    /// into one undo group, so `u` clears everything.
4153    #[test]
4154    fn mouse_click_keeps_one_undo_group_when_undobreak_off() {
4155        let mut e = Editor::new(
4156            hjkl_buffer::Buffer::new(),
4157            crate::types::DefaultHost::new(),
4158            crate::types::Options::default(),
4159        );
4160        e.set_content("hello world");
4161        e.settings_mut().undo_break_on_motion = false;
4162        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
4163        e.handle_key(key(KeyCode::Char('i')));
4164        e.handle_key(key(KeyCode::Char('A')));
4165        e.handle_key(key(KeyCode::Char('A')));
4166        e.mouse_click_in_rect(area, 10, 1);
4167        e.handle_key(key(KeyCode::Char('B')));
4168        e.handle_key(key(KeyCode::Char('B')));
4169        e.handle_key(key(KeyCode::Esc));
4170        e.handle_key(key(KeyCode::Char('u')));
4171        let line = e.buffer().line(0).unwrap_or("").to_string();
4172        assert!(
4173            !line.contains("AA") && !line.contains("BB"),
4174            "with undobreak off, single `u` must reverse whole insert: {line:?}"
4175        );
4176        assert_eq!(line, "hello world");
4177    }
4178
4179    // ── Patch B (0.0.29): Host trait wired into Editor ──
4180
4181    #[test]
4182    fn host_clipboard_round_trip_via_default_host() {
4183        // DefaultHost stores write_clipboard in-memory; read_clipboard
4184        // returns the most recent payload.
4185        let mut e = Editor::new(
4186            hjkl_buffer::Buffer::new(),
4187            crate::types::DefaultHost::new(),
4188            crate::types::Options::default(),
4189        );
4190        e.host_mut().write_clipboard("payload".to_string());
4191        assert_eq!(e.host_mut().read_clipboard().as_deref(), Some("payload"));
4192    }
4193
4194    #[test]
4195    fn host_records_clipboard_on_yank() {
4196        // `yy` on a single-line buffer must drive `Host::write_clipboard`
4197        // (the new Patch B side-channel) in addition to the legacy
4198        // `last_yank` mirror.
4199        let mut e = Editor::new(
4200            hjkl_buffer::Buffer::new(),
4201            crate::types::DefaultHost::new(),
4202            crate::types::Options::default(),
4203        );
4204        e.set_content("hello\n");
4205        e.handle_key(key(KeyCode::Char('y')));
4206        e.handle_key(key(KeyCode::Char('y')));
4207        // Clipboard cache holds the linewise yank.
4208        let clip = e.host_mut().read_clipboard();
4209        assert!(
4210            clip.as_deref().unwrap_or("").starts_with("hello"),
4211            "host clipboard should carry the yank: {clip:?}"
4212        );
4213        // Legacy mirror still populated for 0.0.28-era hosts.
4214        assert!(e.last_yank.as_deref().unwrap_or("").starts_with("hello"));
4215    }
4216
4217    #[test]
4218    fn host_cursor_shape_via_shared_recorder() {
4219        // Recording host backed by a leaked `Mutex` so the test can
4220        // inspect the emit sequence after the editor has consumed the
4221        // host. (Host: Send rules out Rc/RefCell.)
4222        let shapes_ptr: &'static std::sync::Mutex<Vec<crate::types::CursorShape>> =
4223            Box::leak(Box::new(std::sync::Mutex::new(Vec::new())));
4224        struct LeakHost {
4225            shapes: &'static std::sync::Mutex<Vec<crate::types::CursorShape>>,
4226            viewport: crate::types::Viewport,
4227        }
4228        impl crate::types::Host for LeakHost {
4229            type Intent = ();
4230            fn write_clipboard(&mut self, _: String) {}
4231            fn read_clipboard(&mut self) -> Option<String> {
4232                None
4233            }
4234            fn now(&self) -> core::time::Duration {
4235                core::time::Duration::ZERO
4236            }
4237            fn prompt_search(&mut self) -> Option<String> {
4238                None
4239            }
4240            fn emit_cursor_shape(&mut self, s: crate::types::CursorShape) {
4241                self.shapes.lock().unwrap().push(s);
4242            }
4243            fn viewport(&self) -> &crate::types::Viewport {
4244                &self.viewport
4245            }
4246            fn viewport_mut(&mut self) -> &mut crate::types::Viewport {
4247                &mut self.viewport
4248            }
4249            fn emit_intent(&mut self, _: Self::Intent) {}
4250        }
4251        let mut e = Editor::new(
4252            hjkl_buffer::Buffer::new(),
4253            LeakHost {
4254                shapes: shapes_ptr,
4255                viewport: crate::types::Viewport::default(),
4256            },
4257            crate::types::Options::default(),
4258        );
4259        e.set_content("abc");
4260        // Normal → Insert: Bar emit.
4261        e.handle_key(key(KeyCode::Char('i')));
4262        // Insert → Normal: Block emit.
4263        e.handle_key(key(KeyCode::Esc));
4264        let shapes = shapes_ptr.lock().unwrap().clone();
4265        assert_eq!(
4266            shapes,
4267            vec![
4268                crate::types::CursorShape::Bar,
4269                crate::types::CursorShape::Block,
4270            ],
4271            "host should observe Insert(Bar) → Normal(Block) transitions"
4272        );
4273    }
4274
4275    #[test]
4276    fn host_now_drives_chord_timeout_deterministically() {
4277        // Custom host whose `now()` is host-controlled; we drive it
4278        // forward by `timeout_len + 1ms` between the first `g` and
4279        // the second so the chord-timeout fires regardless of
4280        // wall-clock progress.
4281        let now_ptr: &'static std::sync::Mutex<core::time::Duration> =
4282            Box::leak(Box::new(std::sync::Mutex::new(core::time::Duration::ZERO)));
4283        struct ClockHost {
4284            now: &'static std::sync::Mutex<core::time::Duration>,
4285            viewport: crate::types::Viewport,
4286        }
4287        impl crate::types::Host for ClockHost {
4288            type Intent = ();
4289            fn write_clipboard(&mut self, _: String) {}
4290            fn read_clipboard(&mut self) -> Option<String> {
4291                None
4292            }
4293            fn now(&self) -> core::time::Duration {
4294                *self.now.lock().unwrap()
4295            }
4296            fn prompt_search(&mut self) -> Option<String> {
4297                None
4298            }
4299            fn emit_cursor_shape(&mut self, _: crate::types::CursorShape) {}
4300            fn viewport(&self) -> &crate::types::Viewport {
4301                &self.viewport
4302            }
4303            fn viewport_mut(&mut self) -> &mut crate::types::Viewport {
4304                &mut self.viewport
4305            }
4306            fn emit_intent(&mut self, _: Self::Intent) {}
4307        }
4308        let mut e = Editor::new(
4309            hjkl_buffer::Buffer::new(),
4310            ClockHost {
4311                now: now_ptr,
4312                viewport: crate::types::Viewport::default(),
4313            },
4314            crate::types::Options::default(),
4315        );
4316        e.set_content("a\nb\nc\n");
4317        e.jump_cursor(2, 0);
4318        // First `g` — host time = 0ms, lands in g-pending.
4319        e.handle_key(key(KeyCode::Char('g')));
4320        // Advance host time well past timeout_len (default 1000ms).
4321        *now_ptr.lock().unwrap() = core::time::Duration::from_secs(60);
4322        // Second `g` — chord-timeout fires; bare `g` re-dispatches and
4323        // does nothing on its own. Cursor must NOT have jumped to row 0.
4324        e.handle_key(key(KeyCode::Char('g')));
4325        assert_eq!(
4326            e.cursor().0,
4327            2,
4328            "Host::now() must drive `:set timeoutlen` deterministically"
4329        );
4330    }
4331
4332    // ── ContentEdit emission ─────────────────────────────────────────
4333
4334    fn fresh_editor(initial: &str) -> Editor {
4335        let buffer = hjkl_buffer::Buffer::from_str(initial);
4336        Editor::new(
4337            buffer,
4338            crate::types::DefaultHost::new(),
4339            crate::types::Options::default(),
4340        )
4341    }
4342
4343    #[test]
4344    fn content_edit_insert_char_at_origin() {
4345        let mut e = fresh_editor("");
4346        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
4347            at: hjkl_buffer::Position::new(0, 0),
4348            ch: 'a',
4349        });
4350        let edits = e.take_content_edits();
4351        assert_eq!(edits.len(), 1);
4352        let ce = &edits[0];
4353        assert_eq!(ce.start_byte, 0);
4354        assert_eq!(ce.old_end_byte, 0);
4355        assert_eq!(ce.new_end_byte, 1);
4356        assert_eq!(ce.start_position, (0, 0));
4357        assert_eq!(ce.old_end_position, (0, 0));
4358        assert_eq!(ce.new_end_position, (0, 1));
4359    }
4360
4361    #[test]
4362    fn content_edit_insert_str_multiline() {
4363        // Buffer "x\ny" — insert "ab\ncd" at end of row 0.
4364        let mut e = fresh_editor("x\ny");
4365        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertStr {
4366            at: hjkl_buffer::Position::new(0, 1),
4367            text: "ab\ncd".into(),
4368        });
4369        let edits = e.take_content_edits();
4370        assert_eq!(edits.len(), 1);
4371        let ce = &edits[0];
4372        assert_eq!(ce.start_byte, 1);
4373        assert_eq!(ce.old_end_byte, 1);
4374        assert_eq!(ce.new_end_byte, 1 + 5);
4375        assert_eq!(ce.start_position, (0, 1));
4376        // Insertion contains one '\n', so row+1, col = bytes after last '\n' = 2.
4377        assert_eq!(ce.new_end_position, (1, 2));
4378    }
4379
4380    #[test]
4381    fn content_edit_delete_range_charwise() {
4382        // "abcdef" — delete chars 1..4 ("bcd").
4383        let mut e = fresh_editor("abcdef");
4384        let _ = e.mutate_edit(hjkl_buffer::Edit::DeleteRange {
4385            start: hjkl_buffer::Position::new(0, 1),
4386            end: hjkl_buffer::Position::new(0, 4),
4387            kind: hjkl_buffer::MotionKind::Char,
4388        });
4389        let edits = e.take_content_edits();
4390        assert_eq!(edits.len(), 1);
4391        let ce = &edits[0];
4392        assert_eq!(ce.start_byte, 1);
4393        assert_eq!(ce.old_end_byte, 4);
4394        assert_eq!(ce.new_end_byte, 1);
4395        assert!(ce.old_end_byte > ce.new_end_byte);
4396    }
4397
4398    #[test]
4399    fn content_edit_set_content_resets() {
4400        let mut e = fresh_editor("foo");
4401        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
4402            at: hjkl_buffer::Position::new(0, 0),
4403            ch: 'X',
4404        });
4405        // set_content should clear queued edits and raise the reset
4406        // flag on the next take_content_reset.
4407        e.set_content("brand new");
4408        assert!(e.take_content_reset());
4409        // Subsequent call clears the flag.
4410        assert!(!e.take_content_reset());
4411        // Edits cleared on reset.
4412        assert!(e.take_content_edits().is_empty());
4413    }
4414
4415    #[test]
4416    fn content_edit_multiple_replaces_in_order() {
4417        // Three Replace edits applied left-to-right (mimics the
4418        // substitute path's per-match Replace fan-out). Verify each
4419        // mutation queues exactly one ContentEdit and they're drained
4420        // in source-order with structurally valid byte spans.
4421        let mut e = fresh_editor("xax xbx xcx");
4422        let _ = e.take_content_edits();
4423        let _ = e.take_content_reset();
4424        // Replace each "x" with "yy", left to right. After each replace,
4425        // the next match's char-col shifts by +1 (since "yy" is 1 char
4426        // longer than "x" but they're both ASCII so byte = char here).
4427        let positions = [(0usize, 0usize), (0, 4), (0, 8)];
4428        for (row, col) in positions {
4429            let _ = e.mutate_edit(hjkl_buffer::Edit::Replace {
4430                start: hjkl_buffer::Position::new(row, col),
4431                end: hjkl_buffer::Position::new(row, col + 1),
4432                with: "yy".into(),
4433            });
4434        }
4435        let edits = e.take_content_edits();
4436        assert_eq!(edits.len(), 3);
4437        for ce in &edits {
4438            assert!(ce.start_byte <= ce.old_end_byte);
4439            assert!(ce.start_byte <= ce.new_end_byte);
4440        }
4441        // Document order.
4442        for w in edits.windows(2) {
4443            assert!(w[0].start_byte <= w[1].start_byte);
4444        }
4445    }
4446
4447    #[test]
4448    fn replace_char_at_replaces_single_char_under_cursor() {
4449        // Matches vim's `rx` semantics: replace char under cursor.
4450        let mut e = fresh_editor("abc");
4451        e.jump_cursor(0, 1); // cursor on 'b'
4452        e.replace_char_at('X', 1);
4453        let got = e.content();
4454        let got = got.trim_end_matches('\n');
4455        assert_eq!(
4456            got, "aXc",
4457            "replace_char_at(X, 1) must replace 'b' with 'X'"
4458        );
4459        // Cursor stays on the replaced char.
4460        assert_eq!(e.cursor(), (0, 1));
4461    }
4462
4463    #[test]
4464    fn replace_char_at_count_replaces_multiple_chars() {
4465        // `3rx` in vim replaces 3 chars starting at cursor.
4466        let mut e = fresh_editor("abcde");
4467        e.jump_cursor(0, 0);
4468        e.replace_char_at('Z', 3);
4469        let got = e.content();
4470        let got = got.trim_end_matches('\n');
4471        assert_eq!(
4472            got, "ZZZde",
4473            "replace_char_at(Z, 3) must replace first 3 chars"
4474        );
4475    }
4476
4477    #[test]
4478    fn find_char_method_moves_to_target() {
4479        // buffer "abcabc", cursor (0,0), f<c> → cursor (0,2).
4480        let mut e = fresh_editor("abcabc");
4481        e.jump_cursor(0, 0);
4482        e.find_char('c', true, false, 1);
4483        assert_eq!(
4484            e.cursor(),
4485            (0, 2),
4486            "find_char('c', forward=true, till=false, count=1) must land on 'c' at col 2"
4487        );
4488    }
4489
4490    // ── after_g unit tests (Phase 2b-ii) ────────────────────────────────────
4491
4492    #[test]
4493    fn after_g_gg_jumps_to_top() {
4494        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
4495        let mut e = fresh_editor(&content);
4496        e.jump_cursor(15, 0);
4497        e.after_g('g', 1);
4498        assert_eq!(e.cursor().0, 0, "gg must move cursor to row 0");
4499    }
4500
4501    #[test]
4502    fn after_g_gg_with_count_jumps_line() {
4503        // 5gg → row 4 (0-indexed).
4504        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
4505        let mut e = fresh_editor(&content);
4506        e.jump_cursor(0, 0);
4507        e.after_g('g', 5);
4508        assert_eq!(e.cursor().0, 4, "5gg must land on row 4");
4509    }
4510
4511    #[test]
4512    fn after_g_gv_restores_last_visual() {
4513        // Enter visual, move right, exit, then gv re-enters.
4514        let mut e = fresh_editor("hello world\n");
4515        // Enter char-visual at col 0, move to col 3, then exit.
4516        e.handle_key(key(KeyCode::Char('v')));
4517        e.handle_key(key(KeyCode::Char('l')));
4518        e.handle_key(key(KeyCode::Char('l')));
4519        e.handle_key(key(KeyCode::Char('l')));
4520        e.handle_key(key(KeyCode::Esc));
4521        assert_eq!(e.vim_mode(), VimMode::Normal, "should be Normal after Esc");
4522        // gv via after_g.
4523        e.after_g('v', 1);
4524        assert_eq!(
4525            e.vim_mode(),
4526            VimMode::Visual,
4527            "gv must re-enter Visual mode"
4528        );
4529    }
4530
4531    #[test]
4532    fn after_g_gj_moves_down() {
4533        let mut e = fresh_editor("line0\nline1\nline2\n");
4534        e.jump_cursor(0, 0);
4535        e.after_g('j', 1);
4536        assert_eq!(e.cursor().0, 1, "gj must move down one display row");
4537    }
4538
4539    #[test]
4540    fn after_g_gu_sets_operator_pending() {
4541        // gU enters operator-pending with Uppercase op; next key applies it.
4542        let mut e = fresh_editor("hello\n");
4543        e.after_g('U', 1);
4544        // The engine should now be chord-pending (Pending::Op set).
4545        assert!(
4546            e.is_chord_pending(),
4547            "gU must set engine chord-pending (Pending::Op)"
4548        );
4549    }
4550
4551    #[test]
4552    fn after_g_g_star_searches_forward_non_whole_word() {
4553        // g* on word "foo" in "foobar" should find the match.
4554        let mut e = fresh_editor("foo foobar\n");
4555        e.jump_cursor(0, 0); // cursor on 'f' of "foo"
4556        e.after_g('*', 1);
4557        // After g* the cursor should have moved (ScreenDown motion is
4558        // not applicable here; WordAtCursor forward moves to next match).
4559        // At minimum: no panic and mode stays Normal.
4560        assert_eq!(e.vim_mode(), VimMode::Normal, "g* must stay in Normal mode");
4561    }
4562}