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