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;
10#[cfg(feature = "crossterm")]
11use crate::input::Key;
12use crate::vim::{self, VimState};
13use crate::{KeybindingMode, VimMode};
14#[cfg(feature = "crossterm")]
15use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
16#[cfg(feature = "ratatui")]
17use ratatui::layout::Rect;
18use std::sync::atomic::{AtomicU16, Ordering};
19
20/// Convert a SPEC [`crate::types::Style`] to a [`ratatui::style::Style`].
21///
22/// Lossless within the styles each library represents. Lives behind the
23/// `ratatui` feature so wasm / no_std consumers that opt out don't pay
24/// for the dep. Use the engine-native [`crate::types::Style`] +
25/// [`Editor::intern_engine_style`] surface from feature-disabled hosts.
26#[cfg(feature = "ratatui")]
27pub(crate) fn engine_style_to_ratatui(s: crate::types::Style) -> ratatui::style::Style {
28 use crate::types::Attrs;
29 use ratatui::style::{Color as RColor, Modifier as RMod, Style as RStyle};
30 let mut out = RStyle::default();
31 if let Some(c) = s.fg {
32 out = out.fg(RColor::Rgb(c.0, c.1, c.2));
33 }
34 if let Some(c) = s.bg {
35 out = out.bg(RColor::Rgb(c.0, c.1, c.2));
36 }
37 let mut m = RMod::empty();
38 if s.attrs.contains(Attrs::BOLD) {
39 m |= RMod::BOLD;
40 }
41 if s.attrs.contains(Attrs::ITALIC) {
42 m |= RMod::ITALIC;
43 }
44 if s.attrs.contains(Attrs::UNDERLINE) {
45 m |= RMod::UNDERLINED;
46 }
47 if s.attrs.contains(Attrs::REVERSE) {
48 m |= RMod::REVERSED;
49 }
50 if s.attrs.contains(Attrs::DIM) {
51 m |= RMod::DIM;
52 }
53 if s.attrs.contains(Attrs::STRIKE) {
54 m |= RMod::CROSSED_OUT;
55 }
56 out.add_modifier(m)
57}
58
59/// Inverse of [`engine_style_to_ratatui`]. Lossy for ratatui colors
60/// the engine doesn't model (Indexed, named ANSI) — flattens to
61/// nearest RGB. Behind the `ratatui` feature.
62#[cfg(feature = "ratatui")]
63pub(crate) fn ratatui_style_to_engine(s: ratatui::style::Style) -> crate::types::Style {
64 use crate::types::{Attrs, Color, Style};
65 use ratatui::style::{Color as RColor, Modifier as RMod};
66 fn c(rc: RColor) -> Color {
67 match rc {
68 RColor::Rgb(r, g, b) => Color(r, g, b),
69 RColor::Black => Color(0, 0, 0),
70 RColor::Red => Color(205, 49, 49),
71 RColor::Green => Color(13, 188, 121),
72 RColor::Yellow => Color(229, 229, 16),
73 RColor::Blue => Color(36, 114, 200),
74 RColor::Magenta => Color(188, 63, 188),
75 RColor::Cyan => Color(17, 168, 205),
76 RColor::Gray => Color(229, 229, 229),
77 RColor::DarkGray => Color(102, 102, 102),
78 RColor::LightRed => Color(241, 76, 76),
79 RColor::LightGreen => Color(35, 209, 139),
80 RColor::LightYellow => Color(245, 245, 67),
81 RColor::LightBlue => Color(59, 142, 234),
82 RColor::LightMagenta => Color(214, 112, 214),
83 RColor::LightCyan => Color(41, 184, 219),
84 RColor::White => Color(255, 255, 255),
85 _ => Color(0, 0, 0),
86 }
87 }
88 let mut attrs = Attrs::empty();
89 if s.add_modifier.contains(RMod::BOLD) {
90 attrs |= Attrs::BOLD;
91 }
92 if s.add_modifier.contains(RMod::ITALIC) {
93 attrs |= Attrs::ITALIC;
94 }
95 if s.add_modifier.contains(RMod::UNDERLINED) {
96 attrs |= Attrs::UNDERLINE;
97 }
98 if s.add_modifier.contains(RMod::REVERSED) {
99 attrs |= Attrs::REVERSE;
100 }
101 if s.add_modifier.contains(RMod::DIM) {
102 attrs |= Attrs::DIM;
103 }
104 if s.add_modifier.contains(RMod::CROSSED_OUT) {
105 attrs |= Attrs::STRIKE;
106 }
107 Style {
108 fg: s.fg.map(c),
109 bg: s.bg.map(c),
110 attrs,
111 }
112}
113
114/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
115/// [`crate::types::Edit`] (`EditOp`) records.
116///
117/// Most buffer edits map to a single EditOp. Block ops
118/// ([`hjkl_buffer::Edit::InsertBlock`] /
119/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
120/// touched — they edit non-contiguous cells and a single
121/// `range..range` can't represent the rectangle.
122///
123/// Returns an empty vec when the edit isn't representable (no buffer
124/// variant currently fails this check).
125fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
126 use crate::types::{Edit as Op, Pos};
127 use hjkl_buffer::Edit as B;
128 let to_pos = |p: hjkl_buffer::Position| Pos {
129 line: p.row as u32,
130 col: p.col as u32,
131 };
132 match edit {
133 B::InsertChar { at, ch } => vec![Op {
134 range: to_pos(*at)..to_pos(*at),
135 replacement: ch.to_string(),
136 }],
137 B::InsertStr { at, text } => vec![Op {
138 range: to_pos(*at)..to_pos(*at),
139 replacement: text.clone(),
140 }],
141 B::DeleteRange { start, end, .. } => vec![Op {
142 range: to_pos(*start)..to_pos(*end),
143 replacement: String::new(),
144 }],
145 B::Replace { start, end, with } => vec![Op {
146 range: to_pos(*start)..to_pos(*end),
147 replacement: with.clone(),
148 }],
149 B::JoinLines {
150 row,
151 count,
152 with_space,
153 } => {
154 // Joining `count` rows after `row` collapses
155 // [(row+1, 0) .. (row+count, EOL)] into the joined
156 // sentinel. The replacement is either an empty string
157 // (gJ) or " " between segments (J).
158 let start = Pos {
159 line: *row as u32 + 1,
160 col: 0,
161 };
162 let end = Pos {
163 line: (*row + *count) as u32,
164 col: u32::MAX, // covers to EOL of the last source row
165 };
166 vec![Op {
167 range: start..end,
168 replacement: if *with_space {
169 " ".into()
170 } else {
171 String::new()
172 },
173 }]
174 }
175 B::SplitLines {
176 row,
177 cols,
178 inserted_space: _,
179 } => {
180 // SplitLines reverses a JoinLines: insert a `\n`
181 // (and optional dropped space) at each col on `row`.
182 cols.iter()
183 .map(|c| {
184 let p = Pos {
185 line: *row as u32,
186 col: *c as u32,
187 };
188 Op {
189 range: p..p,
190 replacement: "\n".into(),
191 }
192 })
193 .collect()
194 }
195 B::InsertBlock { at, chunks } => {
196 // One EditOp per row in the block — non-contiguous edits.
197 chunks
198 .iter()
199 .enumerate()
200 .map(|(i, chunk)| {
201 let p = Pos {
202 line: at.row as u32 + i as u32,
203 col: at.col as u32,
204 };
205 Op {
206 range: p..p,
207 replacement: chunk.clone(),
208 }
209 })
210 .collect()
211 }
212 B::DeleteBlockChunks { at, widths } => {
213 // One EditOp per row, deleting `widths[i]` chars at
214 // `(at.row + i, at.col)`.
215 widths
216 .iter()
217 .enumerate()
218 .map(|(i, w)| {
219 let start = Pos {
220 line: at.row as u32 + i as u32,
221 col: at.col as u32,
222 };
223 let end = Pos {
224 line: at.row as u32 + i as u32,
225 col: at.col as u32 + *w as u32,
226 };
227 Op {
228 range: start..end,
229 replacement: String::new(),
230 }
231 })
232 .collect()
233 }
234 }
235}
236
237/// Sum of bytes from the start of the buffer to the start of `row`.
238/// Walks lines + their separating `\n` bytes — matches the canonical
239/// `lines().join("\n")` byte rendering used by syntax tooling.
240#[inline]
241fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
242 let n = buf.row_count();
243 let row = row.min(n);
244 let mut acc = 0usize;
245 for r in 0..row {
246 acc += buf.line(r).map(str::len).unwrap_or(0);
247 if r + 1 < n {
248 acc += 1; // separator '\n'
249 }
250 }
251 acc
252}
253
254/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
255/// coordinates `(byte_within_buffer, (row, col_byte))` against the
256/// **pre-edit** buffer.
257fn position_to_byte_coords(
258 buf: &hjkl_buffer::Buffer,
259 pos: hjkl_buffer::Position,
260) -> (usize, (u32, u32)) {
261 let row = pos.row.min(buf.row_count().saturating_sub(1));
262 let line = buf.line(row).unwrap_or("");
263 let col_byte = pos.byte_offset(line);
264 let byte = buffer_byte_of_row(buf, row) + col_byte;
265 (byte, (row as u32, col_byte as u32))
266}
267
268/// Compute the byte position after inserting `text` starting at
269/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
270fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
271 let new_end_byte = start_byte + text.len();
272 let newlines = text.bytes().filter(|&b| b == b'\n').count();
273 let end_pos = if newlines == 0 {
274 (start_pos.0, start_pos.1 + text.len() as u32)
275 } else {
276 // Bytes after the last newline determine the trailing column.
277 let last_nl = text.rfind('\n').unwrap();
278 let tail_bytes = (text.len() - last_nl - 1) as u32;
279 (start_pos.0 + newlines as u32, tail_bytes)
280 };
281 (new_end_byte, end_pos)
282}
283
284/// Translate a single `hjkl_buffer::Edit` into one or more
285/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
286/// state for byte/position lookups. Block ops fan out to one entry per
287/// touched row (matches `edit_to_editops`).
288fn content_edits_from_buffer_edit(
289 buf: &hjkl_buffer::Buffer,
290 edit: &hjkl_buffer::Edit,
291) -> Vec<crate::types::ContentEdit> {
292 use hjkl_buffer::Edit as B;
293 use hjkl_buffer::Position;
294
295 let mut out: Vec<crate::types::ContentEdit> = Vec::new();
296
297 match edit {
298 B::InsertChar { at, ch } => {
299 let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
300 let new_end_byte = start_byte + ch.len_utf8();
301 let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
302 out.push(crate::types::ContentEdit {
303 start_byte,
304 old_end_byte: start_byte,
305 new_end_byte,
306 start_position: start_pos,
307 old_end_position: start_pos,
308 new_end_position: new_end_pos,
309 });
310 }
311 B::InsertStr { at, text } => {
312 let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
313 let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
314 out.push(crate::types::ContentEdit {
315 start_byte,
316 old_end_byte: start_byte,
317 new_end_byte,
318 start_position: start_pos,
319 old_end_position: start_pos,
320 new_end_position: new_end_pos,
321 });
322 }
323 B::DeleteRange { start, end, kind } => {
324 let (start, end) = if start <= end {
325 (*start, *end)
326 } else {
327 (*end, *start)
328 };
329 match kind {
330 hjkl_buffer::MotionKind::Char => {
331 let (start_byte, start_pos) = position_to_byte_coords(buf, start);
332 let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
333 out.push(crate::types::ContentEdit {
334 start_byte,
335 old_end_byte,
336 new_end_byte: start_byte,
337 start_position: start_pos,
338 old_end_position: old_end_pos,
339 new_end_position: start_pos,
340 });
341 }
342 hjkl_buffer::MotionKind::Line => {
343 // Linewise delete drops rows [start.row..=end.row]. Map
344 // to a span from start of `start.row` through start of
345 // (end.row + 1). The buffer's own `do_delete_range`
346 // collapses to row `start.row` after dropping.
347 let lo = start.row;
348 let hi = end.row.min(buf.row_count().saturating_sub(1));
349 let start_byte = buffer_byte_of_row(buf, lo);
350 let next_row_byte = if hi + 1 < buf.row_count() {
351 buffer_byte_of_row(buf, hi + 1)
352 } else {
353 // No row after; clamp to end-of-buffer byte.
354 buffer_byte_of_row(buf, buf.row_count())
355 + buf
356 .line(buf.row_count().saturating_sub(1))
357 .map(str::len)
358 .unwrap_or(0)
359 };
360 out.push(crate::types::ContentEdit {
361 start_byte,
362 old_end_byte: next_row_byte,
363 new_end_byte: start_byte,
364 start_position: (lo as u32, 0),
365 old_end_position: ((hi + 1) as u32, 0),
366 new_end_position: (lo as u32, 0),
367 });
368 }
369 hjkl_buffer::MotionKind::Block => {
370 // Block delete removes a rectangle of chars per row.
371 // Fan out to one ContentEdit per row.
372 let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
373 for row in start.row..=end.row {
374 let row_start_pos = Position::new(row, left_col);
375 let row_end_pos = Position::new(row, right_col + 1);
376 let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
377 let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
378 if eb <= sb {
379 continue;
380 }
381 out.push(crate::types::ContentEdit {
382 start_byte: sb,
383 old_end_byte: eb,
384 new_end_byte: sb,
385 start_position: sp,
386 old_end_position: ep,
387 new_end_position: sp,
388 });
389 }
390 }
391 }
392 }
393 B::Replace { start, end, with } => {
394 let (start, end) = if start <= end {
395 (*start, *end)
396 } else {
397 (*end, *start)
398 };
399 let (start_byte, start_pos) = position_to_byte_coords(buf, start);
400 let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
401 let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
402 out.push(crate::types::ContentEdit {
403 start_byte,
404 old_end_byte,
405 new_end_byte,
406 start_position: start_pos,
407 old_end_position: old_end_pos,
408 new_end_position: new_end_pos,
409 });
410 }
411 B::JoinLines {
412 row,
413 count,
414 with_space,
415 } => {
416 // Joining `count` rows after `row` collapses the bytes
417 // between EOL of `row` and EOL of `row + count` into either
418 // an empty string (gJ) or a single space per join (J — but
419 // only when both sides are non-empty; we approximate with
420 // a single space for simplicity).
421 let row = (*row).min(buf.row_count().saturating_sub(1));
422 let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
423 let line = buf.line(row).unwrap_or("");
424 let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
425 let row_eol_col = line.len() as u32;
426 let next_row_after = last_join_row + 1;
427 let old_end_byte = if next_row_after < buf.row_count() {
428 buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
429 } else {
430 buffer_byte_of_row(buf, buf.row_count())
431 + buf
432 .line(buf.row_count().saturating_sub(1))
433 .map(str::len)
434 .unwrap_or(0)
435 };
436 let last_line = buf.line(last_join_row).unwrap_or("");
437 let old_end_pos = (last_join_row as u32, last_line.len() as u32);
438 let replacement_len = if *with_space { 1 } else { 0 };
439 let new_end_byte = row_eol_byte + replacement_len;
440 let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
441 out.push(crate::types::ContentEdit {
442 start_byte: row_eol_byte,
443 old_end_byte,
444 new_end_byte,
445 start_position: (row as u32, row_eol_col),
446 old_end_position: old_end_pos,
447 new_end_position: new_end_pos,
448 });
449 }
450 B::SplitLines {
451 row,
452 cols,
453 inserted_space,
454 } => {
455 // Splits insert "\n" (or "\n " inverse) at each col on `row`.
456 // The buffer applies all splits left-to-right via the
457 // do_split_lines path; we emit one ContentEdit per col,
458 // each treated as an insert at that col on `row`. Note: the
459 // buffer state during emission is *pre-edit*, so all cols
460 // index into the same pre-edit row.
461 let row = (*row).min(buf.row_count().saturating_sub(1));
462 let line = buf.line(row).unwrap_or("");
463 let row_byte = buffer_byte_of_row(buf, row);
464 let insert = if *inserted_space { "\n " } else { "\n" };
465 for &c in cols {
466 let pos = Position::new(row, c);
467 let col_byte = pos.byte_offset(line);
468 let start_byte = row_byte + col_byte;
469 let start_pos = (row as u32, col_byte as u32);
470 let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
471 out.push(crate::types::ContentEdit {
472 start_byte,
473 old_end_byte: start_byte,
474 new_end_byte,
475 start_position: start_pos,
476 old_end_position: start_pos,
477 new_end_position: new_end_pos,
478 });
479 }
480 }
481 B::InsertBlock { at, chunks } => {
482 // One ContentEdit per chunk; each lands at `(at.row + i,
483 // at.col)` in the pre-edit buffer.
484 for (i, chunk) in chunks.iter().enumerate() {
485 let pos = Position::new(at.row + i, at.col);
486 let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
487 let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
488 out.push(crate::types::ContentEdit {
489 start_byte,
490 old_end_byte: start_byte,
491 new_end_byte,
492 start_position: start_pos,
493 old_end_position: start_pos,
494 new_end_position: new_end_pos,
495 });
496 }
497 }
498 B::DeleteBlockChunks { at, widths } => {
499 for (i, w) in widths.iter().enumerate() {
500 let row = at.row + i;
501 let start_pos = Position::new(row, at.col);
502 let end_pos = Position::new(row, at.col + *w);
503 let (sb, sp) = position_to_byte_coords(buf, start_pos);
504 let (eb, ep) = position_to_byte_coords(buf, end_pos);
505 if eb <= sb {
506 continue;
507 }
508 out.push(crate::types::ContentEdit {
509 start_byte: sb,
510 old_end_byte: eb,
511 new_end_byte: sb,
512 start_position: sp,
513 old_end_position: ep,
514 new_end_position: sp,
515 });
516 }
517 }
518 }
519
520 out
521}
522
523/// Where the cursor should land in the viewport after a `z`-family
524/// scroll (`zz` / `zt` / `zb`).
525#[derive(Debug, Clone, Copy, PartialEq, Eq)]
526pub(super) enum CursorScrollTarget {
527 Center,
528 Top,
529 Bottom,
530}
531
532// ── Trait-surface cast helpers ────────────────────────────────────
533//
534// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
535// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
536// their reaches through the same primitives. Re-import via
537// `use` so the editor body keeps its terse call shape.
538
539use crate::buf_helpers::{
540 apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
541 buf_lines_to_vec, buf_row_count, buf_set_cursor_rc,
542};
543
544pub struct Editor<
545 B: crate::types::Buffer = hjkl_buffer::Buffer,
546 H: crate::types::Host = crate::types::DefaultHost,
547> {
548 pub keybinding_mode: KeybindingMode,
549 /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
550 pub last_yank: Option<String>,
551 /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
552 /// Internal — exposed via Editor accessor methods
553 /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
554 /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
555 pub(crate) vim: VimState,
556 /// Undo history: each entry is (lines, cursor) before the edit.
557 /// Internal — managed by [`Editor::push_undo`] / [`Editor::restore`]
558 /// / [`Editor::pop_last_undo`].
559 pub(crate) undo_stack: Vec<(Vec<String>, (usize, usize))>,
560 /// Redo history: entries pushed when undoing.
561 pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
562 /// Set whenever the buffer content changes; cleared by `take_dirty`.
563 pub(super) content_dirty: bool,
564 /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
565 /// so repeated `content_arc()` calls within the same un-mutated
566 /// window are free (ref-count bump instead of a full-buffer join).
567 /// Invalidated by every [`mark_content_dirty`] call.
568 pub(super) cached_content: Option<std::sync::Arc<String>>,
569 /// Last rendered viewport height (text rows only, no chrome). Written
570 /// by the draw path via [`set_viewport_height`] so the scroll helpers
571 /// can clamp the cursor to stay visible without plumbing the height
572 /// through every call.
573 pub(super) viewport_height: AtomicU16,
574 /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
575 /// goto-definition). The host app drains this each step and fires
576 /// the matching request against its own LSP client.
577 pub(super) pending_lsp: Option<LspIntent>,
578 /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
579 /// the `:fold*` Ex commands, or the edit pipeline's
580 /// "edits-inside-a-fold open it" invalidation. Drained by hosts
581 /// via [`Editor::take_fold_ops`]; the engine also applies each op
582 /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
583 /// so the in-tree buffer fold storage stays in sync without host
584 /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
585 pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
586 /// Buffer storage.
587 ///
588 /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
589 /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
590 /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
591 /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
592 pub(super) buffer: B,
593 /// Style intern table for the migration buffer's opaque
594 /// `Span::style` ids. Phase 7d-ii-a wiring — `apply_window_spans`
595 /// produces `(start, end, Style)` tuples for the textarea; we
596 /// translate those to `hjkl_buffer::Span` by interning the
597 /// `Style` here and storing the table index. The render path's
598 /// `StyleResolver` looks the style back up by id.
599 ///
600 /// Behind the `ratatui` feature; non-ratatui hosts use the
601 /// engine-native [`crate::types::Style`] surface via
602 /// [`Editor::intern_engine_style`] (which lives on a parallel
603 /// engine-side table when ratatui is off).
604 #[cfg(feature = "ratatui")]
605 pub(super) style_table: Vec<ratatui::style::Style>,
606 /// Engine-native style intern table. Used directly by
607 /// [`Editor::intern_engine_style`] when the `ratatui` feature is
608 /// off; when it's on, the table is derived from `style_table` via
609 /// [`ratatui_style_to_engine`] / [`engine_style_to_ratatui`].
610 #[cfg(not(feature = "ratatui"))]
611 pub(super) engine_style_table: Vec<crate::types::Style>,
612 /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
613 /// every `p` / `P` via the active selector (default unnamed).
614 /// Internal — read via [`Editor::registers`]; mutated by yank /
615 /// delete / paste FSM paths and by [`Editor::seed_yank`].
616 pub(crate) registers: crate::registers::Registers,
617 /// Per-row syntax styling, kept here so the host can do
618 /// incremental window updates (see `apply_window_spans` in
619 /// the host). Same `(start_byte, end_byte, Style)` tuple shape
620 /// the textarea used to host. The Buffer-side opaque-id spans are
621 /// derived from this on every install. Behind the `ratatui`
622 /// feature.
623 #[cfg(feature = "ratatui")]
624 pub styled_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>,
625 /// Per-editor settings tweakable via `:set`. Exposed by reference
626 /// so handlers (indent, search) read the live value rather than a
627 /// snapshot taken at startup. Read via [`Editor::settings`];
628 /// mutate via [`Editor::settings_mut`].
629 pub(crate) settings: Settings,
630 /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
631 /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
632 /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
633 /// "file marks" that survive [`Editor::set_content`] calls so
634 /// they persist across tab swaps within the same Editor.
635 ///
636 /// 0.0.36: consolidated from three former storages:
637 /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
638 /// - `vim::VimState::marks` (lowercase) (deleted).
639 /// - `Editor::file_marks` (uppercase) (replaced by this map).
640 ///
641 /// `BTreeMap` so iteration is deterministic for snapshot tests
642 /// and the `:marks` ex command. Mark-shift on edits is handled
643 /// by [`Editor::shift_marks_after_edit`].
644 pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
645 /// Block ranges (`(start_row, end_row)` inclusive) the host has
646 /// extracted from a syntax tree. `:foldsyntax` reads these to
647 /// populate folds. The host refreshes them on every re-parse via
648 /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
649 /// [`Editor::syntax_fold_ranges`].
650 pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
651 /// Pending edit log drained by [`Editor::take_changes`]. Each entry
652 /// is a SPEC [`crate::types::Edit`] mapped from the underlying
653 /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
654 /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
655 /// best-effort EditOp covering the touched range; hosts wanting
656 /// per-cell deltas should diff their own snapshot of `lines()`.
657 /// Sealed at 0.1.0 trait extraction.
658 /// Drained by [`Editor::take_changes`].
659 pub(crate) change_log: Vec<crate::types::Edit>,
660 /// Vim's "sticky column" (curswant). `None` before the first
661 /// motion — the next vertical motion bootstraps from the live
662 /// cursor column. Horizontal motions refresh this to the new
663 /// column; vertical motions read it back so bouncing through a
664 /// shorter row doesn't drag the cursor to col 0. Hoisted out of
665 /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
666 /// the single owner now. Buffer motion methods that need it
667 /// take a `&mut Option<usize>` parameter.
668 pub(crate) sticky_col: Option<usize>,
669 /// Host adapter for clipboard, cursor-shape, time, viewport, and
670 /// search-prompt / cancellation side-channels.
671 ///
672 /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
673 /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
674 /// dyn-shim is gone — every method now dispatches through `H`'s
675 /// `Host` trait surface directly.
676 pub(crate) host: H,
677 /// Last public mode the cursor-shape emitter saw. Drives
678 /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
679 /// fires exactly once per mode transition without sprinkling the
680 /// call across every `vim.mode = ...` site.
681 pub(crate) last_emitted_mode: crate::VimMode,
682 /// Search FSM state (pattern + per-row match cache + wrapscan).
683 /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
684 /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
685 /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
686 /// gone; `BufferView` now takes the active regex as a `&Regex`
687 /// parameter, sourced from `Editor::search_state().pattern`.
688 pub(crate) search_state: crate::search::SearchState,
689 /// Per-row syntax span overlay. Source of truth for the host's
690 /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
691 /// [`Editor::install_syntax_spans`] /
692 /// [`Editor::install_ratatui_syntax_spans`] (and, in due course,
693 /// by `Host::syntax_highlights` once the engine drives that path
694 /// directly).
695 ///
696 /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
697 /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
698 /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
699 pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
700 /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
701 /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
702 /// tree (or any other content-change observer that needs byte-level
703 /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
704 pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
705 /// Pending "reset" flag set when the entire buffer is replaced
706 /// (e.g. `set_content` / `restore`). Supersedes any queued
707 /// `pending_content_edits` on the same frame: hosts call
708 /// [`Editor::take_content_reset`] before draining edits.
709 pub(crate) pending_content_reset: bool,
710 /// Row range touched by the most recent `auto_indent_rows` call.
711 /// `(top_row, bot_row)` inclusive. Set by the engine after every
712 /// auto-indent operation; drained (and cleared) by the host via
713 /// [`Editor::take_last_indent_range`] so it can display a brief
714 /// visual flash over the reindented rows.
715 pub(crate) last_indent_range: Option<(usize, usize)>,
716}
717
718/// Vim-style options surfaced by `:set`. New fields land here as
719/// individual ex commands gain `:set` plumbing.
720#[derive(Debug, Clone)]
721pub struct Settings {
722 /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
723 pub shiftwidth: usize,
724 /// Visual width of a `\t` character. Stored for future render
725 /// hookup; not yet consumed by the buffer renderer.
726 pub tabstop: usize,
727 /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
728 /// without an explicit `i` flag.
729 pub ignore_case: bool,
730 /// When true *and* `ignore_case` is true, an uppercase letter in
731 /// the pattern flips that search back to case-sensitive. Matches
732 /// vim's `:set smartcase`. Default `false`.
733 pub smartcase: bool,
734 /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
735 /// Default `true`.
736 pub wrapscan: bool,
737 /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
738 pub textwidth: usize,
739 /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
740 /// instead of a literal `\t`. Matches vim's `:set expandtab`.
741 /// Default `false`.
742 pub expandtab: bool,
743 /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
744 /// next softtabstop boundary (when `expandtab`), and Backspace at the
745 /// end of a softtabstop-aligned space run deletes the entire run as
746 /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
747 pub softtabstop: usize,
748 /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
749 /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
750 /// past the right edge and `top_col` clips the left side.
751 /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
752 /// to word-break wrap; `:set nowrap` resets.
753 pub wrap: hjkl_buffer::Wrap,
754 /// When true, the engine drops every edit before it touches the
755 /// buffer — undo, dirty flag, and change log all stay clean.
756 /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
757 pub readonly: bool,
758 /// When `true`, pressing Enter in insert mode copies the leading
759 /// whitespace of the current line onto the new line. Matches vim's
760 /// `:set autoindent`. Default `true` (vim parity).
761 pub autoindent: bool,
762 /// When `true`, bumps indent by one `shiftwidth` after a line ending
763 /// in `{` / `(` / `[`, and strips one indent unit when the user types
764 /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
765 /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
766 pub smartindent: bool,
767 /// Cap on undo-stack length. Older entries are pruned past this
768 /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
769 /// Default `1000`.
770 pub undo_levels: u32,
771 /// When `true`, cursor motions inside insert mode break the
772 /// current undo group (so a single `u` only reverses the run of
773 /// keystrokes that preceded the motion). Default `true`.
774 /// Currently a no-op — engine doesn't yet break the undo group
775 /// on insert-mode motions; field is wired through `:set
776 /// undobreak` for forward compatibility.
777 pub undo_break_on_motion: bool,
778 /// Vim-flavoured "what counts as a word" character class.
779 /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
780 /// `_`, `48-57` = decimal char range, bare integer = single char
781 /// code, single ASCII punctuation = literal. Default
782 /// `"@,48-57,_,192-255"` matches vim.
783 pub iskeyword: String,
784 /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
785 /// pauses longer than this between keys, any pending prefix is
786 /// abandoned and the next key starts a fresh sequence. Matches
787 /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
788 pub timeout_len: core::time::Duration,
789 /// When true, render absolute line numbers in the gutter. Matches
790 /// vim's `:set number` / `:set nu`. Default `true`.
791 pub number: bool,
792 /// When true, render line numbers as offsets from the cursor row.
793 /// Combined with `number`, the cursor row shows its absolute number
794 /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
795 /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
796 pub relativenumber: bool,
797 /// Minimum gutter width in cells for the line-number column.
798 /// Width grows past this to fit the largest displayed number.
799 /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
800 /// Range 1..=20.
801 pub numberwidth: usize,
802 /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
803 /// Default `false`.
804 pub cursorline: bool,
805 /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
806 /// Default `false`.
807 pub cursorcolumn: bool,
808 /// Sign-column display mode. Matches vim's `:set signcolumn`.
809 /// Default [`crate::types::SignColumnMode::Auto`].
810 pub signcolumn: crate::types::SignColumnMode,
811 /// Number of cells reserved for a fold-marker gutter.
812 /// Matches vim's `:set foldcolumn`. Default `0`.
813 pub foldcolumn: u32,
814 /// Comma-separated 1-based column indices for vertical rulers.
815 /// Matches vim's `:set colorcolumn`. Default `""`.
816 pub colorcolumn: String,
817}
818
819impl Default for Settings {
820 fn default() -> Self {
821 Self {
822 shiftwidth: 4,
823 tabstop: 4,
824 softtabstop: 4,
825 ignore_case: false,
826 smartcase: false,
827 wrapscan: true,
828 textwidth: 79,
829 expandtab: true,
830 wrap: hjkl_buffer::Wrap::None,
831 readonly: false,
832 autoindent: true,
833 smartindent: true,
834 undo_levels: 1000,
835 undo_break_on_motion: true,
836 iskeyword: "@,48-57,_,192-255".to_string(),
837 timeout_len: core::time::Duration::from_millis(1000),
838 number: true,
839 relativenumber: false,
840 numberwidth: 4,
841 cursorline: false,
842 cursorcolumn: false,
843 signcolumn: crate::types::SignColumnMode::Auto,
844 foldcolumn: 0,
845 colorcolumn: String::new(),
846 }
847 }
848}
849
850/// Translate a SPEC [`crate::types::Options`] into the engine's
851/// internal [`Settings`] representation. Field-by-field map; the
852/// shapes are isomorphic except for type widths
853/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
854/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
855/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
856/// is the canonical entry point.
857fn settings_from_options(o: &crate::types::Options) -> Settings {
858 Settings {
859 shiftwidth: o.shiftwidth as usize,
860 tabstop: o.tabstop as usize,
861 softtabstop: o.softtabstop as usize,
862 ignore_case: o.ignorecase,
863 smartcase: o.smartcase,
864 wrapscan: o.wrapscan,
865 textwidth: o.textwidth as usize,
866 expandtab: o.expandtab,
867 wrap: match o.wrap {
868 crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
869 crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
870 crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
871 },
872 readonly: o.readonly,
873 autoindent: o.autoindent,
874 smartindent: o.smartindent,
875 undo_levels: o.undo_levels,
876 undo_break_on_motion: o.undo_break_on_motion,
877 iskeyword: o.iskeyword.clone(),
878 timeout_len: o.timeout_len,
879 number: o.number,
880 relativenumber: o.relativenumber,
881 numberwidth: o.numberwidth,
882 cursorline: o.cursorline,
883 cursorcolumn: o.cursorcolumn,
884 signcolumn: o.signcolumn,
885 foldcolumn: o.foldcolumn,
886 colorcolumn: o.colorcolumn.clone(),
887 }
888}
889
890/// Host-observable LSP requests triggered by editor bindings. The
891/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
892/// intent that the TUI layer picks up and routes to `sqls`.
893#[derive(Debug, Clone, Copy, PartialEq, Eq)]
894pub enum LspIntent {
895 /// `gd` — textDocument/definition at the cursor.
896 GotoDefinition,
897}
898
899impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
900 /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
901 ///
902 /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
903 /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
904 /// `with_host` / `with_options` triad — there is no shim.
905 ///
906 /// Consumers that don't need a custom host pass
907 /// [`crate::types::DefaultHost::new()`]; consumers that don't need
908 /// custom options pass [`crate::types::Options::default()`].
909 pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
910 let settings = settings_from_options(&options);
911 Self {
912 keybinding_mode: KeybindingMode::Vim,
913 last_yank: None,
914 vim: VimState::default(),
915 undo_stack: Vec::new(),
916 redo_stack: Vec::new(),
917 content_dirty: false,
918 cached_content: None,
919 viewport_height: AtomicU16::new(0),
920 pending_lsp: None,
921 pending_fold_ops: Vec::new(),
922 buffer,
923 #[cfg(feature = "ratatui")]
924 style_table: Vec::new(),
925 #[cfg(not(feature = "ratatui"))]
926 engine_style_table: Vec::new(),
927 registers: crate::registers::Registers::default(),
928 #[cfg(feature = "ratatui")]
929 styled_spans: Vec::new(),
930 settings,
931 marks: std::collections::BTreeMap::new(),
932 syntax_fold_ranges: Vec::new(),
933 change_log: Vec::new(),
934 sticky_col: None,
935 host,
936 last_emitted_mode: crate::VimMode::Normal,
937 search_state: crate::search::SearchState::new(),
938 buffer_spans: Vec::new(),
939 pending_content_edits: Vec::new(),
940 pending_content_reset: false,
941 last_indent_range: None,
942 }
943 }
944}
945
946impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
947 /// Borrow the buffer (typed `&B`). Host renders through this via
948 /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
949 pub fn buffer(&self) -> &B {
950 &self.buffer
951 }
952
953 /// Mutably borrow the buffer (typed `&mut B`).
954 pub fn buffer_mut(&mut self) -> &mut B {
955 &mut self.buffer
956 }
957
958 /// Borrow the host adapter directly (typed `&H`).
959 pub fn host(&self) -> &H {
960 &self.host
961 }
962
963 /// Mutably borrow the host adapter (typed `&mut H`).
964 pub fn host_mut(&mut self) -> &mut H {
965 &mut self.host
966 }
967}
968
969impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
970 /// Update the active `iskeyword` spec for word motions
971 /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
972 /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
973 /// single owner now. Equivalent to assigning
974 /// `settings_mut().iskeyword` directly; the dedicated setter is
975 /// retained for source-compatibility with 0.0.27 callers.
976 pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
977 self.settings.iskeyword = spec.into();
978 }
979
980 /// Emit `Host::emit_cursor_shape` if the public mode has changed
981 /// since the last emit. Engine calls this at the end of every input
982 /// step so mode transitions surface to the host without sprinkling
983 /// the call across every `vim.mode = ...` site.
984 pub fn emit_cursor_shape_if_changed(&mut self) {
985 let mode = self.vim_mode();
986 if mode == self.last_emitted_mode {
987 return;
988 }
989 let shape = match mode {
990 crate::VimMode::Insert => crate::types::CursorShape::Bar,
991 _ => crate::types::CursorShape::Block,
992 };
993 self.host.emit_cursor_shape(shape);
994 self.last_emitted_mode = mode;
995 }
996
997 /// Record a yank/cut payload. Writes both the legacy
998 /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
999 /// hosts) and the new [`crate::types::Host::write_clipboard`]
1000 /// side-channel (Patch B). Consumers should migrate to a `Host`
1001 /// impl whose `write_clipboard` queues the platform-clipboard
1002 /// write; the `last_yank` mirror will be removed at 0.1.0.
1003 pub(crate) fn record_yank_to_host(&mut self, text: String) {
1004 self.host.write_clipboard(text.clone());
1005 self.last_yank = Some(text);
1006 }
1007
1008 /// Vim's sticky column (curswant). `None` before the first motion;
1009 /// hosts shouldn't normally need to read this directly — it's
1010 /// surfaced for migration off `Buffer::sticky_col` and for
1011 /// snapshot tests.
1012 pub fn sticky_col(&self) -> Option<usize> {
1013 self.sticky_col
1014 }
1015
1016 /// Replace the sticky column. Hosts should rarely touch this —
1017 /// motion code maintains it through the standard horizontal /
1018 /// vertical motion paths.
1019 pub fn set_sticky_col(&mut self, col: Option<usize>) {
1020 self.sticky_col = col;
1021 }
1022
1023 /// Host hook: replace the cached syntax-derived block ranges that
1024 /// `:foldsyntax` consumes. the host calls this on every re-parse;
1025 /// the cost is just a `Vec` swap.
1026 /// Look up a named mark by character. Returns `(row, col)` if
1027 /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
1028 /// uppercase (`'A`–`'Z`) marks live in the same unified
1029 /// [`Editor::marks`] map as of 0.0.36.
1030 pub fn mark(&self, c: char) -> Option<(usize, usize)> {
1031 self.marks.get(&c).copied()
1032 }
1033
1034 /// Set the named mark `c` to `(row, col)`. Used by the FSM's
1035 /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
1036 pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
1037 self.marks.insert(c, pos);
1038 }
1039
1040 /// Remove the named mark `c` (no-op if unset).
1041 pub fn clear_mark(&mut self, c: char) {
1042 self.marks.remove(&c);
1043 }
1044
1045 /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
1046 /// thin wrapper over [`Editor::mark`] for source compatibility
1047 /// with pre-0.0.36 callers; new code should call
1048 /// [`Editor::mark`] directly.
1049 #[deprecated(
1050 since = "0.0.36",
1051 note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
1052 )]
1053 pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
1054 self.mark(c)
1055 }
1056
1057 /// Discard the most recent undo entry. Used by ex commands that
1058 /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
1059 /// matching nothing — popping prevents a no-op undo step from
1060 /// polluting the user's history.
1061 ///
1062 /// Returns `true` if an entry was discarded.
1063 pub fn pop_last_undo(&mut self) -> bool {
1064 self.undo_stack.pop().is_some()
1065 }
1066
1067 /// Read all named marks set this session — both lowercase
1068 /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
1069 /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
1070 /// output is stable.
1071 pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1072 self.marks.iter().map(|(c, p)| (*c, *p))
1073 }
1074
1075 /// Read all buffer-local lowercase marks. Kept for source
1076 /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
1077 /// command); new code should use [`Editor::marks`] which
1078 /// iterates the unified map.
1079 #[deprecated(
1080 since = "0.0.36",
1081 note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
1082 )]
1083 pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1084 self.marks
1085 .iter()
1086 .filter(|(c, _)| c.is_ascii_lowercase())
1087 .map(|(c, p)| (*c, *p))
1088 }
1089
1090 /// Position the cursor was at when the user last jumped via
1091 /// `<C-o>` / `g;` / similar. `None` before any jump.
1092 pub fn last_jump_back(&self) -> Option<(usize, usize)> {
1093 self.vim.jump_back.last().copied()
1094 }
1095
1096 /// Position of the last edit (where `.` would replay). `None` if
1097 /// no edit has happened yet in this session.
1098 pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
1099 self.vim.last_edit_pos
1100 }
1101
1102 /// Read-only view of the file-marks table — uppercase / "file"
1103 /// marks (`'A`–`'Z`) the host has set this session. Returns an
1104 /// iterator of `(mark_char, (row, col))` pairs.
1105 ///
1106 /// Mutate via the FSM (`m{A-Z}` keystroke) or via
1107 /// [`Editor::restore_snapshot`].
1108 ///
1109 /// 0.0.36: file marks now live in the unified [`Editor::marks`]
1110 /// map; this accessor is kept for source compatibility and
1111 /// filters the unified map to uppercase entries.
1112 pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1113 self.marks
1114 .iter()
1115 .filter(|(c, _)| c.is_ascii_uppercase())
1116 .map(|(c, p)| (*c, *p))
1117 }
1118
1119 /// Read-only view of the cached syntax-derived block ranges that
1120 /// `:foldsyntax` consumes. Returns the slice the host last
1121 /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1122 /// no syntax integration is active.
1123 pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1124 &self.syntax_fold_ranges
1125 }
1126
1127 pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1128 self.syntax_fold_ranges = ranges;
1129 }
1130
1131 /// Live settings (read-only). `:set` mutates these via
1132 /// [`Editor::settings_mut`].
1133 pub fn settings(&self) -> &Settings {
1134 &self.settings
1135 }
1136
1137 /// Live settings (mutable). `:set` flows through here to mutate
1138 /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1139 /// configuring at startup typically construct a [`Settings`]
1140 /// snapshot and overwrite via `*editor.settings_mut() = …`.
1141 pub fn settings_mut(&mut self) -> &mut Settings {
1142 &mut self.settings
1143 }
1144
1145 /// Returns `true` when `:set readonly` is active. Convenience
1146 /// accessor for hosts that cannot import the internal [`Settings`]
1147 /// type. Phase 5 binary uses this to gate `:w` writes.
1148 pub fn is_readonly(&self) -> bool {
1149 self.settings.readonly
1150 }
1151
1152 /// Borrow the engine search state. Hosts inspecting the
1153 /// committed `/` / `?` pattern (e.g. for status-line display) or
1154 /// feeding the active regex into `BufferView::search_pattern`
1155 /// read it from here.
1156 pub fn search_state(&self) -> &crate::search::SearchState {
1157 &self.search_state
1158 }
1159
1160 /// Mutable engine search state. Hosts driving search
1161 /// programmatically (test fixtures, scripted demos) write the
1162 /// pattern through here.
1163 pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1164 &mut self.search_state
1165 }
1166
1167 /// Install `pattern` as the active search regex on the engine
1168 /// state and clear the cached row matches. Pass `None` to clear.
1169 /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1170 /// — `BufferView` now takes the regex through its `search_pattern`
1171 /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1172 pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1173 self.search_state.set_pattern(pattern);
1174 }
1175
1176 /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1177 /// to the next match of `search_state.pattern` from the cursor's
1178 /// current position. Returns `true` when a match was found.
1179 /// `skip_current = true` excludes a match the cursor sits on.
1180 pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1181 crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1182 }
1183
1184 /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1185 pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1186 crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1187 }
1188
1189 /// Install styled syntax spans using `ratatui::style::Style`. The
1190 /// ratatui-flavoured variant of [`Editor::install_syntax_spans`].
1191 /// Drops zero-width runs and clamps `end` to the line's char length
1192 /// so the buffer cache doesn't see runaway ranges. Behind the
1193 /// `ratatui` feature; non-ratatui hosts use the unprefixed
1194 /// [`Editor::install_syntax_spans`] (engine-native `Style`).
1195 ///
1196 /// Renamed from `install_syntax_spans` in 0.0.32 — the unprefixed
1197 /// name now belongs to the engine-native variant per SPEC 0.1.0
1198 /// freeze ("engine never imports ratatui").
1199 #[cfg(feature = "ratatui")]
1200 pub fn install_ratatui_syntax_spans(
1201 &mut self,
1202 spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>,
1203 ) {
1204 // Look up `line_byte_lens` lazily — only fetch a row's length
1205 // when it has at least one span. On a 100k-line file with
1206 // ~50 visible rows, this avoids an O(N) buffer walk per frame.
1207 let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1208 for (row, row_spans) in spans.iter().enumerate() {
1209 if row_spans.is_empty() {
1210 by_row.push(Vec::new());
1211 continue;
1212 }
1213 let line_len = buf_line(&self.buffer, row).map(str::len).unwrap_or(0);
1214 let mut translated = Vec::with_capacity(row_spans.len());
1215 for (start, end, style) in row_spans {
1216 let end_clamped = (*end).min(line_len);
1217 if end_clamped <= *start {
1218 continue;
1219 }
1220 let id = self.intern_ratatui_style(*style);
1221 translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1222 }
1223 by_row.push(translated);
1224 }
1225 self.buffer_spans = by_row;
1226 self.styled_spans = spans;
1227 }
1228
1229 /// Snapshot of the unnamed register (the default `p` / `P` source).
1230 pub fn yank(&self) -> &str {
1231 &self.registers.unnamed.text
1232 }
1233
1234 /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1235 pub fn registers(&self) -> &crate::registers::Registers {
1236 &self.registers
1237 }
1238
1239 /// Mutably borrow the full register bank. Hosts that share registers
1240 /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1241 /// the slots here on buffer switch.
1242 pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1243 &mut self.registers
1244 }
1245
1246 /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1247 /// register slot. the host calls this before letting vim consume a
1248 /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1249 /// stale snapshot from the last yank.
1250 pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1251 self.registers.set_clipboard(text, linewise);
1252 }
1253
1254 /// Return the user's pending register selection (set via `"<reg>` chord
1255 /// before an operator). `None` if no register was selected — caller should
1256 /// use the unnamed register `"`.
1257 ///
1258 /// Read-only — does not consume / clear the pending selection. The
1259 /// register is cleared by the engine after the next operator fires.
1260 ///
1261 /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1262 /// honor `"a` + visual op chord sequences.
1263 pub fn pending_register(&self) -> Option<char> {
1264 self.vim.pending_register
1265 }
1266
1267 /// True when the user's pending register selector is `+` or `*`.
1268 /// the host peeks this so it can refresh `sync_clipboard_register`
1269 /// only when a clipboard read is actually about to happen.
1270 pub fn pending_register_is_clipboard(&self) -> bool {
1271 matches!(self.vim.pending_register, Some('+') | Some('*'))
1272 }
1273
1274 /// Register currently being recorded into via `q{reg}`. `None` when
1275 /// no recording is active. Hosts use this to surface a "recording @r"
1276 /// indicator in the status line.
1277 pub fn recording_register(&self) -> Option<char> {
1278 self.vim.recording_macro
1279 }
1280
1281 /// Pending repeat count the user has typed but not yet resolved
1282 /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1283 /// Hosts surface this in a "showcmd" area.
1284 pub fn pending_count(&self) -> Option<u32> {
1285 self.vim.pending_count_val()
1286 }
1287
1288 /// The operator character for any in-flight operator that is waiting
1289 /// for a motion (e.g. `d` after the user types `d` but before a
1290 /// motion). Returns `None` when no operator is pending.
1291 pub fn pending_op(&self) -> Option<char> {
1292 self.vim.pending_op_char()
1293 }
1294
1295 /// `true` when the engine is in any pending chord state — waiting for
1296 /// the next key to complete a command (e.g. `r<char>` replace,
1297 /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1298 /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1299 /// register selection `"<reg>`, macro recording target, etc).
1300 ///
1301 /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1302 /// and forward keys directly to the engine so in-flight commands can
1303 /// complete without the host eating their continuation keys.
1304 pub fn is_chord_pending(&self) -> bool {
1305 self.vim.is_chord_pending()
1306 }
1307
1308 /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1309 /// is waiting for the next typed character to name the register to paste.
1310 /// The dispatcher should call `insert_paste_register(c)` instead of
1311 /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1312 ///
1313 /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1314 /// without having to drive the full FSM.
1315 pub fn is_insert_register_pending(&self) -> bool {
1316 self.vim.insert_pending_register
1317 }
1318
1319 /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1320 /// before `insert_paste_register(c)` in app-level dispatchers so that the
1321 /// flag does not persist into the next key. Call before
1322 /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1323 ///
1324 /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1325 pub fn clear_insert_register_pending(&mut self) {
1326 self.vim.insert_pending_register = false;
1327 }
1328
1329 /// Read-only view of the jump-back list (positions pushed on "big"
1330 /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1331 #[allow(clippy::type_complexity)]
1332 pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1333 (&self.vim.jump_back, &self.vim.jump_fwd)
1334 }
1335
1336 /// Read-only view of the change list (positions of recent edits) plus
1337 /// the current walk cursor. Newest entry is at the back.
1338 pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1339 (&self.vim.change_list, self.vim.change_list_cursor)
1340 }
1341
1342 /// Replace the unnamed register without touching any other slot.
1343 /// For host-driven imports (e.g. system clipboard); operator
1344 /// code uses [`record_yank`] / [`record_delete`].
1345 pub fn set_yank(&mut self, text: impl Into<String>) {
1346 let text = text.into();
1347 let linewise = self.vim.yank_linewise;
1348 self.registers.unnamed = crate::registers::Slot { text, linewise };
1349 }
1350
1351 /// Record a yank into `"` and `"0`, plus the named target if the
1352 /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1353 /// paste path.
1354 pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1355 self.vim.yank_linewise = linewise;
1356 let target = self.vim.pending_register.take();
1357 self.registers.record_yank(text, linewise, target);
1358 }
1359
1360 /// Direct write to a named register slot — bypasses the unnamed
1361 /// `"` and `"0` updates that `record_yank` does. Used by the
1362 /// macro recorder so finishing a `q{reg}` recording doesn't
1363 /// pollute the user's last yank.
1364 pub(crate) fn set_named_register_text(&mut self, reg: char, text: String) {
1365 if let Some(slot) = match reg {
1366 'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1367 'A'..='Z' => {
1368 Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1369 }
1370 _ => None,
1371 } {
1372 slot.text = text;
1373 slot.linewise = false;
1374 }
1375 }
1376
1377 /// Record a delete / change into `"` and the `"1`–`"9` ring.
1378 /// Honours the active named-register prefix.
1379 pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1380 self.vim.yank_linewise = linewise;
1381 let target = self.vim.pending_register.take();
1382 self.registers.record_delete(text, linewise, target);
1383 }
1384
1385 /// Install styled syntax spans using the engine-native
1386 /// [`crate::types::Style`]. Always available, regardless of the
1387 /// `ratatui` feature. Hosts depending on ratatui can use the
1388 /// ratatui-flavoured [`Editor::install_ratatui_syntax_spans`].
1389 ///
1390 /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1391 /// 0.1.0 freeze the unprefixed name is the universally-available
1392 /// engine-native variant ("engine never imports ratatui").
1393 pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1394 let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1395 .map(|r| buf_line(&self.buffer, r).map(str::len).unwrap_or(0))
1396 .collect();
1397 let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1398 #[cfg(feature = "ratatui")]
1399 let mut ratatui_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>> =
1400 Vec::with_capacity(spans.len());
1401 for (row, row_spans) in spans.iter().enumerate() {
1402 let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1403 let mut translated = Vec::with_capacity(row_spans.len());
1404 #[cfg(feature = "ratatui")]
1405 let mut translated_r = Vec::with_capacity(row_spans.len());
1406 for (start, end, style) in row_spans {
1407 let end_clamped = (*end).min(line_len);
1408 if end_clamped <= *start {
1409 continue;
1410 }
1411 let id = self.intern_style(*style);
1412 translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1413 #[cfg(feature = "ratatui")]
1414 translated_r.push((*start, end_clamped, engine_style_to_ratatui(*style)));
1415 }
1416 by_row.push(translated);
1417 #[cfg(feature = "ratatui")]
1418 ratatui_spans.push(translated_r);
1419 }
1420 self.buffer_spans = by_row;
1421 #[cfg(feature = "ratatui")]
1422 {
1423 self.styled_spans = ratatui_spans;
1424 }
1425 }
1426
1427 /// Intern a `ratatui::style::Style` and return the opaque id used
1428 /// in `hjkl_buffer::Span::style`. The ratatui-flavoured variant of
1429 /// [`Editor::intern_style`]. Linear-scan dedup — the table grows
1430 /// only as new tree-sitter token kinds appear, so it stays tiny.
1431 /// Behind the `ratatui` feature.
1432 ///
1433 /// Renamed from `intern_style` in 0.0.32 — at 0.1.0 freeze the
1434 /// unprefixed name belongs to the engine-native variant.
1435 #[cfg(feature = "ratatui")]
1436 pub fn intern_ratatui_style(&mut self, style: ratatui::style::Style) -> u32 {
1437 if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1438 return idx as u32;
1439 }
1440 self.style_table.push(style);
1441 (self.style_table.len() - 1) as u32
1442 }
1443
1444 /// Read-only view of the style table — id `i` → `style_table[i]`.
1445 /// The render path passes a closure backed by this slice as the
1446 /// `StyleResolver` for `BufferView`. Behind the `ratatui` feature.
1447 #[cfg(feature = "ratatui")]
1448 pub fn style_table(&self) -> &[ratatui::style::Style] {
1449 &self.style_table
1450 }
1451
1452 /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1453 /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1454 /// per draw frame.
1455 ///
1456 /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1457 /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1458 /// caches spans; they live on the engine and route through the
1459 /// `Host::syntax_highlights` pipeline.
1460 pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1461 &self.buffer_spans
1462 }
1463
1464 /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1465 /// With the `ratatui` feature on, the id matches the one
1466 /// [`Editor::intern_ratatui_style`] would return for the equivalent
1467 /// `ratatui::Style` (both share the underlying table). With it off,
1468 /// the engine keeps a parallel `crate::types::Style`-keyed table
1469 /// — ids are still stable per-editor.
1470 ///
1471 /// Hosts that don't depend on ratatui (buffr, future GUI shells)
1472 /// reach this method to populate the table during syntax span
1473 /// installation.
1474 ///
1475 /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1476 /// the unprefixed name is the universally-available engine-native
1477 /// variant.
1478 pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1479 #[cfg(feature = "ratatui")]
1480 {
1481 let r = engine_style_to_ratatui(style);
1482 self.intern_ratatui_style(r)
1483 }
1484 #[cfg(not(feature = "ratatui"))]
1485 {
1486 if let Some(idx) = self.engine_style_table.iter().position(|s| *s == style) {
1487 return idx as u32;
1488 }
1489 self.engine_style_table.push(style);
1490 (self.engine_style_table.len() - 1) as u32
1491 }
1492 }
1493
1494 /// Look up an interned style by id and return it as a SPEC
1495 /// [`crate::types::Style`]. Returns `None` for ids past the end
1496 /// of the table.
1497 pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1498 #[cfg(feature = "ratatui")]
1499 {
1500 let r = self.style_table.get(id as usize).copied()?;
1501 Some(ratatui_style_to_engine(r))
1502 }
1503 #[cfg(not(feature = "ratatui"))]
1504 {
1505 self.engine_style_table.get(id as usize).copied()
1506 }
1507 }
1508
1509 /// Historical reverse-sync hook from when the textarea mirrored
1510 /// the buffer. Now that Buffer is the cursor authority this is a
1511 /// no-op; call sites can remain in place during the migration.
1512 pub fn push_buffer_cursor_to_textarea(&mut self) {}
1513
1514 /// Force the host viewport's top row without touching the
1515 /// cursor. Used by tests that simulate a scroll without the
1516 /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1517 /// apply.
1518 ///
1519 /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1520 /// instead of the (now-deleted) `Buffer::viewport_mut`.
1521 pub fn set_viewport_top(&mut self, row: usize) {
1522 let last = buf_row_count(&self.buffer).saturating_sub(1);
1523 let target = row.min(last);
1524 self.host.viewport_mut().top_row = target;
1525 }
1526
1527 /// Set the cursor to `(row, col)`, clamped to the buffer's
1528 /// content. Hosts use this for goto-line, jump-to-mark, and
1529 /// programmatic cursor placement.
1530 ///
1531 /// Resets `sticky_col` (curswant) to `col` — every explicit jump
1532 /// (goto-line, jump-to-mark, search hit, click, `]d`) follows vim
1533 /// semantics. Only `j`/`k`/`+`/`-` READ `sticky_col`; everything
1534 /// else resets it to the column where the cursor actually landed.
1535 pub fn jump_cursor(&mut self, row: usize, col: usize) {
1536 buf_set_cursor_rc(&mut self.buffer, row, col);
1537 self.sticky_col = Some(col);
1538 }
1539
1540 /// Set the cursor to `(row, col)` without modifying `sticky_col`.
1541 ///
1542 /// Use this for host-side state restores (viewport sync, snapshot
1543 /// replay) where the cursor was already at this position semantically
1544 /// and the host's sticky tracking should remain authoritative.
1545 ///
1546 /// For user-facing jumps (goto-line, search hit, picker `<CR>`, `]d`,
1547 /// click), use [`Editor::jump_cursor`] which DOES reset `sticky_col`
1548 /// per vim curswant semantics.
1549 pub fn set_cursor_quiet(&mut self, row: usize, col: usize) {
1550 buf_set_cursor_rc(&mut self.buffer, row, col);
1551 }
1552
1553 /// `(row, col)` cursor read sourced from the migration buffer.
1554 /// Equivalent to `self.textarea.cursor()` when the two are in
1555 /// sync — which is the steady state during Phase 7f because
1556 /// every step opens with `sync_buffer_content_from_textarea` and
1557 /// every ported motion pushes the result back. Prefer this over
1558 /// `self.textarea.cursor()` so call sites keep working unchanged
1559 /// once the textarea field is ripped.
1560 pub fn cursor(&self) -> (usize, usize) {
1561 buf_cursor_rc(&self.buffer)
1562 }
1563
1564 /// Drain any pending LSP intent raised by the last key. Returns
1565 /// `None` when no intent is armed.
1566 pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1567 self.pending_lsp.take()
1568 }
1569
1570 /// Drain every [`crate::types::FoldOp`] raised since the last
1571 /// call. Hosts that mirror the engine's fold storage (or that
1572 /// project folds onto a separate fold tree, LSP folding ranges,
1573 /// …) drain this each step and dispatch as their own
1574 /// [`crate::types::Host::Intent`] requires.
1575 ///
1576 /// The engine has already applied every op locally against the
1577 /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1578 /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1579 /// don't track folds independently can ignore the queue
1580 /// (or simply never call this drain).
1581 ///
1582 /// Introduced in 0.0.38 (Patch C-δ.4).
1583 pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1584 std::mem::take(&mut self.pending_fold_ops)
1585 }
1586
1587 /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1588 /// surface: queue it for host observation (drained by
1589 /// [`Editor::take_fold_ops`]) and apply it locally against the
1590 /// in-tree buffer fold storage via
1591 /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1592 /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1593 /// invalidation) route every fold mutation through this method.
1594 ///
1595 /// Introduced in 0.0.38 (Patch C-δ.4).
1596 pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1597 use crate::types::FoldProvider;
1598 self.pending_fold_ops.push(op);
1599 let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1600 provider.apply(op);
1601 }
1602
1603 /// Refresh the host viewport's height from the cached
1604 /// `viewport_height_value()`. Called from the per-step
1605 /// boilerplate; was the textarea → buffer mirror before Phase 7f
1606 /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1607 /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1608 /// `Host::viewport_mut`.
1609 pub(crate) fn sync_buffer_from_textarea(&mut self) {
1610 let height = self.viewport_height_value();
1611 self.host.viewport_mut().height = height;
1612 }
1613
1614 /// Was the full textarea → buffer content sync. Buffer is the
1615 /// content authority now; this remains as a no-op so the per-step
1616 /// call sites don't have to be ripped in the same patch.
1617 pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1618 self.sync_buffer_from_textarea();
1619 }
1620
1621 /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1622 /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1623 /// the cursor without going through the vim engine's motion
1624 /// machinery, where push_jump fires automatically.
1625 pub fn record_jump(&mut self, pos: (usize, usize)) {
1626 const JUMPLIST_MAX: usize = 100;
1627 self.vim.jump_back.push(pos);
1628 if self.vim.jump_back.len() > JUMPLIST_MAX {
1629 self.vim.jump_back.remove(0);
1630 }
1631 self.vim.jump_fwd.clear();
1632 }
1633
1634 /// Host apps call this each draw with the current text area height so
1635 /// scroll helpers can clamp the cursor without recomputing layout.
1636 pub fn set_viewport_height(&self, height: u16) {
1637 self.viewport_height.store(height, Ordering::Relaxed);
1638 }
1639
1640 /// Last height published by `set_viewport_height` (in rows).
1641 pub fn viewport_height_value(&self) -> u16 {
1642 self.viewport_height.load(Ordering::Relaxed)
1643 }
1644
1645 /// Apply `edit` against the buffer and return the inverse so the
1646 /// host can push it onto an undo stack. Side effects: dirty
1647 /// flag, change-list ring, mark / jump-list shifts, change_log
1648 /// append, fold invalidation around the touched rows.
1649 ///
1650 /// The primary edit funnel — both FSM operators and ex commands
1651 /// route mutations through here so the side effects fire
1652 /// uniformly.
1653 pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1654 // `:set readonly` short-circuits every mutation funnel: no
1655 // buffer change, no dirty flag, no undo entry, no change-log
1656 // emission. We swallow the requested `edit` and hand back a
1657 // self-inverse no-op (`InsertStr` of an empty string at the
1658 // current cursor) so callers that push the return value onto
1659 // an undo stack still get a structurally valid round trip.
1660 if self.settings.readonly {
1661 let _ = edit;
1662 return hjkl_buffer::Edit::InsertStr {
1663 at: buf_cursor_pos(&self.buffer),
1664 text: String::new(),
1665 };
1666 }
1667 let pre_row = buf_cursor_row(&self.buffer);
1668 let pre_rows = buf_row_count(&self.buffer);
1669 // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1670 // Vim's `:h '.` says "the position where the last change was made",
1671 // meaning the change-start, not the post-insert cursor. We snap it
1672 // here before `apply_buffer_edit` moves the cursor.
1673 let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1674 // Map the underlying buffer edit to a SPEC EditOp for
1675 // change-log emission before consuming it. Coarse — see
1676 // change_log field doc on the struct.
1677 self.change_log.extend(edit_to_editops(&edit));
1678 // Compute ContentEdit fan-out from the pre-edit buffer state.
1679 // Done before `apply_buffer_edit` consumes `edit` so we can
1680 // inspect the operation's fields and the buffer's pre-edit row
1681 // bytes (needed for byte_of_row / col_byte conversion). Edits
1682 // are pushed onto `pending_content_edits` for host drain.
1683 let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1684 self.pending_content_edits.extend(content_edits);
1685 // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1686 // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1687 // the 0.0.42 plan — see that fn's doc comment). The free fn
1688 // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1689 // no longer carries a `self.buffer.<inherent>` hop.
1690 let inverse = apply_buffer_edit(&mut self.buffer, edit);
1691 let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1692 // Drop any folds the edit's range overlapped — vim opens the
1693 // surrounding fold automatically when you edit inside it. The
1694 // approximation here invalidates folds covering either the
1695 // pre-edit cursor row or the post-edit cursor row, which
1696 // catches the common single-line / multi-line edit shapes.
1697 let lo = pre_row.min(pos_row);
1698 let hi = pre_row.max(pos_row);
1699 self.apply_fold_op(crate::types::FoldOp::Invalidate {
1700 start_row: lo,
1701 end_row: hi,
1702 });
1703 // Dot mark records the PRE-edit position (change start), matching
1704 // vim's `:h '.` semantics. Previously this stored the post-edit
1705 // cursor, which diverged from nvim on `iX<Esc>j`.
1706 self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1707 // Append to the change-list ring (skip when the cursor sits on
1708 // the same cell as the last entry — back-to-back keystrokes on
1709 // one column shouldn't pollute the ring). A new edit while
1710 // walking the ring trims the forward half, vim style.
1711 let entry = (pos_row, pos_col);
1712 if self.vim.change_list.last() != Some(&entry) {
1713 if let Some(idx) = self.vim.change_list_cursor.take() {
1714 self.vim.change_list.truncate(idx + 1);
1715 }
1716 self.vim.change_list.push(entry);
1717 let len = self.vim.change_list.len();
1718 if len > crate::vim::CHANGE_LIST_MAX {
1719 self.vim
1720 .change_list
1721 .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1722 }
1723 }
1724 self.vim.change_list_cursor = None;
1725 // Shift / drop marks + jump-list entries to track the row
1726 // delta the edit produced. Without this, every line-changing
1727 // edit silently invalidates `'a`-style positions.
1728 let post_rows = buf_row_count(&self.buffer);
1729 let delta = post_rows as isize - pre_rows as isize;
1730 if delta != 0 {
1731 self.shift_marks_after_edit(pre_row, delta);
1732 }
1733 self.push_buffer_content_to_textarea();
1734 self.mark_content_dirty();
1735 inverse
1736 }
1737
1738 /// Migrate user marks + jumplist entries when an edit at row
1739 /// `edit_start` changes the buffer's row count by `delta` (positive
1740 /// for inserts, negative for deletes). Marks tied to a deleted row
1741 /// are dropped; marks past the affected band shift by `delta`.
1742 fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1743 if delta == 0 {
1744 return;
1745 }
1746 // Deleted-row band (only meaningful for delta < 0). Inclusive
1747 // start, exclusive end.
1748 let drop_end = if delta < 0 {
1749 edit_start.saturating_add((-delta) as usize)
1750 } else {
1751 edit_start
1752 };
1753 let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1754
1755 // 0.0.36: lowercase + uppercase marks share the unified
1756 // `marks` map; one pass migrates both.
1757 let mut to_drop: Vec<char> = Vec::new();
1758 for (c, (row, _col)) in self.marks.iter_mut() {
1759 if (edit_start..drop_end).contains(row) {
1760 to_drop.push(*c);
1761 } else if *row >= shift_threshold {
1762 *row = ((*row as isize) + delta).max(0) as usize;
1763 }
1764 }
1765 for c in to_drop {
1766 self.marks.remove(&c);
1767 }
1768
1769 let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1770 entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1771 for (row, _) in entries.iter_mut() {
1772 if *row >= shift_threshold {
1773 *row = ((*row as isize) + delta).max(0) as usize;
1774 }
1775 }
1776 };
1777 shift_jumps(&mut self.vim.jump_back);
1778 shift_jumps(&mut self.vim.jump_fwd);
1779 }
1780
1781 /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1782 /// the textarea from the buffer's lines + cursor, preserving yank
1783 /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1784 /// textarea field disappears at the end of Phase 7f anyway.
1785 /// No-op since Buffer is the content authority. Retained as a
1786 /// shim so call sites in `mutate_edit` and friends don't have to
1787 /// be ripped in lockstep with the field removal.
1788 pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1789
1790 /// Single choke-point for "the buffer just changed". Sets the
1791 /// dirty flag and drops the cached `content_arc` snapshot so
1792 /// subsequent reads rebuild from the live textarea. Callers
1793 /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1794 /// path) must invoke this to keep the cache honest.
1795 pub fn mark_content_dirty(&mut self) {
1796 self.content_dirty = true;
1797 self.cached_content = None;
1798 }
1799
1800 /// Returns true if content changed since the last call, then clears the flag.
1801 pub fn take_dirty(&mut self) -> bool {
1802 let dirty = self.content_dirty;
1803 self.content_dirty = false;
1804 dirty
1805 }
1806
1807 /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1808 /// the last call. Each entry corresponds to a single buffer
1809 /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1810 /// fan out to one entry per row touched.
1811 ///
1812 /// Hosts call this each frame (after [`Editor::take_content_reset`])
1813 /// to fan edits into a tree-sitter parser via `Tree::edit`.
1814 pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1815 std::mem::take(&mut self.pending_content_edits)
1816 }
1817
1818 /// Returns `true` if a bulk buffer replacement happened since the
1819 /// last call (e.g. `set_content` / `restore` / undo restore), then
1820 /// clears the flag. When this returns `true`, hosts should drop
1821 /// any retained syntax tree before consuming
1822 /// [`Editor::take_content_edits`].
1823 pub fn take_content_reset(&mut self) -> bool {
1824 let r = self.pending_content_reset;
1825 self.pending_content_reset = false;
1826 r
1827 }
1828
1829 /// Pull-model coarse change observation. If content changed since
1830 /// the last call, returns `Some(Arc<String>)` with the new content
1831 /// and clears the dirty flag; otherwise returns `None`.
1832 ///
1833 /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1834 /// the character level) should diff against their own previous
1835 /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1836 /// once every edit path inside the engine is instrumented; this
1837 /// coarse form covers the pull-model use case in the meantime.
1838 pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1839 if !self.content_dirty {
1840 return None;
1841 }
1842 let arc = self.content_arc();
1843 self.content_dirty = false;
1844 Some(arc)
1845 }
1846
1847 /// Width in cells of the line-number gutter for the current buffer
1848 /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
1849 /// in front of the text column. Returns `0` when both `number` and
1850 /// `relativenumber` are off.
1851 pub fn lnum_width(&self) -> u16 {
1852 if self.settings.number || self.settings.relativenumber {
1853 let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1854 needed.max(self.settings.numberwidth) as u16
1855 } else {
1856 0
1857 }
1858 }
1859
1860 /// Returns the cursor's row within the visible textarea (0-based), updating
1861 /// the stored viewport top so subsequent calls remain accurate.
1862 pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1863 let cursor = buf_cursor_row(&self.buffer);
1864 let top = self.host.viewport().top_row;
1865 cursor.saturating_sub(top).min(height as usize - 1) as u16
1866 }
1867
1868 /// Returns the cursor's screen position `(x, y)` for the textarea
1869 /// described by `(area_x, area_y, area_width, area_height)`.
1870 /// Accounts for line-number gutter, viewport scroll, and any extra
1871 /// gutter width to the left of the number column (sign column, fold
1872 /// column). Returns `None` if the cursor is outside the visible
1873 /// viewport. Always available (engine-native; no ratatui dependency).
1874 ///
1875 /// `extra_gutter_width` is added to the number-column width before
1876 /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
1877 /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
1878 /// when a dedicated sign or fold column is present.
1879 ///
1880 /// Renamed from `cursor_screen_pos_xywh` in 0.0.32 — the
1881 /// ratatui-flavoured `Rect` variant is now
1882 /// [`Editor::cursor_screen_pos_in_rect`] (cfg `ratatui`).
1883 pub fn cursor_screen_pos(
1884 &self,
1885 area_x: u16,
1886 area_y: u16,
1887 area_width: u16,
1888 area_height: u16,
1889 extra_gutter_width: u16,
1890 ) -> Option<(u16, u16)> {
1891 let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1892 let v = self.host.viewport();
1893 if pos_row < v.top_row || pos_col < v.top_col {
1894 return None;
1895 }
1896 let lnum_width = self.lnum_width();
1897 // Full offset from the left edge of the window to the first text cell.
1898 let gutter_total = lnum_width + extra_gutter_width;
1899 let dy = (pos_row - v.top_row) as u16;
1900 // Convert char column to visual column so cursor lands on the
1901 // correct cell when the line contains tabs (which the renderer
1902 // expands to TAB_WIDTH stops). Tab width must match the renderer.
1903 let line = self.buffer.line(pos_row).unwrap_or("");
1904 let tab_width = if v.tab_width == 0 {
1905 4
1906 } else {
1907 v.tab_width as usize
1908 };
1909 let visual_pos = visual_col_for_char(line, pos_col, tab_width);
1910 let visual_top = visual_col_for_char(line, v.top_col, tab_width);
1911 let dx = (visual_pos - visual_top) as u16;
1912 if dy >= area_height || dx + gutter_total >= area_width {
1913 return None;
1914 }
1915 Some((area_x + gutter_total + dx, area_y + dy))
1916 }
1917
1918 /// Ratatui [`Rect`]-flavoured wrapper around
1919 /// [`Editor::cursor_screen_pos`]. Behind the `ratatui` feature.
1920 ///
1921 /// Renamed from `cursor_screen_pos` in 0.0.32 — the unprefixed
1922 /// name now belongs to the engine-native variant.
1923 ///
1924 /// `extra_gutter_width` is the combined width of the sign column and
1925 /// fold column that sit to the left of the number column (and thus
1926 /// to the left of the text area). Pass `sign_w + fold_w` from the
1927 /// render layer.
1928 #[cfg(feature = "ratatui")]
1929 pub fn cursor_screen_pos_in_rect(
1930 &self,
1931 area: Rect,
1932 extra_gutter_width: u16,
1933 ) -> Option<(u16, u16)> {
1934 self.cursor_screen_pos(area.x, area.y, area.width, area.height, extra_gutter_width)
1935 }
1936
1937 /// Returns the current vim mode. Phase 6.3: reads from the stable
1938 /// `current_mode` field (kept in sync by both the FSM step loop and
1939 /// the Phase 6.3 primitive bridges) rather than deriving from the
1940 /// FSM-internal `mode` field via `public_mode()`.
1941 pub fn vim_mode(&self) -> VimMode {
1942 self.vim.current_mode
1943 }
1944
1945 /// Bounds of the active visual-block rectangle as
1946 /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1947 /// `None` when we're not in VisualBlock mode.
1948 /// Read-only view of the live `/` or `?` prompt. `None` outside
1949 /// search-prompt mode.
1950 pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1951 self.vim.search_prompt.as_ref()
1952 }
1953
1954 /// Most recent committed search pattern (persists across `n` / `N`
1955 /// and across prompt exits). `None` before the first search.
1956 pub fn last_search(&self) -> Option<&str> {
1957 self.vim.last_search.as_deref()
1958 }
1959
1960 /// Whether the last committed search was a forward `/` (`true`) or
1961 /// a backward `?` (`false`). `n` and `N` consult this to honour the
1962 /// direction the user committed.
1963 pub fn last_search_forward(&self) -> bool {
1964 self.vim.last_search_forward
1965 }
1966
1967 /// Set the most recent committed search text + direction. Used by
1968 /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1969 /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1970 /// most recent commit with the right direction. Pass `None` /
1971 /// `true` to clear.
1972 pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1973 self.vim.last_search = text;
1974 self.vim.last_search_forward = forward;
1975 }
1976
1977 /// Start/end `(row, col)` of the active char-wise Visual selection
1978 /// (inclusive on both ends, positionally ordered). `None` when not
1979 /// in Visual mode.
1980 pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1981 if self.vim_mode() != VimMode::Visual {
1982 return None;
1983 }
1984 let anchor = self.vim.visual_anchor;
1985 let cursor = self.cursor();
1986 let (start, end) = if anchor <= cursor {
1987 (anchor, cursor)
1988 } else {
1989 (cursor, anchor)
1990 };
1991 Some((start, end))
1992 }
1993
1994 /// Top/bottom rows of the active VisualLine selection (inclusive).
1995 /// `None` when we're not in VisualLine mode.
1996 pub fn line_highlight(&self) -> Option<(usize, usize)> {
1997 if self.vim_mode() != VimMode::VisualLine {
1998 return None;
1999 }
2000 let anchor = self.vim.visual_line_anchor;
2001 let cursor = buf_cursor_row(&self.buffer);
2002 Some((anchor.min(cursor), anchor.max(cursor)))
2003 }
2004
2005 pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
2006 if self.vim_mode() != VimMode::VisualBlock {
2007 return None;
2008 }
2009 let (ar, ac) = self.vim.block_anchor;
2010 let cr = buf_cursor_row(&self.buffer);
2011 let cc = self.vim.block_vcol;
2012 let top = ar.min(cr);
2013 let bot = ar.max(cr);
2014 let left = ac.min(cc);
2015 let right = ac.max(cc);
2016 Some((top, bot, left, right))
2017 }
2018
2019 /// Active selection in `hjkl_buffer::Selection` shape. `None` when
2020 /// not in a Visual mode. Phase 7d-i wiring — the host hands this
2021 /// straight to `BufferView` once render flips off textarea
2022 /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
2023 /// switch).
2024 pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
2025 use hjkl_buffer::{Position, Selection};
2026 match self.vim_mode() {
2027 VimMode::Visual => {
2028 let (ar, ac) = self.vim.visual_anchor;
2029 let head = buf_cursor_pos(&self.buffer);
2030 Some(Selection::Char {
2031 anchor: Position::new(ar, ac),
2032 head,
2033 })
2034 }
2035 VimMode::VisualLine => {
2036 let anchor_row = self.vim.visual_line_anchor;
2037 let head_row = buf_cursor_row(&self.buffer);
2038 Some(Selection::Line {
2039 anchor_row,
2040 head_row,
2041 })
2042 }
2043 VimMode::VisualBlock => {
2044 let (ar, ac) = self.vim.block_anchor;
2045 let cr = buf_cursor_row(&self.buffer);
2046 let cc = self.vim.block_vcol;
2047 Some(Selection::Block {
2048 anchor: Position::new(ar, ac),
2049 head: Position::new(cr, cc),
2050 })
2051 }
2052 _ => None,
2053 }
2054 }
2055
2056 /// Force back to normal mode (used when dismissing completions etc.)
2057 pub fn force_normal(&mut self) {
2058 self.vim.force_normal();
2059 }
2060
2061 pub fn content(&self) -> String {
2062 let n = buf_row_count(&self.buffer);
2063 let mut s = String::new();
2064 for r in 0..n {
2065 if r > 0 {
2066 s.push('\n');
2067 }
2068 s.push_str(crate::types::Query::line(&self.buffer, r as u32));
2069 }
2070 s.push('\n');
2071 s
2072 }
2073
2074 /// Same logical output as [`content`], but returns a cached
2075 /// `Arc<String>` so back-to-back reads within an un-mutated window
2076 /// are ref-count bumps instead of multi-MB joins. The cache is
2077 /// invalidated by every [`mark_content_dirty`] call.
2078 pub fn content_arc(&mut self) -> std::sync::Arc<String> {
2079 if let Some(arc) = &self.cached_content {
2080 return std::sync::Arc::clone(arc);
2081 }
2082 let arc = std::sync::Arc::new(self.content());
2083 self.cached_content = Some(std::sync::Arc::clone(&arc));
2084 arc
2085 }
2086
2087 pub fn set_content(&mut self, text: &str) {
2088 let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2089 while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2090 lines.pop();
2091 }
2092 if lines.is_empty() {
2093 lines.push(String::new());
2094 }
2095 let _ = lines;
2096 crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2097 self.undo_stack.clear();
2098 self.redo_stack.clear();
2099 // Whole-buffer replace supersedes any queued ContentEdits.
2100 self.pending_content_edits.clear();
2101 self.pending_content_reset = true;
2102 self.mark_content_dirty();
2103 }
2104
2105 /// Whole-buffer replace that **preserves the undo history**.
2106 ///
2107 /// Equivalent to [`Editor::set_content`] but pushes the current buffer
2108 /// state onto the undo stack first, so a subsequent `u` walks back to
2109 /// the pre-replacement content. Use this for any operation the user
2110 /// expects to undo as a single step — e.g. external formatter output
2111 /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
2112 ///
2113 /// Like `push_undo`, this clears the redo stack (vim semantics: any
2114 /// new edit invalidates redo).
2115 pub fn set_content_undoable(&mut self, text: &str) {
2116 self.push_undo();
2117 let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2118 while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2119 lines.pop();
2120 }
2121 if lines.is_empty() {
2122 lines.push(String::new());
2123 }
2124 let _ = lines;
2125 crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2126 // Whole-buffer replace supersedes any queued ContentEdits.
2127 self.pending_content_edits.clear();
2128 self.pending_content_reset = true;
2129 self.mark_content_dirty();
2130 }
2131
2132 /// Drain the pending change log produced by buffer mutations.
2133 ///
2134 /// Returns a `Vec<EditOp>` covering edits applied since the last
2135 /// call. Empty when no edits ran. Pull-model, complementary to
2136 /// [`Editor::take_content_change`] which gives back the new full
2137 /// content.
2138 ///
2139 /// Mapping coverage:
2140 /// - InsertChar / InsertStr → exact `EditOp` with empty range +
2141 /// replacement.
2142 /// - DeleteRange (`Char` kind) → exact range + empty replacement.
2143 /// - Replace → exact range + new replacement.
2144 /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
2145 /// InsertBlock, DeleteBlockChunks → best-effort placeholder
2146 /// covering the touched range. Hosts wanting per-cell deltas
2147 /// should diff their own `lines()` snapshot.
2148 pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
2149 std::mem::take(&mut self.change_log)
2150 }
2151
2152 /// Read the engine's current settings as a SPEC
2153 /// [`crate::types::Options`].
2154 ///
2155 /// Bridges between the legacy [`Settings`] (which carries fewer
2156 /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
2157 /// not present in `Settings` fall back to vim defaults (e.g.,
2158 /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
2159 /// Once trait extraction lands, this becomes the canonical config
2160 /// reader and `Settings` retires.
2161 pub fn current_options(&self) -> crate::types::Options {
2162 crate::types::Options {
2163 shiftwidth: self.settings.shiftwidth as u32,
2164 tabstop: self.settings.tabstop as u32,
2165 softtabstop: self.settings.softtabstop as u32,
2166 textwidth: self.settings.textwidth as u32,
2167 expandtab: self.settings.expandtab,
2168 ignorecase: self.settings.ignore_case,
2169 smartcase: self.settings.smartcase,
2170 wrapscan: self.settings.wrapscan,
2171 wrap: match self.settings.wrap {
2172 hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
2173 hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
2174 hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
2175 },
2176 readonly: self.settings.readonly,
2177 autoindent: self.settings.autoindent,
2178 smartindent: self.settings.smartindent,
2179 undo_levels: self.settings.undo_levels,
2180 undo_break_on_motion: self.settings.undo_break_on_motion,
2181 iskeyword: self.settings.iskeyword.clone(),
2182 timeout_len: self.settings.timeout_len,
2183 ..crate::types::Options::default()
2184 }
2185 }
2186
2187 /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
2188 /// Only the fields backed by today's [`Settings`] take effect;
2189 /// remaining options become live once trait extraction wires them
2190 /// through.
2191 pub fn apply_options(&mut self, opts: &crate::types::Options) {
2192 self.settings.shiftwidth = opts.shiftwidth as usize;
2193 self.settings.tabstop = opts.tabstop as usize;
2194 self.settings.softtabstop = opts.softtabstop as usize;
2195 self.settings.textwidth = opts.textwidth as usize;
2196 self.settings.expandtab = opts.expandtab;
2197 self.settings.ignore_case = opts.ignorecase;
2198 self.settings.smartcase = opts.smartcase;
2199 self.settings.wrapscan = opts.wrapscan;
2200 self.settings.wrap = match opts.wrap {
2201 crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
2202 crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
2203 crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
2204 };
2205 self.settings.readonly = opts.readonly;
2206 self.settings.autoindent = opts.autoindent;
2207 self.settings.smartindent = opts.smartindent;
2208 self.settings.undo_levels = opts.undo_levels;
2209 self.settings.undo_break_on_motion = opts.undo_break_on_motion;
2210 self.set_iskeyword(opts.iskeyword.clone());
2211 self.settings.timeout_len = opts.timeout_len;
2212 self.settings.number = opts.number;
2213 self.settings.relativenumber = opts.relativenumber;
2214 self.settings.numberwidth = opts.numberwidth;
2215 self.settings.cursorline = opts.cursorline;
2216 self.settings.cursorcolumn = opts.cursorcolumn;
2217 self.settings.signcolumn = opts.signcolumn;
2218 self.settings.foldcolumn = opts.foldcolumn;
2219 self.settings.colorcolumn = opts.colorcolumn.clone();
2220 }
2221
2222 /// Active visual selection as a SPEC [`crate::types::Highlight`]
2223 /// with [`crate::types::HighlightKind::Selection`].
2224 ///
2225 /// Returns `None` when the editor isn't in a Visual mode.
2226 /// Visual-line and visual-block selections collapse to the
2227 /// bounding char range of the selection — the SPEC `Selection`
2228 /// kind doesn't carry sub-line info today; hosts that need full
2229 /// line / block geometry continue to read [`buffer_selection`]
2230 /// (the legacy [`hjkl_buffer::Selection`] shape).
2231 pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2232 use crate::types::{Highlight, HighlightKind, Pos};
2233 let sel = self.buffer_selection()?;
2234 let (start, end) = match sel {
2235 hjkl_buffer::Selection::Char { anchor, head } => {
2236 let a = (anchor.row, anchor.col);
2237 let h = (head.row, head.col);
2238 if a <= h { (a, h) } else { (h, a) }
2239 }
2240 hjkl_buffer::Selection::Line {
2241 anchor_row,
2242 head_row,
2243 } => {
2244 let (top, bot) = if anchor_row <= head_row {
2245 (anchor_row, head_row)
2246 } else {
2247 (head_row, anchor_row)
2248 };
2249 let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2250 ((top, 0), (bot, last_col))
2251 }
2252 hjkl_buffer::Selection::Block { anchor, head } => {
2253 let (top, bot) = if anchor.row <= head.row {
2254 (anchor.row, head.row)
2255 } else {
2256 (head.row, anchor.row)
2257 };
2258 let (left, right) = if anchor.col <= head.col {
2259 (anchor.col, head.col)
2260 } else {
2261 (head.col, anchor.col)
2262 };
2263 ((top, left), (bot, right))
2264 }
2265 };
2266 Some(Highlight {
2267 range: Pos {
2268 line: start.0 as u32,
2269 col: start.1 as u32,
2270 }..Pos {
2271 line: end.0 as u32,
2272 col: end.1 as u32,
2273 },
2274 kind: HighlightKind::Selection,
2275 })
2276 }
2277
2278 /// SPEC-typed highlights for `line`.
2279 ///
2280 /// Two emission modes:
2281 ///
2282 /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2283 /// `Editor::search_prompt` is `Some`. Live-preview matches of
2284 /// the in-flight pattern surface as
2285 /// [`crate::types::HighlightKind::IncSearch`].
2286 /// - **SearchMatch**: the prompt has been committed (or absent)
2287 /// and the buffer's armed pattern is non-empty. Matches surface
2288 /// as [`crate::types::HighlightKind::SearchMatch`].
2289 ///
2290 /// Selection / MatchParen / Syntax(id) variants land once the
2291 /// trait extraction routes the FSM's selection set + the host's
2292 /// syntax pipeline through the [`crate::types::Host`] trait.
2293 ///
2294 /// Returns an empty vec when there is nothing to highlight or
2295 /// `line` is out of bounds.
2296 pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2297 use crate::types::{Highlight, HighlightKind, Pos};
2298 let row = line as usize;
2299 if row >= buf_row_count(&self.buffer) {
2300 return Vec::new();
2301 }
2302
2303 // Live preview while the prompt is open beats the committed
2304 // pattern.
2305 if let Some(prompt) = self.search_prompt() {
2306 if prompt.text.is_empty() {
2307 return Vec::new();
2308 }
2309 let Ok(re) = regex::Regex::new(&prompt.text) else {
2310 return Vec::new();
2311 };
2312 let Some(haystack) = buf_line(&self.buffer, row) else {
2313 return Vec::new();
2314 };
2315 return re
2316 .find_iter(haystack)
2317 .map(|m| Highlight {
2318 range: Pos {
2319 line,
2320 col: m.start() as u32,
2321 }..Pos {
2322 line,
2323 col: m.end() as u32,
2324 },
2325 kind: HighlightKind::IncSearch,
2326 })
2327 .collect();
2328 }
2329
2330 if self.search_state.pattern.is_none() {
2331 return Vec::new();
2332 }
2333 let dgen = crate::types::Query::dirty_gen(&self.buffer);
2334 crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2335 .into_iter()
2336 .map(|(start, end)| Highlight {
2337 range: Pos {
2338 line,
2339 col: start as u32,
2340 }..Pos {
2341 line,
2342 col: end as u32,
2343 },
2344 kind: HighlightKind::SearchMatch,
2345 })
2346 .collect()
2347 }
2348
2349 /// Build the engine's [`crate::types::RenderFrame`] for the
2350 /// current state. Hosts call this once per redraw and diff
2351 /// across frames.
2352 ///
2353 /// Coarse today — covers mode + cursor + cursor shape + viewport
2354 /// top + line count. SPEC-target fields (selections, highlights,
2355 /// command line, search prompt, status line) land once trait
2356 /// extraction routes them through `SelectionSet` and the
2357 /// `Highlight` pipeline.
2358 pub fn render_frame(&self) -> crate::types::RenderFrame {
2359 use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2360 let (cursor_row, cursor_col) = self.cursor();
2361 let (mode, shape) = match self.vim_mode() {
2362 crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2363 crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2364 crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2365 crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2366 crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2367 };
2368 RenderFrame {
2369 mode,
2370 cursor_row: cursor_row as u32,
2371 cursor_col: cursor_col as u32,
2372 cursor_shape: shape,
2373 viewport_top: self.host.viewport().top_row as u32,
2374 line_count: crate::types::Query::line_count(&self.buffer),
2375 }
2376 }
2377
2378 /// Capture the editor's coarse state into a serde-friendly
2379 /// [`crate::types::EditorSnapshot`].
2380 ///
2381 /// Today's snapshot covers mode, cursor, lines, viewport top.
2382 /// Registers, marks, jump list, undo tree, and full options arrive
2383 /// once phase 5 trait extraction lands the generic
2384 /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2385 /// stays stable; only the snapshot's internal fields grow.
2386 ///
2387 /// Distinct from the internal `snapshot` used by undo (which
2388 /// returns `(Vec<String>, (usize, usize))`); host-facing
2389 /// persistence goes through this one.
2390 pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2391 use crate::types::{EditorSnapshot, SnapshotMode};
2392 let mode = match self.vim_mode() {
2393 crate::VimMode::Normal => SnapshotMode::Normal,
2394 crate::VimMode::Insert => SnapshotMode::Insert,
2395 crate::VimMode::Visual => SnapshotMode::Visual,
2396 crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2397 crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2398 };
2399 let cursor = self.cursor();
2400 let cursor = (cursor.0 as u32, cursor.1 as u32);
2401 let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2402 let viewport_top = self.host.viewport().top_row as u32;
2403 let marks = self
2404 .marks
2405 .iter()
2406 .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2407 .collect();
2408 EditorSnapshot {
2409 version: EditorSnapshot::VERSION,
2410 mode,
2411 cursor,
2412 lines,
2413 viewport_top,
2414 registers: self.registers.clone(),
2415 marks,
2416 }
2417 }
2418
2419 /// Restore editor state from an [`EditorSnapshot`]. Returns
2420 /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2421 /// `version` doesn't match [`EditorSnapshot::VERSION`].
2422 ///
2423 /// Mode is best-effort: `SnapshotMode` only round-trips the
2424 /// status-line summary, not the full FSM state. Visual / Insert
2425 /// mode entry happens through synthetic key dispatch when needed.
2426 pub fn restore_snapshot(
2427 &mut self,
2428 snap: crate::types::EditorSnapshot,
2429 ) -> Result<(), crate::EngineError> {
2430 use crate::types::EditorSnapshot;
2431 if snap.version != EditorSnapshot::VERSION {
2432 return Err(crate::EngineError::SnapshotVersion(
2433 snap.version,
2434 EditorSnapshot::VERSION,
2435 ));
2436 }
2437 let text = snap.lines.join("\n");
2438 self.set_content(&text);
2439 self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2440 self.host.viewport_mut().top_row = snap.viewport_top as usize;
2441 self.registers = snap.registers;
2442 self.marks = snap
2443 .marks
2444 .into_iter()
2445 .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2446 .collect();
2447 Ok(())
2448 }
2449
2450 /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2451 /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2452 /// shape their payload.
2453 pub fn seed_yank(&mut self, text: String) {
2454 let linewise = text.ends_with('\n');
2455 self.vim.yank_linewise = linewise;
2456 self.registers.unnamed = crate::registers::Slot { text, linewise };
2457 }
2458
2459 /// Scroll the viewport down by `rows`. The cursor stays on its
2460 /// absolute line (vim convention) unless the scroll would take it
2461 /// off-screen — in that case it's clamped to the first row still
2462 /// visible.
2463 pub fn scroll_down(&mut self, rows: i16) {
2464 self.scroll_viewport(rows);
2465 }
2466
2467 /// Scroll the viewport up by `rows`. Cursor stays unless it would
2468 /// fall off the bottom of the new viewport, then clamp to the
2469 /// bottom-most visible row.
2470 pub fn scroll_up(&mut self, rows: i16) {
2471 self.scroll_viewport(-rows);
2472 }
2473
2474 /// Scroll the viewport right by `cols` columns. Only the horizontal
2475 /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2476 /// vim's `zl` behaviour for horizontal scroll without wrap).
2477 pub fn scroll_right(&mut self, cols: i16) {
2478 let vp = self.host.viewport_mut();
2479 let cols_i = cols as isize;
2480 let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2481 vp.top_col = new_top;
2482 }
2483
2484 /// Scroll the viewport left by `cols` columns. Delegates to
2485 /// `scroll_right` with a negated argument so the floor-at-zero
2486 /// clamp is shared.
2487 pub fn scroll_left(&mut self, cols: i16) {
2488 self.scroll_right(-cols);
2489 }
2490
2491 /// Vim's `scrolloff` default — keep the cursor at least this many
2492 /// rows away from the top / bottom edge of the viewport while
2493 /// scrolling. Collapses to `height / 2` for tiny viewports.
2494 const SCROLLOFF: usize = 5;
2495
2496 /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2497 /// rows from each edge. Replaces the bare
2498 /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2499 /// don't park the cursor on the very last visible row.
2500 pub fn ensure_cursor_in_scrolloff(&mut self) {
2501 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2502 if height == 0 {
2503 // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2504 // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2505 // Disjoint-field borrow split: `self.buffer` (immutable via
2506 // `folds` snapshot + cursor) and `self.host` (mutable
2507 // viewport ref) live on distinct struct fields, so one
2508 // statement satisfies the borrow checker.
2509 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2510 crate::viewport_math::ensure_cursor_visible(
2511 &self.buffer,
2512 &folds,
2513 self.host.viewport_mut(),
2514 );
2515 return;
2516 }
2517 // Cap margin at (height - 1) / 2 so the upper + lower bands
2518 // can't overlap on tiny windows (margin=5 + height=10 would
2519 // otherwise produce contradictory clamp ranges).
2520 let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2521 // Soft-wrap path: scrolloff math runs in *screen rows*, not
2522 // doc rows, since a wrapped doc row spans many visual lines.
2523 if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2524 self.ensure_scrolloff_wrap(height, margin);
2525 return;
2526 }
2527 let cursor_row = buf_cursor_row(&self.buffer);
2528 let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2529 let v = self.host.viewport_mut();
2530 // Top edge: cursor_row should sit at >= top_row + margin.
2531 if cursor_row < v.top_row + margin {
2532 v.top_row = cursor_row.saturating_sub(margin);
2533 }
2534 // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2535 let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2536 if cursor_row > v.top_row + max_bottom {
2537 v.top_row = cursor_row.saturating_sub(max_bottom);
2538 }
2539 // Clamp top_row so we never scroll past the buffer's bottom.
2540 let max_top = last_row.saturating_sub(height.saturating_sub(1));
2541 if v.top_row > max_top {
2542 v.top_row = max_top;
2543 }
2544 // Defer to Buffer for column-side scroll (no scrolloff for
2545 // horizontal scrolling — vim default `sidescrolloff = 0`).
2546 let cursor = buf_cursor_pos(&self.buffer);
2547 self.host.viewport_mut().ensure_visible(cursor);
2548 }
2549
2550 /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2551 /// at a time so the cursor's *screen* row stays inside
2552 /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2553 /// buffer's bottom never leaves blank rows below it.
2554 fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2555 let cursor_row = buf_cursor_row(&self.buffer);
2556 // Step 1 — cursor above viewport: snap top to cursor row,
2557 // then we'll fix up the margin below.
2558 if cursor_row < self.host.viewport().top_row {
2559 let v = self.host.viewport_mut();
2560 v.top_row = cursor_row;
2561 v.top_col = 0;
2562 }
2563 // Step 2 — push top forward until cursor's screen row is
2564 // within the bottom margin (`csr <= height - 1 - margin`).
2565 // 0.0.33 (Patch C-γ): fold-iteration goes through the
2566 // [`crate::types::FoldProvider`] surface via
2567 // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2568 // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2569 // a `&Viewport` parameter; the host owns the viewport, so the
2570 // disjoint `(self.host, self.buffer)` borrows split cleanly.
2571 let max_csr = height.saturating_sub(1).saturating_sub(margin);
2572 loop {
2573 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2574 let csr =
2575 crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2576 .unwrap_or(0);
2577 if csr <= max_csr {
2578 break;
2579 }
2580 let top = self.host.viewport().top_row;
2581 let row_count = buf_row_count(&self.buffer);
2582 let next = {
2583 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2584 <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2585 };
2586 let Some(next) = next else {
2587 break;
2588 };
2589 // Don't walk past the cursor's row.
2590 if next > cursor_row {
2591 self.host.viewport_mut().top_row = cursor_row;
2592 break;
2593 }
2594 self.host.viewport_mut().top_row = next;
2595 }
2596 // Step 3 — pull top backward until cursor's screen row is
2597 // past the top margin (`csr >= margin`).
2598 loop {
2599 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2600 let csr =
2601 crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2602 .unwrap_or(0);
2603 if csr >= margin {
2604 break;
2605 }
2606 let top = self.host.viewport().top_row;
2607 let prev = {
2608 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2609 <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2610 };
2611 let Some(prev) = prev else {
2612 break;
2613 };
2614 self.host.viewport_mut().top_row = prev;
2615 }
2616 // Step 4 — clamp top so the buffer's bottom doesn't leave
2617 // blank rows below it. `max_top_for_height` walks segments
2618 // backward from the last row until it accumulates `height`
2619 // screen rows.
2620 let max_top = {
2621 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2622 crate::viewport_math::max_top_for_height(
2623 &self.buffer,
2624 &folds,
2625 self.host.viewport(),
2626 height,
2627 )
2628 };
2629 if self.host.viewport().top_row > max_top {
2630 self.host.viewport_mut().top_row = max_top;
2631 }
2632 self.host.viewport_mut().top_col = 0;
2633 }
2634
2635 fn scroll_viewport(&mut self, delta: i16) {
2636 if delta == 0 {
2637 return;
2638 }
2639 // Bump the host viewport's top within bounds.
2640 let total_rows = buf_row_count(&self.buffer) as isize;
2641 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2642 let cur_top = self.host.viewport().top_row as isize;
2643 let new_top = (cur_top + delta as isize)
2644 .max(0)
2645 .min((total_rows - 1).max(0)) as usize;
2646 self.host.viewport_mut().top_row = new_top;
2647 // Mirror to textarea so its viewport reads (still consumed by
2648 // a couple of helpers) stay accurate.
2649 let _ = cur_top;
2650 if height == 0 {
2651 return;
2652 }
2653 // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2654 // from the visible viewport edges.
2655 let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2656 let margin = Self::SCROLLOFF.min(height / 2);
2657 let min_row = new_top + margin;
2658 let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2659 let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2660 if target_row != cursor_row {
2661 let line_len = buf_line(&self.buffer, target_row)
2662 .map(|l| l.chars().count())
2663 .unwrap_or(0);
2664 let target_col = cursor_col.min(line_len.saturating_sub(1));
2665 buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2666 }
2667 }
2668
2669 pub fn goto_line(&mut self, line: usize) {
2670 let row = line.saturating_sub(1);
2671 let max = buf_row_count(&self.buffer).saturating_sub(1);
2672 let target = row.min(max);
2673 buf_set_cursor_rc(&mut self.buffer, target, 0);
2674 // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2675 // the cursor lands off-screen and the user has to scroll
2676 // manually to see it.
2677 self.ensure_cursor_in_scrolloff();
2678 }
2679
2680 /// Scroll so the cursor row lands at the given viewport position:
2681 /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2682 /// Cursor stays on its absolute line; only the viewport moves.
2683 pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2684 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2685 if height == 0 {
2686 return;
2687 }
2688 let cur_row = buf_cursor_row(&self.buffer);
2689 let cur_top = self.host.viewport().top_row;
2690 // Scrolloff awareness: `zt` lands the cursor at the top edge
2691 // of the viable area (top + margin), `zb` at the bottom edge
2692 // (top + height - 1 - margin). Match the cap used by
2693 // `ensure_cursor_in_scrolloff` so contradictory bounds are
2694 // impossible on tiny viewports.
2695 let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2696 let new_top = match pos {
2697 CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2698 CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2699 CursorScrollTarget::Bottom => {
2700 cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2701 }
2702 };
2703 if new_top == cur_top {
2704 return;
2705 }
2706 self.host.viewport_mut().top_row = new_top;
2707 }
2708
2709 /// Jump the cursor to the given 1-based line/column, clamped to the document.
2710 pub fn jump_to(&mut self, line: usize, col: usize) {
2711 let r = line.saturating_sub(1);
2712 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2713 let r = r.min(max_row);
2714 let line_len = buf_line(&self.buffer, r)
2715 .map(|l| l.chars().count())
2716 .unwrap_or(0);
2717 let c = col.saturating_sub(1).min(line_len);
2718 buf_set_cursor_rc(&mut self.buffer, r, c);
2719 }
2720
2721 // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2722 //
2723 // These primitives operate on document (row, col) coordinates that the HOST
2724 // computes from its own layout knowledge (cell geometry for the TUI host,
2725 // pixel geometry for the future GUI host). The engine has no u16 terminal
2726 // assumption here — it just moves the cursor in doc-space.
2727
2728 /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2729 /// document bounds. Hosts use this for programmatic cursor placement and
2730 /// as the building block for the mouse-click path.
2731 ///
2732 /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2733 /// position); values beyond that are clamped to `char_count`.
2734 pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2735 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2736 let r = row.min(max_row);
2737 let line_len = buf_line(&self.buffer, r)
2738 .map(|l| l.chars().count())
2739 .unwrap_or(0);
2740 let c = col.min(line_len);
2741 buf_set_cursor_rc(&mut self.buffer, r, c);
2742 }
2743
2744 /// Handle a left-button click at doc-space `(row, col)`.
2745 ///
2746 /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2747 /// parity for `undo_break_on_motion`), then moves the cursor. The host
2748 /// performs cell→doc or pixel→doc translation before calling this.
2749 ///
2750 /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2751 /// cursor lives on chars and never on the implicit `\n` — `col` is
2752 /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2753 /// allows the one-past-EOL insert position (`col == chars().count()`).
2754 ///
2755 /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2756 /// motion uses the clicked column as the intended visual column
2757 /// (otherwise the cursor would snap back to the keyboard-tracked
2758 /// column on the first vertical motion after a click).
2759 pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2760 if self.vim.is_visual() {
2761 self.vim.force_normal();
2762 }
2763 // Mouse-position click counts as a motion — break the active
2764 // insert-mode undo group when the toggle is on (vim parity).
2765 crate::vim::break_undo_group_in_insert(self);
2766
2767 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2768 let r = row.min(max_row);
2769 let line_len = buf_line(&self.buffer, r)
2770 .map(|l| l.chars().count())
2771 .unwrap_or(0);
2772 let cap = if self.vim.current_mode == crate::VimMode::Insert {
2773 line_len
2774 } else {
2775 line_len.saturating_sub(1)
2776 };
2777 let c = col.min(cap);
2778 buf_set_cursor_rc(&mut self.buffer, r, c);
2779 self.sticky_col = Some(c);
2780 }
2781
2782 /// Begin a mouse-drag selection: anchor at the current cursor and enter
2783 /// Visual-char mode. Idempotent if already in Visual-char mode.
2784 pub fn mouse_begin_drag(&mut self) {
2785 if !self.vim.is_visual_char() {
2786 vim::enter_visual_char_bridge(self);
2787 }
2788 }
2789
2790 /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2791 ///
2792 /// Moves the live cursor; the Visual anchor stays where
2793 /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2794 /// translated the drag position to doc coordinates.
2795 pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2796 self.set_cursor_doc(row, col);
2797 }
2798
2799 pub fn insert_str(&mut self, text: &str) {
2800 let pos = crate::types::Cursor::cursor(&self.buffer);
2801 crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2802 self.push_buffer_content_to_textarea();
2803 self.mark_content_dirty();
2804 }
2805
2806 pub fn accept_completion(&mut self, completion: &str) {
2807 use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2808 let cursor_pos = CursorTrait::cursor(&self.buffer);
2809 let cursor_row = cursor_pos.line as usize;
2810 let cursor_col = cursor_pos.col as usize;
2811 let line = buf_line(&self.buffer, cursor_row).unwrap_or("").to_string();
2812 let chars: Vec<char> = line.chars().collect();
2813 let prefix_len = chars[..cursor_col.min(chars.len())]
2814 .iter()
2815 .rev()
2816 .take_while(|c| c.is_alphanumeric() || **c == '_')
2817 .count();
2818 if prefix_len > 0 {
2819 let start = Pos {
2820 line: cursor_row as u32,
2821 col: (cursor_col - prefix_len) as u32,
2822 };
2823 BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2824 }
2825 let cursor = CursorTrait::cursor(&self.buffer);
2826 BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2827 self.push_buffer_content_to_textarea();
2828 self.mark_content_dirty();
2829 }
2830
2831 pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2832 let rc = buf_cursor_rc(&self.buffer);
2833 (buf_lines_to_vec(&self.buffer), rc)
2834 }
2835
2836 /// Walk one step back through the undo history. Equivalent to the
2837 /// user pressing `u` in normal mode. Drains the most recent undo
2838 /// entry and pushes it onto the redo stack.
2839 pub fn undo(&mut self) {
2840 crate::vim::do_undo(self);
2841 }
2842
2843 /// Walk one step forward through the redo history. Equivalent to
2844 /// `<C-r>` in normal mode.
2845 pub fn redo(&mut self) {
2846 crate::vim::do_redo(self);
2847 }
2848
2849 /// Snapshot current buffer state onto the undo stack and clear
2850 /// the redo stack. Bounded by `settings.undo_levels` — older
2851 /// entries pruned. Call before any group of buffer mutations the
2852 /// user might want to undo as a single step.
2853 pub fn push_undo(&mut self) {
2854 let snap = self.snapshot();
2855 self.undo_stack.push(snap);
2856 self.cap_undo();
2857 self.redo_stack.clear();
2858 }
2859
2860 /// Trim the undo stack down to `settings.undo_levels`, dropping
2861 /// the oldest entries. `undo_levels == 0` is treated as
2862 /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2863 /// skipped — guarding with `> 0` is one line shorter than gating
2864 /// the cap path with an explicit zero-check above the call site).
2865 pub(crate) fn cap_undo(&mut self) {
2866 let cap = self.settings.undo_levels as usize;
2867 if cap > 0 && self.undo_stack.len() > cap {
2868 let diff = self.undo_stack.len() - cap;
2869 self.undo_stack.drain(..diff);
2870 }
2871 }
2872
2873 /// Test-only accessor for the undo stack length.
2874 #[doc(hidden)]
2875 pub fn undo_stack_len(&self) -> usize {
2876 self.undo_stack.len()
2877 }
2878
2879 /// Replace the buffer with `lines` joined by `\n` and set the
2880 /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2881 /// paths. Marks the editor dirty.
2882 pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2883 let text = lines.join("\n");
2884 crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2885 buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2886 // Bulk replace — supersedes any queued ContentEdits.
2887 self.pending_content_edits.clear();
2888 self.pending_content_reset = true;
2889 self.mark_content_dirty();
2890 }
2891
2892 /// Returns true if the key was consumed by the editor.
2893 /// Replace the char under the cursor with `ch`, `count` times. Matches
2894 /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2895 /// snapshot taken once at start. Promoted to public surface in 0.5.5
2896 /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2897 /// re-entering the FSM.
2898 pub fn replace_char_at(&mut self, ch: char, count: usize) {
2899 vim::replace_char(self, ch, count);
2900 }
2901
2902 /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2903 /// to the `count`-th occurrence of `ch` on the current line, respecting
2904 /// `forward` (direction) and `till` (stop one char before target).
2905 /// Records `last_find` so `;` / `,` repeat work.
2906 ///
2907 /// No-op if the target char isn't on the current line within range.
2908 /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2909 pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2910 vim::apply_find_char(self, ch, forward, till, count.max(1));
2911 }
2912
2913 /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2914 /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2915 /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2916 ///
2917 /// Promoted to public surface in 0.5.10 so hjkl-vim's
2918 /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2919 /// re-entering the engine FSM.
2920 pub fn after_g(&mut self, ch: char, count: usize) {
2921 vim::apply_after_g(self, ch, count);
2922 }
2923
2924 /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2925 /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2926 /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2927 /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2928 ///
2929 /// Promoted to public surface in 0.5.11 so hjkl-vim's
2930 /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2931 /// re-entering the engine FSM.
2932 pub fn after_z(&mut self, ch: char, count: usize) {
2933 vim::apply_after_z(self, ch, count);
2934 }
2935
2936 /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2937 /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2938 /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2939 /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2940 /// then calls `apply_op_with_motion`. `total_count` is already the product of
2941 /// the prefix count and any inner count accumulated by the reducer.
2942 ///
2943 /// No-op when `motion_key` does not map to a known motion (engine silently
2944 /// cancels the operator, matching vim's behaviour on unknown motions).
2945 ///
2946 /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2947 /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2948 /// re-entering the engine FSM.
2949 pub fn apply_op_motion(
2950 &mut self,
2951 op: crate::vim::Operator,
2952 motion_key: char,
2953 total_count: usize,
2954 ) {
2955 vim::apply_op_motion_key(self, op, motion_key, total_count);
2956 }
2957
2958 /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2959 /// `total_count` is the product of prefix count and inner count.
2960 ///
2961 /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2962 /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2963 /// re-entering the engine FSM.
2964 pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2965 vim::apply_op_double(self, op, total_count);
2966 }
2967
2968 /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2969 /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2970 /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2971 /// updates `last_change` when `op` is Change (for dot-repeat).
2972 ///
2973 /// `total_count` is the product of prefix count and any inner count
2974 /// accumulated by the reducer — already folded at transition time.
2975 ///
2976 /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2977 /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2978 /// re-entering the engine FSM. `handle_op_find_target` (used by the
2979 /// chord-init op path) delegates here to avoid logic duplication.
2980 pub fn apply_op_find(
2981 &mut self,
2982 op: crate::vim::Operator,
2983 ch: char,
2984 forward: bool,
2985 till: bool,
2986 total_count: usize,
2987 ) {
2988 vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2989 }
2990
2991 /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2992 /// Maps `ch` to a `TextObject` per the standard vim table, calls
2993 /// `apply_op_with_text_object`, and records `last_change` when `op` is
2994 /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2995 /// matching the engine FSM's behaviour on unrecognised text-object chars.
2996 ///
2997 /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2998 /// `apply_op_find` but is currently unused — text objects don't repeat in
2999 /// vim's current grammar. Kept for future-proofing.
3000 ///
3001 /// Promoted to the public surface in 0.5.15 so the hjkl-vim
3002 /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
3003 /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
3004 /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
3005 /// duplication.
3006 pub fn apply_op_text_obj(
3007 &mut self,
3008 op: crate::vim::Operator,
3009 ch: char,
3010 inner: bool,
3011 total_count: usize,
3012 ) {
3013 vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
3014 }
3015
3016 /// Apply an operator over a g-chord motion or case-op linewise form
3017 /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
3018 ///
3019 /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
3020 /// letter (`U`/`u`/`~`), executes the line op (linewise form).
3021 /// - Otherwise maps `ch` to a motion:
3022 /// - `'g'` → `Motion::FileTop` (gg)
3023 /// - `'e'` → `Motion::WordEndBack` (ge)
3024 /// - `'E'` → `Motion::BigWordEndBack` (gE)
3025 /// - `'j'` → `Motion::ScreenDown` (gj)
3026 /// - `'k'` → `Motion::ScreenUp` (gk)
3027 /// - unknown → no-op (silently ignored, matching engine FSM behaviour)
3028 /// - Updates `last_change` for dot-repeat when `op` is a change operator.
3029 ///
3030 /// `total_count` is the already-folded product of prefix and inner counts.
3031 ///
3032 /// Promoted to the public surface in 0.5.16 so the hjkl-vim
3033 /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
3034 /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
3035 /// delegates to the shared `apply_op_g_inner` helper to avoid logic
3036 /// duplication.
3037 pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
3038 vim::apply_op_g_inner(self, op, ch, total_count);
3039 }
3040
3041 // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
3042
3043 /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
3044 /// row and the motion's target row. Used by the app layer to compute the
3045 /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
3046 /// format job.
3047 ///
3048 /// Returns `None` when `motion_key` does not map to a known motion (same
3049 /// condition that makes `apply_op_motion` a no-op).
3050 ///
3051 /// The cursor is restored to its original position after the probe —
3052 /// the buffer content is not touched.
3053 pub fn range_for_op_motion(
3054 &mut self,
3055 motion_key: char,
3056 total_count: usize,
3057 ) -> Option<(usize, usize)> {
3058 let start = self.cursor();
3059 // Reuse the same logic as apply_op_motion_key but only read the
3060 // target row — we parse the motion, apply it to move the cursor,
3061 // then immediately restore.
3062 let input = crate::input::Input {
3063 key: crate::input::Key::Char(motion_key),
3064 ctrl: false,
3065 alt: false,
3066 shift: false,
3067 };
3068 let motion = vim::parse_motion(&input)?;
3069 // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
3070 let motion = match motion {
3071 vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
3072 Some((ch, forward, till)) => vim::Motion::Find {
3073 ch,
3074 forward: if reverse { !forward } else { forward },
3075 till,
3076 },
3077 None => return None,
3078 },
3079 m => m,
3080 };
3081 vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3082 let end = self.cursor();
3083 // Restore cursor.
3084 buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3085 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3086 Some((r0, r1))
3087 }
3088
3089 /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
3090 /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
3091 /// linewise forms that don't map to a row range.
3092 ///
3093 /// The cursor is restored after the probe.
3094 pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
3095 let start = self.cursor();
3096 let motion = match ch {
3097 'g' => vim::Motion::FileTop,
3098 'e' => vim::Motion::WordEndBack,
3099 'E' => vim::Motion::BigWordEndBack,
3100 'j' => vim::Motion::ScreenDown,
3101 'k' => vim::Motion::ScreenUp,
3102 _ => return None,
3103 };
3104 vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3105 let end = self.cursor();
3106 buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3107 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3108 Some((r0, r1))
3109 }
3110
3111 /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
3112 /// matched region. Returns `None` when `ch` is not a known text-object
3113 /// kind or the text object could not be resolved (e.g. no enclosing bracket).
3114 ///
3115 /// The buffer is not mutated.
3116 pub fn range_for_op_text_obj(
3117 &self,
3118 ch: char,
3119 inner: bool,
3120 _total_count: usize,
3121 ) -> Option<(usize, usize)> {
3122 let obj = match ch {
3123 'w' => vim::TextObject::Word { big: false },
3124 'W' => vim::TextObject::Word { big: true },
3125 '"' | '\'' | '`' => vim::TextObject::Quote(ch),
3126 '(' | ')' | 'b' => vim::TextObject::Bracket('('),
3127 '[' | ']' => vim::TextObject::Bracket('['),
3128 '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
3129 '<' | '>' => vim::TextObject::Bracket('<'),
3130 'p' => vim::TextObject::Paragraph,
3131 't' => vim::TextObject::XmlTag,
3132 's' => vim::TextObject::Sentence,
3133 _ => return None,
3134 };
3135 let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
3136 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3137 Some((r0, r1))
3138 }
3139
3140 // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
3141 //
3142 // These do not consume input — the caller (hjkl-vim's visual-mode operator
3143 // path, chunk 4e) has already resolved the range from the visual selection
3144 // before calling in. Normal-mode op dispatch continues to use
3145 // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
3146
3147 /// Delete the region `[start, end)` and stash the removed text in
3148 /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
3149 /// select named registers.
3150 ///
3151 /// Pure range-mutation primitive — does not consume input. Called by
3152 /// hjkl-vim's visual-mode operator path which has already resolved the range
3153 /// from the visual selection.
3154 ///
3155 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3156 /// grammar migration (kryptic-sh/hjkl#70).
3157 pub fn delete_range(
3158 &mut self,
3159 start: (usize, usize),
3160 end: (usize, usize),
3161 kind: crate::vim::RangeKind,
3162 register: char,
3163 ) {
3164 vim::delete_range_bridge(self, start, end, kind, register);
3165 }
3166
3167 /// Yank (copy) the region `[start, end)` into `register` without mutating
3168 /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
3169 /// register; `'a'`–`'z'` select named registers.
3170 ///
3171 /// Pure range-mutation primitive — does not consume input. Called by
3172 /// hjkl-vim's visual-mode operator path which has already resolved the range
3173 /// from the visual selection.
3174 ///
3175 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3176 /// grammar migration (kryptic-sh/hjkl#70).
3177 pub fn yank_range(
3178 &mut self,
3179 start: (usize, usize),
3180 end: (usize, usize),
3181 kind: crate::vim::RangeKind,
3182 register: char,
3183 ) {
3184 vim::yank_range_bridge(self, start, end, kind, register);
3185 }
3186
3187 /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
3188 /// operator). The deleted text is stashed in `register`. On return the
3189 /// editor is in Insert mode; the caller must not issue further normal-mode
3190 /// ops until the insert session ends.
3191 ///
3192 /// Pure range-mutation primitive — does not consume input. Called by
3193 /// hjkl-vim's visual-mode operator path which has already resolved the range
3194 /// from the visual selection.
3195 ///
3196 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3197 /// grammar migration (kryptic-sh/hjkl#70).
3198 pub fn change_range(
3199 &mut self,
3200 start: (usize, usize),
3201 end: (usize, usize),
3202 kind: crate::vim::RangeKind,
3203 register: char,
3204 ) {
3205 vim::change_range_bridge(self, start, end, kind, register);
3206 }
3207
3208 /// Indent (`count > 0`) or outdent (`count < 0`) the row span
3209 /// `[start.0, end.0]`. Column components are ignored — indent is always
3210 /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
3211 /// this call; pass `0` to use the current editor setting. `count == 0` is a
3212 /// no-op.
3213 ///
3214 /// Pure range-mutation primitive — does not consume input. Called by
3215 /// hjkl-vim's visual-mode operator path which has already resolved the range
3216 /// from the visual selection.
3217 ///
3218 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3219 /// grammar migration (kryptic-sh/hjkl#70).
3220 pub fn indent_range(
3221 &mut self,
3222 start: (usize, usize),
3223 end: (usize, usize),
3224 count: i32,
3225 shiftwidth: u32,
3226 ) {
3227 vim::indent_range_bridge(self, start, end, count, shiftwidth);
3228 }
3229
3230 /// Apply a case transformation (`Operator::Uppercase` /
3231 /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3232 /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3233 /// Yanks registers are left untouched — vim's case operators do not write
3234 /// to registers.
3235 ///
3236 /// Pure range-mutation primitive — does not consume input. Called by
3237 /// hjkl-vim's visual-mode operator path which has already resolved the range
3238 /// from the visual selection.
3239 ///
3240 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3241 /// grammar migration (kryptic-sh/hjkl#70).
3242 pub fn case_range(
3243 &mut self,
3244 start: (usize, usize),
3245 end: (usize, usize),
3246 kind: crate::vim::RangeKind,
3247 op: crate::vim::Operator,
3248 ) {
3249 vim::case_range_bridge(self, start, end, kind, op);
3250 }
3251
3252 // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3253 //
3254 // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3255 // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3256 // Ragged-edge handling (short lines not reaching `right_col`) matches the
3257 // engine FSM's `apply_block_operator` path — short lines lose only the
3258 // chars that exist.
3259 //
3260 // `register` is the target register; `'"'` selects the unnamed register.
3261
3262 /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3263 /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3264 /// bounds at the visual (display) column level. Ragged-edge handling
3265 /// matches engine FSM's VisualBlock op behavior — short lines that don't
3266 /// reach `right_col` lose only the chars that exist.
3267 ///
3268 /// `register` honors the user's pending register selection.
3269 ///
3270 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3271 pub fn delete_block(
3272 &mut self,
3273 top_row: usize,
3274 bot_row: usize,
3275 left_col: usize,
3276 right_col: usize,
3277 register: char,
3278 ) {
3279 vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3280 }
3281
3282 /// Yank a rectangular VisualBlock selection into `register` without
3283 /// mutating the buffer. `'"'` selects the unnamed register.
3284 ///
3285 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3286 pub fn yank_block(
3287 &mut self,
3288 top_row: usize,
3289 bot_row: usize,
3290 left_col: usize,
3291 right_col: usize,
3292 register: char,
3293 ) {
3294 vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3295 }
3296
3297 /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3298 /// operator). The deleted text is stashed in `register`. Mode is Insert
3299 /// on return; the caller must not issue further normal-mode ops until the
3300 /// insert session ends.
3301 ///
3302 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3303 pub fn change_block(
3304 &mut self,
3305 top_row: usize,
3306 bot_row: usize,
3307 left_col: usize,
3308 right_col: usize,
3309 register: char,
3310 ) {
3311 vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3312 }
3313
3314 /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3315 /// Column bounds are ignored — vim's block indent is always linewise.
3316 /// `count == 0` is a no-op.
3317 ///
3318 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3319 pub fn indent_block(
3320 &mut self,
3321 top_row: usize,
3322 bot_row: usize,
3323 _left_col: usize,
3324 _right_col: usize,
3325 count: i32,
3326 ) {
3327 vim::indent_block_bridge(self, top_row, bot_row, count);
3328 }
3329
3330 /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3331 /// Column components are ignored — auto-indent is always linewise.
3332 ///
3333 /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3334 /// row 0 to compute the correct depth at `start.0`, then for each line in
3335 /// the target range strips existing leading whitespace and prepends
3336 /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3337 /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3338 /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3339 /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3340 ///
3341 /// After the operation the cursor lands on the first non-whitespace
3342 /// character of `start_row` (vim parity for `==`).
3343 ///
3344 /// **v1 limitation**: the bracket scan does not detect brackets inside
3345 /// string literals or comments. Code such as `let s = "{";` will increment
3346 /// the depth counter even though the brace is not a structural opener.
3347 /// Tree-sitter / LSP indentation is deferred to a follow-up.
3348 pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3349 vim::auto_indent_range_bridge(self, start, end);
3350 }
3351
3352 /// Drain the row range set by the most recent auto-indent operation.
3353 ///
3354 /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3355 /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3356 /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3357 /// uses this to arm a brief visual flash over the reindented rows.
3358 pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3359 self.last_indent_range.take()
3360 }
3361
3362 // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3363 //
3364 // Pure functions — no cursor mutation, no mode change, no register write.
3365 // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3366 // the existing `word_text_object` private resolver in vim.rs.
3367 //
3368 // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3369 // before invoking a range-mutation primitive (`delete_range`, etc.).
3370 //
3371 // Return value: `Some((start, end))` where both positions are `(row, col)`
3372 // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3373 // on), matching the convention used by `delete_range` / `yank_range` / etc.
3374 // Returns `None` when the cursor is on an empty line or the resolver cannot
3375 // find a word boundary.
3376
3377 /// Resolve the range of `iw` (inner word) at the current cursor position.
3378 ///
3379 /// An inner word is the contiguous run of keyword characters (or punctuation
3380 /// characters if the cursor is on punctuation) under the cursor, without any
3381 /// surrounding whitespace. Whitespace-only positions return `None`.
3382 ///
3383 /// Pure function — does not move the cursor or change any editor state.
3384 /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3385 /// invoking a range-mutation primitive (`delete_range`, etc.).
3386 ///
3387 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3388 /// migration (kryptic-sh/hjkl#70).
3389 pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3390 vim::text_object_inner_word_bridge(self)
3391 }
3392
3393 /// Resolve the range of `aw` (around word) at the current cursor position.
3394 ///
3395 /// Like `iw` but extends the range to include trailing whitespace after the
3396 /// word. If no trailing whitespace exists, leading whitespace before the word
3397 /// is absorbed instead (vim `:help text-objects` behaviour).
3398 ///
3399 /// Pure function — does not move the cursor or change any editor state.
3400 ///
3401 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3402 /// migration (kryptic-sh/hjkl#70).
3403 pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3404 vim::text_object_around_word_bridge(self)
3405 }
3406
3407 /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3408 ///
3409 /// A WORD is any contiguous run of non-whitespace characters — punctuation
3410 /// is not treated as a word boundary. Returns the span of the WORD under the
3411 /// cursor, without surrounding whitespace.
3412 ///
3413 /// Pure function — does not move the cursor or change any editor state.
3414 ///
3415 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3416 /// migration (kryptic-sh/hjkl#70).
3417 pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3418 vim::text_object_inner_big_word_bridge(self)
3419 }
3420
3421 /// Resolve the range of `aW` (around WORD) at the current cursor position.
3422 ///
3423 /// Like `iW` but extends the range to include trailing whitespace after the
3424 /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3425 /// is absorbed instead.
3426 ///
3427 /// Pure function — does not move the cursor or change any editor state.
3428 ///
3429 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3430 /// migration (kryptic-sh/hjkl#70).
3431 pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3432 vim::text_object_around_big_word_bridge(self)
3433 }
3434
3435 // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3436 //
3437 // Pure functions — no cursor mutation, no mode change, no register write.
3438 // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3439 // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3440 // in vim.rs.
3441 //
3442 // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3443 // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3444 // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3445 // hjkl-vim grammar layer normalises close→open before calling these methods.
3446 //
3447 // Return value: `Some((start, end))` where both positions are `(row, col)`
3448 // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3449 // on), matching the convention used by `delete_range` / `yank_range` / etc.
3450 // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3451 // ranges for multi-line pairs; that tag is stripped here — callers receive
3452 // the same flat shape as all other text-object resolvers.
3453
3454 /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3455 ///
3456 /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3457 /// cursor's line contains fewer than two occurrences of `quote`, or when no
3458 /// matching pair can be found around or ahead of the cursor.
3459 ///
3460 /// Inner range excludes the quote characters themselves.
3461 ///
3462 /// Pure function — no cursor mutation.
3463 ///
3464 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3465 /// migration (kryptic-sh/hjkl#70).
3466 pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3467 vim::text_object_inner_quote_bridge(self, quote)
3468 }
3469
3470 /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3471 ///
3472 /// Like `i<quote>` but includes the quote characters themselves plus
3473 /// surrounding whitespace on one side: trailing whitespace after the closing
3474 /// quote if any exists; otherwise leading whitespace before the opening
3475 /// quote. This matches vim `:help text-objects` behaviour.
3476 ///
3477 /// Pure function — no cursor mutation.
3478 ///
3479 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3480 /// migration (kryptic-sh/hjkl#70).
3481 pub fn text_object_around_quote(
3482 &self,
3483 quote: char,
3484 ) -> Option<((usize, usize), (usize, usize))> {
3485 vim::text_object_around_quote_bridge(self, quote)
3486 }
3487
3488 /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3489 ///
3490 /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3491 /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3492 /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3493 /// calling this method. Returns `None` when no enclosing pair is found.
3494 ///
3495 /// The cursor may be anywhere inside the pair or on a bracket character
3496 /// itself. When not inside any pair the resolver falls back to a forward
3497 /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3498 ///
3499 /// Inner range excludes the bracket characters. Multi-line pairs are
3500 /// supported; the returned range spans the full content between the
3501 /// brackets.
3502 ///
3503 /// Pure function — no cursor mutation.
3504 ///
3505 /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3506 /// handled here.
3507 ///
3508 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3509 /// migration (kryptic-sh/hjkl#70).
3510 pub fn text_object_inner_bracket(
3511 &self,
3512 open: char,
3513 ) -> Option<((usize, usize), (usize, usize))> {
3514 vim::text_object_inner_bracket_bridge(self, open)
3515 }
3516
3517 /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
3518 ///
3519 /// Like `i<bracket>` but includes the bracket characters themselves.
3520 /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
3521 ///
3522 /// Pure function — no cursor mutation.
3523 ///
3524 /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
3525 ///
3526 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3527 /// migration (kryptic-sh/hjkl#70).
3528 pub fn text_object_around_bracket(
3529 &self,
3530 open: char,
3531 ) -> Option<((usize, usize), (usize, usize))> {
3532 vim::text_object_around_bracket_bridge(self, open)
3533 }
3534
3535 // ── Sentence text objects (is / as) ───────────────────────────────────
3536
3537 /// Resolve `is` (inner sentence) at the cursor position.
3538 ///
3539 /// Returns the range of the current sentence, excluding trailing
3540 /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
3541 /// `?` / `!` followed by whitespace or end-of-paragraph).
3542 ///
3543 /// Pure function — no cursor mutation.
3544 ///
3545 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3546 /// grammar migration (kryptic-sh/hjkl#70).
3547 pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3548 vim::text_object_inner_sentence_bridge(self)
3549 }
3550
3551 /// Resolve `as` (around sentence) at the cursor position.
3552 ///
3553 /// Like `is` but includes trailing whitespace after the sentence
3554 /// terminator.
3555 ///
3556 /// Pure function — no cursor mutation.
3557 ///
3558 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3559 /// grammar migration (kryptic-sh/hjkl#70).
3560 pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3561 vim::text_object_around_sentence_bridge(self)
3562 }
3563
3564 // ── Paragraph text objects (ip / ap) ──────────────────────────────────
3565
3566 /// Resolve `ip` (inner paragraph) at the cursor position.
3567 ///
3568 /// A paragraph is a block of non-blank lines bounded by blank lines or
3569 /// buffer edges. Returns `None` when the cursor is on a blank line.
3570 ///
3571 /// Pure function — no cursor mutation.
3572 ///
3573 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3574 /// grammar migration (kryptic-sh/hjkl#70).
3575 pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3576 vim::text_object_inner_paragraph_bridge(self)
3577 }
3578
3579 /// Resolve `ap` (around paragraph) at the cursor position.
3580 ///
3581 /// Like `ip` but includes one trailing blank line when present.
3582 ///
3583 /// Pure function — no cursor mutation.
3584 ///
3585 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3586 /// grammar migration (kryptic-sh/hjkl#70).
3587 pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3588 vim::text_object_around_paragraph_bridge(self)
3589 }
3590
3591 // ── Tag text objects (it / at) ────────────────────────────────────────
3592
3593 /// Resolve `it` (inner tag) at the cursor position.
3594 ///
3595 /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
3596 /// inner content between the open and close tags (excluding the tags
3597 /// themselves).
3598 ///
3599 /// Pure function — no cursor mutation.
3600 ///
3601 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3602 /// grammar migration (kryptic-sh/hjkl#70).
3603 pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3604 vim::text_object_inner_tag_bridge(self)
3605 }
3606
3607 /// Resolve `at` (around tag) at the cursor position.
3608 ///
3609 /// Like `it` but includes the open and close tag delimiters themselves.
3610 ///
3611 /// Pure function — no cursor mutation.
3612 ///
3613 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3614 /// grammar migration (kryptic-sh/hjkl#70).
3615 pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3616 vim::text_object_around_tag_bridge(self)
3617 }
3618
3619 /// Execute a named cursor motion `kind` repeated `count` times.
3620 ///
3621 /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
3622 /// motion primitives, bypassing the engine FSM. Identical cursor semantics
3623 /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
3624 /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
3625 /// used by the FSM arms.
3626 ///
3627 /// Introduced in 0.6.1 as the host entry point for Phase 3a of
3628 /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
3629 /// calls this method rather than re-entering the engine FSM.
3630 ///
3631 /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
3632 /// intact for macro-replay coverage (macros re-feed raw keys through the
3633 /// FSM). This method is the keymap / controller path only.
3634 pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
3635 vim::apply_motion_kind(self, kind, count);
3636 }
3637
3638 /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
3639 /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
3640 /// chars are silently ignored (no-op), matching the engine FSM's
3641 /// `handle_select_register` behaviour.
3642 ///
3643 /// Promoted to the public surface in 0.5.17 so the hjkl-vim
3644 /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
3645 /// without re-entering the engine FSM. `handle_select_register` (engine FSM
3646 /// path for macro-replay / defensive coverage) delegates here to avoid
3647 /// logic duplication.
3648 pub fn set_pending_register(&mut self, reg: char) {
3649 if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
3650 self.vim.pending_register = Some(reg);
3651 }
3652 // Invalid chars silently no-op (matches engine FSM behavior).
3653 }
3654
3655 /// Record a mark named `ch` at the current cursor position.
3656 ///
3657 /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
3658 /// rules). Invalid chars are silently ignored (no-op), matching the engine
3659 /// FSM's `handle_set_mark` behaviour.
3660 ///
3661 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3662 /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
3663 /// without re-entering the engine FSM. `handle_set_mark` delegates here.
3664 pub fn set_mark_at_cursor(&mut self, ch: char) {
3665 vim::set_mark_at_cursor(self, ch);
3666 }
3667
3668 /// `.` dot-repeat: replay the last buffered change at the current cursor.
3669 /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
3670 /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
3671 ///
3672 /// Storage of `LastChange` stays inside engine for now; Phase 5c of
3673 /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
3674 /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
3675 /// FSM `.` arm stays for macro-replay defensive coverage.
3676 pub fn replay_last_change(&mut self, count: usize) {
3677 vim::replay_last_change(self, count);
3678 }
3679
3680 /// Jump to the mark named `ch`, linewise (row only; col snaps to first
3681 /// non-blank). Pushes the pre-jump position onto the jumplist if the
3682 /// cursor actually moved.
3683 ///
3684 /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
3685 /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3686 /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3687 /// are silently ignored (no-op), matching the engine FSM's
3688 /// `handle_goto_mark` behaviour.
3689 ///
3690 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3691 /// `PendingState::GotoMarkLine` reducer can dispatch
3692 /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
3693 pub fn goto_mark_line(&mut self, ch: char) {
3694 vim::goto_mark(self, ch, true);
3695 }
3696
3697 /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
3698 /// pre-jump position onto the jumplist if the cursor actually moved.
3699 ///
3700 /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
3701 /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3702 /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3703 /// are silently ignored (no-op), matching the engine FSM's
3704 /// `handle_goto_mark` behaviour.
3705 ///
3706 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3707 /// `PendingState::GotoMarkChar` reducer can dispatch
3708 /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
3709 pub fn goto_mark_char(&mut self, ch: char) {
3710 vim::goto_mark(self, ch, false);
3711 }
3712
3713 // ── Macro controller API (Phase 5b) ──────────────────────────────────────
3714
3715 /// Begin recording keystrokes into register `reg`. The caller (app) is
3716 /// responsible for stopping the recording via `stop_macro_record` when the
3717 /// user presses bare `q`.
3718 ///
3719 /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
3720 /// recording by pre-seeding `recording_keys` with the decoded text of the
3721 /// matching lowercase register, matching vim's capital-register append
3722 /// semantics.
3723 /// - Lowercase `reg` clears `recording_keys` (fresh recording).
3724 /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
3725 ///
3726 /// Promoted to the public surface in Phase 5b so the app's
3727 /// `route_chord_key` can start a recording without re-entering the engine
3728 /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
3729 /// defensive coverage) continues to use the same logic via delegation.
3730 pub fn start_macro_record(&mut self, reg: char) {
3731 if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
3732 return;
3733 }
3734 self.vim.recording_macro = Some(reg);
3735 if reg.is_ascii_uppercase() {
3736 // Seed recording_keys with the existing lowercase register's text
3737 // decoded back to inputs so capital-register append continues from
3738 // where the previous recording left off.
3739 let lower = reg.to_ascii_lowercase();
3740 let text = self
3741 .registers
3742 .read(lower)
3743 .map(|s| s.text.clone())
3744 .unwrap_or_default();
3745 self.vim.recording_keys = crate::input::decode_macro(&text);
3746 } else {
3747 self.vim.recording_keys.clear();
3748 }
3749 }
3750
3751 /// Finalize the active recording: encode `recording_keys` as text and write
3752 /// to the matching (lowercase) named register. Clears both `recording_macro`
3753 /// and `recording_keys`. No-ops if no recording is active.
3754 ///
3755 /// Promoted to the public surface in Phase 5b so the app's `QChord` action
3756 /// can stop a recording when the user presses bare `q` without re-entering
3757 /// the engine FSM.
3758 pub fn stop_macro_record(&mut self) {
3759 let Some(reg) = self.vim.recording_macro.take() else {
3760 return;
3761 };
3762 let keys = std::mem::take(&mut self.vim.recording_keys);
3763 let text = crate::input::encode_macro(&keys);
3764 self.set_named_register_text(reg.to_ascii_lowercase(), text);
3765 }
3766
3767 /// Returns `true` while a `q{reg}` recording is in progress.
3768 /// Hosts use this to show a "recording @r" status indicator and to decide
3769 /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
3770 /// chord.
3771 pub fn is_recording_macro(&self) -> bool {
3772 self.vim.recording_macro.is_some()
3773 }
3774
3775 /// Returns `true` while a macro is being replayed. The app sets this flag
3776 /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
3777 /// re-feed loop so the recorder hook can skip double-capture.
3778 pub fn is_replaying_macro(&self) -> bool {
3779 self.vim.replaying_macro
3780 }
3781
3782 /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
3783 /// prepare for replay, returning the inputs the app should re-feed through
3784 /// `route_chord_key`.
3785 ///
3786 /// Resolves `reg`:
3787 /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
3788 /// - Any other char → lowercase it, read the register, decode.
3789 ///
3790 /// Side-effects:
3791 /// - Sets `vim.last_macro` to the resolved register.
3792 /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
3793 /// replay. The app calls `end_macro_replay` after the loop finishes.
3794 ///
3795 /// Returns an empty vec (and no side-effects for `'@'`) if the register is
3796 /// unset or empty.
3797 pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
3798 let resolved = if reg == '@' {
3799 match self.vim.last_macro {
3800 Some(r) => r,
3801 None => return vec![],
3802 }
3803 } else {
3804 reg.to_ascii_lowercase()
3805 };
3806 let text = match self.registers.read(resolved) {
3807 Some(slot) if !slot.text.is_empty() => slot.text.clone(),
3808 _ => return vec![],
3809 };
3810 let keys = crate::input::decode_macro(&text);
3811 self.vim.last_macro = Some(resolved);
3812 self.vim.replaying_macro = true;
3813 // Multiply by count (minimum 1).
3814 keys.repeat(count.max(1))
3815 }
3816
3817 /// Clear the `replaying_macro` flag. Called by the app after the
3818 /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
3819 pub fn end_macro_replay(&mut self) {
3820 self.vim.replaying_macro = false;
3821 }
3822
3823 /// Append `input` to the active recording (`recording_keys`) if and only
3824 /// if a recording is in progress AND we are not currently replaying.
3825 /// Called by the app's `route_chord_key` recorder hook so that user
3826 /// keystrokes captured through the app-level chord path are recorded
3827 /// (rather than relying solely on the engine FSM's in-step hook).
3828 pub fn record_input(&mut self, input: crate::input::Input) {
3829 if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
3830 self.vim.recording_keys.push(input);
3831 }
3832 }
3833
3834 // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
3835 //
3836 // Each method is the publicly callable form of one insert-mode action.
3837 // All logic lives in the corresponding `vim::*_bridge` free function;
3838 // these methods are thin delegators so the public surface stays on `Editor`.
3839 //
3840 // Invariants (enforced by the bridge fns):
3841 // - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
3842 // - Navigation keys call `break_undo_group_in_insert` when the FSM did.
3843 // - `push_buffer_cursor_to_textarea` is called after every mutation
3844 // (currently a no-op, kept for migration hygiene).
3845
3846 /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
3847 /// the cursor instead of inserting; at end-of-line, always appends. With
3848 /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
3849 /// dedent on an otherwise-whitespace line.
3850 ///
3851 /// Callers must ensure the editor is in Insert or Replace mode before
3852 /// calling this method.
3853 pub fn insert_char(&mut self, ch: char) {
3854 let mutated = vim::insert_char_bridge(self, ch);
3855 if mutated {
3856 self.mark_content_dirty();
3857 let (row, _) = self.cursor();
3858 self.vim.widen_insert_row(row);
3859 }
3860 }
3861
3862 /// Insert a newline at the cursor, applying autoindent / smartindent to
3863 /// prefix the new line with the appropriate leading whitespace.
3864 ///
3865 /// Callers must ensure the editor is in Insert mode before calling.
3866 pub fn insert_newline(&mut self) {
3867 let mutated = vim::insert_newline_bridge(self);
3868 if mutated {
3869 self.mark_content_dirty();
3870 let (row, _) = self.cursor();
3871 self.vim.widen_insert_row(row);
3872 }
3873 }
3874
3875 /// Insert a tab character (or spaces up to the next `softtabstop` boundary
3876 /// when `expandtab` is set).
3877 ///
3878 /// Callers must ensure the editor is in Insert mode before calling.
3879 pub fn insert_tab(&mut self) {
3880 let mutated = vim::insert_tab_bridge(self);
3881 if mutated {
3882 self.mark_content_dirty();
3883 let (row, _) = self.cursor();
3884 self.vim.widen_insert_row(row);
3885 }
3886 }
3887
3888 /// Delete the character before the cursor (Backspace). With `softtabstop`
3889 /// active, deletes the entire soft-tab run at an aligned boundary. Joins
3890 /// with the previous line when at column 0.
3891 ///
3892 /// Callers must ensure the editor is in Insert mode before calling.
3893 pub fn insert_backspace(&mut self) {
3894 let mutated = vim::insert_backspace_bridge(self);
3895 if mutated {
3896 self.mark_content_dirty();
3897 let (row, _) = self.cursor();
3898 self.vim.widen_insert_row(row);
3899 }
3900 }
3901
3902 /// Delete the character under the cursor (Delete key). Joins with the
3903 /// next line when at end-of-line.
3904 ///
3905 /// Callers must ensure the editor is in Insert mode before calling.
3906 pub fn insert_delete(&mut self) {
3907 let mutated = vim::insert_delete_bridge(self);
3908 if mutated {
3909 self.mark_content_dirty();
3910 let (row, _) = self.cursor();
3911 self.vim.widen_insert_row(row);
3912 }
3913 }
3914
3915 /// Move the cursor one step in `dir` (arrow key), breaking the undo group
3916 /// per `undo_break_on_motion`.
3917 ///
3918 /// Callers must ensure the editor is in Insert mode before calling.
3919 pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
3920 vim::insert_arrow_bridge(self, dir);
3921 let (row, _) = self.cursor();
3922 self.vim.widen_insert_row(row);
3923 }
3924
3925 /// Move the cursor to the start of the current line (Home key), breaking
3926 /// the undo group.
3927 ///
3928 /// Callers must ensure the editor is in Insert mode before calling.
3929 pub fn insert_home(&mut self) {
3930 vim::insert_home_bridge(self);
3931 let (row, _) = self.cursor();
3932 self.vim.widen_insert_row(row);
3933 }
3934
3935 /// Move the cursor to the end of the current line (End key), breaking the
3936 /// undo group.
3937 ///
3938 /// Callers must ensure the editor is in Insert mode before calling.
3939 pub fn insert_end(&mut self) {
3940 vim::insert_end_bridge(self);
3941 let (row, _) = self.cursor();
3942 self.vim.widen_insert_row(row);
3943 }
3944
3945 /// Scroll up one full viewport height (PageUp), moving the cursor with it.
3946 /// `viewport_h` is the current viewport height in rows; pass
3947 /// `self.viewport_height_value()` if the stored value is current.
3948 ///
3949 /// Callers must ensure the editor is in Insert mode before calling.
3950 pub fn insert_pageup(&mut self, viewport_h: u16) {
3951 vim::insert_pageup_bridge(self, viewport_h);
3952 let (row, _) = self.cursor();
3953 self.vim.widen_insert_row(row);
3954 }
3955
3956 /// Scroll down one full viewport height (PageDown), moving the cursor with
3957 /// it. `viewport_h` is the current viewport height in rows.
3958 ///
3959 /// Callers must ensure the editor is in Insert mode before calling.
3960 pub fn insert_pagedown(&mut self, viewport_h: u16) {
3961 vim::insert_pagedown_bridge(self, viewport_h);
3962 let (row, _) = self.cursor();
3963 self.vim.widen_insert_row(row);
3964 }
3965
3966 /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
3967 /// At column 0, joins with the previous line (vim `b`-motion semantics).
3968 ///
3969 /// Callers must ensure the editor is in Insert mode before calling.
3970 pub fn insert_ctrl_w(&mut self) {
3971 let mutated = vim::insert_ctrl_w_bridge(self);
3972 if mutated {
3973 self.mark_content_dirty();
3974 let (row, _) = self.cursor();
3975 self.vim.widen_insert_row(row);
3976 }
3977 }
3978
3979 /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
3980 /// No-op when already at column 0.
3981 ///
3982 /// Callers must ensure the editor is in Insert mode before calling.
3983 pub fn insert_ctrl_u(&mut self) {
3984 let mutated = vim::insert_ctrl_u_bridge(self);
3985 if mutated {
3986 self.mark_content_dirty();
3987 let (row, _) = self.cursor();
3988 self.vim.widen_insert_row(row);
3989 }
3990 }
3991
3992 /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
3993 /// insert mode. Joins with the previous line when at col 0.
3994 ///
3995 /// Callers must ensure the editor is in Insert mode before calling.
3996 pub fn insert_ctrl_h(&mut self) {
3997 let mutated = vim::insert_ctrl_h_bridge(self);
3998 if mutated {
3999 self.mark_content_dirty();
4000 let (row, _) = self.cursor();
4001 self.vim.widen_insert_row(row);
4002 }
4003 }
4004
4005 /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
4006 /// complete normal-mode command, then return to insert automatically.
4007 ///
4008 /// Callers must ensure the editor is in Insert mode before calling.
4009 pub fn insert_ctrl_o_arm(&mut self) {
4010 vim::insert_ctrl_o_bridge(self);
4011 }
4012
4013 /// Arm the register-paste selector (`Ctrl-R`). The next call to
4014 /// `insert_paste_register(reg)` will insert the register contents.
4015 /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
4016 /// the armed state and paste register `c`.
4017 ///
4018 /// Callers must ensure the editor is in Insert mode before calling.
4019 pub fn insert_ctrl_r_arm(&mut self) {
4020 vim::insert_ctrl_r_bridge(self);
4021 }
4022
4023 /// Indent the current line by one `shiftwidth` and shift the cursor right
4024 /// by the same amount (`Ctrl-T`).
4025 ///
4026 /// Callers must ensure the editor is in Insert mode before calling.
4027 pub fn insert_ctrl_t(&mut self) {
4028 let mutated = vim::insert_ctrl_t_bridge(self);
4029 if mutated {
4030 self.mark_content_dirty();
4031 let (row, _) = self.cursor();
4032 self.vim.widen_insert_row(row);
4033 }
4034 }
4035
4036 /// Outdent the current line by up to one `shiftwidth` and shift the cursor
4037 /// left by the amount stripped (`Ctrl-D`).
4038 ///
4039 /// Callers must ensure the editor is in Insert mode before calling.
4040 pub fn insert_ctrl_d(&mut self) {
4041 let mutated = vim::insert_ctrl_d_bridge(self);
4042 if mutated {
4043 self.mark_content_dirty();
4044 let (row, _) = self.cursor();
4045 self.vim.widen_insert_row(row);
4046 }
4047 }
4048
4049 /// Paste the contents of register `reg` at the cursor (the commit arm of
4050 /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
4051 ///
4052 /// Callers must ensure the editor is in Insert mode before calling.
4053 pub fn insert_paste_register(&mut self, reg: char) {
4054 vim::insert_paste_register_bridge(self, reg);
4055 let (row, _) = self.cursor();
4056 self.vim.widen_insert_row(row);
4057 }
4058
4059 /// Exit insert mode to Normal: finish the insert session, step the cursor
4060 /// one cell left (vim convention on Esc), record the `gi` target position,
4061 /// and update the sticky column.
4062 ///
4063 /// Callers must ensure the editor is in Insert mode before calling.
4064 pub fn leave_insert_to_normal(&mut self) {
4065 vim::leave_insert_to_normal_bridge(self);
4066 }
4067
4068 // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
4069 //
4070 // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4071 // `vim.rs` following the same pattern as Phase 6.1. The FSM's
4072 // `handle_normal_only` now calls the same bridges so both paths are
4073 // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
4074
4075 /// `i` — transition to Insert mode at the current cursor position.
4076 /// `count` is stored in the insert session and replayed by dot-repeat
4077 /// as a repeat count on the inserted text.
4078 pub fn enter_insert_i(&mut self, count: usize) {
4079 vim::enter_insert_i_bridge(self, count);
4080 }
4081
4082 /// `I` — move to the first non-blank character on the line, then
4083 /// transition to Insert mode. `count` is stored for dot-repeat.
4084 pub fn enter_insert_shift_i(&mut self, count: usize) {
4085 vim::enter_insert_shift_i_bridge(self, count);
4086 }
4087
4088 /// `a` — advance the cursor one cell past the current position, then
4089 /// transition to Insert mode (append). `count` is stored for dot-repeat.
4090 pub fn enter_insert_a(&mut self, count: usize) {
4091 vim::enter_insert_a_bridge(self, count);
4092 }
4093
4094 /// `A` — move the cursor to the end of the line, then transition to
4095 /// Insert mode (append at end). `count` is stored for dot-repeat.
4096 pub fn enter_insert_shift_a(&mut self, count: usize) {
4097 vim::enter_insert_shift_a_bridge(self, count);
4098 }
4099
4100 /// `o` — open a new line below the current line with smart-indent, then
4101 /// transition to Insert mode. `count` is stored for dot-repeat replay.
4102 pub fn open_line_below(&mut self, count: usize) {
4103 vim::open_line_below_bridge(self, count);
4104 }
4105
4106 /// `O` — open a new line above the current line with smart-indent, then
4107 /// transition to Insert mode. `count` is stored for dot-repeat replay.
4108 pub fn open_line_above(&mut self, count: usize) {
4109 vim::open_line_above_bridge(self, count);
4110 }
4111
4112 /// `R` — enter Replace mode: subsequent typed characters overstrike the
4113 /// cell under the cursor rather than inserting. `count` is for replay.
4114 pub fn enter_replace_mode(&mut self, count: usize) {
4115 vim::enter_replace_mode_bridge(self, count);
4116 }
4117
4118 /// `x` — delete `count` characters forward from the cursor and write them
4119 /// to the unnamed register. No-op on an empty line. Records for `.`.
4120 pub fn delete_char_forward(&mut self, count: usize) {
4121 vim::delete_char_forward_bridge(self, count);
4122 }
4123
4124 /// `X` — delete `count` characters backward from the cursor and write
4125 /// them to the unnamed register. No-op at column 0. Records for `.`.
4126 pub fn delete_char_backward(&mut self, count: usize) {
4127 vim::delete_char_backward_bridge(self, count);
4128 }
4129
4130 /// `s` — substitute `count` characters: delete them (writing to the
4131 /// unnamed register) then enter Insert mode. Equivalent to `cl`.
4132 /// Records as `OpMotion { Change, Right }` for dot-repeat.
4133 pub fn substitute_char(&mut self, count: usize) {
4134 vim::substitute_char_bridge(self, count);
4135 }
4136
4137 /// `S` — substitute the current line: wipe its contents (writing to the
4138 /// unnamed register) then enter Insert mode. Equivalent to `cc`.
4139 /// Records as `LineOp { Change }` for dot-repeat.
4140 pub fn substitute_line(&mut self, count: usize) {
4141 vim::substitute_line_bridge(self, count);
4142 }
4143
4144 /// `D` — delete from the cursor to end-of-line, writing to the unnamed
4145 /// register. The cursor parks on the new last character. Records for `.`.
4146 pub fn delete_to_eol(&mut self) {
4147 vim::delete_to_eol_bridge(self);
4148 }
4149
4150 /// `C` — change from the cursor to end-of-line: delete to EOL then enter
4151 /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
4152 /// (the insert session records `DeleteToEol` on exit, like `c` motions).
4153 pub fn change_to_eol(&mut self) {
4154 vim::change_to_eol_bridge(self);
4155 }
4156
4157 /// `Y` — yank from the cursor to end-of-line into the unnamed register.
4158 /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
4159 pub fn yank_to_eol(&mut self, count: usize) {
4160 vim::yank_to_eol_bridge(self, count);
4161 }
4162
4163 /// `J` — join `count` lines (default 2) onto the current line, inserting
4164 /// a single space between each non-empty pair. Records for dot-repeat.
4165 pub fn join_line(&mut self, count: usize) {
4166 vim::join_line_bridge(self, count);
4167 }
4168
4169 /// `~` — toggle the case of `count` characters from the cursor, advancing
4170 /// right after each toggle. Records `ToggleCase` for dot-repeat.
4171 pub fn toggle_case_at_cursor(&mut self, count: usize) {
4172 vim::toggle_case_at_cursor_bridge(self, count);
4173 }
4174
4175 /// `p` — paste the unnamed register (or the register selected via `"r`)
4176 /// after the cursor. Linewise content opens a new line below; charwise
4177 /// content is inserted inline. Records `Paste { before: false }` for `.`.
4178 pub fn paste_after(&mut self, count: usize) {
4179 vim::paste_after_bridge(self, count);
4180 }
4181
4182 /// `P` — paste the unnamed register (or the `"r` register) before the
4183 /// cursor. Linewise content opens a new line above; charwise is inline.
4184 /// Records `Paste { before: true }` for dot-repeat.
4185 pub fn paste_before(&mut self, count: usize) {
4186 vim::paste_before_bridge(self, count);
4187 }
4188
4189 /// `<C-o>` — jump back `count` entries in the jumplist, saving the
4190 /// current position on the forward stack so `<C-i>` can return.
4191 pub fn jump_back(&mut self, count: usize) {
4192 vim::jump_back_bridge(self, count);
4193 }
4194
4195 /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
4196 /// saving the current position on the backward stack.
4197 pub fn jump_forward(&mut self, count: usize) {
4198 vim::jump_forward_bridge(self, count);
4199 }
4200
4201 /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
4202 /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
4203 /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
4204 pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
4205 vim::scroll_full_page_bridge(self, dir, count);
4206 }
4207
4208 /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
4209 /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
4210 pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
4211 vim::scroll_half_page_bridge(self, dir, count);
4212 }
4213
4214 /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
4215 /// the cursor (cursor is clamped to the new visible region if necessary).
4216 /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
4217 pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
4218 vim::scroll_line_bridge(self, dir, count);
4219 }
4220
4221 /// `n` — repeat the last `/` or `?` search `count` times in its original
4222 /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4223 pub fn search_repeat(&mut self, forward: bool, count: usize) {
4224 vim::search_repeat_bridge(self, forward, count);
4225 }
4226
4227 /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4228 /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4229 /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4230 pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4231 vim::word_search_bridge(self, forward, whole_word, count);
4232 }
4233
4234 // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4235 //
4236 // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4237 // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4238 // and these wrappers write `current_mode` so `vim_mode()` returns correct
4239 // values regardless of which path performed the transition.
4240 // See kryptic-sh/hjkl#89 for the full promotion plan.
4241
4242 /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4243 /// at the current cursor position.
4244 pub fn enter_visual_char(&mut self) {
4245 vim::enter_visual_char_bridge(self);
4246 }
4247
4248 /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4249 /// line. Motions extend the selection by whole lines.
4250 pub fn enter_visual_line(&mut self) {
4251 vim::enter_visual_line_bridge(self);
4252 }
4253
4254 /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4255 /// rectangle whose corners are the anchor and the live cursor.
4256 pub fn enter_visual_block(&mut self) {
4257 vim::enter_visual_block_bridge(self);
4258 }
4259
4260 /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4261 /// for `gv` re-entry, then return to Normal mode.
4262 pub fn exit_visual_to_normal(&mut self) {
4263 vim::exit_visual_to_normal_bridge(self);
4264 }
4265
4266 /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4267 /// so the user can extend the other end of the selection. Does NOT
4268 /// mutate the selection range; only the active endpoint changes.
4269 pub fn visual_o_toggle(&mut self) {
4270 vim::visual_o_toggle_bridge(self);
4271 }
4272
4273 /// `gv` — restore the last visual selection (mode + anchor + cursor
4274 /// position). No-op when no visual selection has been exited yet.
4275 pub fn reenter_last_visual(&mut self) {
4276 vim::reenter_last_visual_bridge(self);
4277 }
4278
4279 /// Direct mode-transition entry point. Sets both the internal FSM mode
4280 /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4281 ///
4282 /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4283 /// …) which also set up required bookkeeping (anchors, sessions, …).
4284 /// Use `set_mode` only when you need a raw mode flip without side-effects.
4285 pub fn set_mode(&mut self, mode: VimMode) {
4286 vim::set_mode_bridge(self, mode);
4287 }
4288}
4289
4290// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4291//
4292// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4293// `Editor` accessors and mutators defined in this block. Each method gets a
4294// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4295// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4296
4297/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4298///
4299/// Treat as opaque — construct by calling `begin_step` and pass the
4300/// returned value directly into `end_step` without modification.
4301/// The fields capture per-step pre-dispatch state that the epilogue
4302/// needs to run its invariants correctly.
4303pub struct StepBookkeeping {
4304 /// True when the pending chord before this step was a macro-chord
4305 /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4306 /// keys so that only the *payload* keys enter `recording_keys`.
4307 pub pending_was_macro_chord: bool,
4308 /// True when the mode was Insert *before* the FSM body ran. Used by
4309 /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4310 /// back into Insert.
4311 pub was_insert: bool,
4312 /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4313 /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4314 /// store `last_visual` for `gv`.
4315 pub pre_visual_snapshot: Option<vim::LastVisual>,
4316}
4317
4318impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4319 // ── Pending chord ─────────────────────────────────────────────────────────
4320
4321 /// Return a clone of the current pending chord state.
4322 pub fn pending(&self) -> vim::Pending {
4323 self.vim.pending.clone()
4324 }
4325
4326 /// Overwrite the pending chord state.
4327 pub fn set_pending(&mut self, p: vim::Pending) {
4328 self.vim.pending = p;
4329 }
4330
4331 /// Atomically take the pending chord, replacing it with `Pending::None`.
4332 pub fn take_pending(&mut self) -> vim::Pending {
4333 std::mem::take(&mut self.vim.pending)
4334 }
4335
4336 // ── Count prefix ──────────────────────────────────────────────────────────
4337
4338 /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4339 pub fn count(&self) -> usize {
4340 self.vim.count
4341 }
4342
4343 /// Overwrite the digit-prefix count directly.
4344 pub fn set_count(&mut self, c: usize) {
4345 self.vim.count = c;
4346 }
4347
4348 /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4349 pub fn accumulate_count_digit(&mut self, digit: usize) {
4350 self.vim.count = self.vim.count.saturating_mul(10) + digit;
4351 }
4352
4353 /// Reset the count prefix to zero (no pending count).
4354 pub fn reset_count(&mut self) {
4355 self.vim.count = 0;
4356 }
4357
4358 /// Consume the count and return it; resets to zero. Returns `1` when no
4359 /// prefix was typed (mirrors `take_count` in vim.rs).
4360 pub fn take_count(&mut self) -> usize {
4361 if self.vim.count > 0 {
4362 let n = self.vim.count;
4363 self.vim.count = 0;
4364 n
4365 } else {
4366 1
4367 }
4368 }
4369
4370 // ── Internal FSM mode ─────────────────────────────────────────────────────
4371
4372 /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4373 pub fn fsm_mode(&self) -> vim::Mode {
4374 self.vim.mode
4375 }
4376
4377 /// Overwrite the FSM-internal mode without side-effects. Prefer the
4378 /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4379 pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4380 self.vim.mode = m;
4381 self.vim.current_mode = self.vim.public_mode();
4382 }
4383
4384 // ── Replaying flag ────────────────────────────────────────────────────────
4385
4386 /// `true` while the `.` dot-repeat replay is running.
4387 pub fn is_replaying(&self) -> bool {
4388 self.vim.replaying
4389 }
4390
4391 /// Set or clear the dot-replay flag.
4392 pub fn set_replaying(&mut self, v: bool) {
4393 self.vim.replaying = v;
4394 }
4395
4396 // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4397
4398 /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4399 /// to Insert after the next complete command.
4400 pub fn is_one_shot_normal(&self) -> bool {
4401 self.vim.one_shot_normal
4402 }
4403
4404 /// Set or clear the Ctrl-o one-shot-normal flag.
4405 pub fn set_one_shot_normal(&mut self, v: bool) {
4406 self.vim.one_shot_normal = v;
4407 }
4408
4409 // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4410
4411 /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4412 /// `None` before any find command was executed.
4413 pub fn last_find(&self) -> Option<(char, bool, bool)> {
4414 self.vim.last_find
4415 }
4416
4417 /// Overwrite the stored last-find target.
4418 pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4419 self.vim.last_find = target;
4420 }
4421
4422 // ── Last change (dot-repeat payload) ─────────────────────────────────────
4423
4424 /// Return a clone of the last recorded mutating change, or `None` before
4425 /// any change has been made.
4426 pub fn last_change(&self) -> Option<vim::LastChange> {
4427 self.vim.last_change.clone()
4428 }
4429
4430 /// Overwrite the stored last-change record.
4431 pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4432 self.vim.last_change = lc;
4433 }
4434
4435 /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4436 /// field after the insert session completes).
4437 pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4438 self.vim.last_change.as_mut()
4439 }
4440
4441 // ── Insert session ────────────────────────────────────────────────────────
4442
4443 /// Borrow the active insert session, or `None` when not in Insert mode.
4444 pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4445 self.vim.insert_session.as_ref()
4446 }
4447
4448 /// Borrow the active insert session mutably.
4449 pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4450 self.vim.insert_session.as_mut()
4451 }
4452
4453 /// Atomically take the insert session out, leaving `None`.
4454 pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4455 self.vim.insert_session.take()
4456 }
4457
4458 /// Install a new insert session, replacing any existing one.
4459 pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4460 self.vim.insert_session = s;
4461 }
4462
4463 // ── Visual anchors ────────────────────────────────────────────────────────
4464
4465 /// Return the charwise Visual-mode anchor `(row, col)`.
4466 pub fn visual_anchor(&self) -> (usize, usize) {
4467 self.vim.visual_anchor
4468 }
4469
4470 /// Overwrite the charwise Visual-mode anchor.
4471 pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4472 self.vim.visual_anchor = anchor;
4473 }
4474
4475 /// Return the VisualLine anchor row.
4476 pub fn visual_line_anchor(&self) -> usize {
4477 self.vim.visual_line_anchor
4478 }
4479
4480 /// Overwrite the VisualLine anchor row.
4481 pub fn set_visual_line_anchor(&mut self, row: usize) {
4482 self.vim.visual_line_anchor = row;
4483 }
4484
4485 /// Return the VisualBlock anchor `(row, col)`.
4486 pub fn block_anchor(&self) -> (usize, usize) {
4487 self.vim.block_anchor
4488 }
4489
4490 /// Overwrite the VisualBlock anchor.
4491 pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4492 self.vim.block_anchor = anchor;
4493 }
4494
4495 /// Return the VisualBlock virtual column used to survive j/k row clamping.
4496 pub fn block_vcol(&self) -> usize {
4497 self.vim.block_vcol
4498 }
4499
4500 /// Overwrite the VisualBlock virtual column.
4501 pub fn set_block_vcol(&mut self, vcol: usize) {
4502 self.vim.block_vcol = vcol;
4503 }
4504
4505 // ── Yank linewise flag ────────────────────────────────────────────────────
4506
4507 /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
4508 pub fn yank_linewise(&self) -> bool {
4509 self.vim.yank_linewise
4510 }
4511
4512 /// Set or clear the linewise-yank flag.
4513 pub fn set_yank_linewise(&mut self, v: bool) {
4514 self.vim.yank_linewise = v;
4515 }
4516
4517 // ── Pending register selector ─────────────────────────────────────────────
4518 // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
4519 // Only the mutators are new here.
4520
4521 /// Overwrite the pending register selector (Phase 6.6b mutator companion to
4522 /// the existing `pending_register()` getter).
4523 pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
4524 self.vim.pending_register = reg;
4525 }
4526
4527 /// Atomically take the pending register, returning `None` afterward.
4528 pub fn take_pending_register_raw(&mut self) -> Option<char> {
4529 self.vim.pending_register.take()
4530 }
4531
4532 // ── Macro recording ───────────────────────────────────────────────────────
4533
4534 /// Return the register currently being recorded into, or `None`.
4535 pub fn recording_macro(&self) -> Option<char> {
4536 self.vim.recording_macro
4537 }
4538
4539 /// Overwrite the recording-macro target register.
4540 pub fn set_recording_macro(&mut self, reg: Option<char>) {
4541 self.vim.recording_macro = reg;
4542 }
4543
4544 /// Append one input to the in-progress macro recording buffer.
4545 pub fn push_recording_key(&mut self, input: crate::input::Input) {
4546 self.vim.recording_keys.push(input);
4547 }
4548
4549 /// Atomically take the recorded key sequence, leaving an empty vec.
4550 pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
4551 std::mem::take(&mut self.vim.recording_keys)
4552 }
4553
4554 /// Overwrite the recording-keys buffer (e.g. to seed from a register).
4555 pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
4556 self.vim.recording_keys = keys;
4557 }
4558
4559 // ── Macro replay flag ─────────────────────────────────────────────────────
4560
4561 /// `true` while `@reg` macro replay is running (suppresses re-recording).
4562 pub fn is_replaying_macro_raw(&self) -> bool {
4563 self.vim.replaying_macro
4564 }
4565
4566 /// Set or clear the macro-replay-in-progress flag.
4567 pub fn set_replaying_macro_raw(&mut self, v: bool) {
4568 self.vim.replaying_macro = v;
4569 }
4570
4571 // ── Last macro register ───────────────────────────────────────────────────
4572
4573 /// Return the register of the most recently played macro (`@@` source).
4574 pub fn last_macro(&self) -> Option<char> {
4575 self.vim.last_macro
4576 }
4577
4578 /// Overwrite the last-played-macro register.
4579 pub fn set_last_macro(&mut self, reg: Option<char>) {
4580 self.vim.last_macro = reg;
4581 }
4582
4583 // ── Last insert position ──────────────────────────────────────────────────
4584
4585 /// Return the cursor position when Insert mode was last exited (for `gi`).
4586 pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
4587 self.vim.last_insert_pos
4588 }
4589
4590 /// Overwrite the stored last-insert position.
4591 pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
4592 self.vim.last_insert_pos = pos;
4593 }
4594
4595 // ── Last visual selection ─────────────────────────────────────────────────
4596
4597 /// Return the saved visual selection snapshot for `gv`, or `None`.
4598 pub fn last_visual(&self) -> Option<vim::LastVisual> {
4599 self.vim.last_visual
4600 }
4601
4602 /// Overwrite the saved visual selection snapshot.
4603 pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
4604 self.vim.last_visual = snap;
4605 }
4606
4607 // ── Viewport-pinned flag ──────────────────────────────────────────────────
4608
4609 /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
4610 /// the end-of-step scrolloff pass).
4611 pub fn viewport_pinned(&self) -> bool {
4612 self.vim.viewport_pinned
4613 }
4614
4615 /// Set or clear the viewport-pinned flag.
4616 pub fn set_viewport_pinned(&mut self, v: bool) {
4617 self.vim.viewport_pinned = v;
4618 }
4619
4620 // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
4621
4622 /// `true` while waiting for the register-name key after `Ctrl-R` in
4623 /// Insert mode.
4624 pub fn insert_pending_register(&self) -> bool {
4625 self.vim.insert_pending_register
4626 }
4627
4628 /// Set or clear the `Ctrl-R` register-wait flag.
4629 pub fn set_insert_pending_register(&mut self, v: bool) {
4630 self.vim.insert_pending_register = v;
4631 }
4632
4633 // ── Change-mark start ─────────────────────────────────────────────────────
4634
4635 /// Return the stashed `[` mark start for a Change operation, or `None`.
4636 pub fn change_mark_start(&self) -> Option<(usize, usize)> {
4637 self.vim.change_mark_start
4638 }
4639
4640 /// Atomically take the change-mark start, leaving `None`.
4641 pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
4642 self.vim.change_mark_start.take()
4643 }
4644
4645 /// Overwrite the change-mark start.
4646 pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
4647 self.vim.change_mark_start = pos;
4648 }
4649
4650 // ── Timeout tracking ──────────────────────────────────────────────────────
4651
4652 /// Return the wall-clock `Instant` of the last keystroke.
4653 pub fn last_input_at(&self) -> Option<std::time::Instant> {
4654 self.vim.last_input_at
4655 }
4656
4657 /// Overwrite the wall-clock last-input timestamp.
4658 pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
4659 self.vim.last_input_at = t;
4660 }
4661
4662 /// Return the `Host::now()` duration at the last keystroke.
4663 pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
4664 self.vim.last_input_host_at
4665 }
4666
4667 /// Overwrite the host-clock last-input timestamp.
4668 pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
4669 self.vim.last_input_host_at = d;
4670 }
4671
4672 // ── Search prompt ──────────────────────────────────────────────────────────
4673
4674 /// Borrow the live search prompt, or `None` when not in search-prompt mode.
4675 pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
4676 self.vim.search_prompt.as_ref()
4677 }
4678
4679 /// Borrow the live search prompt mutably.
4680 pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
4681 self.vim.search_prompt.as_mut()
4682 }
4683
4684 /// Atomically take the search prompt, leaving `None`.
4685 pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
4686 self.vim.search_prompt.take()
4687 }
4688
4689 /// Install a new search prompt (entering search-prompt mode).
4690 pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
4691 self.vim.search_prompt = prompt;
4692 }
4693
4694 // ── Last search pattern / direction ───────────────────────────────────────
4695 // Note: `last_search_forward()` getter already exists at line ~1909.
4696 // `set_last_search()` combined mutator exists at line ~1918.
4697 // Only new / complementary accessors are added here.
4698
4699 /// Return the most recently committed search pattern, or `None`.
4700 pub fn last_search_pattern(&self) -> Option<&str> {
4701 self.vim.last_search.as_deref()
4702 }
4703
4704 /// Overwrite the stored last-search pattern without changing direction
4705 /// (use the existing `set_last_search` for the combined update).
4706 pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
4707 self.vim.last_search = pattern;
4708 }
4709
4710 /// Overwrite only the last-search direction flag.
4711 pub fn set_last_search_forward_only(&mut self, forward: bool) {
4712 self.vim.last_search_forward = forward;
4713 }
4714
4715 // ── Search history ────────────────────────────────────────────────────────
4716
4717 /// Borrow the committed search-pattern history (oldest first).
4718 pub fn search_history(&self) -> &[String] {
4719 &self.vim.search_history
4720 }
4721
4722 /// Borrow the search history mutably (e.g. to push a new entry).
4723 pub fn search_history_mut(&mut self) -> &mut Vec<String> {
4724 &mut self.vim.search_history
4725 }
4726
4727 /// Return the current search-history navigation cursor index.
4728 pub fn search_history_cursor(&self) -> Option<usize> {
4729 self.vim.search_history_cursor
4730 }
4731
4732 /// Overwrite the search-history navigation cursor.
4733 pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
4734 self.vim.search_history_cursor = idx;
4735 }
4736
4737 // ── Jump lists ────────────────────────────────────────────────────────────
4738
4739 /// Borrow the back half of the jump list (entries Ctrl-o pops from).
4740 pub fn jump_back_list(&self) -> &[(usize, usize)] {
4741 &self.vim.jump_back
4742 }
4743
4744 /// Borrow the back jump list mutably (push / pop).
4745 pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4746 &mut self.vim.jump_back
4747 }
4748
4749 /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
4750 pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
4751 &self.vim.jump_fwd
4752 }
4753
4754 /// Borrow the forward jump list mutably (push / pop / clear).
4755 pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4756 &mut self.vim.jump_fwd
4757 }
4758
4759 // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
4760 //
4761 // `push_search_pattern`, `push_jump`, `record_search_history`, and
4762 // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
4763 // search-prompt and normal-mode FSM can call them via the public API.
4764
4765 /// Compile `pattern` into a regex and install it as the active search
4766 /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
4767 /// invalid pattern clears the highlight without raising an error.
4768 pub fn push_search_pattern(&mut self, pattern: &str) {
4769 let compiled = if pattern.is_empty() {
4770 None
4771 } else {
4772 let case_insensitive = self.settings().ignore_case
4773 && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
4774 let effective: std::borrow::Cow<'_, str> = if case_insensitive {
4775 std::borrow::Cow::Owned(format!("(?i){pattern}"))
4776 } else {
4777 std::borrow::Cow::Borrowed(pattern)
4778 };
4779 regex::Regex::new(&effective).ok()
4780 };
4781 let wrap = self.settings().wrapscan;
4782 self.set_search_pattern(compiled);
4783 self.search_state_mut().wrap_around = wrap;
4784 }
4785
4786 /// Record a pre-jump cursor position onto the back jumplist. Called
4787 /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
4788 /// committed `/` or `?`, …). Branching off the history clears the
4789 /// forward half, matching vim's "redo-is-lost" semantics.
4790 pub fn push_jump(&mut self, from: (usize, usize)) {
4791 self.vim.jump_back.push(from);
4792 if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
4793 self.vim.jump_back.remove(0);
4794 }
4795 self.vim.jump_fwd.clear();
4796 }
4797
4798 /// Push `pattern` onto the committed search history. Skips if the
4799 /// most recent entry already matches (consecutive dedupe) and trims
4800 /// the oldest entries beyond the history cap.
4801 pub fn record_search_history(&mut self, pattern: &str) {
4802 if pattern.is_empty() {
4803 return;
4804 }
4805 if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
4806 return;
4807 }
4808 self.vim.search_history.push(pattern.to_string());
4809 let len = self.vim.search_history.len();
4810 if len > vim::SEARCH_HISTORY_MAX {
4811 self.vim
4812 .search_history
4813 .drain(0..len - vim::SEARCH_HISTORY_MAX);
4814 }
4815 }
4816
4817 /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
4818 /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
4819 /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
4820 /// active search prompt.
4821 pub fn walk_search_history(&mut self, dir: isize) {
4822 if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
4823 return;
4824 }
4825 let len = self.vim.search_history.len();
4826 let next_idx = match (self.vim.search_history_cursor, dir) {
4827 (None, -1) => Some(len - 1),
4828 (None, 1) => return,
4829 (Some(i), -1) => i.checked_sub(1),
4830 (Some(i), 1) if i + 1 < len => Some(i + 1),
4831 _ => None,
4832 };
4833 let Some(idx) = next_idx else {
4834 return;
4835 };
4836 self.vim.search_history_cursor = Some(idx);
4837 let text = self.vim.search_history[idx].clone();
4838 if let Some(prompt) = self.vim.search_prompt.as_mut() {
4839 prompt.cursor = text.chars().count();
4840 prompt.text = text.clone();
4841 }
4842 self.push_search_pattern(&text);
4843 }
4844
4845 // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
4846 //
4847 // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
4848 // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
4849
4850 /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
4851 ///
4852 /// Call this at the start of every step; pass the returned
4853 /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
4854 ///
4855 /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
4856 /// Returns `Err(consumed)` when the prelude itself handled the input
4857 /// (macro-stop chord); in that case skip the FSM body and do NOT call
4858 /// `end_step` — the macro-stop path is a true short-circuit with no
4859 /// epilogue needed.
4860 ///
4861 /// This method does NOT handle the search-prompt intercept — callers
4862 /// must check `search_prompt_state().is_some()` before calling `begin_step`
4863 /// and dispatch to the search-prompt FSM body directly.
4864 pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
4865 use crate::input::Key;
4866 use vim::{Mode, Pending};
4867 // ── Timestamps ───────────────────────────────────────────────────────
4868 // Phase 7f: sync buffer before motion handlers see it.
4869 self.sync_buffer_content_from_textarea();
4870 // `:set timeoutlen` chord-timeout handling.
4871 let now = std::time::Instant::now();
4872 let host_now = self.host.now();
4873 let timed_out = match self.vim.last_input_host_at {
4874 Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
4875 None => false,
4876 };
4877 if timed_out {
4878 let chord_in_flight = !matches!(self.vim.pending, Pending::None)
4879 || self.vim.count != 0
4880 || self.vim.pending_register.is_some()
4881 || self.vim.insert_pending_register;
4882 if chord_in_flight {
4883 self.vim.clear_pending_prefix();
4884 }
4885 }
4886 self.vim.last_input_at = Some(now);
4887 self.vim.last_input_host_at = Some(host_now);
4888 // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
4889 if self.vim.recording_macro.is_some()
4890 && !self.vim.replaying_macro
4891 && matches!(self.vim.pending, Pending::None)
4892 && self.vim.mode != Mode::Insert
4893 && input.key == Key::Char('q')
4894 && !input.ctrl
4895 && !input.alt
4896 {
4897 let reg = self.vim.recording_macro.take().unwrap();
4898 let keys = std::mem::take(&mut self.vim.recording_keys);
4899 let text = crate::input::encode_macro(&keys);
4900 self.set_named_register_text(reg.to_ascii_lowercase(), text);
4901 return Err(true);
4902 }
4903 // ── Snapshots for epilogue ────────────────────────────────────────────
4904 let pending_was_macro_chord = matches!(
4905 self.vim.pending,
4906 Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
4907 );
4908 let was_insert = self.vim.mode == Mode::Insert;
4909 let pre_visual_snapshot = match self.vim.mode {
4910 Mode::Visual => Some(vim::LastVisual {
4911 mode: Mode::Visual,
4912 anchor: self.vim.visual_anchor,
4913 cursor: self.cursor(),
4914 block_vcol: 0,
4915 }),
4916 Mode::VisualLine => Some(vim::LastVisual {
4917 mode: Mode::VisualLine,
4918 anchor: (self.vim.visual_line_anchor, 0),
4919 cursor: self.cursor(),
4920 block_vcol: 0,
4921 }),
4922 Mode::VisualBlock => Some(vim::LastVisual {
4923 mode: Mode::VisualBlock,
4924 anchor: self.vim.block_anchor,
4925 cursor: self.cursor(),
4926 block_vcol: self.vim.block_vcol,
4927 }),
4928 _ => None,
4929 };
4930 Ok(StepBookkeeping {
4931 pending_was_macro_chord,
4932 was_insert,
4933 pre_visual_snapshot,
4934 })
4935 }
4936
4937 /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
4938 ///
4939 /// `input` is the same input that was passed to `begin_step`.
4940 /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
4941 /// `consumed` is the return value of the FSM body; this method returns
4942 /// it after running all epilogue invariants.
4943 ///
4944 /// Must NOT be called when `begin_step` returned `Err(...)`.
4945 pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
4946 use crate::input::Key;
4947 use vim::{Mode, Pending};
4948 let StepBookkeeping {
4949 pending_was_macro_chord,
4950 was_insert,
4951 pre_visual_snapshot,
4952 } = bk;
4953 // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
4954 if let Some(snap) = pre_visual_snapshot
4955 && !matches!(
4956 self.vim.mode,
4957 Mode::Visual | Mode::VisualLine | Mode::VisualBlock
4958 )
4959 {
4960 let (lo, hi) = match snap.mode {
4961 Mode::Visual => {
4962 if snap.anchor <= snap.cursor {
4963 (snap.anchor, snap.cursor)
4964 } else {
4965 (snap.cursor, snap.anchor)
4966 }
4967 }
4968 Mode::VisualLine => {
4969 let r_lo = snap.anchor.0.min(snap.cursor.0);
4970 let r_hi = snap.anchor.0.max(snap.cursor.0);
4971 let last_col = self
4972 .buffer()
4973 .lines()
4974 .get(r_hi)
4975 .map(|l| l.chars().count().saturating_sub(1))
4976 .unwrap_or(0);
4977 ((r_lo, 0), (r_hi, last_col))
4978 }
4979 Mode::VisualBlock => {
4980 let (r1, c1) = snap.anchor;
4981 let (r2, c2) = snap.cursor;
4982 ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
4983 }
4984 _ => {
4985 if snap.anchor <= snap.cursor {
4986 (snap.anchor, snap.cursor)
4987 } else {
4988 (snap.cursor, snap.anchor)
4989 }
4990 }
4991 };
4992 self.set_mark('<', lo);
4993 self.set_mark('>', hi);
4994 self.vim.last_visual = Some(snap);
4995 }
4996 // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
4997 if !was_insert
4998 && self.vim.one_shot_normal
4999 && self.vim.mode == Mode::Normal
5000 && matches!(self.vim.pending, Pending::None)
5001 {
5002 self.vim.one_shot_normal = false;
5003 self.vim.mode = Mode::Insert;
5004 }
5005 // ── Content + viewport sync ───────────────────────────────────────────
5006 self.sync_buffer_content_from_textarea();
5007 if !self.vim.viewport_pinned {
5008 self.ensure_cursor_in_scrolloff();
5009 }
5010 self.vim.viewport_pinned = false;
5011 // ── Recorder hook ─────────────────────────────────────────────────────
5012 if self.vim.recording_macro.is_some()
5013 && !self.vim.replaying_macro
5014 && input.key != Key::Char('q')
5015 && !pending_was_macro_chord
5016 {
5017 self.vim.recording_keys.push(input);
5018 }
5019 // ── Phase 6.3: current_mode sync ─────────────────────────────────────
5020 self.vim.current_mode = self.vim.public_mode();
5021 consumed
5022 }
5023
5024 // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
5025
5026 /// `true` when the editor is in any visual mode (Visual / VisualLine /
5027 /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
5028 pub fn is_visual(&self) -> bool {
5029 matches!(
5030 self.vim.mode,
5031 vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
5032 )
5033 }
5034
5035 /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
5036 /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
5037 /// virtual column, which survives j/k clamping to shorter rows).
5038 ///
5039 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
5040 /// extents needed for VisualBlock `I` / `A` / `r` without accessing
5041 /// engine-private helpers.
5042 pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
5043 let (ar, ac) = self.vim.block_anchor;
5044 let (cr, _) = self.cursor();
5045 let cc = self.vim.block_vcol;
5046 let top = ar.min(cr);
5047 let bot = ar.max(cr);
5048 let left = ac.min(cc);
5049 let right = ac.max(cc);
5050 (top, bot, left, right)
5051 }
5052
5053 /// Return the character count (code-point count) of line `row`, or `0`
5054 /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
5055 /// I / A column computations.
5056 pub fn line_char_count(&self, row: usize) -> usize {
5057 buf_line_chars(&self.buffer, row)
5058 }
5059
5060 /// Apply operator over `motion` with `count` repetitions. The full
5061 /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
5062 ///
5063 /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
5064 /// relocated `handle_after_op` can call it directly with a parsed `Motion`
5065 /// without re-entering the engine FSM.
5066 pub fn apply_op_with_motion_direct(
5067 &mut self,
5068 op: crate::vim::Operator,
5069 motion: &crate::vim::Motion,
5070 count: usize,
5071 ) {
5072 vim::apply_op_with_motion(self, op, motion, count);
5073 }
5074
5075 /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
5076 /// `delta = 1` increments; `delta = -1` decrements; larger deltas
5077 /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
5078 /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
5079 pub fn adjust_number(&mut self, delta: i64) {
5080 vim::adjust_number(self, delta);
5081 }
5082
5083 /// Open the `/` or `?` search prompt. `forward = true` for `/`,
5084 /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
5085 /// dispatch `/` and `?` without re-entering the engine FSM.
5086 pub fn enter_search(&mut self, forward: bool) {
5087 vim::enter_search(self, forward);
5088 }
5089
5090 /// Enter Insert mode at the left edge of a VisualBlock selection for
5091 /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
5092 /// then begins an insert session with `InsertReason::BlockEdge`.
5093 ///
5094 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5095 /// VisualBlock `I` command without accessing engine-private helpers.
5096 pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
5097 self.jump_cursor(top, col);
5098 self.vim.mode = vim::Mode::Normal;
5099 vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5100 }
5101
5102 /// Enter Insert mode at the right edge of a VisualBlock selection for
5103 /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
5104 /// then begins an insert session with `InsertReason::BlockEdge`.
5105 ///
5106 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5107 /// VisualBlock `A` command without accessing engine-private helpers.
5108 pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
5109 self.jump_cursor(top, col);
5110 self.vim.mode = vim::Mode::Normal;
5111 vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5112 }
5113
5114 /// Execute a motion (cursor movement), push to the jumplist for big jumps,
5115 /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
5116 /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5117 pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
5118 vim::execute_motion(self, motion, count);
5119 }
5120
5121 /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
5122 /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
5123 /// non-h/l motions leave it alone so the intended column survives clamping
5124 /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5125 pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
5126 vim::update_block_vcol(self, motion);
5127 }
5128
5129 /// Apply `op` over the current visual selection (char-wise, linewise, or
5130 /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
5131 /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
5132 pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
5133 vim::apply_visual_operator(self, op);
5134 }
5135
5136 /// Replace each character cell in the current VisualBlock selection with
5137 /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
5138 /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
5139 pub fn replace_block_char(&mut self, ch: char) {
5140 vim::block_replace(self, ch);
5141 }
5142
5143 /// Extend the current visual selection to cover the text object identified
5144 /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
5145 /// via `text_object_range`, then updates the visual anchor and cursor.
5146 ///
5147 /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
5148 /// in `hjkl-vim::normal::handle_visual_text_obj`.
5149 pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
5150 use crate::vim::{Mode, TextObject};
5151 let obj = match ch {
5152 'w' => TextObject::Word { big: false },
5153 'W' => TextObject::Word { big: true },
5154 '"' | '\'' | '`' => TextObject::Quote(ch),
5155 '(' | ')' | 'b' => TextObject::Bracket('('),
5156 '[' | ']' => TextObject::Bracket('['),
5157 '{' | '}' | 'B' => TextObject::Bracket('{'),
5158 '<' | '>' => TextObject::Bracket('<'),
5159 'p' => TextObject::Paragraph,
5160 't' => TextObject::XmlTag,
5161 's' => TextObject::Sentence,
5162 _ => return,
5163 };
5164 let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
5165 return;
5166 };
5167 match kind {
5168 crate::vim::RangeKind::Linewise => {
5169 self.vim.visual_line_anchor = start.0;
5170 self.vim.mode = Mode::VisualLine;
5171 self.vim.current_mode = VimMode::VisualLine;
5172 self.jump_cursor(end.0, 0);
5173 }
5174 _ => {
5175 self.vim.mode = Mode::Visual;
5176 self.vim.current_mode = VimMode::Visual;
5177 self.vim.visual_anchor = (start.0, start.1);
5178 let (er, ec) = vim::retreat_one(self, end);
5179 self.jump_cursor(er, ec);
5180 }
5181 }
5182 }
5183}
5184
5185/// Visual column of the character at `char_col` in `line`, treating `\t`
5186/// as expansion to the next `tab_width` stop and every other char as
5187/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
5188/// the cursor math elsewhere also assumes single-cell chars.
5189fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
5190 let mut visual = 0usize;
5191 for (i, ch) in line.chars().enumerate() {
5192 if i >= char_col {
5193 break;
5194 }
5195 if ch == '\t' {
5196 visual += tab_width - (visual % tab_width);
5197 } else {
5198 visual += 1;
5199 }
5200 }
5201 visual
5202}
5203
5204#[cfg(feature = "crossterm")]
5205impl From<KeyEvent> for Input {
5206 fn from(key: KeyEvent) -> Self {
5207 let k = match key.code {
5208 KeyCode::Char(c) => Key::Char(c),
5209 KeyCode::Backspace => Key::Backspace,
5210 KeyCode::Delete => Key::Delete,
5211 KeyCode::Enter => Key::Enter,
5212 KeyCode::Left => Key::Left,
5213 KeyCode::Right => Key::Right,
5214 KeyCode::Up => Key::Up,
5215 KeyCode::Down => Key::Down,
5216 KeyCode::Home => Key::Home,
5217 KeyCode::End => Key::End,
5218 KeyCode::Tab => Key::Tab,
5219 KeyCode::Esc => Key::Esc,
5220 _ => Key::Null,
5221 };
5222 Input {
5223 key: k,
5224 ctrl: key.modifiers.contains(KeyModifiers::CONTROL),
5225 alt: key.modifiers.contains(KeyModifiers::ALT),
5226 shift: key.modifiers.contains(KeyModifiers::SHIFT),
5227 }
5228 }
5229}
5230
5231/// Crossterm `KeyEvent` → engine `Input`. Thin wrapper that delegates
5232/// to the [`From`] impl above; kept as a free fn for the in-tree
5233/// callers in the legacy ratatui-coupled paths.
5234#[cfg(feature = "crossterm")]
5235pub fn crossterm_to_input(key: KeyEvent) -> Input {
5236 Input::from(key)
5237}
5238
5239#[cfg(all(test, feature = "crossterm", feature = "ratatui"))]
5240mod tests {
5241 use super::*;
5242 use crate::types::Host;
5243 use crossterm::event::KeyEvent;
5244
5245 #[allow(dead_code)]
5246 fn key(code: KeyCode) -> KeyEvent {
5247 KeyEvent::new(code, KeyModifiers::NONE)
5248 }
5249 #[allow(dead_code)]
5250 fn shift_key(code: KeyCode) -> KeyEvent {
5251 KeyEvent::new(code, KeyModifiers::SHIFT)
5252 }
5253 #[allow(dead_code)]
5254 fn ctrl_key(code: KeyCode) -> KeyEvent {
5255 KeyEvent::new(code, KeyModifiers::CONTROL)
5256 }
5257
5258 #[test]
5259 fn intern_style_dedups_engine_native_styles() {
5260 use crate::types::{Attrs, Color, Style};
5261 let mut e = Editor::new(
5262 hjkl_buffer::Buffer::new(),
5263 crate::types::DefaultHost::new(),
5264 crate::types::Options::default(),
5265 );
5266 let s = Style {
5267 fg: Some(Color(255, 0, 0)),
5268 bg: None,
5269 attrs: Attrs::BOLD,
5270 };
5271 let id_a = e.intern_style(s);
5272 // Re-interning the same engine style returns the same id.
5273 let id_b = e.intern_style(s);
5274 assert_eq!(id_a, id_b);
5275 // Engine accessor returns the same style back.
5276 let back = e.engine_style_at(id_a).expect("interned");
5277 assert_eq!(back, s);
5278 }
5279
5280 #[test]
5281 fn engine_style_at_out_of_range_returns_none() {
5282 let e = Editor::new(
5283 hjkl_buffer::Buffer::new(),
5284 crate::types::DefaultHost::new(),
5285 crate::types::Options::default(),
5286 );
5287 assert!(e.engine_style_at(99).is_none());
5288 }
5289
5290 #[test]
5291 fn options_bridge_roundtrip() {
5292 let mut e = Editor::new(
5293 hjkl_buffer::Buffer::new(),
5294 crate::types::DefaultHost::new(),
5295 crate::types::Options::default(),
5296 );
5297 let opts = e.current_options();
5298 // 0.2.0: defaults flipped to modern editor norms — 4-space soft tabs.
5299 assert_eq!(opts.shiftwidth, 4);
5300 assert_eq!(opts.tabstop, 4);
5301
5302 let new_opts = crate::types::Options {
5303 shiftwidth: 4,
5304 tabstop: 2,
5305 ignorecase: true,
5306 ..crate::types::Options::default()
5307 };
5308 e.apply_options(&new_opts);
5309
5310 let after = e.current_options();
5311 assert_eq!(after.shiftwidth, 4);
5312 assert_eq!(after.tabstop, 2);
5313 assert!(after.ignorecase);
5314 }
5315
5316 #[test]
5317 fn selection_highlight_none_in_normal() {
5318 let mut e = Editor::new(
5319 hjkl_buffer::Buffer::new(),
5320 crate::types::DefaultHost::new(),
5321 crate::types::Options::default(),
5322 );
5323 e.set_content("hello");
5324 assert!(e.selection_highlight().is_none());
5325 }
5326
5327 #[test]
5328 fn highlights_emit_search_matches() {
5329 use crate::types::HighlightKind;
5330 let mut e = Editor::new(
5331 hjkl_buffer::Buffer::new(),
5332 crate::types::DefaultHost::new(),
5333 crate::types::Options::default(),
5334 );
5335 e.set_content("foo bar foo\nbaz qux\n");
5336 // 0.0.35: arm via the engine search state. The buffer
5337 // accessor still works (deprecated) but new code goes
5338 // through Editor.
5339 e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5340 let hs = e.highlights_for_line(0);
5341 assert_eq!(hs.len(), 2);
5342 for h in &hs {
5343 assert_eq!(h.kind, HighlightKind::SearchMatch);
5344 assert_eq!(h.range.start.line, 0);
5345 assert_eq!(h.range.end.line, 0);
5346 }
5347 }
5348
5349 #[test]
5350 fn highlights_empty_without_pattern() {
5351 let mut e = Editor::new(
5352 hjkl_buffer::Buffer::new(),
5353 crate::types::DefaultHost::new(),
5354 crate::types::Options::default(),
5355 );
5356 e.set_content("foo bar");
5357 assert!(e.highlights_for_line(0).is_empty());
5358 }
5359
5360 #[test]
5361 fn highlights_empty_for_out_of_range_line() {
5362 let mut e = Editor::new(
5363 hjkl_buffer::Buffer::new(),
5364 crate::types::DefaultHost::new(),
5365 crate::types::Options::default(),
5366 );
5367 e.set_content("foo");
5368 e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5369 assert!(e.highlights_for_line(99).is_empty());
5370 }
5371
5372 #[test]
5373 fn snapshot_roundtrips_through_restore() {
5374 use crate::types::SnapshotMode;
5375 let mut e = Editor::new(
5376 hjkl_buffer::Buffer::new(),
5377 crate::types::DefaultHost::new(),
5378 crate::types::Options::default(),
5379 );
5380 e.set_content("alpha\nbeta\ngamma");
5381 e.jump_cursor(2, 3);
5382 let snap = e.take_snapshot();
5383 assert_eq!(snap.mode, SnapshotMode::Normal);
5384 assert_eq!(snap.cursor, (2, 3));
5385 assert_eq!(snap.lines.len(), 3);
5386
5387 let mut other = Editor::new(
5388 hjkl_buffer::Buffer::new(),
5389 crate::types::DefaultHost::new(),
5390 crate::types::Options::default(),
5391 );
5392 other.restore_snapshot(snap).expect("restore");
5393 assert_eq!(other.cursor(), (2, 3));
5394 assert_eq!(other.buffer().lines().len(), 3);
5395 }
5396
5397 #[test]
5398 fn restore_snapshot_rejects_version_mismatch() {
5399 let mut e = Editor::new(
5400 hjkl_buffer::Buffer::new(),
5401 crate::types::DefaultHost::new(),
5402 crate::types::Options::default(),
5403 );
5404 let mut snap = e.take_snapshot();
5405 snap.version = 9999;
5406 match e.restore_snapshot(snap) {
5407 Err(crate::EngineError::SnapshotVersion(got, want)) => {
5408 assert_eq!(got, 9999);
5409 assert_eq!(want, crate::types::EditorSnapshot::VERSION);
5410 }
5411 other => panic!("expected SnapshotVersion err, got {other:?}"),
5412 }
5413 }
5414
5415 #[test]
5416 fn take_content_change_returns_some_on_first_dirty() {
5417 let mut e = Editor::new(
5418 hjkl_buffer::Buffer::new(),
5419 crate::types::DefaultHost::new(),
5420 crate::types::Options::default(),
5421 );
5422 e.set_content("hello");
5423 let first = e.take_content_change();
5424 assert!(first.is_some());
5425 let second = e.take_content_change();
5426 assert!(second.is_none());
5427 }
5428
5429 fn many_lines(n: usize) -> String {
5430 (0..n)
5431 .map(|i| format!("line{i}"))
5432 .collect::<Vec<_>>()
5433 .join("\n")
5434 }
5435
5436 #[allow(dead_code)]
5437 fn prime_viewport<H: Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, height: u16) {
5438 e.set_viewport_height(height);
5439 }
5440
5441 /// Contract that the TUI drain relies on: `set_content` flags the
5442 /// editor dirty (so the next `take_dirty` call reports the change),
5443 /// and a second `take_dirty` returns `false` after consumption. The
5444 /// TUI drains this flag after every programmatic content load so
5445 /// opening a tab doesn't get mistaken for a user edit and mark the
5446 /// tab dirty (which would then trigger the quit-prompt on `:q`).
5447 #[test]
5448 fn set_content_dirties_then_take_dirty_clears() {
5449 let mut e = Editor::new(
5450 hjkl_buffer::Buffer::new(),
5451 crate::types::DefaultHost::new(),
5452 crate::types::Options::default(),
5453 );
5454 e.set_content("hello");
5455 assert!(
5456 e.take_dirty(),
5457 "set_content should leave content_dirty=true"
5458 );
5459 assert!(!e.take_dirty(), "take_dirty should clear the flag");
5460 }
5461
5462 #[test]
5463 fn content_arc_cache_invalidated_by_set_content() {
5464 let mut e = Editor::new(
5465 hjkl_buffer::Buffer::new(),
5466 crate::types::DefaultHost::new(),
5467 crate::types::Options::default(),
5468 );
5469 e.set_content("one");
5470 let a = e.content_arc();
5471 e.set_content("two");
5472 let b = e.content_arc();
5473 assert!(!std::sync::Arc::ptr_eq(&a, &b));
5474 assert!(b.starts_with("two"));
5475 }
5476
5477 // ── lnum_width ──────────────────────────────────────────────────────────
5478
5479 #[test]
5480 fn lnum_width_numberwidth_floor_enforced() {
5481 let mut e = Editor::new(
5482 hjkl_buffer::Buffer::new(),
5483 crate::types::DefaultHost::new(),
5484 crate::types::Options::default(),
5485 );
5486 // Default: number=true, numberwidth=4; buffer has 1 line → digits=1,
5487 // needed=2 which is less than floor of 4.
5488 e.set_content("single line");
5489 assert_eq!(e.lnum_width(), 4, "should be floored to numberwidth (4)");
5490 }
5491
5492 #[test]
5493 fn lnum_width_zero_when_both_flags_off() {
5494 let mut e = Editor::new(
5495 hjkl_buffer::Buffer::new(),
5496 crate::types::DefaultHost::new(),
5497 crate::types::Options {
5498 number: false,
5499 relativenumber: false,
5500 ..crate::types::Options::default()
5501 },
5502 );
5503 e.set_content("some content");
5504 assert_eq!(
5505 e.lnum_width(),
5506 0,
5507 "gutter should be 0 when number flags are off"
5508 );
5509 }
5510
5511 // ── doc-coord mouse primitives (Phase 1 — issue #114) ──────────────────
5512
5513 #[test]
5514 fn mouse_click_doc_moves_cursor_to_doc_coords() {
5515 let mut e = Editor::new(
5516 hjkl_buffer::Buffer::new(),
5517 crate::types::DefaultHost::new(),
5518 crate::types::Options::default(),
5519 );
5520 e.set_content("hello\nworld");
5521 e.mouse_click_doc(1, 2);
5522 assert_eq!(e.cursor(), (1, 2));
5523 }
5524
5525 #[test]
5526 fn mouse_click_doc_normal_mode_clamps_past_eol_to_last_char() {
5527 let mut e = Editor::new(
5528 hjkl_buffer::Buffer::new(),
5529 crate::types::DefaultHost::new(),
5530 crate::types::Options::default(),
5531 );
5532 e.set_content("hello");
5533 // Normal mode (default after construction): "hello" has 5 chars,
5534 // past-EOL click clamps to col=4 (last char 'o' — never on the
5535 // implicit \n, vim/neovim convention).
5536 e.mouse_click_doc(0, 99);
5537 assert_eq!(e.cursor(), (0, 4));
5538 }
5539
5540 #[test]
5541 fn mouse_click_doc_normal_mode_clamps_past_eol_multibyte() {
5542 let mut e = Editor::new(
5543 hjkl_buffer::Buffer::new(),
5544 crate::types::DefaultHost::new(),
5545 crate::types::Options::default(),
5546 );
5547 // 5 chars, 6 bytes — clamping must be char-counted, not byte-counted.
5548 e.set_content("héllo");
5549 e.mouse_click_doc(0, 99);
5550 assert_eq!(e.cursor(), (0, 4));
5551 }
5552
5553 #[test]
5554 fn mouse_click_doc_insert_mode_allows_one_past_eol() {
5555 let mut e = Editor::new(
5556 hjkl_buffer::Buffer::new(),
5557 crate::types::DefaultHost::new(),
5558 crate::types::Options::default(),
5559 );
5560 e.set_content("hello");
5561 e.enter_insert_i(1);
5562 // Insert mode allows the one-past-EOL position (col=5 for 5-char
5563 // line) — that's the canonical insert-here sentinel.
5564 e.mouse_click_doc(0, 99);
5565 assert_eq!(e.cursor(), (0, 5));
5566 }
5567
5568 #[test]
5569 fn mouse_click_doc_resets_sticky_col() {
5570 let mut e = Editor::new(
5571 hjkl_buffer::Buffer::new(),
5572 crate::types::DefaultHost::new(),
5573 crate::types::Options::default(),
5574 );
5575 e.set_content("aaaaa\nbb\naaaaa");
5576 // Pretend a previous keyboard motion put intended col at 4 (e.g.
5577 // user navigated $ on row 0).
5578 e.sticky_col = Some(4);
5579 // Click on row 1, col 1 (the second 'b' on a short line).
5580 e.mouse_click_doc(1, 1);
5581 assert_eq!(e.cursor(), (1, 1));
5582 assert_eq!(
5583 e.sticky_col,
5584 Some(1),
5585 "click must reset sticky_col so a subsequent j/k uses the clicked column \
5586 as the intended visual column (not the previous keyboard-tracked col)"
5587 );
5588 }
5589
5590 #[test]
5591 fn mouse_click_doc_exits_visual_mode() {
5592 use crate::VimMode;
5593 let mut e = Editor::new(
5594 hjkl_buffer::Buffer::new(),
5595 crate::types::DefaultHost::new(),
5596 crate::types::Options::default(),
5597 );
5598 e.set_content("hello");
5599 e.enter_visual_char();
5600 assert_eq!(e.vim_mode(), VimMode::Visual);
5601 e.mouse_click_doc(0, 2);
5602 assert_eq!(e.vim_mode(), VimMode::Normal);
5603 assert_eq!(e.cursor(), (0, 2));
5604 }
5605
5606 #[test]
5607 fn set_cursor_doc_clamps_past_last_row() {
5608 let mut e = Editor::new(
5609 hjkl_buffer::Buffer::new(),
5610 crate::types::DefaultHost::new(),
5611 crate::types::Options::default(),
5612 );
5613 e.set_content("one\ntwo");
5614 // doc has 2 rows (0 and 1); row 99 clamps to 1.
5615 e.set_cursor_doc(99, 0);
5616 assert_eq!(e.cursor(), (1, 0));
5617 }
5618
5619 #[test]
5620 fn mouse_begin_drag_enters_visual_char() {
5621 use crate::VimMode;
5622 let mut e = Editor::new(
5623 hjkl_buffer::Buffer::new(),
5624 crate::types::DefaultHost::new(),
5625 crate::types::Options::default(),
5626 );
5627 e.set_content("hello");
5628 e.mouse_begin_drag();
5629 assert_eq!(e.vim_mode(), VimMode::Visual);
5630 }
5631
5632 #[test]
5633 fn mouse_extend_drag_doc_moves_cursor_leaving_visual_anchor() {
5634 use crate::VimMode;
5635 let mut e = Editor::new(
5636 hjkl_buffer::Buffer::new(),
5637 crate::types::DefaultHost::new(),
5638 crate::types::Options::default(),
5639 );
5640 e.set_content("hello world");
5641 e.mouse_begin_drag(); // anchor at (0,0)
5642 e.mouse_extend_drag_doc(0, 5);
5643 assert_eq!(e.vim_mode(), VimMode::Visual);
5644 assert_eq!(e.cursor(), (0, 5));
5645 }
5646
5647 // ── Patch B (0.0.29): Host trait wired into Editor ──
5648
5649 #[test]
5650 fn host_clipboard_round_trip_via_default_host() {
5651 // DefaultHost stores write_clipboard in-memory; read_clipboard
5652 // returns the most recent payload.
5653 let mut e = Editor::new(
5654 hjkl_buffer::Buffer::new(),
5655 crate::types::DefaultHost::new(),
5656 crate::types::Options::default(),
5657 );
5658 e.host_mut().write_clipboard("payload".to_string());
5659 assert_eq!(e.host_mut().read_clipboard().as_deref(), Some("payload"));
5660 }
5661
5662 // ── ContentEdit emission ─────────────────────────────────────────
5663
5664 fn fresh_editor(initial: &str) -> Editor {
5665 let buffer = hjkl_buffer::Buffer::from_str(initial);
5666 Editor::new(
5667 buffer,
5668 crate::types::DefaultHost::new(),
5669 crate::types::Options::default(),
5670 )
5671 }
5672
5673 #[test]
5674 fn content_edit_insert_char_at_origin() {
5675 let mut e = fresh_editor("");
5676 let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5677 at: hjkl_buffer::Position::new(0, 0),
5678 ch: 'a',
5679 });
5680 let edits = e.take_content_edits();
5681 assert_eq!(edits.len(), 1);
5682 let ce = &edits[0];
5683 assert_eq!(ce.start_byte, 0);
5684 assert_eq!(ce.old_end_byte, 0);
5685 assert_eq!(ce.new_end_byte, 1);
5686 assert_eq!(ce.start_position, (0, 0));
5687 assert_eq!(ce.old_end_position, (0, 0));
5688 assert_eq!(ce.new_end_position, (0, 1));
5689 }
5690
5691 #[test]
5692 fn content_edit_insert_str_multiline() {
5693 // Buffer "x\ny" — insert "ab\ncd" at end of row 0.
5694 let mut e = fresh_editor("x\ny");
5695 let _ = e.mutate_edit(hjkl_buffer::Edit::InsertStr {
5696 at: hjkl_buffer::Position::new(0, 1),
5697 text: "ab\ncd".into(),
5698 });
5699 let edits = e.take_content_edits();
5700 assert_eq!(edits.len(), 1);
5701 let ce = &edits[0];
5702 assert_eq!(ce.start_byte, 1);
5703 assert_eq!(ce.old_end_byte, 1);
5704 assert_eq!(ce.new_end_byte, 1 + 5);
5705 assert_eq!(ce.start_position, (0, 1));
5706 // Insertion contains one '\n', so row+1, col = bytes after last '\n' = 2.
5707 assert_eq!(ce.new_end_position, (1, 2));
5708 }
5709
5710 #[test]
5711 fn content_edit_delete_range_charwise() {
5712 // "abcdef" — delete chars 1..4 ("bcd").
5713 let mut e = fresh_editor("abcdef");
5714 let _ = e.mutate_edit(hjkl_buffer::Edit::DeleteRange {
5715 start: hjkl_buffer::Position::new(0, 1),
5716 end: hjkl_buffer::Position::new(0, 4),
5717 kind: hjkl_buffer::MotionKind::Char,
5718 });
5719 let edits = e.take_content_edits();
5720 assert_eq!(edits.len(), 1);
5721 let ce = &edits[0];
5722 assert_eq!(ce.start_byte, 1);
5723 assert_eq!(ce.old_end_byte, 4);
5724 assert_eq!(ce.new_end_byte, 1);
5725 assert!(ce.old_end_byte > ce.new_end_byte);
5726 }
5727
5728 #[test]
5729 fn content_edit_set_content_resets() {
5730 let mut e = fresh_editor("foo");
5731 let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5732 at: hjkl_buffer::Position::new(0, 0),
5733 ch: 'X',
5734 });
5735 // set_content should clear queued edits and raise the reset
5736 // flag on the next take_content_reset.
5737 e.set_content("brand new");
5738 assert!(e.take_content_reset());
5739 // Subsequent call clears the flag.
5740 assert!(!e.take_content_reset());
5741 // Edits cleared on reset.
5742 assert!(e.take_content_edits().is_empty());
5743 }
5744
5745 #[test]
5746 fn content_edit_multiple_replaces_in_order() {
5747 // Three Replace edits applied left-to-right (mimics the
5748 // substitute path's per-match Replace fan-out). Verify each
5749 // mutation queues exactly one ContentEdit and they're drained
5750 // in source-order with structurally valid byte spans.
5751 let mut e = fresh_editor("xax xbx xcx");
5752 let _ = e.take_content_edits();
5753 let _ = e.take_content_reset();
5754 // Replace each "x" with "yy", left to right. After each replace,
5755 // the next match's char-col shifts by +1 (since "yy" is 1 char
5756 // longer than "x" but they're both ASCII so byte = char here).
5757 let positions = [(0usize, 0usize), (0, 4), (0, 8)];
5758 for (row, col) in positions {
5759 let _ = e.mutate_edit(hjkl_buffer::Edit::Replace {
5760 start: hjkl_buffer::Position::new(row, col),
5761 end: hjkl_buffer::Position::new(row, col + 1),
5762 with: "yy".into(),
5763 });
5764 }
5765 let edits = e.take_content_edits();
5766 assert_eq!(edits.len(), 3);
5767 for ce in &edits {
5768 assert!(ce.start_byte <= ce.old_end_byte);
5769 assert!(ce.start_byte <= ce.new_end_byte);
5770 }
5771 // Document order.
5772 for w in edits.windows(2) {
5773 assert!(w[0].start_byte <= w[1].start_byte);
5774 }
5775 }
5776
5777 #[test]
5778 fn replace_char_at_replaces_single_char_under_cursor() {
5779 // Matches vim's `rx` semantics: replace char under cursor.
5780 let mut e = fresh_editor("abc");
5781 e.jump_cursor(0, 1); // cursor on 'b'
5782 e.replace_char_at('X', 1);
5783 let got = e.content();
5784 let got = got.trim_end_matches('\n');
5785 assert_eq!(
5786 got, "aXc",
5787 "replace_char_at(X, 1) must replace 'b' with 'X'"
5788 );
5789 // Cursor stays on the replaced char.
5790 assert_eq!(e.cursor(), (0, 1));
5791 }
5792
5793 #[test]
5794 fn replace_char_at_count_replaces_multiple_chars() {
5795 // `3rx` in vim replaces 3 chars starting at cursor.
5796 let mut e = fresh_editor("abcde");
5797 e.jump_cursor(0, 0);
5798 e.replace_char_at('Z', 3);
5799 let got = e.content();
5800 let got = got.trim_end_matches('\n');
5801 assert_eq!(
5802 got, "ZZZde",
5803 "replace_char_at(Z, 3) must replace first 3 chars"
5804 );
5805 }
5806
5807 #[test]
5808 fn find_char_method_moves_to_target() {
5809 // buffer "abcabc", cursor (0,0), f<c> → cursor (0,2).
5810 let mut e = fresh_editor("abcabc");
5811 e.jump_cursor(0, 0);
5812 e.find_char('c', true, false, 1);
5813 assert_eq!(
5814 e.cursor(),
5815 (0, 2),
5816 "find_char('c', forward=true, till=false, count=1) must land on 'c' at col 2"
5817 );
5818 }
5819
5820 // ── after_g unit tests (Phase 2b-ii) ────────────────────────────────────
5821
5822 #[test]
5823 fn after_g_gg_jumps_to_top() {
5824 let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5825 let mut e = fresh_editor(&content);
5826 e.jump_cursor(15, 0);
5827 e.after_g('g', 1);
5828 assert_eq!(e.cursor().0, 0, "gg must move cursor to row 0");
5829 }
5830
5831 #[test]
5832 fn after_g_gg_with_count_jumps_line() {
5833 // 5gg → row 4 (0-indexed).
5834 let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5835 let mut e = fresh_editor(&content);
5836 e.jump_cursor(0, 0);
5837 e.after_g('g', 5);
5838 assert_eq!(e.cursor().0, 4, "5gg must land on row 4");
5839 }
5840
5841 #[test]
5842 fn after_g_gj_moves_down() {
5843 let mut e = fresh_editor("line0\nline1\nline2\n");
5844 e.jump_cursor(0, 0);
5845 e.after_g('j', 1);
5846 assert_eq!(e.cursor().0, 1, "gj must move down one display row");
5847 }
5848
5849 #[test]
5850 fn after_g_gu_sets_operator_pending() {
5851 // gU enters operator-pending with Uppercase op; next key applies it.
5852 let mut e = fresh_editor("hello\n");
5853 e.after_g('U', 1);
5854 // The engine should now be chord-pending (Pending::Op set).
5855 assert!(
5856 e.is_chord_pending(),
5857 "gU must set engine chord-pending (Pending::Op)"
5858 );
5859 }
5860
5861 #[test]
5862 fn after_g_g_star_searches_forward_non_whole_word() {
5863 // g* on word "foo" in "foobar" should find the match.
5864 let mut e = fresh_editor("foo foobar\n");
5865 e.jump_cursor(0, 0); // cursor on 'f' of "foo"
5866 e.after_g('*', 1);
5867 // After g* the cursor should have moved (ScreenDown motion is
5868 // not applicable here; WordAtCursor forward moves to next match).
5869 // At minimum: no panic and mode stays Normal.
5870 assert_eq!(e.vim_mode(), VimMode::Normal, "g* must stay in Normal mode");
5871 }
5872
5873 // ── apply_motion controller tests (Phase 3a) ────────────────────────────
5874
5875 #[test]
5876 fn apply_motion_char_left_moves_cursor() {
5877 let mut e = fresh_editor("hello\n");
5878 e.jump_cursor(0, 3);
5879 e.apply_motion(crate::MotionKind::CharLeft, 1);
5880 assert_eq!(e.cursor(), (0, 2), "CharLeft moves one col left");
5881 }
5882
5883 #[test]
5884 fn apply_motion_char_left_clamps_at_col_zero() {
5885 let mut e = fresh_editor("hello\n");
5886 e.jump_cursor(0, 0);
5887 e.apply_motion(crate::MotionKind::CharLeft, 1);
5888 assert_eq!(e.cursor(), (0, 0), "CharLeft at col 0 must not wrap");
5889 }
5890
5891 #[test]
5892 fn apply_motion_char_left_with_count() {
5893 let mut e = fresh_editor("hello\n");
5894 e.jump_cursor(0, 4);
5895 e.apply_motion(crate::MotionKind::CharLeft, 3);
5896 assert_eq!(e.cursor(), (0, 1), "CharLeft count=3 moves three cols left");
5897 }
5898
5899 #[test]
5900 fn apply_motion_char_right_moves_cursor() {
5901 let mut e = fresh_editor("hello\n");
5902 e.jump_cursor(0, 0);
5903 e.apply_motion(crate::MotionKind::CharRight, 1);
5904 assert_eq!(e.cursor(), (0, 1), "CharRight moves one col right");
5905 }
5906
5907 #[test]
5908 fn apply_motion_char_right_clamps_at_last_char() {
5909 let mut e = fresh_editor("hello\n");
5910 // "hello" has chars at 0..=4; normal mode clamps at 4.
5911 e.jump_cursor(0, 4);
5912 e.apply_motion(crate::MotionKind::CharRight, 1);
5913 assert_eq!(
5914 e.cursor(),
5915 (0, 4),
5916 "CharRight at end must not go past last char"
5917 );
5918 }
5919
5920 #[test]
5921 fn apply_motion_line_down_moves_cursor() {
5922 let mut e = fresh_editor("line0\nline1\nline2\n");
5923 e.jump_cursor(0, 0);
5924 e.apply_motion(crate::MotionKind::LineDown, 1);
5925 assert_eq!(e.cursor().0, 1, "LineDown moves one row down");
5926 }
5927
5928 #[test]
5929 fn apply_motion_line_down_with_count() {
5930 let mut e = fresh_editor("line0\nline1\nline2\n");
5931 e.jump_cursor(0, 0);
5932 e.apply_motion(crate::MotionKind::LineDown, 2);
5933 assert_eq!(e.cursor().0, 2, "LineDown count=2 moves two rows down");
5934 }
5935
5936 #[test]
5937 fn apply_motion_line_up_moves_cursor() {
5938 let mut e = fresh_editor("line0\nline1\nline2\n");
5939 e.jump_cursor(2, 0);
5940 e.apply_motion(crate::MotionKind::LineUp, 1);
5941 assert_eq!(e.cursor().0, 1, "LineUp moves one row up");
5942 }
5943
5944 #[test]
5945 fn apply_motion_line_up_clamps_at_top() {
5946 let mut e = fresh_editor("line0\nline1\n");
5947 e.jump_cursor(0, 0);
5948 e.apply_motion(crate::MotionKind::LineUp, 1);
5949 assert_eq!(e.cursor().0, 0, "LineUp at top must not go negative");
5950 }
5951
5952 #[test]
5953 fn apply_motion_first_non_blank_down_moves_and_lands_on_non_blank() {
5954 // Line 0: " hello" (indent 2), line 1: " world" (indent 2).
5955 let mut e = fresh_editor(" hello\n world\n");
5956 e.jump_cursor(0, 0);
5957 e.apply_motion(crate::MotionKind::FirstNonBlankDown, 1);
5958 assert_eq!(e.cursor().0, 1, "FirstNonBlankDown must move to next row");
5959 assert_eq!(
5960 e.cursor().1,
5961 2,
5962 "FirstNonBlankDown must land on first non-blank col"
5963 );
5964 }
5965
5966 #[test]
5967 fn apply_motion_first_non_blank_up_moves_and_lands_on_non_blank() {
5968 let mut e = fresh_editor(" hello\n world\n");
5969 e.jump_cursor(1, 4);
5970 e.apply_motion(crate::MotionKind::FirstNonBlankUp, 1);
5971 assert_eq!(e.cursor().0, 0, "FirstNonBlankUp must move to prev row");
5972 assert_eq!(
5973 e.cursor().1,
5974 2,
5975 "FirstNonBlankUp must land on first non-blank col"
5976 );
5977 }
5978
5979 #[test]
5980 fn apply_motion_count_zero_treated_as_one() {
5981 // count=0 must be normalised to 1 (count.max(1) in apply_motion_kind).
5982 let mut e = fresh_editor("hello\n");
5983 e.jump_cursor(0, 3);
5984 e.apply_motion(crate::MotionKind::CharLeft, 0);
5985 assert_eq!(e.cursor(), (0, 2), "count=0 treated as 1 for CharLeft");
5986 }
5987
5988 // ── apply_motion controller tests (Phase 3b) — word motions ─────────────
5989
5990 #[test]
5991 fn apply_motion_word_forward_moves_to_next_word() {
5992 // "hello world\n": 'w' from col 0 lands on 'w' of "world" at col 6.
5993 let mut e = fresh_editor("hello world\n");
5994 e.jump_cursor(0, 0);
5995 e.apply_motion(crate::MotionKind::WordForward, 1);
5996 assert_eq!(
5997 e.cursor(),
5998 (0, 6),
5999 "WordForward moves to start of next word"
6000 );
6001 }
6002
6003 #[test]
6004 fn apply_motion_word_forward_with_count() {
6005 // "one two three\n": 2w from col 0 → start of "three" at col 8.
6006 let mut e = fresh_editor("one two three\n");
6007 e.jump_cursor(0, 0);
6008 e.apply_motion(crate::MotionKind::WordForward, 2);
6009 assert_eq!(e.cursor(), (0, 8), "WordForward count=2 skips two words");
6010 }
6011
6012 #[test]
6013 fn apply_motion_big_word_forward_moves_to_next_big_word() {
6014 // "foo.bar baz\n": W from col 0 skips entire "foo.bar" (one WORD) to 'b' at col 8.
6015 let mut e = fresh_editor("foo.bar baz\n");
6016 e.jump_cursor(0, 0);
6017 e.apply_motion(crate::MotionKind::BigWordForward, 1);
6018 assert_eq!(e.cursor(), (0, 8), "BigWordForward skips the whole WORD");
6019 }
6020
6021 #[test]
6022 fn apply_motion_big_word_forward_with_count() {
6023 // "aa bb cc\n": 2W from col 0 → start of "cc" at col 6.
6024 let mut e = fresh_editor("aa bb cc\n");
6025 e.jump_cursor(0, 0);
6026 e.apply_motion(crate::MotionKind::BigWordForward, 2);
6027 assert_eq!(e.cursor(), (0, 6), "BigWordForward count=2 skips two WORDs");
6028 }
6029
6030 #[test]
6031 fn apply_motion_word_backward_moves_to_prev_word() {
6032 // "hello world\n": 'b' from col 6 ('w') lands back at col 0 ('h').
6033 let mut e = fresh_editor("hello world\n");
6034 e.jump_cursor(0, 6);
6035 e.apply_motion(crate::MotionKind::WordBackward, 1);
6036 assert_eq!(
6037 e.cursor(),
6038 (0, 0),
6039 "WordBackward moves to start of prev word"
6040 );
6041 }
6042
6043 #[test]
6044 fn apply_motion_word_backward_with_count() {
6045 // "one two three\n": 2b from col 8 ('t' of "three") → col 0 ('o' of "one").
6046 let mut e = fresh_editor("one two three\n");
6047 e.jump_cursor(0, 8);
6048 e.apply_motion(crate::MotionKind::WordBackward, 2);
6049 assert_eq!(
6050 e.cursor(),
6051 (0, 0),
6052 "WordBackward count=2 skips two words back"
6053 );
6054 }
6055
6056 #[test]
6057 fn apply_motion_big_word_backward_moves_to_prev_big_word() {
6058 // "foo.bar baz\n": B from col 8 ('b' of "baz") → col 0 (start of "foo.bar" WORD).
6059 let mut e = fresh_editor("foo.bar baz\n");
6060 e.jump_cursor(0, 8);
6061 e.apply_motion(crate::MotionKind::BigWordBackward, 1);
6062 assert_eq!(
6063 e.cursor(),
6064 (0, 0),
6065 "BigWordBackward jumps to start of prev WORD"
6066 );
6067 }
6068
6069 #[test]
6070 fn apply_motion_big_word_backward_with_count() {
6071 // "aa bb cc\n": 2B from col 6 ('c') → col 0 ('a').
6072 let mut e = fresh_editor("aa bb cc\n");
6073 e.jump_cursor(0, 6);
6074 e.apply_motion(crate::MotionKind::BigWordBackward, 2);
6075 assert_eq!(
6076 e.cursor(),
6077 (0, 0),
6078 "BigWordBackward count=2 skips two WORDs back"
6079 );
6080 }
6081
6082 #[test]
6083 fn apply_motion_word_end_moves_to_end_of_word() {
6084 // "hello world\n": 'e' from col 0 lands on 'o' of "hello" at col 4.
6085 let mut e = fresh_editor("hello world\n");
6086 e.jump_cursor(0, 0);
6087 e.apply_motion(crate::MotionKind::WordEnd, 1);
6088 assert_eq!(e.cursor(), (0, 4), "WordEnd moves to end of current word");
6089 }
6090
6091 #[test]
6092 fn apply_motion_word_end_with_count() {
6093 // "one two three\n": 2e from col 0 → end of "two" at col 6.
6094 let mut e = fresh_editor("one two three\n");
6095 e.jump_cursor(0, 0);
6096 e.apply_motion(crate::MotionKind::WordEnd, 2);
6097 assert_eq!(
6098 e.cursor(),
6099 (0, 6),
6100 "WordEnd count=2 lands on end of second word"
6101 );
6102 }
6103
6104 #[test]
6105 fn apply_motion_big_word_end_moves_to_end_of_big_word() {
6106 // "foo.bar baz\n": E from col 0 → end of "foo.bar" WORD at col 6.
6107 let mut e = fresh_editor("foo.bar baz\n");
6108 e.jump_cursor(0, 0);
6109 e.apply_motion(crate::MotionKind::BigWordEnd, 1);
6110 assert_eq!(e.cursor(), (0, 6), "BigWordEnd lands on end of WORD");
6111 }
6112
6113 #[test]
6114 fn apply_motion_big_word_end_with_count() {
6115 // "aa bb cc\n": 2E from col 0 → end of "bb" at col 4.
6116 let mut e = fresh_editor("aa bb cc\n");
6117 e.jump_cursor(0, 0);
6118 e.apply_motion(crate::MotionKind::BigWordEnd, 2);
6119 assert_eq!(
6120 e.cursor(),
6121 (0, 4),
6122 "BigWordEnd count=2 lands on end of second WORD"
6123 );
6124 }
6125
6126 // ── apply_motion controller tests (Phase 3c) — line-anchor motions ────────
6127
6128 #[test]
6129 fn apply_motion_line_start_lands_at_col_zero() {
6130 // " foo bar \n": `0` from col 5 → col 0 unconditionally.
6131 let mut e = fresh_editor(" foo bar \n");
6132 e.jump_cursor(0, 5);
6133 e.apply_motion(crate::MotionKind::LineStart, 1);
6134 assert_eq!(e.cursor(), (0, 0), "LineStart lands at col 0");
6135 }
6136
6137 #[test]
6138 fn apply_motion_line_start_from_beginning_stays_at_col_zero() {
6139 // Already at col 0 — motion is a no-op but must not panic.
6140 let mut e = fresh_editor(" foo bar \n");
6141 e.jump_cursor(0, 0);
6142 e.apply_motion(crate::MotionKind::LineStart, 1);
6143 assert_eq!(e.cursor(), (0, 0), "LineStart from col 0 stays at col 0");
6144 }
6145
6146 #[test]
6147 fn apply_motion_first_non_blank_lands_on_first_non_blank() {
6148 // " foo bar \n": `^` from col 0 → col 2 ('f').
6149 let mut e = fresh_editor(" foo bar \n");
6150 e.jump_cursor(0, 0);
6151 e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
6152 assert_eq!(
6153 e.cursor(),
6154 (0, 2),
6155 "FirstNonBlank lands on first non-blank char"
6156 );
6157 }
6158
6159 #[test]
6160 fn apply_motion_first_non_blank_on_blank_line_lands_at_zero() {
6161 // " \n": all whitespace — `^` must land at col 0.
6162 let mut e = fresh_editor(" \n");
6163 e.jump_cursor(0, 2);
6164 e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
6165 assert_eq!(
6166 e.cursor(),
6167 (0, 0),
6168 "FirstNonBlank on blank line stays at col 0"
6169 );
6170 }
6171
6172 #[test]
6173 fn apply_motion_line_end_lands_on_last_char() {
6174 // " foo bar \n": last char is the second space at col 10.
6175 let mut e = fresh_editor(" foo bar \n");
6176 e.jump_cursor(0, 0);
6177 e.apply_motion(crate::MotionKind::LineEnd, 1);
6178 assert_eq!(e.cursor(), (0, 10), "LineEnd lands on last char of line");
6179 }
6180
6181 #[test]
6182 fn apply_motion_line_end_on_empty_line_stays_at_zero() {
6183 // "\n": empty line — `$` must stay at col 0.
6184 let mut e = fresh_editor("\n");
6185 e.jump_cursor(0, 0);
6186 e.apply_motion(crate::MotionKind::LineEnd, 1);
6187 assert_eq!(e.cursor(), (0, 0), "LineEnd on empty line stays at col 0");
6188 }
6189
6190 // ── apply_motion controller tests (Phase 3d) — doc-level motion ───────────
6191
6192 #[test]
6193 fn goto_line_count_1_lands_on_last_line() {
6194 // "foo\nbar\nbaz\n": bare `G` (count=1) → last content line (row 2).
6195 // Count convention: apply_motion_kind normalises 1 → execute_motion
6196 // with count=1 → FileBottom arm sees count <= 1 → move_bottom(0) =
6197 // last content row.
6198 let mut e = fresh_editor("foo\nbar\nbaz\n");
6199 e.jump_cursor(0, 0);
6200 e.apply_motion(crate::MotionKind::GotoLine, 1);
6201 assert_eq!(e.cursor(), (2, 0), "bare G lands on last content row");
6202 }
6203
6204 #[test]
6205 fn goto_line_count_5_lands_on_line_5() {
6206 // 6-line buffer (rows 0-5); `5G` → row 4 (1-based line 5).
6207 let mut e = fresh_editor("a\nb\nc\nd\ne\nf\n");
6208 e.jump_cursor(0, 0);
6209 e.apply_motion(crate::MotionKind::GotoLine, 5);
6210 assert_eq!(e.cursor(), (4, 0), "5G lands on row 4 (1-based line 5)");
6211 }
6212
6213 #[test]
6214 fn goto_line_count_past_buffer_clamps_to_last_line() {
6215 // "foo\nbar\nbaz\n": `100G` → last content line (row 2), clamped.
6216 let mut e = fresh_editor("foo\nbar\nbaz\n");
6217 e.jump_cursor(0, 0);
6218 e.apply_motion(crate::MotionKind::GotoLine, 100);
6219 assert_eq!(e.cursor(), (2, 0), "100G clamps to last content row");
6220 }
6221
6222 // ── FindRepeat / FindRepeatReverse controller tests (Phase 3e) ────────────
6223
6224 #[test]
6225 fn find_repeat_after_f_finds_next_occurrence() {
6226 // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` repeats → (0,5).
6227 let mut e = fresh_editor("abcabc");
6228 e.jump_cursor(0, 0);
6229 e.find_char('c', true, false, 1);
6230 assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6231 e.apply_motion(crate::MotionKind::FindRepeat, 1);
6232 assert_eq!(
6233 e.cursor(),
6234 (0, 5),
6235 "find_repeat (;) must advance to second 'c'"
6236 );
6237 }
6238
6239 #[test]
6240 fn find_repeat_reverse_after_f_finds_prev_occurrence() {
6241 // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` → (0,5). `,` back → (0,2).
6242 let mut e = fresh_editor("abcabc");
6243 e.jump_cursor(0, 0);
6244 e.find_char('c', true, false, 1);
6245 assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6246 e.apply_motion(crate::MotionKind::FindRepeat, 1);
6247 assert_eq!(e.cursor(), (0, 5), "; must advance to second 'c'");
6248 e.apply_motion(crate::MotionKind::FindRepeatReverse, 1);
6249 assert_eq!(
6250 e.cursor(),
6251 (0, 2),
6252 "find_repeat_reverse (,) must go back to first 'c'"
6253 );
6254 }
6255
6256 #[test]
6257 fn find_repeat_with_no_prior_find_is_noop() {
6258 // Fresh editor, no prior find — `;` must not move cursor.
6259 let mut e = fresh_editor("abcabc");
6260 e.jump_cursor(0, 3);
6261 e.apply_motion(crate::MotionKind::FindRepeat, 1);
6262 assert_eq!(
6263 e.cursor(),
6264 (0, 3),
6265 "find_repeat with no prior find must be a no-op"
6266 );
6267 }
6268
6269 #[test]
6270 fn find_repeat_with_count_advances_count_times() {
6271 // "aXaXaX", cursor (0,0). `fX` → (0,1). `3;` → repeats 3× → (0,5).
6272 let mut e = fresh_editor("aXaXaX");
6273 e.jump_cursor(0, 0);
6274 e.find_char('X', true, false, 1);
6275 assert_eq!(e.cursor(), (0, 1), "fX must land on first 'X' at col 1");
6276 e.apply_motion(crate::MotionKind::FindRepeat, 3);
6277 assert_eq!(
6278 e.cursor(),
6279 (0, 5),
6280 "3; must advance 3 times from col 1 to col 5"
6281 );
6282 }
6283
6284 // ── BracketMatch controller tests (Phase 3f) ───────────────────────────────
6285
6286 #[test]
6287 fn bracket_match_jumps_to_matching_close_paren() {
6288 // "(abc)", cursor at (0,0) on `(` — `%` must jump to `)` at (0,4).
6289 let mut e = fresh_editor("(abc)");
6290 e.jump_cursor(0, 0);
6291 e.apply_motion(crate::MotionKind::BracketMatch, 1);
6292 assert_eq!(
6293 e.cursor(),
6294 (0, 4),
6295 "% on '(' must land on matching ')' at col 4"
6296 );
6297 }
6298
6299 #[test]
6300 fn bracket_match_jumps_to_matching_open_paren() {
6301 // "(abc)", cursor at (0,4) on `)` — `%` must jump back to `(` at (0,0).
6302 let mut e = fresh_editor("(abc)");
6303 e.jump_cursor(0, 4);
6304 e.apply_motion(crate::MotionKind::BracketMatch, 1);
6305 assert_eq!(
6306 e.cursor(),
6307 (0, 0),
6308 "% on ')' must land on matching '(' at col 0"
6309 );
6310 }
6311
6312 #[test]
6313 fn bracket_match_with_no_match_on_line_is_noop_or_engine_behaviour() {
6314 // "abcd", cursor at (0,2) — no bracket under cursor; engine returns
6315 // false from matching_bracket, cursor must not move.
6316 let mut e = fresh_editor("abcd");
6317 e.jump_cursor(0, 2);
6318 e.apply_motion(crate::MotionKind::BracketMatch, 1);
6319 assert_eq!(
6320 e.cursor(),
6321 (0, 2),
6322 "% with no bracket under cursor must be a no-op"
6323 );
6324 }
6325
6326 // ── Scroll / viewport motion controller tests (Phase 3g) ──────────────────
6327
6328 /// Helper: build a 20-line buffer, set viewport to rows [5..14] (height=10).
6329 fn fresh_viewport_editor() -> Editor {
6330 let content = many_lines(20);
6331 let mut e = Editor::new(
6332 hjkl_buffer::Buffer::from_str(&content),
6333 crate::types::DefaultHost::new(),
6334 crate::types::Options::default(),
6335 );
6336 // height=10, top_row=5 → visible rows 5..14.
6337 // set_viewport_height stores to the atomic; sync_buffer_from_textarea
6338 // propagates it to host.viewport_mut().height so motion helpers see it.
6339 e.set_viewport_height(10);
6340 e.sync_buffer_from_textarea();
6341 e.host_mut().viewport_mut().top_row = 5;
6342 e
6343 }
6344
6345 #[test]
6346 fn viewport_top_lands_on_first_visible_row() {
6347 // Viewport top=5, height=10. H (count=1) should land on row 5
6348 // (the first visible row, offset = count-1 = 0).
6349 let mut e = fresh_viewport_editor();
6350 e.jump_cursor(10, 0);
6351 e.apply_motion(crate::MotionKind::ViewportTop, 1);
6352 assert_eq!(
6353 e.cursor().0,
6354 5,
6355 "H (count=1) must land on viewport top row (5)"
6356 );
6357 }
6358
6359 #[test]
6360 fn viewport_top_with_count_offsets_down() {
6361 // H with count=3 → viewport top + (3-1) = 5 + 2 = row 7.
6362 let mut e = fresh_viewport_editor();
6363 e.jump_cursor(12, 0);
6364 e.apply_motion(crate::MotionKind::ViewportTop, 3);
6365 assert_eq!(e.cursor().0, 7, "3H must land at viewport top + 2 = row 7");
6366 }
6367
6368 #[test]
6369 fn viewport_middle_lands_on_middle_visible_row() {
6370 // Viewport top=5, height=10 → last visible = 14, mid = 5 + (14-5)/2 = 9.
6371 let mut e = fresh_viewport_editor();
6372 e.jump_cursor(0, 0);
6373 e.apply_motion(crate::MotionKind::ViewportMiddle, 1);
6374 assert_eq!(e.cursor().0, 9, "M must land on middle visible row (9)");
6375 }
6376
6377 #[test]
6378 fn viewport_bottom_lands_on_last_visible_row() {
6379 // L (count=1) → viewport bottom, offset = count-1 = 0 → row 14.
6380 let mut e = fresh_viewport_editor();
6381 e.jump_cursor(5, 0);
6382 e.apply_motion(crate::MotionKind::ViewportBottom, 1);
6383 assert_eq!(
6384 e.cursor().0,
6385 14,
6386 "L (count=1) must land on viewport bottom row (14)"
6387 );
6388 }
6389
6390 #[test]
6391 fn half_page_down_moves_cursor_by_half_window() {
6392 // viewport height=10, so half=5. Cursor at row 0 → row 5 after C-d.
6393 let mut e = Editor::new(
6394 hjkl_buffer::Buffer::from_str(&many_lines(30)),
6395 crate::types::DefaultHost::new(),
6396 crate::types::Options::default(),
6397 );
6398 e.set_viewport_height(10);
6399 e.jump_cursor(0, 0);
6400 e.apply_motion(crate::MotionKind::HalfPageDown, 1);
6401 assert_eq!(
6402 e.cursor().0,
6403 5,
6404 "<C-d> from row 0 with viewport height=10 must land on row 5"
6405 );
6406 }
6407
6408 #[test]
6409 fn half_page_up_moves_cursor_by_half_window_reverse() {
6410 // viewport height=10, half=5. Cursor at row 10 → row 5 after C-u.
6411 let mut e = Editor::new(
6412 hjkl_buffer::Buffer::from_str(&many_lines(30)),
6413 crate::types::DefaultHost::new(),
6414 crate::types::Options::default(),
6415 );
6416 e.set_viewport_height(10);
6417 e.jump_cursor(10, 0);
6418 e.apply_motion(crate::MotionKind::HalfPageUp, 1);
6419 assert_eq!(
6420 e.cursor().0,
6421 5,
6422 "<C-u> from row 10 with viewport height=10 must land on row 5"
6423 );
6424 }
6425
6426 #[test]
6427 fn full_page_down_moves_cursor_by_full_window() {
6428 // viewport height=10, full = 10 - 2 = 8. Cursor at row 0 → row 8.
6429 let mut e = Editor::new(
6430 hjkl_buffer::Buffer::from_str(&many_lines(30)),
6431 crate::types::DefaultHost::new(),
6432 crate::types::Options::default(),
6433 );
6434 e.set_viewport_height(10);
6435 e.jump_cursor(0, 0);
6436 e.apply_motion(crate::MotionKind::FullPageDown, 1);
6437 assert_eq!(
6438 e.cursor().0,
6439 8,
6440 "<C-f> from row 0 with viewport height=10 must land on row 8"
6441 );
6442 }
6443
6444 #[test]
6445 fn full_page_up_moves_cursor_by_full_window_reverse() {
6446 // viewport height=10, full=8. Cursor at row 10 → row 2.
6447 let mut e = Editor::new(
6448 hjkl_buffer::Buffer::from_str(&many_lines(30)),
6449 crate::types::DefaultHost::new(),
6450 crate::types::Options::default(),
6451 );
6452 e.set_viewport_height(10);
6453 e.jump_cursor(10, 0);
6454 e.apply_motion(crate::MotionKind::FullPageUp, 1);
6455 assert_eq!(
6456 e.cursor().0,
6457 2,
6458 "<C-b> from row 10 with viewport height=10 must land on row 2"
6459 );
6460 }
6461
6462 // ── set_mark_at_cursor unit tests ─────────────────────────────────────────
6463
6464 #[test]
6465 fn set_mark_at_cursor_alphabetic_records() {
6466 // `ma` at (0, 2) — mark 'a' must store (0, 2).
6467 let mut e = fresh_editor("hello");
6468 e.jump_cursor(0, 2);
6469 e.set_mark_at_cursor('a');
6470 assert_eq!(
6471 e.mark('a'),
6472 Some((0, 2)),
6473 "mark 'a' must record current pos"
6474 );
6475 }
6476
6477 #[test]
6478 fn set_mark_at_cursor_invalid_char_no_op() {
6479 // Invalid chars (digits, special) must not store a mark.
6480 let mut e = fresh_editor("hello");
6481 e.jump_cursor(0, 1);
6482 e.set_mark_at_cursor('1'); // digit — not alphanumeric in vim mark sense
6483 assert_eq!(e.mark('1'), None, "digit mark must be a no-op");
6484 e.set_mark_at_cursor('['); // special — only goto uses '[', not set_mark
6485 assert_eq!(
6486 e.mark('['),
6487 None,
6488 "bracket char must be a no-op for set_mark"
6489 );
6490 }
6491
6492 #[test]
6493 fn set_mark_at_cursor_special_left_bracket() {
6494 // Confirm '[' is NOT stored by set_mark_at_cursor (vim's `m[` is invalid).
6495 // The `[` mark is only set automatically by operator paths, not `m[`.
6496 let mut e = fresh_editor("hello");
6497 e.jump_cursor(0, 3);
6498 e.set_mark_at_cursor('[');
6499 assert_eq!(
6500 e.mark('['),
6501 None,
6502 "set_mark_at_cursor must reject '[' (vim: m[ is invalid)"
6503 );
6504 }
6505
6506 // ── goto_mark_line unit tests ─────────────────────────────────────────────
6507
6508 #[test]
6509 fn goto_mark_line_jumps_to_first_non_blank() {
6510 // Set mark 'a' at (1, 3), then jump back to (0, 0).
6511 // `'a` (linewise) must land on row 1, first non-blank column.
6512 let mut e = fresh_editor("hello\n world\n");
6513 e.jump_cursor(1, 3);
6514 e.set_mark_at_cursor('a');
6515 e.jump_cursor(0, 0);
6516 e.goto_mark_line('a');
6517 assert_eq!(e.cursor().0, 1, "goto_mark_line must jump to mark row");
6518 // " world" — first non-blank is col 2.
6519 assert_eq!(
6520 e.cursor().1,
6521 2,
6522 "goto_mark_line must land on first non-blank column"
6523 );
6524 }
6525
6526 #[test]
6527 fn goto_mark_line_unset_mark_no_op() {
6528 // Jumping to an unset mark must not move the cursor.
6529 let mut e = fresh_editor("hello\nworld\n");
6530 e.jump_cursor(1, 2);
6531 e.goto_mark_line('z'); // 'z' not set
6532 assert_eq!(e.cursor(), (1, 2), "unset mark jump must be a no-op");
6533 }
6534
6535 #[test]
6536 fn goto_mark_line_invalid_char_no_op() {
6537 // '!' is not a valid mark char — must not move cursor.
6538 let mut e = fresh_editor("hello\nworld\n");
6539 e.jump_cursor(0, 0);
6540 e.goto_mark_line('!');
6541 assert_eq!(e.cursor(), (0, 0), "invalid mark char must be a no-op");
6542 }
6543
6544 // ── goto_mark_char unit tests ─────────────────────────────────────────────
6545
6546 #[test]
6547 fn goto_mark_char_jumps_to_exact_pos() {
6548 // Set mark 'b' at (1, 4), then jump back to (0, 0).
6549 // `` `b `` (charwise) must land on (1, 4) exactly.
6550 let mut e = fresh_editor("hello\nworld\n");
6551 e.jump_cursor(1, 4);
6552 e.set_mark_at_cursor('b');
6553 e.jump_cursor(0, 0);
6554 e.goto_mark_char('b');
6555 assert_eq!(
6556 e.cursor(),
6557 (1, 4),
6558 "goto_mark_char must jump to exact mark position"
6559 );
6560 }
6561
6562 #[test]
6563 fn goto_mark_char_unset_mark_no_op() {
6564 // Jumping to an unset mark must not move the cursor.
6565 let mut e = fresh_editor("hello\nworld\n");
6566 e.jump_cursor(1, 1);
6567 e.goto_mark_char('x'); // 'x' not set
6568 assert_eq!(
6569 e.cursor(),
6570 (1, 1),
6571 "unset charwise mark jump must be a no-op"
6572 );
6573 }
6574
6575 #[test]
6576 fn goto_mark_char_invalid_char_no_op() {
6577 // '#' is not a valid mark char — must not move cursor.
6578 let mut e = fresh_editor("hello\nworld\n");
6579 e.jump_cursor(0, 2);
6580 e.goto_mark_char('#');
6581 assert_eq!(
6582 e.cursor(),
6583 (0, 2),
6584 "invalid charwise mark char must be a no-op"
6585 );
6586 }
6587
6588 // ── Macro controller API tests (Phase 5b) ─────────────────────────────────
6589
6590 #[test]
6591 fn start_macro_record_records_register() {
6592 let mut e = fresh_editor("hello");
6593 assert!(!e.is_recording_macro());
6594 e.start_macro_record('a');
6595 assert!(e.is_recording_macro());
6596 assert_eq!(e.recording_register(), Some('a'));
6597 }
6598
6599 #[test]
6600 fn start_macro_record_capital_seeds_existing() {
6601 // `qa` records "h", stop. Then `qA` should seed from existing 'a' reg.
6602 let mut e = fresh_editor("hello");
6603 e.start_macro_record('a');
6604 e.record_input(crate::input::Input {
6605 key: crate::input::Key::Char('h'),
6606 ..Default::default()
6607 });
6608 e.stop_macro_record();
6609 // Start capital 'A' — should seed from existing 'a' register.
6610 e.start_macro_record('A');
6611 // recording_keys should now contain 1 input (the seeded 'h').
6612 assert_eq!(
6613 e.vim.recording_keys.len(),
6614 1,
6615 "capital record must seed from existing lowercase reg"
6616 );
6617 }
6618
6619 #[test]
6620 fn stop_macro_record_writes_register() {
6621 let mut e = fresh_editor("hello");
6622 e.start_macro_record('a');
6623 e.record_input(crate::input::Input {
6624 key: crate::input::Key::Char('h'),
6625 ..Default::default()
6626 });
6627 e.record_input(crate::input::Input {
6628 key: crate::input::Key::Char('l'),
6629 ..Default::default()
6630 });
6631 e.stop_macro_record();
6632 assert!(!e.is_recording_macro());
6633 // Register 'a' should contain "hl".
6634 let text = e
6635 .registers()
6636 .read('a')
6637 .map(|s| s.text.clone())
6638 .unwrap_or_default();
6639 assert_eq!(
6640 text, "hl",
6641 "stop_macro_record must write encoded keys to register"
6642 );
6643 }
6644
6645 #[test]
6646 fn is_recording_macro_reflects_state() {
6647 let mut e = fresh_editor("hello");
6648 assert!(!e.is_recording_macro());
6649 e.start_macro_record('b');
6650 assert!(e.is_recording_macro());
6651 e.stop_macro_record();
6652 assert!(!e.is_recording_macro());
6653 }
6654
6655 #[test]
6656 fn play_macro_returns_decoded_inputs() {
6657 let mut e = fresh_editor("hello");
6658 // Write "jj" into register 'a'.
6659 e.set_named_register_text('a', "jj".to_string());
6660 let inputs = e.play_macro('a', 1);
6661 assert_eq!(inputs.len(), 2);
6662 assert_eq!(inputs[0].key, crate::input::Key::Char('j'));
6663 assert_eq!(inputs[1].key, crate::input::Key::Char('j'));
6664 assert!(e.is_replaying_macro(), "play_macro must set replaying flag");
6665 e.end_macro_replay();
6666 assert!(!e.is_replaying_macro());
6667 }
6668
6669 #[test]
6670 fn play_macro_at_uses_last_macro() {
6671 let mut e = fresh_editor("hello");
6672 e.set_named_register_text('a', "k".to_string());
6673 // Play 'a' first to set last_macro.
6674 let _ = e.play_macro('a', 1);
6675 e.end_macro_replay();
6676 // Now `@@` should replay 'a' again.
6677 let inputs = e.play_macro('@', 1);
6678 assert_eq!(inputs.len(), 1);
6679 assert_eq!(inputs[0].key, crate::input::Key::Char('k'));
6680 e.end_macro_replay();
6681 }
6682
6683 #[test]
6684 fn play_macro_with_count_repeats() {
6685 let mut e = fresh_editor("hello");
6686 e.set_named_register_text('a', "j".to_string());
6687 let inputs = e.play_macro('a', 3);
6688 assert_eq!(inputs.len(), 3, "3@a must produce 3 inputs");
6689 e.end_macro_replay();
6690 }
6691
6692 #[test]
6693 fn record_input_appends_when_recording() {
6694 let mut e = fresh_editor("hello");
6695 // Not recording: record_input is a no-op.
6696 e.record_input(crate::input::Input {
6697 key: crate::input::Key::Char('j'),
6698 ..Default::default()
6699 });
6700 assert_eq!(e.vim.recording_keys.len(), 0);
6701 // Start recording: record_input appends.
6702 e.start_macro_record('a');
6703 e.record_input(crate::input::Input {
6704 key: crate::input::Key::Char('j'),
6705 ..Default::default()
6706 });
6707 e.record_input(crate::input::Input {
6708 key: crate::input::Key::Char('k'),
6709 ..Default::default()
6710 });
6711 assert_eq!(e.vim.recording_keys.len(), 2);
6712 // During replay: record_input must NOT append.
6713 e.vim.replaying_macro = true;
6714 e.record_input(crate::input::Input {
6715 key: crate::input::Key::Char('l'),
6716 ..Default::default()
6717 });
6718 assert_eq!(
6719 e.vim.recording_keys.len(),
6720 2,
6721 "record_input must skip during replay"
6722 );
6723 e.vim.replaying_macro = false;
6724 e.stop_macro_record();
6725 }
6726
6727 // ── Phase 6.1 insert-mode primitive tests (kryptic-sh/hjkl#87) ────────────
6728
6729 /// Helper: enter insert mode via the public bridge, then call the method under test.
6730 fn enter_insert(e: &mut Editor) {
6731 e.enter_insert_i(1);
6732 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6733 }
6734
6735 #[test]
6736 fn insert_char_basic() {
6737 let mut e = fresh_editor("hello");
6738 enter_insert(&mut e);
6739 e.insert_char('X');
6740 assert_eq!(e.buffer().lines()[0], "Xhello");
6741 assert!(e.take_dirty());
6742 }
6743
6744 #[test]
6745 fn insert_newline_splits_line() {
6746 let mut e = fresh_editor("hello");
6747 // Move to col 3 so we split "hel" | "lo".
6748 e.jump_cursor(0, 3);
6749 enter_insert(&mut e);
6750 e.insert_newline();
6751 let lines = e.buffer().lines().to_vec();
6752 assert_eq!(lines[0], "hel");
6753 assert_eq!(lines[1], "lo");
6754 }
6755
6756 #[test]
6757 fn insert_tab_expandtab_inserts_spaces() {
6758 let mut e = fresh_editor("");
6759 // Default options: expandtab=true, softtabstop=4, tabstop=4.
6760 enter_insert(&mut e);
6761 e.insert_tab();
6762 // At col 0 with sts=4: 4 spaces inserted.
6763 assert_eq!(e.buffer().lines()[0], " ");
6764 }
6765
6766 #[test]
6767 fn insert_tab_real_tab_when_noexpandtab() {
6768 let opts = crate::types::Options {
6769 expandtab: false,
6770 ..crate::types::Options::default()
6771 };
6772 let mut e = Editor::new(
6773 hjkl_buffer::Buffer::new(),
6774 crate::types::DefaultHost::new(),
6775 opts,
6776 );
6777 e.set_content("");
6778 enter_insert(&mut e);
6779 e.insert_tab();
6780 assert_eq!(e.buffer().lines()[0], "\t");
6781 }
6782
6783 #[test]
6784 fn insert_backspace_single_char() {
6785 // Cursor at col 3 in "hello", backspace deletes 'l'.
6786 let mut e = fresh_editor("hello");
6787 e.jump_cursor(0, 3);
6788 enter_insert(&mut e);
6789 e.insert_backspace();
6790 assert_eq!(e.buffer().lines()[0], "helo");
6791 }
6792
6793 #[test]
6794 fn insert_backspace_softtabstop() {
6795 // With sts=4, expandtab: 4 spaces at col 4 → one backspace deletes all 4.
6796 let mut e = fresh_editor(" hello");
6797 e.jump_cursor(0, 4);
6798 enter_insert(&mut e);
6799 e.insert_backspace();
6800 assert_eq!(e.buffer().lines()[0], "hello");
6801 }
6802
6803 #[test]
6804 fn insert_backspace_join_up() {
6805 // At col 0 on row 1, backspace joins with the previous line.
6806 let mut e = fresh_editor("foo\nbar");
6807 e.jump_cursor(1, 0);
6808 enter_insert(&mut e);
6809 e.insert_backspace();
6810 // Two rows merged into one.
6811 assert_eq!(e.buffer().lines().len(), 1);
6812 assert_eq!(e.buffer().lines()[0], "foobar");
6813 }
6814
6815 #[test]
6816 fn leave_insert_steps_back_col() {
6817 // Esc in insert mode should move the cursor one cell left (vim convention).
6818 let mut e = fresh_editor("hello");
6819 e.jump_cursor(0, 3);
6820 enter_insert(&mut e);
6821 // Type one char so cursor is at col 4, then call leave_insert_to_normal.
6822 e.insert_char('X');
6823 // cursor is now at col 4 (after the inserted 'X').
6824 let pre_col = e.cursor().1;
6825 e.leave_insert_to_normal();
6826 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6827 // Cursor stepped back one.
6828 assert_eq!(e.cursor().1, pre_col - 1);
6829 }
6830
6831 #[test]
6832 fn insert_ctrl_w_word_back() {
6833 // Ctrl-W deletes from cursor back to word start.
6834 // "hello world" — cursor at end of "world" (col 11).
6835 let mut e = fresh_editor("hello world");
6836 // Normal mode clamps cursor to col 10 (last char); jump_cursor doesn't clamp.
6837 e.jump_cursor(0, 11);
6838 enter_insert(&mut e);
6839 e.insert_ctrl_w();
6840 // "world" (5 chars) deleted, leaving "hello ".
6841 assert_eq!(e.buffer().lines()[0], "hello ");
6842 }
6843
6844 #[test]
6845 fn insert_ctrl_u_deletes_to_line_start() {
6846 let mut e = fresh_editor("hello world");
6847 e.jump_cursor(0, 5);
6848 enter_insert(&mut e);
6849 e.insert_ctrl_u();
6850 assert_eq!(e.buffer().lines()[0], " world");
6851 }
6852
6853 #[test]
6854 fn insert_ctrl_h_single_backspace() {
6855 // Ctrl-H is an alias for Backspace in insert mode.
6856 let mut e = fresh_editor("hello");
6857 e.jump_cursor(0, 3);
6858 enter_insert(&mut e);
6859 e.insert_ctrl_h();
6860 assert_eq!(e.buffer().lines()[0], "helo");
6861 }
6862
6863 #[test]
6864 fn insert_ctrl_h_join_up() {
6865 let mut e = fresh_editor("foo\nbar");
6866 e.jump_cursor(1, 0);
6867 enter_insert(&mut e);
6868 e.insert_ctrl_h();
6869 assert_eq!(e.buffer().lines().len(), 1);
6870 assert_eq!(e.buffer().lines()[0], "foobar");
6871 }
6872
6873 #[test]
6874 fn insert_ctrl_t_indents_current_line() {
6875 let mut e = Editor::new(
6876 hjkl_buffer::Buffer::new(),
6877 crate::types::DefaultHost::new(),
6878 crate::types::Options {
6879 shiftwidth: 4,
6880 ..crate::types::Options::default()
6881 },
6882 );
6883 e.set_content("hello");
6884 enter_insert(&mut e);
6885 e.insert_ctrl_t();
6886 assert_eq!(e.buffer().lines()[0], " hello");
6887 }
6888
6889 #[test]
6890 fn insert_ctrl_d_outdents_current_line() {
6891 let mut e = Editor::new(
6892 hjkl_buffer::Buffer::new(),
6893 crate::types::DefaultHost::new(),
6894 crate::types::Options {
6895 shiftwidth: 4,
6896 ..crate::types::Options::default()
6897 },
6898 );
6899 e.set_content(" hello");
6900 enter_insert(&mut e);
6901 e.insert_ctrl_d();
6902 assert_eq!(e.buffer().lines()[0], "hello");
6903 }
6904
6905 #[test]
6906 fn insert_ctrl_o_arm_sets_one_shot_normal() {
6907 let mut e = fresh_editor("hello");
6908 enter_insert(&mut e);
6909 e.insert_ctrl_o_arm();
6910 // Mode should flip to Normal (one-shot).
6911 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6912 }
6913
6914 #[test]
6915 fn insert_ctrl_r_arm_sets_pending_register() {
6916 let mut e = fresh_editor("hello");
6917 enter_insert(&mut e);
6918 e.insert_ctrl_r_arm();
6919 // pending register flag set; mode stays Insert.
6920 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6921 assert!(e.vim.insert_pending_register);
6922 }
6923
6924 #[test]
6925 fn insert_delete_removes_char_under_cursor() {
6926 let mut e = fresh_editor("hello");
6927 e.jump_cursor(0, 2);
6928 enter_insert(&mut e);
6929 e.insert_delete();
6930 assert_eq!(e.buffer().lines()[0], "helo");
6931 }
6932
6933 #[test]
6934 fn insert_delete_joins_lines_at_eol() {
6935 let mut e = fresh_editor("foo\nbar");
6936 // Position at end of row 0 (col 3 = past last char).
6937 e.jump_cursor(0, 3);
6938 enter_insert(&mut e);
6939 e.insert_delete();
6940 assert_eq!(e.buffer().lines().len(), 1);
6941 assert_eq!(e.buffer().lines()[0], "foobar");
6942 }
6943
6944 #[test]
6945 fn insert_arrow_left_moves_cursor() {
6946 let mut e = fresh_editor("hello");
6947 e.jump_cursor(0, 3);
6948 enter_insert(&mut e);
6949 e.insert_arrow(crate::vim::InsertDir::Left);
6950 assert_eq!(e.cursor().1, 2);
6951 }
6952
6953 #[test]
6954 fn insert_arrow_right_moves_cursor() {
6955 let mut e = fresh_editor("hello");
6956 e.jump_cursor(0, 2);
6957 enter_insert(&mut e);
6958 e.insert_arrow(crate::vim::InsertDir::Right);
6959 assert_eq!(e.cursor().1, 3);
6960 }
6961
6962 #[test]
6963 fn insert_arrow_up_moves_cursor() {
6964 let mut e = fresh_editor("foo\nbar");
6965 e.jump_cursor(1, 0);
6966 enter_insert(&mut e);
6967 e.insert_arrow(crate::vim::InsertDir::Up);
6968 assert_eq!(e.cursor().0, 0);
6969 }
6970
6971 #[test]
6972 fn insert_arrow_down_moves_cursor() {
6973 let mut e = fresh_editor("foo\nbar");
6974 e.jump_cursor(0, 0);
6975 enter_insert(&mut e);
6976 e.insert_arrow(crate::vim::InsertDir::Down);
6977 assert_eq!(e.cursor().0, 1);
6978 }
6979
6980 #[test]
6981 fn insert_home_moves_to_line_start() {
6982 let mut e = fresh_editor("hello");
6983 e.jump_cursor(0, 4);
6984 enter_insert(&mut e);
6985 e.insert_home();
6986 assert_eq!(e.cursor().1, 0);
6987 }
6988
6989 #[test]
6990 fn insert_end_moves_to_line_end() {
6991 let mut e = fresh_editor("hello");
6992 e.jump_cursor(0, 0);
6993 enter_insert(&mut e);
6994 e.insert_end();
6995 // move_line_end lands on the last char (col 4) for "hello".
6996 assert_eq!(e.cursor().1, 4);
6997 }
6998
6999 #[test]
7000 fn insert_pageup_does_not_panic() {
7001 let mut e = fresh_editor("line1\nline2\nline3");
7002 e.jump_cursor(2, 0);
7003 enter_insert(&mut e);
7004 // Viewport height 0 → no crash (viewport_h saturates to 1 row effectively).
7005 e.insert_pageup(24);
7006 }
7007
7008 #[test]
7009 fn insert_pagedown_does_not_panic() {
7010 let mut e = fresh_editor("line1\nline2\nline3");
7011 e.jump_cursor(0, 0);
7012 enter_insert(&mut e);
7013 e.insert_pagedown(24);
7014 }
7015
7016 #[test]
7017 fn leave_insert_to_normal_exits_mode() {
7018 let mut e = fresh_editor("hello");
7019 enter_insert(&mut e);
7020 e.leave_insert_to_normal();
7021 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7022 }
7023
7024 #[test]
7025 fn insert_backspace_at_buffer_start_is_noop() {
7026 let mut e = fresh_editor("hello");
7027 e.jump_cursor(0, 0);
7028 enter_insert(&mut e);
7029 // No previous char and no previous row — should not panic.
7030 e.insert_backspace();
7031 assert_eq!(e.buffer().lines()[0], "hello");
7032 }
7033
7034 #[test]
7035 fn insert_delete_at_buffer_end_is_noop() {
7036 let mut e = fresh_editor("hello");
7037 // Cursor at col 5 (past last char index of 4), no next row.
7038 e.jump_cursor(0, 5);
7039 enter_insert(&mut e);
7040 // col 5 >= line_chars (5), no next row → no-op.
7041 e.insert_delete();
7042 assert_eq!(e.buffer().lines()[0], "hello");
7043 }
7044
7045 // ── Phase 6.2: normal-mode primitive tests (kryptic-sh/hjkl#88) ─────────
7046
7047 // Helper: set content and ensure we are in Normal mode.
7048 fn normal_editor(initial: &str) -> Editor {
7049 let e = fresh_editor(initial);
7050 // fresh_editor starts in Normal; this is just a readability alias.
7051 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7052 e
7053 }
7054
7055 // ── Insert-mode entry ────────────────────────────────────────────────────
7056
7057 #[test]
7058 fn enter_insert_i_lands_in_insert_at_cursor() {
7059 let mut e = normal_editor("hello");
7060 e.jump_cursor(0, 2);
7061 e.enter_insert_i(1);
7062 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7063 assert_eq!(e.cursor(), (0, 2));
7064 }
7065
7066 #[test]
7067 fn enter_insert_shift_i_moves_to_first_non_blank_then_insert() {
7068 let mut e = normal_editor(" hello");
7069 e.jump_cursor(0, 5);
7070 e.enter_insert_shift_i(1);
7071 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7072 // First non-blank of " hello" is col 2.
7073 assert_eq!(e.cursor().1, 2);
7074 }
7075
7076 #[test]
7077 fn enter_insert_a_advances_one_then_insert() {
7078 let mut e = normal_editor("hello");
7079 e.jump_cursor(0, 0);
7080 e.enter_insert_a(1);
7081 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7082 assert_eq!(e.cursor().1, 1);
7083 }
7084
7085 #[test]
7086 fn enter_insert_shift_a_lands_at_eol() {
7087 let mut e = normal_editor("hello");
7088 e.enter_insert_shift_a(1);
7089 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7090 assert_eq!(e.cursor().1, 5);
7091 }
7092
7093 #[test]
7094 fn open_line_below_creates_new_line_and_insert() {
7095 let mut e = normal_editor("hello\nworld");
7096 e.open_line_below(1);
7097 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7098 assert_eq!(e.buffer().lines().len(), 3);
7099 }
7100
7101 #[test]
7102 fn open_line_above_creates_line_before_cursor() {
7103 let mut e = normal_editor("hello\nworld");
7104 e.jump_cursor(1, 0);
7105 e.open_line_above(1);
7106 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7107 assert_eq!(e.buffer().lines().len(), 3);
7108 assert_eq!(e.cursor().0, 1);
7109 }
7110
7111 #[test]
7112 fn open_line_above_at_row_0_creates_blank_first_line() {
7113 let mut e = normal_editor("hello");
7114 e.open_line_above(1);
7115 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7116 // New blank line is row 0; old "hello" is row 1.
7117 assert_eq!(e.cursor().0, 0);
7118 assert_eq!(e.buffer().lines()[1], "hello");
7119 }
7120
7121 #[test]
7122 fn enter_replace_mode_sets_insert_mode() {
7123 let mut e = normal_editor("hello");
7124 e.enter_replace_mode(1);
7125 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7126 }
7127
7128 // ── Char / line ops ──────────────────────────────────────────────────────
7129
7130 #[test]
7131 fn delete_char_forward_removes_one_char() {
7132 let mut e = normal_editor("hello");
7133 e.jump_cursor(0, 1);
7134 e.delete_char_forward(1);
7135 assert_eq!(e.buffer().lines()[0], "hllo");
7136 }
7137
7138 #[test]
7139 fn delete_char_forward_count_5_removes_five() {
7140 let mut e = normal_editor("hello world");
7141 e.delete_char_forward(5);
7142 assert_eq!(e.buffer().lines()[0], " world");
7143 }
7144
7145 #[test]
7146 fn delete_char_forward_noop_on_empty_line() {
7147 let mut e = normal_editor("");
7148 let before = e.content().to_string();
7149 e.delete_char_forward(1);
7150 // Empty buffer: no chars to delete, content unchanged.
7151 assert_eq!(e.content(), before.as_str());
7152 }
7153
7154 #[test]
7155 fn delete_char_backward_removes_char_before_cursor() {
7156 let mut e = normal_editor("hello");
7157 e.jump_cursor(0, 3);
7158 e.delete_char_backward(1);
7159 assert_eq!(e.buffer().lines()[0], "helo");
7160 }
7161
7162 #[test]
7163 fn delete_char_backward_noop_at_col_0() {
7164 let mut e = normal_editor("hello");
7165 e.jump_cursor(0, 0);
7166 e.delete_char_backward(1);
7167 assert_eq!(e.buffer().lines()[0], "hello");
7168 }
7169
7170 #[test]
7171 fn substitute_char_deletes_and_enters_insert() {
7172 let mut e = normal_editor("hello");
7173 e.jump_cursor(0, 0);
7174 e.substitute_char(1);
7175 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7176 assert_eq!(e.buffer().lines()[0], "ello");
7177 }
7178
7179 #[test]
7180 fn substitute_char_count_3_deletes_three() {
7181 let mut e = normal_editor("hello");
7182 e.substitute_char(3);
7183 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7184 assert_eq!(e.buffer().lines()[0], "lo");
7185 }
7186
7187 #[test]
7188 fn substitute_line_clears_content_and_enters_insert() {
7189 let mut e = normal_editor("hello world");
7190 e.substitute_line(1);
7191 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7192 assert_eq!(e.buffer().lines()[0], "");
7193 }
7194
7195 #[test]
7196 fn delete_to_eol_removes_from_cursor_to_end() {
7197 let mut e = normal_editor("hello world");
7198 e.jump_cursor(0, 5);
7199 e.delete_to_eol();
7200 // col 5 is ' ' — deletes " world", leaving "hello".
7201 assert_eq!(e.buffer().lines()[0], "hello");
7202 }
7203
7204 #[test]
7205 fn delete_to_eol_noop_when_cursor_past_end() {
7206 let mut e = normal_editor("hi");
7207 e.jump_cursor(0, 2);
7208 e.delete_to_eol();
7209 assert_eq!(e.buffer().lines()[0], "hi");
7210 }
7211
7212 #[test]
7213 fn change_to_eol_enters_insert() {
7214 let mut e = normal_editor("hello world");
7215 e.jump_cursor(0, 5);
7216 e.change_to_eol();
7217 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7218 // col 5 is ' ' — deletes " world", leaving "hello".
7219 assert_eq!(e.buffer().lines()[0], "hello");
7220 }
7221
7222 #[test]
7223 fn yank_to_eol_fills_register() {
7224 let mut e = normal_editor("hello world");
7225 e.jump_cursor(0, 6);
7226 e.yank_to_eol(1);
7227 // Yank does not change mode.
7228 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7229 // Unnamed register holds the yanked text (col 6 is 'w' in "world").
7230 assert!(
7231 e.registers().unnamed.text.starts_with("world")
7232 || e.registers().unnamed.text.contains("world")
7233 );
7234 }
7235
7236 #[test]
7237 fn join_line_merges_next_line_with_space() {
7238 let mut e = normal_editor("foo\nbar");
7239 e.join_line(1);
7240 assert_eq!(e.buffer().lines()[0], "foo bar");
7241 }
7242
7243 #[test]
7244 fn join_line_count_2_merges_three_lines() {
7245 let mut e = normal_editor("a\nb\nc");
7246 e.join_line(2);
7247 // Our bridge calls join_line() `count` times, each joining the
7248 // current line with the next → 2 iterations: "a b c".
7249 assert_eq!(e.buffer().lines()[0], "a b c");
7250 }
7251
7252 #[test]
7253 fn join_line_noop_on_last_line() {
7254 let mut e = normal_editor("only");
7255 e.join_line(1);
7256 assert_eq!(e.buffer().lines()[0], "only");
7257 }
7258
7259 #[test]
7260 fn toggle_case_at_cursor_flips_letter() {
7261 let mut e = normal_editor("hello");
7262 e.toggle_case_at_cursor(1);
7263 assert_eq!(e.buffer().lines()[0], "Hello");
7264 }
7265
7266 #[test]
7267 fn toggle_case_at_cursor_count_3_flips_three() {
7268 let mut e = normal_editor("hello");
7269 e.toggle_case_at_cursor(3);
7270 assert_eq!(e.buffer().lines()[0], "HELlo");
7271 }
7272
7273 // ── Undo / redo round-trip ───────────────────────────────────────────────
7274
7275 #[test]
7276 fn undo_redo_roundtrip_via_public_methods() {
7277 let mut e = normal_editor("hello");
7278 e.delete_char_forward(1);
7279 assert_eq!(e.buffer().lines()[0], "ello");
7280 e.undo();
7281 assert_eq!(e.buffer().lines()[0], "hello");
7282 e.redo();
7283 assert_eq!(e.buffer().lines()[0], "ello");
7284 }
7285
7286 // ── Jump / scroll ────────────────────────────────────────────────────────
7287
7288 #[test]
7289 fn jump_back_and_forward_roundtrip() {
7290 let mut e = fresh_editor("a\nb\nc\nd");
7291 e.set_viewport_height(10);
7292 e.jump_cursor(3, 0);
7293 // Push current pos onto jumplist (big motion done externally; use
7294 // `run_keys` shortcut: `gg` pushes jump then `G` jumps).
7295 // Simpler: just call jump_back with empty stack → no-op (shouldn't panic).
7296 e.jump_back(1);
7297 e.jump_forward(1);
7298 }
7299
7300 #[test]
7301 fn scroll_full_page_down_moves_cursor() {
7302 use crate::vim::ScrollDir;
7303 let lines = (0..30)
7304 .map(|i| format!("line{i}"))
7305 .collect::<Vec<_>>()
7306 .join("\n");
7307 let mut e = fresh_editor(&lines);
7308 e.set_viewport_height(10);
7309 let before = e.cursor().0;
7310 e.scroll_full_page(ScrollDir::Down, 1);
7311 assert!(e.cursor().0 > before);
7312 }
7313
7314 #[test]
7315 fn scroll_full_page_up_moves_cursor() {
7316 use crate::vim::ScrollDir;
7317 let lines = (0..30)
7318 .map(|i| format!("line{i}"))
7319 .collect::<Vec<_>>()
7320 .join("\n");
7321 let mut e = fresh_editor(&lines);
7322 e.set_viewport_height(10);
7323 e.jump_cursor(25, 0);
7324 let before = e.cursor().0;
7325 e.scroll_full_page(ScrollDir::Up, 1);
7326 assert!(e.cursor().0 < before);
7327 }
7328
7329 #[test]
7330 fn scroll_half_page_down_moves_cursor() {
7331 use crate::vim::ScrollDir;
7332 let lines = (0..30)
7333 .map(|i| format!("line{i}"))
7334 .collect::<Vec<_>>()
7335 .join("\n");
7336 let mut e = fresh_editor(&lines);
7337 e.set_viewport_height(10);
7338 let before = e.cursor().0;
7339 e.scroll_half_page(ScrollDir::Down, 1);
7340 assert!(e.cursor().0 > before);
7341 }
7342
7343 #[test]
7344 fn scroll_half_page_up_at_top_is_noop() {
7345 use crate::vim::ScrollDir;
7346 let lines = (0..30)
7347 .map(|i| format!("line{i}"))
7348 .collect::<Vec<_>>()
7349 .join("\n");
7350 let mut e = fresh_editor(&lines);
7351 e.set_viewport_height(10);
7352 // Already at top, scrolling up should not panic and cursor stays at 0.
7353 e.scroll_half_page(ScrollDir::Up, 1);
7354 assert_eq!(e.cursor().0, 0);
7355 }
7356
7357 #[test]
7358 fn scroll_line_down_shifts_viewport_without_moving_cursor() {
7359 use crate::vim::ScrollDir;
7360 let lines = (0..30)
7361 .map(|i| format!("line{i}"))
7362 .collect::<Vec<_>>()
7363 .join("\n");
7364 let mut e = fresh_editor(&lines);
7365 e.set_viewport_height(10);
7366 // Park cursor in the middle of a large buffer.
7367 e.jump_cursor(15, 0);
7368 e.set_viewport_top(10);
7369 let cursor_before = e.cursor().0;
7370 e.scroll_line(ScrollDir::Down, 1);
7371 // Viewport top advances; cursor stays.
7372 assert_eq!(e.cursor().0, cursor_before);
7373 assert_eq!(e.host().viewport().top_row, 11);
7374 }
7375
7376 #[test]
7377 fn scroll_line_up_shifts_viewport() {
7378 use crate::vim::ScrollDir;
7379 let lines = (0..30)
7380 .map(|i| format!("line{i}"))
7381 .collect::<Vec<_>>()
7382 .join("\n");
7383 let mut e = fresh_editor(&lines);
7384 e.set_viewport_height(10);
7385 e.jump_cursor(15, 0);
7386 e.set_viewport_top(10);
7387 let cursor_before = e.cursor().0;
7388 e.scroll_line(ScrollDir::Up, 1);
7389 assert_eq!(e.cursor().0, cursor_before);
7390 assert_eq!(e.host().viewport().top_row, 9);
7391 }
7392
7393 #[test]
7394 fn scroll_line_clamps_cursor_when_off_screen() {
7395 use crate::vim::ScrollDir;
7396 let lines = (0..30)
7397 .map(|i| format!("line{i}"))
7398 .collect::<Vec<_>>()
7399 .join("\n");
7400 let mut e = fresh_editor(&lines);
7401 e.set_viewport_height(10);
7402 // Cursor at viewport top; scrolling down pushes it off — must clamp.
7403 e.jump_cursor(5, 0);
7404 e.set_viewport_top(5);
7405 e.scroll_line(ScrollDir::Down, 3);
7406 // New top = 8; cursor was at 5, which is now off-screen (< 8).
7407 // Cursor clamped to new top.
7408 assert!(e.cursor().0 >= 8);
7409 }
7410
7411 #[test]
7412 fn scroll_doesnt_crash_at_buffer_edges() {
7413 use crate::vim::ScrollDir;
7414 let mut e = normal_editor("single line");
7415 e.set_viewport_height(10);
7416 // Should not panic on any of these at-the-edge scrolls.
7417 e.scroll_full_page(ScrollDir::Down, 99);
7418 e.scroll_full_page(ScrollDir::Up, 99);
7419 e.scroll_half_page(ScrollDir::Down, 99);
7420 e.scroll_half_page(ScrollDir::Up, 99);
7421 e.scroll_line(ScrollDir::Down, 99);
7422 e.scroll_line(ScrollDir::Up, 99);
7423 }
7424
7425 // ── Horizontal scroll ────────────────────────────────────────────────────
7426
7427 #[test]
7428 fn scroll_right_advances_top_col() {
7429 let mut e = fresh_editor("hello world");
7430 e.set_viewport_height(10);
7431 e.scroll_right(5);
7432 assert_eq!(e.host().viewport().top_col, 5);
7433 }
7434
7435 #[test]
7436 fn scroll_left_does_not_underflow() {
7437 let mut e = fresh_editor("hello world");
7438 e.set_viewport_height(10);
7439 e.scroll_right(2);
7440 e.scroll_left(10);
7441 assert_eq!(e.host().viewport().top_col, 0);
7442 }
7443
7444 #[test]
7445 fn scroll_left_then_right_roundtrip() {
7446 let mut e = fresh_editor("hello world");
7447 e.set_viewport_height(10);
7448 e.scroll_right(10);
7449 e.scroll_left(3);
7450 assert_eq!(e.host().viewport().top_col, 7);
7451 }
7452
7453 // ── Search ───────────────────────────────────────────────────────────────
7454
7455 #[test]
7456 fn search_repeat_advances_to_next_match() {
7457 let mut e = fresh_editor("foo bar foo baz");
7458 // Use word_search to seed the search state (no search prompt needed).
7459 // `*` on "foo" at col 0 finds the second "foo" and sets last_search.
7460 e.word_search(true, true, 1);
7461 // Repeating forward wraps and finds the first "foo" again at col 0.
7462 e.search_repeat(true, 1);
7463 // Just ensure no panic and search state is valid.
7464 assert!(e.cursor().0 < e.buffer().lines().len());
7465 }
7466
7467 #[test]
7468 fn search_repeat_no_pattern_is_noop() {
7469 let mut e = normal_editor("hello world");
7470 let before = e.cursor();
7471 // No search pattern loaded — should not panic.
7472 e.search_repeat(true, 1);
7473 assert_eq!(e.cursor(), before);
7474 }
7475
7476 #[test]
7477 fn word_search_finds_word_under_cursor() {
7478 let mut e = fresh_editor("foo bar foo");
7479 // cursor starts at col 0 on "foo"
7480 e.word_search(true, true, 1);
7481 // Should jump to the second "foo" at col 8.
7482 assert_eq!(e.cursor().1, 8);
7483 }
7484
7485 #[test]
7486 fn word_search_whole_word_false_extracts_word_under_cursor() {
7487 // `g*` on "foo" (no `\b`) — use two lines so wrap can find the next match.
7488 let mut e = fresh_editor("foobar\nfoo baz");
7489 // Cursor on second line "foo" at col 0.
7490 e.jump_cursor(1, 0);
7491 // g* with whole_word=false: pattern = "foo", advance forward (skip current).
7492 // Starting at (1, 0), skip "foo" at (1,0), wrap to (0, 0) which matches "foo"
7493 // inside "foobar".
7494 e.word_search(true, false, 1);
7495 // Cursor should land on "foo" at row 0, col 0.
7496 assert_eq!(e.cursor(), (0, 0));
7497 }
7498
7499 #[test]
7500 fn word_search_backward_finds_previous_match() {
7501 let mut e = fresh_editor("foo bar foo");
7502 e.jump_cursor(0, 8); // on second "foo"
7503 e.word_search(false, true, 1);
7504 // Cursor should land on col 0 (first "foo").
7505 assert_eq!(e.cursor().1, 0);
7506 }
7507
7508 // ── Edge cases ───────────────────────────────────────────────────────────
7509
7510 #[test]
7511 fn delete_char_forward_on_single_char_line() {
7512 let mut e = normal_editor("x");
7513 e.delete_char_forward(1);
7514 assert_eq!(e.buffer().lines()[0], "");
7515 }
7516
7517 #[test]
7518 fn substitute_char_on_empty_line_is_noop_for_delete() {
7519 let mut e = normal_editor("");
7520 e.substitute_char(1);
7521 // Nothing to delete — but should enter Insert mode.
7522 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7523 }
7524
7525 #[test]
7526 fn join_line_10_iterations_clamps_gracefully() {
7527 let mut e = normal_editor("a\nb");
7528 // Joining 10 times on a 2-line buffer should not panic.
7529 e.join_line(10);
7530 // After the first join succeeds, the rest are no-ops.
7531 assert_eq!(e.buffer().lines()[0], "a b");
7532 }
7533
7534 #[test]
7535 fn toggle_case_past_line_end_is_noop() {
7536 let mut e = normal_editor("ab");
7537 e.jump_cursor(0, 5); // way past end
7538 e.toggle_case_at_cursor(1);
7539 // Should not panic.
7540 assert_eq!(e.buffer().lines()[0], "ab");
7541 }
7542
7543 // ── Phase 6.3: visual-mode primitive tests (kryptic-sh/hjkl#89) ──────────
7544
7545 // ── Visual entry ─────────────────────────────────────────────────────────
7546
7547 #[test]
7548 fn enter_visual_char_lands_in_visual_at_cursor() {
7549 let mut e = normal_editor("hello world");
7550 e.jump_cursor(0, 3);
7551 e.enter_visual_char();
7552 assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7553 // Anchor should be at the cursor position we entered from.
7554 assert_eq!(e.vim.visual_anchor, (0, 3));
7555 }
7556
7557 #[test]
7558 fn enter_visual_line_lands_in_visual_line() {
7559 let mut e = normal_editor("hello\nworld");
7560 e.jump_cursor(1, 2);
7561 e.enter_visual_line();
7562 assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7563 // Line anchor should be the current row.
7564 assert_eq!(e.vim.visual_line_anchor, 1);
7565 }
7566
7567 #[test]
7568 fn enter_visual_block_lands_in_visual_block() {
7569 let mut e = normal_editor("hello\nworld");
7570 e.jump_cursor(0, 2);
7571 e.enter_visual_block();
7572 assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7573 // Block anchor and vcol should match the cursor column.
7574 assert_eq!(e.vim.block_anchor, (0, 2));
7575 assert_eq!(e.vim.block_vcol, 2);
7576 }
7577
7578 // ── Visual exit ──────────────────────────────────────────────────────────
7579
7580 #[test]
7581 fn exit_visual_to_normal_sets_marks_and_returns_to_normal() {
7582 let mut e = normal_editor("hello world");
7583 // Enter charwise visual at col 2, extend to col 5.
7584 e.jump_cursor(0, 2);
7585 e.enter_visual_char();
7586 e.jump_cursor(0, 5);
7587 e.exit_visual_to_normal();
7588 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7589 // `<` = (0, 2), `>` = (0, 5).
7590 assert_eq!(e.mark('<'), Some((0, 2)));
7591 assert_eq!(e.mark('>'), Some((0, 5)));
7592 }
7593
7594 #[test]
7595 fn exit_visual_to_normal_stores_last_visual() {
7596 let mut e = normal_editor("hello world");
7597 e.jump_cursor(0, 1);
7598 e.enter_visual_char();
7599 e.jump_cursor(0, 4);
7600 e.exit_visual_to_normal();
7601 // last_visual should be set so gv can restore it.
7602 assert!(e.vim.last_visual.is_some());
7603 let lv = e.vim.last_visual.unwrap();
7604 assert_eq!(lv.anchor, (0, 1));
7605 assert_eq!(lv.cursor, (0, 4));
7606 }
7607
7608 #[test]
7609 fn exit_visual_line_sets_marks_at_line_boundaries() {
7610 let mut e = normal_editor("alpha\nbeta\ngamma");
7611 e.enter_visual_line(); // row 0
7612 e.jump_cursor(1, 3);
7613 e.exit_visual_to_normal();
7614 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7615 // `<` snaps to (min_row, 0), `>` snaps to (max_row, last_col).
7616 assert_eq!(e.mark('<'), Some((0, 0)));
7617 let last_col_of_beta = "beta".chars().count() - 1;
7618 assert_eq!(e.mark('>'), Some((1, last_col_of_beta)));
7619 }
7620
7621 // ── visual_o_toggle ───────────────────────────────────────────────────────
7622
7623 #[test]
7624 fn visual_o_toggle_swaps_anchor_and_cursor_charwise() {
7625 let mut e = normal_editor("hello world");
7626 // Enter visual at col 0, extend to col 4.
7627 e.enter_visual_char(); // anchor = (0,0)
7628 e.jump_cursor(0, 4); // cursor at col 4
7629 // Selection bounds before toggle: anchor=0, cursor=4.
7630 let pre_anchor = e.vim.visual_anchor;
7631 let pre_cursor = e.cursor();
7632 e.visual_o_toggle();
7633 // After toggle: cursor jumps to old anchor, anchor = old cursor.
7634 assert_eq!(e.cursor(), pre_anchor, "cursor should move to old anchor");
7635 assert_eq!(
7636 e.vim.visual_anchor, pre_cursor,
7637 "anchor should take old cursor position"
7638 );
7639 // Mode is unchanged.
7640 assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7641 }
7642
7643 #[test]
7644 fn visual_o_toggle_double_returns_to_start() {
7645 let mut e = normal_editor("hello world");
7646 e.enter_visual_char();
7647 e.jump_cursor(0, 4);
7648 let anchor0 = e.vim.visual_anchor;
7649 let cursor0 = e.cursor();
7650 e.visual_o_toggle();
7651 e.visual_o_toggle();
7652 // Two toggles restore original positions.
7653 assert_eq!(e.vim.visual_anchor, anchor0);
7654 assert_eq!(e.cursor(), cursor0);
7655 }
7656
7657 #[test]
7658 fn visual_o_toggle_linewise_swaps_anchor_row() {
7659 let mut e = normal_editor("alpha\nbeta\ngamma");
7660 e.enter_visual_line(); // anchor row = 0
7661 e.jump_cursor(2, 0); // cursor on row 2
7662 e.visual_o_toggle();
7663 // Cursor should jump to old anchor row.
7664 assert_eq!(e.cursor().0, 0, "cursor row should be old anchor row");
7665 // Anchor row should now be the old cursor row.
7666 assert_eq!(e.vim.visual_line_anchor, 2);
7667 }
7668
7669 // ── reenter_last_visual ───────────────────────────────────────────────────
7670
7671 #[test]
7672 fn reenter_last_visual_after_vdollar_esc_restores() {
7673 let mut e = normal_editor("hello world");
7674 // v$ then Esc via FSM to store a real last_visual.
7675 e.enter_visual_char(); // anchor = (0,0)
7676 e.jump_cursor(0, 5); // move cursor to col 5 to create a range
7677 e.exit_visual_to_normal();
7678 // Should be back to Normal.
7679 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7680 // gv — should restore Visual mode.
7681 e.reenter_last_visual();
7682 assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7683 // Cursor should be at the stored last position (col 5).
7684 assert_eq!(e.cursor().1, 5);
7685 }
7686
7687 #[test]
7688 fn reenter_last_visual_noop_when_no_history() {
7689 let mut e = normal_editor("hello");
7690 // No prior visual — should be a no-op, not a panic.
7691 e.reenter_last_visual();
7692 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7693 }
7694
7695 // ── set_mode ─────────────────────────────────────────────────────────────
7696
7697 #[test]
7698 fn set_mode_insert_flips_vim_mode_to_insert() {
7699 let mut e = normal_editor("hello");
7700 e.set_mode(crate::VimMode::Insert);
7701 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7702 }
7703
7704 #[test]
7705 fn set_mode_roundtrip_normal_insert_normal() {
7706 let mut e = normal_editor("hello");
7707 e.set_mode(crate::VimMode::Insert);
7708 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7709 e.set_mode(crate::VimMode::Normal);
7710 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7711 }
7712
7713 #[test]
7714 fn set_mode_visual_variants() {
7715 let mut e = normal_editor("hello");
7716 e.set_mode(crate::VimMode::Visual);
7717 assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7718 e.set_mode(crate::VimMode::VisualLine);
7719 assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7720 e.set_mode(crate::VimMode::VisualBlock);
7721 assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7722 e.set_mode(crate::VimMode::Normal);
7723 assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7724 }
7725
7726 // ── current_mode / vim_mode consistency ───────────────────────────────────
7727
7728 // ── Phase 6.6b: FSM state accessor smoke tests ────────────────────────────
7729
7730 #[test]
7731 fn pending_round_trips() {
7732 let mut e = normal_editor("hello");
7733 assert!(matches!(e.pending(), crate::vim::Pending::None));
7734 e.set_pending(crate::vim::Pending::G);
7735 assert!(matches!(e.pending(), crate::vim::Pending::G));
7736 let taken = e.take_pending();
7737 assert!(matches!(taken, crate::vim::Pending::G));
7738 assert!(matches!(e.pending(), crate::vim::Pending::None));
7739 }
7740
7741 #[test]
7742 fn count_round_trips() {
7743 let mut e = normal_editor("hello");
7744 assert_eq!(e.count(), 0);
7745 e.set_count(5);
7746 assert_eq!(e.count(), 5);
7747 e.accumulate_count_digit(3);
7748 assert_eq!(e.count(), 53);
7749 e.reset_count();
7750 assert_eq!(e.count(), 0);
7751 }
7752
7753 #[test]
7754 fn take_count_returns_one_when_zero() {
7755 let mut e = normal_editor("hello");
7756 assert_eq!(e.take_count(), 1);
7757 }
7758
7759 #[test]
7760 fn take_count_returns_value_and_resets() {
7761 let mut e = normal_editor("hello");
7762 e.set_count(7);
7763 assert_eq!(e.take_count(), 7);
7764 assert_eq!(e.count(), 0);
7765 }
7766
7767 #[test]
7768 fn fsm_mode_round_trips() {
7769 let mut e = normal_editor("hello");
7770 assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7771 e.set_fsm_mode(crate::vim::Mode::Insert);
7772 assert_eq!(e.fsm_mode(), crate::vim::Mode::Insert);
7773 assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7774 e.set_fsm_mode(crate::vim::Mode::Normal);
7775 assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7776 }
7777
7778 #[test]
7779 fn replaying_flag_round_trips() {
7780 let mut e = normal_editor("hello");
7781 assert!(!e.is_replaying());
7782 e.set_replaying(true);
7783 assert!(e.is_replaying());
7784 e.set_replaying(false);
7785 assert!(!e.is_replaying());
7786 }
7787
7788 #[test]
7789 fn one_shot_normal_flag_round_trips() {
7790 let mut e = normal_editor("hello");
7791 assert!(!e.is_one_shot_normal());
7792 e.set_one_shot_normal(true);
7793 assert!(e.is_one_shot_normal());
7794 e.set_one_shot_normal(false);
7795 assert!(!e.is_one_shot_normal());
7796 }
7797
7798 #[test]
7799 fn last_find_round_trips() {
7800 let mut e = normal_editor("hello");
7801 assert_eq!(e.last_find(), None);
7802 e.set_last_find(Some(('x', true, false)));
7803 assert_eq!(e.last_find(), Some(('x', true, false)));
7804 e.set_last_find(None);
7805 assert_eq!(e.last_find(), None);
7806 }
7807
7808 #[test]
7809 fn last_change_round_trips() {
7810 let mut e = normal_editor("hello");
7811 assert!(e.last_change().is_none());
7812 e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 2 }));
7813 let lc = e.last_change();
7814 assert!(matches!(
7815 lc,
7816 Some(crate::vim::LastChange::ToggleCase { count: 2 })
7817 ));
7818 e.set_last_change(None);
7819 assert!(e.last_change().is_none());
7820 }
7821
7822 #[test]
7823 fn last_change_mut_allows_in_place_edit() {
7824 let mut e = normal_editor("hello");
7825 e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 1 }));
7826 if let Some(crate::vim::LastChange::ToggleCase { count }) = e.last_change_mut() {
7827 *count = 42;
7828 }
7829 assert!(matches!(
7830 e.last_change(),
7831 Some(crate::vim::LastChange::ToggleCase { count: 42 })
7832 ));
7833 }
7834
7835 #[test]
7836 fn insert_session_round_trips() {
7837 let mut e = normal_editor("hello");
7838 assert!(e.insert_session().is_none());
7839 e.set_insert_session(Some(crate::vim::InsertSession {
7840 count: 3,
7841 row_min: 0,
7842 row_max: 0,
7843 before_lines: vec!["hello".to_string()],
7844 reason: crate::vim::InsertReason::Enter(crate::vim::InsertEntry::I),
7845 }));
7846 assert_eq!(e.insert_session().map(|s| s.count), Some(3));
7847 let taken = e.take_insert_session();
7848 assert!(taken.is_some());
7849 assert!(e.insert_session().is_none());
7850 }
7851
7852 #[test]
7853 fn visual_anchor_round_trips() {
7854 let mut e = normal_editor("hello");
7855 e.set_visual_anchor((1, 3));
7856 assert_eq!(e.visual_anchor(), (1, 3));
7857 }
7858
7859 #[test]
7860 fn visual_line_anchor_round_trips() {
7861 let mut e = normal_editor("hello\nworld");
7862 e.set_visual_line_anchor(1);
7863 assert_eq!(e.visual_line_anchor(), 1);
7864 }
7865
7866 #[test]
7867 fn block_anchor_and_vcol_round_trip() {
7868 let mut e = normal_editor("hello");
7869 e.set_block_anchor((0, 2));
7870 e.set_block_vcol(4);
7871 assert_eq!(e.block_anchor(), (0, 2));
7872 assert_eq!(e.block_vcol(), 4);
7873 }
7874
7875 #[test]
7876 fn yank_linewise_round_trips() {
7877 let mut e = normal_editor("hello");
7878 assert!(!e.yank_linewise());
7879 e.set_yank_linewise(true);
7880 assert!(e.yank_linewise());
7881 }
7882
7883 #[test]
7884 fn pending_register_raw_round_trips() {
7885 let mut e = normal_editor("hello");
7886 assert_eq!(e.pending_register(), None);
7887 e.set_pending_register_raw(Some('a'));
7888 assert_eq!(e.pending_register(), Some('a'));
7889 let taken = e.take_pending_register_raw();
7890 assert_eq!(taken, Some('a'));
7891 assert_eq!(e.pending_register(), None);
7892 }
7893
7894 #[test]
7895 fn recording_macro_round_trips() {
7896 let mut e = normal_editor("hello");
7897 assert_eq!(e.recording_macro(), None);
7898 e.set_recording_macro(Some('q'));
7899 assert_eq!(e.recording_macro(), Some('q'));
7900 e.set_recording_macro(None);
7901 assert_eq!(e.recording_macro(), None);
7902 }
7903
7904 #[test]
7905 fn recording_keys_round_trips() {
7906 let mut e = normal_editor("hello");
7907 let input = crate::Input {
7908 key: crate::Key::Char('j'),
7909 ctrl: false,
7910 alt: false,
7911 shift: false,
7912 };
7913 e.push_recording_key(input);
7914 assert_eq!(e.take_recording_keys(), vec![input]);
7915 assert!(e.take_recording_keys().is_empty());
7916 }
7917
7918 #[test]
7919 fn replaying_macro_raw_round_trips() {
7920 let mut e = normal_editor("hello");
7921 assert!(!e.is_replaying_macro_raw());
7922 e.set_replaying_macro_raw(true);
7923 assert!(e.is_replaying_macro_raw());
7924 e.set_replaying_macro_raw(false);
7925 assert!(!e.is_replaying_macro_raw());
7926 }
7927
7928 #[test]
7929 fn last_macro_round_trips() {
7930 let mut e = normal_editor("hello");
7931 assert_eq!(e.last_macro(), None);
7932 e.set_last_macro(Some('m'));
7933 assert_eq!(e.last_macro(), Some('m'));
7934 }
7935
7936 #[test]
7937 fn last_insert_pos_round_trips() {
7938 let mut e = normal_editor("hello");
7939 assert_eq!(e.last_insert_pos(), None);
7940 e.set_last_insert_pos(Some((1, 2)));
7941 assert_eq!(e.last_insert_pos(), Some((1, 2)));
7942 }
7943
7944 #[test]
7945 fn last_visual_round_trips() {
7946 let mut e = normal_editor("hello");
7947 assert!(e.last_visual().is_none());
7948 let snap = crate::vim::LastVisual {
7949 mode: crate::vim::Mode::Visual,
7950 anchor: (0, 0),
7951 cursor: (0, 3),
7952 block_vcol: 0,
7953 };
7954 e.set_last_visual(Some(snap));
7955 assert!(e.last_visual().is_some());
7956 e.set_last_visual(None);
7957 assert!(e.last_visual().is_none());
7958 }
7959
7960 #[test]
7961 fn viewport_pinned_round_trips() {
7962 let mut e = normal_editor("hello");
7963 assert!(!e.viewport_pinned());
7964 e.set_viewport_pinned(true);
7965 assert!(e.viewport_pinned());
7966 e.set_viewport_pinned(false);
7967 assert!(!e.viewport_pinned());
7968 }
7969
7970 #[test]
7971 fn insert_pending_register_round_trips() {
7972 let mut e = normal_editor("hello");
7973 assert!(!e.insert_pending_register());
7974 e.set_insert_pending_register(true);
7975 assert!(e.insert_pending_register());
7976 }
7977
7978 #[test]
7979 fn change_mark_start_round_trips() {
7980 let mut e = normal_editor("hello");
7981 assert_eq!(e.change_mark_start(), None);
7982 e.set_change_mark_start(Some((2, 5)));
7983 assert_eq!(e.change_mark_start(), Some((2, 5)));
7984 let taken = e.take_change_mark_start();
7985 assert_eq!(taken, Some((2, 5)));
7986 assert_eq!(e.change_mark_start(), None);
7987 }
7988
7989 #[test]
7990 fn search_prompt_state_round_trips() {
7991 let mut e = normal_editor("hello");
7992 assert!(e.search_prompt_state().is_none());
7993 e.set_search_prompt_state(Some(crate::vim::SearchPrompt {
7994 text: "foo".to_string(),
7995 cursor: 3,
7996 forward: true,
7997 }));
7998 assert_eq!(
7999 e.search_prompt_state().map(|p| p.text.as_str()),
8000 Some("foo")
8001 );
8002 let taken = e.take_search_prompt_state();
8003 assert!(taken.is_some());
8004 assert!(e.search_prompt_state().is_none());
8005 }
8006
8007 #[test]
8008 fn last_search_pattern_and_direction_round_trips() {
8009 let mut e = normal_editor("hello");
8010 assert_eq!(e.last_search_pattern(), None);
8011 e.set_last_search_pattern_only(Some("world".to_string()));
8012 assert_eq!(e.last_search_pattern(), Some("world"));
8013 e.set_last_search_forward_only(false);
8014 assert!(!e.last_search_forward());
8015 }
8016
8017 #[test]
8018 fn search_history_round_trips() {
8019 let mut e = normal_editor("hello");
8020 assert!(e.search_history().is_empty());
8021 e.search_history_mut().push("pattern1".to_string());
8022 assert_eq!(e.search_history(), &["pattern1"]);
8023 e.set_search_history_cursor(Some(0));
8024 assert_eq!(e.search_history_cursor(), Some(0));
8025 e.set_search_history_cursor(None);
8026 assert_eq!(e.search_history_cursor(), None);
8027 }
8028
8029 #[test]
8030 fn jump_lists_round_trips() {
8031 let mut e = normal_editor("hello");
8032 assert!(e.jump_back_list().is_empty());
8033 assert!(e.jump_fwd_list().is_empty());
8034 e.jump_back_list_mut().push((1, 2));
8035 e.jump_fwd_list_mut().push((3, 4));
8036 assert_eq!(e.jump_back_list(), &[(1, 2)]);
8037 assert_eq!(e.jump_fwd_list(), &[(3, 4)]);
8038 }
8039
8040 #[test]
8041 fn last_input_timing_round_trips() {
8042 let mut e = normal_editor("hello");
8043 assert!(e.last_input_at().is_none());
8044 assert!(e.last_input_host_at().is_none());
8045 let now = std::time::Instant::now();
8046 e.set_last_input_at(Some(now));
8047 assert!(e.last_input_at().is_some());
8048 let dur = core::time::Duration::from_millis(100);
8049 e.set_last_input_host_at(Some(dur));
8050 assert_eq!(e.last_input_host_at(), Some(dur));
8051 }
8052
8053 // ── auto_indent_range tests ──────────────────────────────────────────────
8054
8055 /// Helper: build an editor with `expandtab=true` and the given shiftwidth.
8056 fn indent_editor(initial: &str, shiftwidth: usize, expandtab: bool) -> Editor {
8057 let mut e = fresh_editor(initial);
8058 e.settings_mut().shiftwidth = shiftwidth;
8059 e.settings_mut().expandtab = expandtab;
8060 e
8061 }
8062
8063 #[test]
8064 fn auto_indent_single_line_under_open_brace() {
8065 // `{\nfoo\n}` — "foo" is at depth 1 under the `{`.
8066 // With shiftwidth=4 expandtab=true it should become " foo".
8067 let mut e = indent_editor("{\nfoo\n}", 4, true);
8068 // auto-indent only row 1 ("foo").
8069 e.auto_indent_range((1, 0), (1, 0));
8070 let lines = e.buffer().lines();
8071 assert_eq!(lines[1], " foo", "foo should be indented by 4 spaces");
8072 }
8073
8074 #[test]
8075 fn auto_indent_close_brace_outdents() {
8076 // `{\n inner\n}` — the `}` is at depth 1 but starts with a close
8077 // bracket so effective_depth = 0.
8078 let mut e = indent_editor("{\n inner\n}", 4, true);
8079 e.auto_indent_range((2, 0), (2, 0));
8080 let lines = e.buffer().lines();
8081 assert_eq!(lines[2], "}", "`}}` should have zero indent");
8082 }
8083
8084 #[test]
8085 fn auto_indent_whole_buffer_normalizes_mixed_indent() {
8086 // Mixed-indent input: first line un-indented `{`, second line 1-tab
8087 // indented body, third line un-indented `}`.
8088 let src = "{\n\tbody\n}";
8089 let mut e = indent_editor(src, 4, true);
8090 let total = e.buffer().lines().len();
8091 e.auto_indent_range((0, 0), (total - 1, 0));
8092 let lines = e.buffer().lines();
8093 // `{` — depth 0 at start.
8094 assert_eq!(lines[0], "{");
8095 // `body` — depth 1 after `{`.
8096 assert_eq!(lines[1], " body");
8097 // `}` — depth 1 but starts with close → effective_depth 0.
8098 assert_eq!(lines[2], "}");
8099 }
8100
8101 #[test]
8102 fn auto_indent_respects_expandtab_false_uses_tabs() {
8103 // Same buffer, but expandtab=false → indent unit is `\t`.
8104 let src = "{\nbody\n}";
8105 let mut e = indent_editor(src, 4, false);
8106 let total = e.buffer().lines().len();
8107 e.auto_indent_range((0, 0), (total - 1, 0));
8108 let lines = e.buffer().lines();
8109 assert_eq!(lines[0], "{");
8110 assert_eq!(lines[1], "\tbody");
8111 assert_eq!(lines[2], "}");
8112 }
8113
8114 #[test]
8115 fn auto_indent_empty_line_stays_empty() {
8116 // `{\n\nfoo\n}` — blank line in the middle should stay blank.
8117 let src = "{\n\nfoo\n}";
8118 let mut e = indent_editor(src, 4, true);
8119 let total = e.buffer().lines().len();
8120 e.auto_indent_range((0, 0), (total - 1, 0));
8121 let lines = e.buffer().lines();
8122 assert_eq!(lines[1], "", "blank line should stay blank");
8123 assert_eq!(lines[2], " foo");
8124 }
8125
8126 #[test]
8127 fn auto_indent_cursor_lands_on_first_nonws_of_start_row() {
8128 // After `==` / `auto_indent_range` the cursor should be at the first
8129 // non-whitespace character of start_row (vim parity).
8130 let src = "{\nfoo\n}";
8131 let mut e = indent_editor(src, 4, true);
8132 // Reindent only row 1.
8133 e.auto_indent_range((1, 0), (1, 0));
8134 // Row 1 after reindent is " foo"; first non-ws is col 4.
8135 let (row, col) = e.cursor();
8136 assert_eq!(row, 1, "cursor should stay on start_row");
8137 assert_eq!(col, 4, "cursor should land on first non-ws char (col 4)");
8138 }
8139
8140 #[test]
8141 fn auto_indent_sets_last_indent_range() {
8142 // After `auto_indent_range` the engine must store the touched row span.
8143 let src = "{\nfoo\nbar\n}";
8144 let mut e = indent_editor(src, 4, true);
8145 let total = e.buffer().lines().len();
8146 e.auto_indent_range((0, 0), (total - 1, 0));
8147 assert_eq!(
8148 e.take_last_indent_range(),
8149 Some((0, total - 1)),
8150 "take_last_indent_range must return Some with the touched rows"
8151 );
8152 }
8153
8154 #[test]
8155 fn take_last_indent_range_clears() {
8156 // A second call after draining must return None.
8157 let src = "{\nfoo\n}";
8158 let mut e = indent_editor(src, 4, true);
8159 e.auto_indent_range((0, 0), (2, 0));
8160 let _ = e.take_last_indent_range(); // drain
8161 assert_eq!(
8162 e.take_last_indent_range(),
8163 None,
8164 "second take_last_indent_range must return None"
8165 );
8166 }
8167
8168 // ── Diagnostic: auto_indent vs cargo fmt on a real source file ────────
8169 //
8170 // Loads `motions.rs` (~1400 LOC, mixed real-world Rust patterns: method
8171 // chains, multi-line fn args, match arms, where clauses, closures, nested
8172 // types) at compile time, runs `auto_indent_range` over every row, and
8173 // diffs per-line leading-whitespace counts against the cargo-fmt'd source
8174 // (the file is in the repo, fmt'd by CI on every commit).
8175 //
8176 // The test PRINTS divergences and only fails if more than `THRESHOLD`
8177 // lines disagree — the dumb shiftwidth+bracket algorithm is documented
8178 // to mishandle some patterns (chains, where clauses, etc.). A full
8179 // language-aware indenter is a v2 follow-up. The point of this test is
8180 // to surface the divergence list so we can decide which patterns the
8181 // dumb algo CAN be taught to handle without going full tree-sitter.
8182 //
8183 // To diagnose: run with `--nocapture` to see the full diff.
8184 #[test]
8185 #[ignore = "diagnostic — run with --ignored --nocapture to see auto-indent vs cargo fmt diffs"]
8186 fn auto_indent_vs_cargo_fmt_motions_diagnostic() {
8187 let original = include_str!("motions.rs");
8188
8189 let mut e = Editor::new(
8190 hjkl_buffer::Buffer::new(),
8191 crate::types::DefaultHost::new(),
8192 crate::types::Options {
8193 shiftwidth: 4,
8194 expandtab: true,
8195 tabstop: 4,
8196 ..crate::types::Options::default()
8197 },
8198 );
8199 e.set_content(original);
8200
8201 let row_count = buf_row_count(&e.buffer);
8202 e.auto_indent_range((0, 0), (row_count.saturating_sub(1), 0));
8203
8204 let after_lines: Vec<String> = (0..row_count)
8205 .filter_map(|r| buf_line(&e.buffer, r).map(str::to_owned))
8206 .collect();
8207 let original_lines: Vec<&str> = original.lines().collect();
8208
8209 let leading_ws = |s: &str| s.chars().take_while(|c| c.is_whitespace()).count();
8210
8211 let mut diffs: Vec<(usize, String, usize, usize)> = Vec::new();
8212 for (i, (orig, after)) in original_lines.iter().zip(after_lines.iter()).enumerate() {
8213 let want = leading_ws(orig);
8214 let got = leading_ws(after);
8215 if want != got {
8216 diffs.push((i + 1, orig.trim().chars().take(80).collect(), want, got));
8217 }
8218 }
8219
8220 // Print the first 50 divergences for diagnosis.
8221 eprintln!(
8222 "auto_indent_vs_cargo_fmt: {} lines differ out of {} ({}%)",
8223 diffs.len(),
8224 original_lines.len(),
8225 (diffs.len() * 100) / original_lines.len().max(1),
8226 );
8227 for (line_no, content, want, got) in diffs.iter().take(50) {
8228 eprintln!(" L{line_no:5} want={want:2} got={got:2} {content}");
8229 }
8230 if diffs.len() > 50 {
8231 eprintln!(" ... and {} more", diffs.len() - 50);
8232 }
8233
8234 // Soft assertion — track divergence count over time. If the algo
8235 // gets smarter, this number should drop. If a regression makes it
8236 // jump, we'll notice. Set the cap generously above current baseline.
8237 let pct = (diffs.len() * 100) / original_lines.len().max(1);
8238 // 2026-05-16 baseline after fixing bracket scan + chain continuation:
8239 // 5 divergences / 1416 lines (<1%). Remaining lines are a single
8240 // \`let X = if {} else {};\` trailing-\`=\` continuation pattern —
8241 // documented v2 follow-up. Cap at 2% so any regression in the
8242 // bracket scan or chain detection trips the test.
8243 assert!(
8244 pct < 2,
8245 "auto_indent diverges from cargo fmt on {pct}% of lines — regression from <1% baseline"
8246 );
8247 }
8248}