Skip to main content

hjkl_engine/
editor.rs

1//! Editor — the public sqeel-vim type, layered over `hjkl_buffer::Buffer`.
2//!
3//! This file owns the public Editor API — construction, content access,
4//! mouse and goto helpers, the (buffer-level) undo stack, and insert-mode
5//! session bookkeeping. All vim-specific keyboard handling lives in
6//! [`vim`] and communicates with Editor through a small internal API
7//! exposed via `pub(super)` fields and helper methods.
8
9use crate::input::{Input, Key};
10use crate::vim::{self, VimState};
11use crate::{KeybindingMode, VimMode};
12use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
13use ratatui::layout::Rect;
14use std::sync::atomic::{AtomicU16, Ordering};
15
16/// Where the cursor should land in the viewport after a `z`-family
17/// scroll (`zz` / `zt` / `zb`).
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub(super) enum CursorScrollTarget {
20    Center,
21    Top,
22    Bottom,
23}
24
25pub struct Editor<'a> {
26    pub keybinding_mode: KeybindingMode,
27    /// Reserved for the lifetime parameter — Editor used to wrap a
28    /// `TextArea<'a>` whose lifetime came from this slot. Phase 7f
29    /// ripped the field but the lifetime stays so downstream
30    /// `Editor<'a>` consumers don't have to churn.
31    _marker: std::marker::PhantomData<&'a ()>,
32    /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
33    pub last_yank: Option<String>,
34    /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
35    pub(super) vim: VimState,
36    /// Undo history: each entry is (lines, cursor) before the edit.
37    pub(super) undo_stack: Vec<(Vec<String>, (usize, usize))>,
38    /// Redo history: entries pushed when undoing.
39    pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
40    /// Set whenever the buffer content changes; cleared by `take_dirty`.
41    pub(super) content_dirty: bool,
42    /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
43    /// so repeated `content_arc()` calls within the same un-mutated
44    /// window are free (ref-count bump instead of a full-buffer join).
45    /// Invalidated by every [`mark_content_dirty`] call.
46    pub(super) cached_content: Option<std::sync::Arc<String>>,
47    /// Last rendered viewport height (text rows only, no chrome). Written
48    /// by the draw path via [`set_viewport_height`] so the scroll helpers
49    /// can clamp the cursor to stay visible without plumbing the height
50    /// through every call.
51    pub(super) viewport_height: AtomicU16,
52    /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
53    /// goto-definition). The host app drains this each step and fires
54    /// the matching request against its own LSP client.
55    pub(super) pending_lsp: Option<LspIntent>,
56    /// Mirror buffer for the in-flight migration off tui-textarea.
57    /// Phase 7a: content syncs on every `set_content` so the rest of
58    /// the engine can start reading from / writing to it in
59    /// follow-up commits without behaviour changing today.
60    pub(super) buffer: hjkl_buffer::Buffer,
61    /// Style intern table for the migration buffer's opaque
62    /// `Span::style` ids. Phase 7d-ii-a wiring — `apply_window_spans`
63    /// produces `(start, end, Style)` tuples for the textarea; we
64    /// translate those to `hjkl_buffer::Span` by interning the
65    /// `Style` here and storing the table index. The render path's
66    /// `StyleResolver` looks the style back up by id.
67    pub(super) style_table: Vec<ratatui::style::Style>,
68    /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
69    /// every `p` / `P` via the active selector (default unnamed).
70    pub(super) registers: crate::registers::Registers,
71    /// Per-row syntax styling, kept here so the host can do
72    /// incremental window updates (see `apply_window_spans` in
73    /// the host). Same `(start_byte, end_byte, Style)` tuple shape
74    /// the textarea used to host. The Buffer-side opaque-id spans are
75    /// derived from this on every install.
76    pub styled_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>,
77    /// Per-editor settings tweakable via `:set`. Exposed by reference
78    /// so handlers (indent, search) read the live value rather than a
79    /// snapshot taken at startup.
80    pub(super) settings: Settings,
81    /// Vim's uppercase / "file" marks. Survive `set_content` calls so
82    /// they persist across tab swaps within the same Editor — the
83    /// closest sqeel can get to vim's per-file marks without
84    /// host-side persistence. Lowercase marks stay buffer-local on
85    /// `vim.marks`.
86    pub(super) file_marks: std::collections::HashMap<char, (usize, usize)>,
87    /// Block ranges (`(start_row, end_row)` inclusive) the host has
88    /// extracted from a syntax tree. `:foldsyntax` reads these to
89    /// populate folds. The host (the host) refreshes them on every
90    /// re-parse via [`Editor::set_syntax_fold_ranges`].
91    pub(super) syntax_fold_ranges: Vec<(usize, usize)>,
92}
93
94/// Vim-style options surfaced by `:set`. New fields land here as
95/// individual ex commands gain `:set` plumbing.
96#[derive(Debug, Clone)]
97pub struct Settings {
98    /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
99    pub shiftwidth: usize,
100    /// Visual width of a `\t` character. Stored for future render
101    /// hookup; not yet consumed by the buffer renderer.
102    pub tabstop: usize,
103    /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
104    /// without an explicit `i` flag.
105    pub ignore_case: bool,
106    /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
107    pub textwidth: usize,
108    /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
109    /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
110    /// past the right edge and `top_col` clips the left side.
111    /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
112    /// to word-break wrap; `:set nowrap` resets.
113    pub wrap: hjkl_buffer::Wrap,
114}
115
116impl Default for Settings {
117    fn default() -> Self {
118        Self {
119            shiftwidth: 2,
120            tabstop: 8,
121            ignore_case: false,
122            textwidth: 79,
123            wrap: hjkl_buffer::Wrap::None,
124        }
125    }
126}
127
128/// Host-observable LSP requests triggered by editor bindings. The
129/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
130/// intent that the TUI layer picks up and routes to `sqls`.
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum LspIntent {
133    /// `gd` — textDocument/definition at the cursor.
134    GotoDefinition,
135}
136
137impl<'a> Editor<'a> {
138    pub fn new(keybinding_mode: KeybindingMode) -> Self {
139        Self {
140            _marker: std::marker::PhantomData,
141            keybinding_mode,
142            last_yank: None,
143            vim: VimState::default(),
144            undo_stack: Vec::new(),
145            redo_stack: Vec::new(),
146            content_dirty: false,
147            cached_content: None,
148            viewport_height: AtomicU16::new(0),
149            pending_lsp: None,
150            buffer: hjkl_buffer::Buffer::new(),
151            style_table: Vec::new(),
152            registers: crate::registers::Registers::default(),
153            styled_spans: Vec::new(),
154            settings: Settings::default(),
155            file_marks: std::collections::HashMap::new(),
156            syntax_fold_ranges: Vec::new(),
157        }
158    }
159
160    /// Host hook: replace the cached syntax-derived block ranges that
161    /// `:foldsyntax` consumes. the host calls this on every re-parse;
162    /// the cost is just a `Vec` swap.
163    pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
164        self.syntax_fold_ranges = ranges;
165    }
166
167    /// Live settings (read-only). `:set` mutates these via
168    /// [`Editor::settings_mut`].
169    pub fn settings(&self) -> &Settings {
170        &self.settings
171    }
172
173    pub(super) fn settings_mut(&mut self) -> &mut Settings {
174        &mut self.settings
175    }
176
177    /// Install styled syntax spans into both the host-visible cache
178    /// (`styled_spans`) and the buffer's opaque-id span table. Drops
179    /// zero-width runs and clamps `end` to the line's char length so
180    /// the buffer cache doesn't see runaway ranges. Replaces the
181    /// previous `set_syntax_spans` + `sync_buffer_spans_from_textarea`
182    /// round-trip.
183    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>) {
184        let line_byte_lens: Vec<usize> = self.buffer.lines().iter().map(|l| l.len()).collect();
185        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
186        for (row, row_spans) in spans.iter().enumerate() {
187            let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
188            let mut translated = Vec::with_capacity(row_spans.len());
189            for (start, end, style) in row_spans {
190                let end_clamped = (*end).min(line_len);
191                if end_clamped <= *start {
192                    continue;
193                }
194                let id = self.intern_style(*style);
195                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
196            }
197            by_row.push(translated);
198        }
199        self.buffer.set_spans(by_row);
200        self.styled_spans = spans;
201    }
202
203    /// Snapshot of the unnamed register (the default `p` / `P` source).
204    pub fn yank(&self) -> &str {
205        &self.registers.unnamed.text
206    }
207
208    /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
209    pub fn registers(&self) -> &crate::registers::Registers {
210        &self.registers
211    }
212
213    /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
214    /// register slot. the host calls this before letting vim consume a
215    /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
216    /// stale snapshot from the last yank.
217    pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
218        self.registers.set_clipboard(text, linewise);
219    }
220
221    /// True when the user's pending register selector is `+` or `*`.
222    /// the host peeks this so it can refresh `sync_clipboard_register`
223    /// only when a clipboard read is actually about to happen.
224    pub fn pending_register_is_clipboard(&self) -> bool {
225        matches!(self.vim.pending_register, Some('+') | Some('*'))
226    }
227
228    /// Replace the unnamed register without touching any other slot.
229    /// For host-driven imports (e.g. system clipboard); operator
230    /// code uses [`record_yank`] / [`record_delete`].
231    pub fn set_yank(&mut self, text: impl Into<String>) {
232        let text = text.into();
233        let linewise = self.vim.yank_linewise;
234        self.registers.unnamed = crate::registers::Slot { text, linewise };
235    }
236
237    /// Record a yank into `"` and `"0`, plus the named target if the
238    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
239    /// paste path.
240    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
241        self.vim.yank_linewise = linewise;
242        let target = self.vim.pending_register.take();
243        self.registers.record_yank(text, linewise, target);
244    }
245
246    /// Direct write to a named register slot — bypasses the unnamed
247    /// `"` and `"0` updates that `record_yank` does. Used by the
248    /// macro recorder so finishing a `q{reg}` recording doesn't
249    /// pollute the user's last yank.
250    pub(crate) fn set_named_register_text(&mut self, reg: char, text: String) {
251        if let Some(slot) = match reg {
252            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
253            'A'..='Z' => {
254                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
255            }
256            _ => None,
257        } {
258            slot.text = text;
259            slot.linewise = false;
260        }
261    }
262
263    /// Record a delete / change into `"` and the `"1`–`"9` ring.
264    /// Honours the active named-register prefix.
265    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
266        self.vim.yank_linewise = linewise;
267        let target = self.vim.pending_register.take();
268        self.registers.record_delete(text, linewise, target);
269    }
270
271    /// Intern a `ratatui::style::Style` and return the opaque id used
272    /// in `hjkl_buffer::Span::style`. The render-side `StyleResolver`
273    /// closure (built by [`Editor::style_resolver`]) uses the id to
274    /// look up the style back. Linear-scan dedup — the table grows
275    /// only as new tree-sitter token kinds appear, so it stays tiny.
276    pub fn intern_style(&mut self, style: ratatui::style::Style) -> u32 {
277        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
278            return idx as u32;
279        }
280        self.style_table.push(style);
281        (self.style_table.len() - 1) as u32
282    }
283
284    /// Read-only view of the style table — id `i` → `style_table[i]`.
285    /// The render path passes a closure backed by this slice as the
286    /// `StyleResolver` for `BufferView`.
287    pub fn style_table(&self) -> &[ratatui::style::Style] {
288        &self.style_table
289    }
290
291    /// Borrow the migration buffer. Host renders through this via
292    /// `hjkl_buffer::BufferView`.
293    pub fn buffer(&self) -> &hjkl_buffer::Buffer {
294        &self.buffer
295    }
296
297    pub fn buffer_mut(&mut self) -> &mut hjkl_buffer::Buffer {
298        &mut self.buffer
299    }
300
301    /// Historical reverse-sync hook from when the textarea mirrored
302    /// the buffer. Now that Buffer is the cursor authority this is a
303    /// no-op; call sites can remain in place during the migration.
304    pub(crate) fn push_buffer_cursor_to_textarea(&mut self) {}
305
306    /// Force the buffer viewport's top row without touching the
307    /// cursor. Used by tests that simulate a scroll without the
308    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
309    /// apply. Note: does not touch the textarea — the migration
310    /// buffer's viewport is what `BufferView` renders from, and the
311    /// textarea's own scroll path would clamp the cursor into its
312    /// (often-zero) visible window.
313    pub fn set_viewport_top(&mut self, row: usize) {
314        let last = self.buffer.row_count().saturating_sub(1);
315        let target = row.min(last);
316        self.buffer.viewport_mut().top_row = target;
317    }
318
319    /// Set the cursor to `(row, col)`, clamped to the buffer's
320    /// content. Replaces the scattered
321    /// `ed.textarea.move_cursor(CursorMove::Jump(r, c))` pattern that
322    /// existed before Phase 7f.
323    pub(crate) fn jump_cursor(&mut self, row: usize, col: usize) {
324        self.buffer.set_cursor(hjkl_buffer::Position::new(row, col));
325    }
326
327    /// `(row, col)` cursor read sourced from the migration buffer.
328    /// Equivalent to `self.textarea.cursor()` when the two are in
329    /// sync — which is the steady state during Phase 7f because
330    /// every step opens with `sync_buffer_content_from_textarea` and
331    /// every ported motion pushes the result back. Prefer this over
332    /// `self.textarea.cursor()` so call sites keep working unchanged
333    /// once the textarea field is ripped.
334    pub fn cursor(&self) -> (usize, usize) {
335        let pos = self.buffer.cursor();
336        (pos.row, pos.col)
337    }
338
339    /// Drain any pending LSP intent raised by the last key. Returns
340    /// `None` when no intent is armed.
341    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
342        self.pending_lsp.take()
343    }
344
345    /// Refresh the buffer's host-side state — sticky col + viewport
346    /// height. Called from the per-step boilerplate; was the textarea
347    /// → buffer mirror before Phase 7f put Buffer in charge.
348    pub(crate) fn sync_buffer_from_textarea(&mut self) {
349        self.buffer.set_sticky_col(self.vim.sticky_col);
350        let height = self.viewport_height_value();
351        self.buffer.viewport_mut().height = height;
352    }
353
354    /// Was the full textarea → buffer content sync. Buffer is the
355    /// content authority now; this remains as a no-op so the per-step
356    /// call sites don't have to be ripped in the same patch.
357    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
358        self.sync_buffer_from_textarea();
359    }
360
361    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
362    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
363    /// the cursor without going through the vim engine's motion
364    /// machinery, where push_jump fires automatically.
365    pub fn record_jump(&mut self, pos: (usize, usize)) {
366        const JUMPLIST_MAX: usize = 100;
367        self.vim.jump_back.push(pos);
368        if self.vim.jump_back.len() > JUMPLIST_MAX {
369            self.vim.jump_back.remove(0);
370        }
371        self.vim.jump_fwd.clear();
372    }
373
374    /// Host apps call this each draw with the current text area height so
375    /// scroll helpers can clamp the cursor without recomputing layout.
376    pub fn set_viewport_height(&self, height: u16) {
377        self.viewport_height.store(height, Ordering::Relaxed);
378    }
379
380    /// Last height published by `set_viewport_height` (in rows).
381    pub fn viewport_height_value(&self) -> u16 {
382        self.viewport_height.load(Ordering::Relaxed)
383    }
384
385    /// Phase 7f edit funnel: apply `edit` to the migration buffer
386    /// (the eventual edit authority), mirror the result back into
387    /// the textarea so the still-textarea-driven paths (insert mode,
388    /// yank pipe) keep observing the same content. Returns the
389    /// inverse for the host's undo stack.
390    pub(super) fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
391        let pre_row = self.buffer.cursor().row;
392        let pre_rows = self.buffer.row_count();
393        let inverse = self.buffer.apply_edit(edit);
394        let pos = self.buffer.cursor();
395        // Drop any folds the edit's range overlapped — vim opens the
396        // surrounding fold automatically when you edit inside it. The
397        // approximation here invalidates folds covering either the
398        // pre-edit cursor row or the post-edit cursor row, which
399        // catches the common single-line / multi-line edit shapes.
400        let lo = pre_row.min(pos.row);
401        let hi = pre_row.max(pos.row);
402        self.buffer.invalidate_folds_in_range(lo, hi);
403        self.vim.last_edit_pos = Some((pos.row, pos.col));
404        // Append to the change-list ring (skip when the cursor sits on
405        // the same cell as the last entry — back-to-back keystrokes on
406        // one column shouldn't pollute the ring). A new edit while
407        // walking the ring trims the forward half, vim style.
408        let entry = (pos.row, pos.col);
409        if self.vim.change_list.last() != Some(&entry) {
410            if let Some(idx) = self.vim.change_list_cursor.take() {
411                self.vim.change_list.truncate(idx + 1);
412            }
413            self.vim.change_list.push(entry);
414            let len = self.vim.change_list.len();
415            if len > crate::vim::CHANGE_LIST_MAX {
416                self.vim
417                    .change_list
418                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
419            }
420        }
421        self.vim.change_list_cursor = None;
422        // Shift / drop marks + jump-list entries to track the row
423        // delta the edit produced. Without this, every line-changing
424        // edit silently invalidates `'a`-style positions.
425        let post_rows = self.buffer.row_count();
426        let delta = post_rows as isize - pre_rows as isize;
427        if delta != 0 {
428            self.shift_marks_after_edit(pre_row, delta);
429        }
430        self.push_buffer_content_to_textarea();
431        self.mark_content_dirty();
432        inverse
433    }
434
435    /// Migrate user marks + jumplist entries when an edit at row
436    /// `edit_start` changes the buffer's row count by `delta` (positive
437    /// for inserts, negative for deletes). Marks tied to a deleted row
438    /// are dropped; marks past the affected band shift by `delta`.
439    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
440        if delta == 0 {
441            return;
442        }
443        // Deleted-row band (only meaningful for delta < 0). Inclusive
444        // start, exclusive end.
445        let drop_end = if delta < 0 {
446            edit_start.saturating_add((-delta) as usize)
447        } else {
448            edit_start
449        };
450        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
451
452        let mut to_drop: Vec<char> = Vec::new();
453        for (c, (row, _col)) in self.vim.marks.iter_mut() {
454            if (edit_start..drop_end).contains(row) {
455                to_drop.push(*c);
456            } else if *row >= shift_threshold {
457                *row = ((*row as isize) + delta).max(0) as usize;
458            }
459        }
460        for c in to_drop {
461            self.vim.marks.remove(&c);
462        }
463
464        // File marks migrate the same way — only the storage differs.
465        let mut to_drop: Vec<char> = Vec::new();
466        for (c, (row, _col)) in self.file_marks.iter_mut() {
467            if (edit_start..drop_end).contains(row) {
468                to_drop.push(*c);
469            } else if *row >= shift_threshold {
470                *row = ((*row as isize) + delta).max(0) as usize;
471            }
472        }
473        for c in to_drop {
474            self.file_marks.remove(&c);
475        }
476
477        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
478            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
479            for (row, _) in entries.iter_mut() {
480                if *row >= shift_threshold {
481                    *row = ((*row as isize) + delta).max(0) as usize;
482                }
483            }
484        };
485        shift_jumps(&mut self.vim.jump_back);
486        shift_jumps(&mut self.vim.jump_fwd);
487    }
488
489    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
490    /// the textarea from the buffer's lines + cursor, preserving yank
491    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
492    /// textarea field disappears at the end of Phase 7f anyway.
493    /// No-op since Buffer is the content authority. Retained as a
494    /// shim so call sites in `mutate_edit` and friends don't have to
495    /// be ripped in lockstep with the field removal.
496    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
497
498    /// Single choke-point for "the buffer just changed". Sets the
499    /// dirty flag and drops the cached `content_arc` snapshot so
500    /// subsequent reads rebuild from the live textarea. Callers
501    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
502    /// path) must invoke this to keep the cache honest.
503    pub fn mark_content_dirty(&mut self) {
504        self.content_dirty = true;
505        self.cached_content = None;
506    }
507
508    /// Returns true if content changed since the last call, then clears the flag.
509    pub fn take_dirty(&mut self) -> bool {
510        let dirty = self.content_dirty;
511        self.content_dirty = false;
512        dirty
513    }
514
515    /// Returns the cursor's row within the visible textarea (0-based), updating
516    /// the stored viewport top so subsequent calls remain accurate.
517    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
518        let cursor = self.buffer.cursor().row;
519        let top = self.buffer.viewport().top_row;
520        cursor.saturating_sub(top).min(height as usize - 1) as u16
521    }
522
523    /// Returns the cursor's screen position `(x, y)` for `area` (the textarea
524    /// rect). Accounts for line-number gutter and viewport scroll. Returns
525    /// `None` if the cursor is outside the visible viewport.
526    pub fn cursor_screen_pos(&self, area: Rect) -> Option<(u16, u16)> {
527        let pos = self.buffer.cursor();
528        let v = self.buffer.viewport();
529        if pos.row < v.top_row || pos.col < v.top_col {
530            return None;
531        }
532        let lnum_width = self.buffer.row_count().to_string().len() as u16 + 2;
533        let dy = (pos.row - v.top_row) as u16;
534        let dx = (pos.col - v.top_col) as u16;
535        if dy >= area.height || dx + lnum_width >= area.width {
536            return None;
537        }
538        Some((area.x + lnum_width + dx, area.y + dy))
539    }
540
541    pub fn vim_mode(&self) -> VimMode {
542        self.vim.public_mode()
543    }
544
545    /// Bounds of the active visual-block rectangle as
546    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
547    /// `None` when we're not in VisualBlock mode.
548    /// Read-only view of the live `/` or `?` prompt. `None` outside
549    /// search-prompt mode.
550    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
551        self.vim.search_prompt.as_ref()
552    }
553
554    /// Most recent committed search pattern (persists across `n` / `N`
555    /// and across prompt exits). `None` before the first search.
556    pub fn last_search(&self) -> Option<&str> {
557        self.vim.last_search.as_deref()
558    }
559
560    /// Start/end `(row, col)` of the active char-wise Visual selection
561    /// (inclusive on both ends, positionally ordered). `None` when not
562    /// in Visual mode.
563    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
564        if self.vim_mode() != VimMode::Visual {
565            return None;
566        }
567        let anchor = self.vim.visual_anchor;
568        let cursor = self.cursor();
569        let (start, end) = if anchor <= cursor {
570            (anchor, cursor)
571        } else {
572            (cursor, anchor)
573        };
574        Some((start, end))
575    }
576
577    /// Top/bottom rows of the active VisualLine selection (inclusive).
578    /// `None` when we're not in VisualLine mode.
579    pub fn line_highlight(&self) -> Option<(usize, usize)> {
580        if self.vim_mode() != VimMode::VisualLine {
581            return None;
582        }
583        let anchor = self.vim.visual_line_anchor;
584        let cursor = self.buffer.cursor().row;
585        Some((anchor.min(cursor), anchor.max(cursor)))
586    }
587
588    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
589        if self.vim_mode() != VimMode::VisualBlock {
590            return None;
591        }
592        let (ar, ac) = self.vim.block_anchor;
593        let cr = self.buffer.cursor().row;
594        let cc = self.vim.block_vcol;
595        let top = ar.min(cr);
596        let bot = ar.max(cr);
597        let left = ac.min(cc);
598        let right = ac.max(cc);
599        Some((top, bot, left, right))
600    }
601
602    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
603    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
604    /// straight to `BufferView` once render flips off textarea
605    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
606    /// switch).
607    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
608        use hjkl_buffer::{Position, Selection};
609        match self.vim_mode() {
610            VimMode::Visual => {
611                let (ar, ac) = self.vim.visual_anchor;
612                let head = self.buffer.cursor();
613                Some(Selection::Char {
614                    anchor: Position::new(ar, ac),
615                    head,
616                })
617            }
618            VimMode::VisualLine => {
619                let anchor_row = self.vim.visual_line_anchor;
620                let head_row = self.buffer.cursor().row;
621                Some(Selection::Line {
622                    anchor_row,
623                    head_row,
624                })
625            }
626            VimMode::VisualBlock => {
627                let (ar, ac) = self.vim.block_anchor;
628                let cr = self.buffer.cursor().row;
629                let cc = self.vim.block_vcol;
630                Some(Selection::Block {
631                    anchor: Position::new(ar, ac),
632                    head: Position::new(cr, cc),
633                })
634            }
635            _ => None,
636        }
637    }
638
639    /// Force back to normal mode (used when dismissing completions etc.)
640    pub fn force_normal(&mut self) {
641        self.vim.force_normal();
642    }
643
644    pub fn content(&self) -> String {
645        let mut s = self.buffer.lines().join("\n");
646        s.push('\n');
647        s
648    }
649
650    /// Same logical output as [`content`], but returns a cached
651    /// `Arc<String>` so back-to-back reads within an un-mutated window
652    /// are ref-count bumps instead of multi-MB joins. The cache is
653    /// invalidated by every [`mark_content_dirty`] call.
654    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
655        if let Some(arc) = &self.cached_content {
656            return std::sync::Arc::clone(arc);
657        }
658        let arc = std::sync::Arc::new(self.content());
659        self.cached_content = Some(std::sync::Arc::clone(&arc));
660        arc
661    }
662
663    pub fn set_content(&mut self, text: &str) {
664        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
665        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
666            lines.pop();
667        }
668        if lines.is_empty() {
669            lines.push(String::new());
670        }
671        let _ = lines;
672        self.buffer = hjkl_buffer::Buffer::from_str(text);
673        self.undo_stack.clear();
674        self.redo_stack.clear();
675        self.mark_content_dirty();
676    }
677
678    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
679    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
680    /// shape their payload.
681    pub fn seed_yank(&mut self, text: String) {
682        let linewise = text.ends_with('\n');
683        self.vim.yank_linewise = linewise;
684        self.registers.unnamed = crate::registers::Slot { text, linewise };
685    }
686
687    /// Scroll the viewport down by `rows`. The cursor stays on its
688    /// absolute line (vim convention) unless the scroll would take it
689    /// off-screen — in that case it's clamped to the first row still
690    /// visible.
691    pub fn scroll_down(&mut self, rows: i16) {
692        self.scroll_viewport(rows);
693    }
694
695    /// Scroll the viewport up by `rows`. Cursor stays unless it would
696    /// fall off the bottom of the new viewport, then clamp to the
697    /// bottom-most visible row.
698    pub fn scroll_up(&mut self, rows: i16) {
699        self.scroll_viewport(-rows);
700    }
701
702    /// Vim's `scrolloff` default — keep the cursor at least this many
703    /// rows away from the top / bottom edge of the viewport while
704    /// scrolling. Collapses to `height / 2` for tiny viewports.
705    const SCROLLOFF: usize = 5;
706
707    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
708    /// rows from each edge. Replaces the bare
709    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
710    /// don't park the cursor on the very last visible row.
711    pub(crate) fn ensure_cursor_in_scrolloff(&mut self) {
712        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
713        if height == 0 {
714            self.buffer.ensure_cursor_visible();
715            return;
716        }
717        // Cap margin at (height - 1) / 2 so the upper + lower bands
718        // can't overlap on tiny windows (margin=5 + height=10 would
719        // otherwise produce contradictory clamp ranges).
720        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
721        // Soft-wrap path: scrolloff math runs in *screen rows*, not
722        // doc rows, since a wrapped doc row spans many visual lines.
723        if !matches!(self.buffer.viewport().wrap, hjkl_buffer::Wrap::None) {
724            self.ensure_scrolloff_wrap(height, margin);
725            return;
726        }
727        let cursor_row = self.buffer.cursor().row;
728        let last_row = self.buffer.row_count().saturating_sub(1);
729        let v = self.buffer.viewport_mut();
730        // Top edge: cursor_row should sit at >= top_row + margin.
731        if cursor_row < v.top_row + margin {
732            v.top_row = cursor_row.saturating_sub(margin);
733        }
734        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
735        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
736        if cursor_row > v.top_row + max_bottom {
737            v.top_row = cursor_row.saturating_sub(max_bottom);
738        }
739        // Clamp top_row so we never scroll past the buffer's bottom.
740        let max_top = last_row.saturating_sub(height.saturating_sub(1));
741        if v.top_row > max_top {
742            v.top_row = max_top;
743        }
744        // Defer to Buffer for column-side scroll (no scrolloff for
745        // horizontal scrolling — vim default `sidescrolloff = 0`).
746        let cursor = self.buffer.cursor();
747        self.buffer.viewport_mut().ensure_visible(cursor);
748    }
749
750    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
751    /// at a time so the cursor's *screen* row stays inside
752    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
753    /// buffer's bottom never leaves blank rows below it.
754    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
755        let cursor_row = self.buffer.cursor().row;
756        // Step 1 — cursor above viewport: snap top to cursor row,
757        // then we'll fix up the margin below.
758        if cursor_row < self.buffer.viewport().top_row {
759            self.buffer.viewport_mut().top_row = cursor_row;
760            self.buffer.viewport_mut().top_col = 0;
761        }
762        // Step 2 — push top forward until cursor's screen row is
763        // within the bottom margin (`csr <= height - 1 - margin`).
764        let max_csr = height.saturating_sub(1).saturating_sub(margin);
765        loop {
766            let csr = self.buffer.cursor_screen_row().unwrap_or(0);
767            if csr <= max_csr {
768                break;
769            }
770            let top = self.buffer.viewport().top_row;
771            let Some(next) = self.buffer.next_visible_row(top) else {
772                break;
773            };
774            // Don't walk past the cursor's row.
775            if next > cursor_row {
776                self.buffer.viewport_mut().top_row = cursor_row;
777                break;
778            }
779            self.buffer.viewport_mut().top_row = next;
780        }
781        // Step 3 — pull top backward until cursor's screen row is
782        // past the top margin (`csr >= margin`).
783        loop {
784            let csr = self.buffer.cursor_screen_row().unwrap_or(0);
785            if csr >= margin {
786                break;
787            }
788            let top = self.buffer.viewport().top_row;
789            let Some(prev) = self.buffer.prev_visible_row(top) else {
790                break;
791            };
792            self.buffer.viewport_mut().top_row = prev;
793        }
794        // Step 4 — clamp top so the buffer's bottom doesn't leave
795        // blank rows below it. `max_top_for_height` walks segments
796        // backward from the last row until it accumulates `height`
797        // screen rows.
798        let max_top = self.buffer.max_top_for_height(height);
799        if self.buffer.viewport().top_row > max_top {
800            self.buffer.viewport_mut().top_row = max_top;
801        }
802        self.buffer.viewport_mut().top_col = 0;
803    }
804
805    fn scroll_viewport(&mut self, delta: i16) {
806        if delta == 0 {
807            return;
808        }
809        // Bump the buffer's viewport top within bounds.
810        let total_rows = self.buffer.row_count() as isize;
811        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
812        let cur_top = self.buffer.viewport().top_row as isize;
813        let new_top = (cur_top + delta as isize)
814            .max(0)
815            .min((total_rows - 1).max(0)) as usize;
816        self.buffer.viewport_mut().top_row = new_top;
817        // Mirror to textarea so its viewport reads (still consumed by
818        // a couple of helpers) stay accurate.
819        let _ = cur_top;
820        if height == 0 {
821            return;
822        }
823        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
824        // from the visible viewport edges.
825        let cursor = self.buffer.cursor();
826        let margin = Self::SCROLLOFF.min(height / 2);
827        let min_row = new_top + margin;
828        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
829        let target_row = cursor.row.clamp(min_row, max_row.max(min_row));
830        if target_row != cursor.row {
831            let line_len = self
832                .buffer
833                .line(target_row)
834                .map(|l| l.chars().count())
835                .unwrap_or(0);
836            let target_col = cursor.col.min(line_len.saturating_sub(1));
837            self.buffer
838                .set_cursor(hjkl_buffer::Position::new(target_row, target_col));
839        }
840    }
841
842    pub fn goto_line(&mut self, line: usize) {
843        let row = line.saturating_sub(1);
844        let max = self.buffer.row_count().saturating_sub(1);
845        let target = row.min(max);
846        self.buffer
847            .set_cursor(hjkl_buffer::Position::new(target, 0));
848    }
849
850    /// Scroll so the cursor row lands at the given viewport position:
851    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
852    /// Cursor stays on its absolute line; only the viewport moves.
853    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
854        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
855        if height == 0 {
856            return;
857        }
858        let cur_row = self.buffer.cursor().row;
859        let cur_top = self.buffer.viewport().top_row;
860        // Scrolloff awareness: `zt` lands the cursor at the top edge
861        // of the viable area (top + margin), `zb` at the bottom edge
862        // (top + height - 1 - margin). Match the cap used by
863        // `ensure_cursor_in_scrolloff` so contradictory bounds are
864        // impossible on tiny viewports.
865        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
866        let new_top = match pos {
867            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
868            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
869            CursorScrollTarget::Bottom => {
870                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
871            }
872        };
873        if new_top == cur_top {
874            return;
875        }
876        self.buffer.viewport_mut().top_row = new_top;
877    }
878
879    /// Translate a terminal mouse position into a (row, col) inside the document.
880    /// `area` is the outer editor rect: 1-row tab bar at top (flush), then the
881    /// textarea with 1 cell of horizontal pane padding on each side. Clicks
882    /// past the line's last character clamp to the last char (Normal-mode
883    /// invariant) — never past it. Char-counted, not byte-counted, so
884    /// multibyte runs land where the user expects.
885    fn mouse_to_doc_pos(&self, area: Rect, col: u16, row: u16) -> (usize, usize) {
886        let lines = self.buffer.lines();
887        let inner_top = area.y.saturating_add(1); // tab bar row
888        let lnum_width = lines.len().to_string().len() as u16 + 2;
889        let content_x = area.x.saturating_add(1).saturating_add(lnum_width);
890        let rel_row = row.saturating_sub(inner_top) as usize;
891        let top = self.buffer.viewport().top_row;
892        let doc_row = (top + rel_row).min(lines.len().saturating_sub(1));
893        let rel_col = col.saturating_sub(content_x) as usize;
894        let line_chars = lines.get(doc_row).map(|l| l.chars().count()).unwrap_or(0);
895        let last_col = line_chars.saturating_sub(1);
896        (doc_row, rel_col.min(last_col))
897    }
898
899    /// Jump the cursor to the given 1-based line/column, clamped to the document.
900    pub fn jump_to(&mut self, line: usize, col: usize) {
901        let r = line.saturating_sub(1);
902        let max_row = self.buffer.row_count().saturating_sub(1);
903        let r = r.min(max_row);
904        let line_len = self.buffer.line(r).map(|l| l.chars().count()).unwrap_or(0);
905        let c = col.saturating_sub(1).min(line_len);
906        self.buffer.set_cursor(hjkl_buffer::Position::new(r, c));
907    }
908
909    /// Jump cursor to the terminal-space mouse position; exits Visual modes if active.
910    pub fn mouse_click(&mut self, area: Rect, col: u16, row: u16) {
911        if self.vim.is_visual() {
912            self.vim.force_normal();
913        }
914        let (r, c) = self.mouse_to_doc_pos(area, col, row);
915        self.buffer.set_cursor(hjkl_buffer::Position::new(r, c));
916    }
917
918    /// Begin a mouse-drag selection: anchor at current cursor and enter Visual mode.
919    pub fn mouse_begin_drag(&mut self) {
920        if !self.vim.is_visual_char() {
921            let cursor = self.cursor();
922            self.vim.enter_visual(cursor);
923        }
924    }
925
926    /// Extend an in-progress mouse drag to the given terminal-space position.
927    pub fn mouse_extend_drag(&mut self, area: Rect, col: u16, row: u16) {
928        let (r, c) = self.mouse_to_doc_pos(area, col, row);
929        self.buffer.set_cursor(hjkl_buffer::Position::new(r, c));
930    }
931
932    pub fn insert_str(&mut self, text: &str) {
933        let pos = self.buffer.cursor();
934        self.buffer.apply_edit(hjkl_buffer::Edit::InsertStr {
935            at: pos,
936            text: text.to_string(),
937        });
938        self.push_buffer_content_to_textarea();
939        self.mark_content_dirty();
940    }
941
942    pub fn accept_completion(&mut self, completion: &str) {
943        use hjkl_buffer::{Edit, MotionKind, Position};
944        let cursor = self.buffer.cursor();
945        let line = self.buffer.line(cursor.row).unwrap_or("").to_string();
946        let chars: Vec<char> = line.chars().collect();
947        let prefix_len = chars[..cursor.col.min(chars.len())]
948            .iter()
949            .rev()
950            .take_while(|c| c.is_alphanumeric() || **c == '_')
951            .count();
952        if prefix_len > 0 {
953            let start = Position::new(cursor.row, cursor.col - prefix_len);
954            self.buffer.apply_edit(Edit::DeleteRange {
955                start,
956                end: cursor,
957                kind: MotionKind::Char,
958            });
959        }
960        let cursor = self.buffer.cursor();
961        self.buffer.apply_edit(Edit::InsertStr {
962            at: cursor,
963            text: completion.to_string(),
964        });
965        self.push_buffer_content_to_textarea();
966        self.mark_content_dirty();
967    }
968
969    pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
970        let pos = self.buffer.cursor();
971        (self.buffer.lines().to_vec(), (pos.row, pos.col))
972    }
973
974    pub(super) fn push_undo(&mut self) {
975        let snap = self.snapshot();
976        if self.undo_stack.len() >= 200 {
977            self.undo_stack.remove(0);
978        }
979        self.undo_stack.push(snap);
980        self.redo_stack.clear();
981    }
982
983    pub(super) fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
984        let text = lines.join("\n");
985        self.buffer.replace_all(&text);
986        self.buffer
987            .set_cursor(hjkl_buffer::Position::new(cursor.0, cursor.1));
988        self.mark_content_dirty();
989    }
990
991    /// Returns true if the key was consumed by the editor.
992    pub fn handle_key(&mut self, key: KeyEvent) -> bool {
993        let input = crossterm_to_input(key);
994        if input.key == Key::Null {
995            return false;
996        }
997        vim::step(self, input)
998    }
999}
1000
1001pub(super) fn crossterm_to_input(key: KeyEvent) -> Input {
1002    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
1003    let alt = key.modifiers.contains(KeyModifiers::ALT);
1004    let shift = key.modifiers.contains(KeyModifiers::SHIFT);
1005    let k = match key.code {
1006        KeyCode::Char(c) => Key::Char(c),
1007        KeyCode::Backspace => Key::Backspace,
1008        KeyCode::Delete => Key::Delete,
1009        KeyCode::Enter => Key::Enter,
1010        KeyCode::Left => Key::Left,
1011        KeyCode::Right => Key::Right,
1012        KeyCode::Up => Key::Up,
1013        KeyCode::Down => Key::Down,
1014        KeyCode::Home => Key::Home,
1015        KeyCode::End => Key::End,
1016        KeyCode::Tab => Key::Tab,
1017        KeyCode::Esc => Key::Esc,
1018        _ => Key::Null,
1019    };
1020    Input {
1021        key: k,
1022        ctrl,
1023        alt,
1024        shift,
1025    }
1026}
1027
1028#[cfg(test)]
1029mod tests {
1030    use super::*;
1031    use crossterm::event::KeyEvent;
1032
1033    fn key(code: KeyCode) -> KeyEvent {
1034        KeyEvent::new(code, KeyModifiers::NONE)
1035    }
1036    fn shift_key(code: KeyCode) -> KeyEvent {
1037        KeyEvent::new(code, KeyModifiers::SHIFT)
1038    }
1039    fn ctrl_key(code: KeyCode) -> KeyEvent {
1040        KeyEvent::new(code, KeyModifiers::CONTROL)
1041    }
1042
1043    #[test]
1044    fn vim_normal_to_insert() {
1045        let mut e = Editor::new(KeybindingMode::Vim);
1046        e.handle_key(key(KeyCode::Char('i')));
1047        assert_eq!(e.vim_mode(), VimMode::Insert);
1048    }
1049
1050    #[test]
1051    fn vim_insert_to_normal() {
1052        let mut e = Editor::new(KeybindingMode::Vim);
1053        e.handle_key(key(KeyCode::Char('i')));
1054        e.handle_key(key(KeyCode::Esc));
1055        assert_eq!(e.vim_mode(), VimMode::Normal);
1056    }
1057
1058    #[test]
1059    fn vim_normal_to_visual() {
1060        let mut e = Editor::new(KeybindingMode::Vim);
1061        e.handle_key(key(KeyCode::Char('v')));
1062        assert_eq!(e.vim_mode(), VimMode::Visual);
1063    }
1064
1065    #[test]
1066    fn vim_visual_to_normal() {
1067        let mut e = Editor::new(KeybindingMode::Vim);
1068        e.handle_key(key(KeyCode::Char('v')));
1069        e.handle_key(key(KeyCode::Esc));
1070        assert_eq!(e.vim_mode(), VimMode::Normal);
1071    }
1072
1073    #[test]
1074    fn vim_shift_i_moves_to_first_non_whitespace() {
1075        let mut e = Editor::new(KeybindingMode::Vim);
1076        e.set_content("   hello");
1077        e.jump_cursor(0, 8);
1078        e.handle_key(shift_key(KeyCode::Char('I')));
1079        assert_eq!(e.vim_mode(), VimMode::Insert);
1080        assert_eq!(e.cursor(), (0, 3));
1081    }
1082
1083    #[test]
1084    fn vim_shift_a_moves_to_end_and_insert() {
1085        let mut e = Editor::new(KeybindingMode::Vim);
1086        e.set_content("hello");
1087        e.handle_key(shift_key(KeyCode::Char('A')));
1088        assert_eq!(e.vim_mode(), VimMode::Insert);
1089        assert_eq!(e.cursor().1, 5);
1090    }
1091
1092    #[test]
1093    fn count_10j_moves_down_10() {
1094        let mut e = Editor::new(KeybindingMode::Vim);
1095        e.set_content(
1096            (0..20)
1097                .map(|i| format!("line{i}"))
1098                .collect::<Vec<_>>()
1099                .join("\n")
1100                .as_str(),
1101        );
1102        for d in "10".chars() {
1103            e.handle_key(key(KeyCode::Char(d)));
1104        }
1105        e.handle_key(key(KeyCode::Char('j')));
1106        assert_eq!(e.cursor().0, 10);
1107    }
1108
1109    #[test]
1110    fn count_o_repeats_insert_on_esc() {
1111        let mut e = Editor::new(KeybindingMode::Vim);
1112        e.set_content("hello");
1113        for d in "3".chars() {
1114            e.handle_key(key(KeyCode::Char(d)));
1115        }
1116        e.handle_key(key(KeyCode::Char('o')));
1117        assert_eq!(e.vim_mode(), VimMode::Insert);
1118        for c in "world".chars() {
1119            e.handle_key(key(KeyCode::Char(c)));
1120        }
1121        e.handle_key(key(KeyCode::Esc));
1122        assert_eq!(e.vim_mode(), VimMode::Normal);
1123        assert_eq!(e.buffer().lines().len(), 4);
1124        assert!(e.buffer().lines().iter().skip(1).all(|l| l == "world"));
1125    }
1126
1127    #[test]
1128    fn count_i_repeats_text_on_esc() {
1129        let mut e = Editor::new(KeybindingMode::Vim);
1130        e.set_content("");
1131        for d in "3".chars() {
1132            e.handle_key(key(KeyCode::Char(d)));
1133        }
1134        e.handle_key(key(KeyCode::Char('i')));
1135        for c in "ab".chars() {
1136            e.handle_key(key(KeyCode::Char(c)));
1137        }
1138        e.handle_key(key(KeyCode::Esc));
1139        assert_eq!(e.vim_mode(), VimMode::Normal);
1140        assert_eq!(e.buffer().lines()[0], "ababab");
1141    }
1142
1143    #[test]
1144    fn vim_shift_o_opens_line_above() {
1145        let mut e = Editor::new(KeybindingMode::Vim);
1146        e.set_content("hello");
1147        e.handle_key(shift_key(KeyCode::Char('O')));
1148        assert_eq!(e.vim_mode(), VimMode::Insert);
1149        assert_eq!(e.cursor(), (0, 0));
1150        assert_eq!(e.buffer().lines().len(), 2);
1151    }
1152
1153    #[test]
1154    fn vim_gg_goes_to_top() {
1155        let mut e = Editor::new(KeybindingMode::Vim);
1156        e.set_content("a\nb\nc");
1157        e.jump_cursor(2, 0);
1158        e.handle_key(key(KeyCode::Char('g')));
1159        e.handle_key(key(KeyCode::Char('g')));
1160        assert_eq!(e.cursor().0, 0);
1161    }
1162
1163    #[test]
1164    fn vim_shift_g_goes_to_bottom() {
1165        let mut e = Editor::new(KeybindingMode::Vim);
1166        e.set_content("a\nb\nc");
1167        e.handle_key(shift_key(KeyCode::Char('G')));
1168        assert_eq!(e.cursor().0, 2);
1169    }
1170
1171    #[test]
1172    fn vim_dd_deletes_line() {
1173        let mut e = Editor::new(KeybindingMode::Vim);
1174        e.set_content("first\nsecond");
1175        e.handle_key(key(KeyCode::Char('d')));
1176        e.handle_key(key(KeyCode::Char('d')));
1177        assert_eq!(e.buffer().lines().len(), 1);
1178        assert_eq!(e.buffer().lines()[0], "second");
1179    }
1180
1181    #[test]
1182    fn vim_dw_deletes_word() {
1183        let mut e = Editor::new(KeybindingMode::Vim);
1184        e.set_content("hello world");
1185        e.handle_key(key(KeyCode::Char('d')));
1186        e.handle_key(key(KeyCode::Char('w')));
1187        assert_eq!(e.vim_mode(), VimMode::Normal);
1188        assert!(!e.buffer().lines()[0].starts_with("hello"));
1189    }
1190
1191    #[test]
1192    fn vim_yy_yanks_line() {
1193        let mut e = Editor::new(KeybindingMode::Vim);
1194        e.set_content("hello\nworld");
1195        e.handle_key(key(KeyCode::Char('y')));
1196        e.handle_key(key(KeyCode::Char('y')));
1197        assert!(e.last_yank.as_deref().unwrap_or("").starts_with("hello"));
1198    }
1199
1200    #[test]
1201    fn vim_yy_does_not_move_cursor() {
1202        let mut e = Editor::new(KeybindingMode::Vim);
1203        e.set_content("first\nsecond\nthird");
1204        e.jump_cursor(1, 0);
1205        let before = e.cursor();
1206        e.handle_key(key(KeyCode::Char('y')));
1207        e.handle_key(key(KeyCode::Char('y')));
1208        assert_eq!(e.cursor(), before);
1209        assert_eq!(e.vim_mode(), VimMode::Normal);
1210    }
1211
1212    #[test]
1213    fn vim_yw_yanks_word() {
1214        let mut e = Editor::new(KeybindingMode::Vim);
1215        e.set_content("hello world");
1216        e.handle_key(key(KeyCode::Char('y')));
1217        e.handle_key(key(KeyCode::Char('w')));
1218        assert_eq!(e.vim_mode(), VimMode::Normal);
1219        assert!(e.last_yank.is_some());
1220    }
1221
1222    #[test]
1223    fn vim_cc_changes_line() {
1224        let mut e = Editor::new(KeybindingMode::Vim);
1225        e.set_content("hello\nworld");
1226        e.handle_key(key(KeyCode::Char('c')));
1227        e.handle_key(key(KeyCode::Char('c')));
1228        assert_eq!(e.vim_mode(), VimMode::Insert);
1229    }
1230
1231    #[test]
1232    fn vim_u_undoes_insert_session_as_chunk() {
1233        let mut e = Editor::new(KeybindingMode::Vim);
1234        e.set_content("hello");
1235        e.handle_key(key(KeyCode::Char('i')));
1236        e.handle_key(key(KeyCode::Enter));
1237        e.handle_key(key(KeyCode::Enter));
1238        e.handle_key(key(KeyCode::Esc));
1239        assert_eq!(e.buffer().lines().len(), 3);
1240        e.handle_key(key(KeyCode::Char('u')));
1241        assert_eq!(e.buffer().lines().len(), 1);
1242        assert_eq!(e.buffer().lines()[0], "hello");
1243    }
1244
1245    #[test]
1246    fn vim_undo_redo_roundtrip() {
1247        let mut e = Editor::new(KeybindingMode::Vim);
1248        e.set_content("hello");
1249        e.handle_key(key(KeyCode::Char('i')));
1250        for c in "world".chars() {
1251            e.handle_key(key(KeyCode::Char(c)));
1252        }
1253        e.handle_key(key(KeyCode::Esc));
1254        let after = e.buffer().lines()[0].clone();
1255        e.handle_key(key(KeyCode::Char('u')));
1256        assert_eq!(e.buffer().lines()[0], "hello");
1257        e.handle_key(ctrl_key(KeyCode::Char('r')));
1258        assert_eq!(e.buffer().lines()[0], after);
1259    }
1260
1261    #[test]
1262    fn vim_u_undoes_dd() {
1263        let mut e = Editor::new(KeybindingMode::Vim);
1264        e.set_content("first\nsecond");
1265        e.handle_key(key(KeyCode::Char('d')));
1266        e.handle_key(key(KeyCode::Char('d')));
1267        assert_eq!(e.buffer().lines().len(), 1);
1268        e.handle_key(key(KeyCode::Char('u')));
1269        assert_eq!(e.buffer().lines().len(), 2);
1270        assert_eq!(e.buffer().lines()[0], "first");
1271    }
1272
1273    #[test]
1274    fn vim_ctrl_r_redoes() {
1275        let mut e = Editor::new(KeybindingMode::Vim);
1276        e.set_content("hello");
1277        e.handle_key(ctrl_key(KeyCode::Char('r')));
1278    }
1279
1280    #[test]
1281    fn vim_r_replaces_char() {
1282        let mut e = Editor::new(KeybindingMode::Vim);
1283        e.set_content("hello");
1284        e.handle_key(key(KeyCode::Char('r')));
1285        e.handle_key(key(KeyCode::Char('x')));
1286        assert_eq!(e.buffer().lines()[0].chars().next(), Some('x'));
1287    }
1288
1289    #[test]
1290    fn vim_tilde_toggles_case() {
1291        let mut e = Editor::new(KeybindingMode::Vim);
1292        e.set_content("hello");
1293        e.handle_key(key(KeyCode::Char('~')));
1294        assert_eq!(e.buffer().lines()[0].chars().next(), Some('H'));
1295    }
1296
1297    #[test]
1298    fn vim_visual_d_cuts() {
1299        let mut e = Editor::new(KeybindingMode::Vim);
1300        e.set_content("hello");
1301        e.handle_key(key(KeyCode::Char('v')));
1302        e.handle_key(key(KeyCode::Char('l')));
1303        e.handle_key(key(KeyCode::Char('l')));
1304        e.handle_key(key(KeyCode::Char('d')));
1305        assert_eq!(e.vim_mode(), VimMode::Normal);
1306        assert!(e.last_yank.is_some());
1307    }
1308
1309    #[test]
1310    fn vim_visual_c_enters_insert() {
1311        let mut e = Editor::new(KeybindingMode::Vim);
1312        e.set_content("hello");
1313        e.handle_key(key(KeyCode::Char('v')));
1314        e.handle_key(key(KeyCode::Char('l')));
1315        e.handle_key(key(KeyCode::Char('c')));
1316        assert_eq!(e.vim_mode(), VimMode::Insert);
1317    }
1318
1319    #[test]
1320    fn vim_normal_unknown_key_consumed() {
1321        let mut e = Editor::new(KeybindingMode::Vim);
1322        // Unknown keys are consumed (swallowed) rather than returning false.
1323        let consumed = e.handle_key(key(KeyCode::Char('z')));
1324        assert!(consumed);
1325    }
1326
1327    #[test]
1328    fn force_normal_clears_operator() {
1329        let mut e = Editor::new(KeybindingMode::Vim);
1330        e.handle_key(key(KeyCode::Char('d')));
1331        e.force_normal();
1332        assert_eq!(e.vim_mode(), VimMode::Normal);
1333    }
1334
1335    fn many_lines(n: usize) -> String {
1336        (0..n)
1337            .map(|i| format!("line{i}"))
1338            .collect::<Vec<_>>()
1339            .join("\n")
1340    }
1341
1342    fn prime_viewport(e: &mut Editor<'_>, height: u16) {
1343        e.set_viewport_height(height);
1344    }
1345
1346    #[test]
1347    fn zz_centers_cursor_in_viewport() {
1348        let mut e = Editor::new(KeybindingMode::Vim);
1349        e.set_content(&many_lines(100));
1350        prime_viewport(&mut e, 20);
1351        e.jump_cursor(50, 0);
1352        e.handle_key(key(KeyCode::Char('z')));
1353        e.handle_key(key(KeyCode::Char('z')));
1354        assert_eq!(e.buffer().viewport().top_row, 40);
1355        assert_eq!(e.cursor().0, 50);
1356    }
1357
1358    #[test]
1359    fn zt_puts_cursor_at_viewport_top_with_scrolloff() {
1360        let mut e = Editor::new(KeybindingMode::Vim);
1361        e.set_content(&many_lines(100));
1362        prime_viewport(&mut e, 20);
1363        e.jump_cursor(50, 0);
1364        e.handle_key(key(KeyCode::Char('z')));
1365        e.handle_key(key(KeyCode::Char('t')));
1366        // Cursor lands at top of viable area = top + SCROLLOFF (5).
1367        // Viewport top therefore sits at cursor - 5.
1368        assert_eq!(e.buffer().viewport().top_row, 45);
1369        assert_eq!(e.cursor().0, 50);
1370    }
1371
1372    #[test]
1373    fn ctrl_a_increments_number_at_cursor() {
1374        let mut e = Editor::new(KeybindingMode::Vim);
1375        e.set_content("x = 41");
1376        e.handle_key(ctrl_key(KeyCode::Char('a')));
1377        assert_eq!(e.buffer().lines()[0], "x = 42");
1378        assert_eq!(e.cursor(), (0, 5));
1379    }
1380
1381    #[test]
1382    fn ctrl_a_finds_number_to_right_of_cursor() {
1383        let mut e = Editor::new(KeybindingMode::Vim);
1384        e.set_content("foo 99 bar");
1385        e.handle_key(ctrl_key(KeyCode::Char('a')));
1386        assert_eq!(e.buffer().lines()[0], "foo 100 bar");
1387        assert_eq!(e.cursor(), (0, 6));
1388    }
1389
1390    #[test]
1391    fn ctrl_a_with_count_adds_count() {
1392        let mut e = Editor::new(KeybindingMode::Vim);
1393        e.set_content("x = 10");
1394        for d in "5".chars() {
1395            e.handle_key(key(KeyCode::Char(d)));
1396        }
1397        e.handle_key(ctrl_key(KeyCode::Char('a')));
1398        assert_eq!(e.buffer().lines()[0], "x = 15");
1399    }
1400
1401    #[test]
1402    fn ctrl_x_decrements_number() {
1403        let mut e = Editor::new(KeybindingMode::Vim);
1404        e.set_content("n=5");
1405        e.handle_key(ctrl_key(KeyCode::Char('x')));
1406        assert_eq!(e.buffer().lines()[0], "n=4");
1407    }
1408
1409    #[test]
1410    fn ctrl_x_crosses_zero_into_negative() {
1411        let mut e = Editor::new(KeybindingMode::Vim);
1412        e.set_content("v=0");
1413        e.handle_key(ctrl_key(KeyCode::Char('x')));
1414        assert_eq!(e.buffer().lines()[0], "v=-1");
1415    }
1416
1417    #[test]
1418    fn ctrl_a_on_negative_number_increments_toward_zero() {
1419        let mut e = Editor::new(KeybindingMode::Vim);
1420        e.set_content("a = -5");
1421        e.handle_key(ctrl_key(KeyCode::Char('a')));
1422        assert_eq!(e.buffer().lines()[0], "a = -4");
1423    }
1424
1425    #[test]
1426    fn ctrl_a_noop_when_no_digit_on_line() {
1427        let mut e = Editor::new(KeybindingMode::Vim);
1428        e.set_content("no digits here");
1429        e.handle_key(ctrl_key(KeyCode::Char('a')));
1430        assert_eq!(e.buffer().lines()[0], "no digits here");
1431    }
1432
1433    #[test]
1434    fn zb_puts_cursor_at_viewport_bottom_with_scrolloff() {
1435        let mut e = Editor::new(KeybindingMode::Vim);
1436        e.set_content(&many_lines(100));
1437        prime_viewport(&mut e, 20);
1438        e.jump_cursor(50, 0);
1439        e.handle_key(key(KeyCode::Char('z')));
1440        e.handle_key(key(KeyCode::Char('b')));
1441        // Cursor lands at bottom of viable area = top + height - 1 -
1442        // SCROLLOFF. For height 20, scrolloff 5: cursor at top + 14,
1443        // so top = cursor - 14 = 36.
1444        assert_eq!(e.buffer().viewport().top_row, 36);
1445        assert_eq!(e.cursor().0, 50);
1446    }
1447
1448    /// Contract that the TUI drain relies on: `set_content` flags the
1449    /// editor dirty (so the next `take_dirty` call reports the change),
1450    /// and a second `take_dirty` returns `false` after consumption. The
1451    /// TUI drains this flag after every programmatic content load so
1452    /// opening a tab doesn't get mistaken for a user edit and mark the
1453    /// tab dirty (which would then trigger the quit-prompt on `:q`).
1454    #[test]
1455    fn set_content_dirties_then_take_dirty_clears() {
1456        let mut e = Editor::new(KeybindingMode::Vim);
1457        e.set_content("hello");
1458        assert!(
1459            e.take_dirty(),
1460            "set_content should leave content_dirty=true"
1461        );
1462        assert!(!e.take_dirty(), "take_dirty should clear the flag");
1463    }
1464
1465    #[test]
1466    fn content_arc_returns_same_arc_until_mutation() {
1467        let mut e = Editor::new(KeybindingMode::Vim);
1468        e.set_content("hello");
1469        let a = e.content_arc();
1470        let b = e.content_arc();
1471        assert!(
1472            std::sync::Arc::ptr_eq(&a, &b),
1473            "repeated content_arc() should hit the cache"
1474        );
1475
1476        // Any mutation must invalidate the cache.
1477        e.handle_key(key(KeyCode::Char('i')));
1478        e.handle_key(key(KeyCode::Char('!')));
1479        let c = e.content_arc();
1480        assert!(
1481            !std::sync::Arc::ptr_eq(&a, &c),
1482            "mutation should invalidate content_arc() cache"
1483        );
1484        assert!(c.contains('!'));
1485    }
1486
1487    #[test]
1488    fn content_arc_cache_invalidated_by_set_content() {
1489        let mut e = Editor::new(KeybindingMode::Vim);
1490        e.set_content("one");
1491        let a = e.content_arc();
1492        e.set_content("two");
1493        let b = e.content_arc();
1494        assert!(!std::sync::Arc::ptr_eq(&a, &b));
1495        assert!(b.starts_with("two"));
1496    }
1497
1498    /// Click past the last char of a line should land the cursor on
1499    /// the line's last char (Normal mode), not one past it. The
1500    /// previous bug clamped to the line's BYTE length and used `>=`
1501    /// past-end, so clicking deep into the trailing space parked the
1502    /// cursor at `chars().count()` — past where Normal mode lives.
1503    #[test]
1504    fn mouse_click_past_eol_lands_on_last_char() {
1505        let mut e = Editor::new(KeybindingMode::Vim);
1506        e.set_content("hello");
1507        // Outer editor area: x=0, y=0, width=80. mouse_to_doc_pos
1508        // reserves row 0 for the tab bar and adds gutter padding,
1509        // so click row 1, way past the line end.
1510        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
1511        e.mouse_click(area, 78, 1);
1512        assert_eq!(e.cursor(), (0, 4));
1513    }
1514
1515    #[test]
1516    fn mouse_click_past_eol_handles_multibyte_line() {
1517        let mut e = Editor::new(KeybindingMode::Vim);
1518        // 5 chars, 6 bytes — old code's `String::len()` clamp was
1519        // wrong here.
1520        e.set_content("héllo");
1521        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
1522        e.mouse_click(area, 78, 1);
1523        assert_eq!(e.cursor(), (0, 4));
1524    }
1525
1526    #[test]
1527    fn mouse_click_inside_line_lands_on_clicked_char() {
1528        let mut e = Editor::new(KeybindingMode::Vim);
1529        e.set_content("hello world");
1530        // Gutter is `lnum_width + 1` = (1-digit row count + 2) + 1
1531        // pane padding = 4 cells; click col 4 is the first char.
1532        let area = ratatui::layout::Rect::new(0, 0, 80, 10);
1533        e.mouse_click(area, 4, 1);
1534        assert_eq!(e.cursor(), (0, 0));
1535        e.mouse_click(area, 6, 1);
1536        assert_eq!(e.cursor(), (0, 2));
1537    }
1538}