Skip to main content

fresh/app/
mod.rs

1mod action_events;
2mod active_focus;
3mod async_dispatch;
4mod async_messages;
5mod bookmark_actions;
6mod bookmarks;
7mod buffer_close;
8mod buffer_config_resolve;
9mod buffer_groups;
10mod buffer_management;
11mod calibration_actions;
12pub mod calibration_wizard;
13mod click_geometry;
14mod click_handlers;
15mod clipboard;
16mod composite_buffer_actions;
17mod dabbrev_actions;
18mod diagnostic_jumps;
19mod editor_accessors;
20mod editor_init;
21mod event_apply;
22pub mod event_debug;
23mod event_debug_actions;
24mod file_explorer;
25pub mod file_open;
26mod file_open_input;
27mod file_open_orchestrators;
28mod file_open_queue;
29mod file_operations;
30mod help;
31mod help_actions;
32mod hover;
33mod input;
34mod input_dispatch;
35mod input_helpers;
36pub mod keybinding_editor;
37mod keybinding_editor_actions;
38mod lifecycle;
39mod line_scan;
40mod lsp_actions;
41mod lsp_event_notify;
42mod lsp_requests;
43mod lsp_status;
44mod macro_actions;
45mod macros;
46mod menu_actions;
47mod menu_context;
48mod mouse_input;
49mod navigation;
50mod on_save_actions;
51mod orchestrator_persistence;
52mod path_utils;
53mod plugin_commands;
54mod plugin_dispatch;
55mod popup_actions;
56mod popup_dialogs;
57mod popup_overlay_actions;
58mod prompt_actions;
59mod prompt_lifecycle;
60mod recovery_actions;
61mod regex_replace;
62mod render;
63mod scan_orchestrators;
64mod scroll_sync;
65mod scrollbar_input;
66mod scrollbar_math;
67mod search_ops;
68mod search_scan;
69mod settings_actions;
70mod settings_prompts;
71mod shell_command;
72mod smart_home;
73mod split_actions;
74mod stdin_stream;
75mod tab_drag;
76mod terminal;
77mod terminal_input;
78mod terminal_mouse;
79mod text_ops;
80mod theme_inspect;
81mod toggle_actions;
82pub mod types;
83mod undo_actions;
84mod view_actions;
85mod virtual_buffers;
86pub mod warning_domains;
87pub mod window;
88mod window_actions;
89pub mod window_resources;
90pub mod workspace;
91
92use anyhow::Result as AnyhowResult;
93use rust_i18n::t;
94
95/// Shared per-tick housekeeping: process async messages, check timers, auto-save, etc.
96/// Returns true if a render is needed. The `clear_terminal` callback handles full-redraw
97/// requests (terminal clears the screen; GUI can ignore or handle differently).
98/// Used by both the terminal event loop and the GUI event loop.
99pub fn editor_tick(
100    editor: &mut Editor,
101    mut clear_terminal: impl FnMut() -> AnyhowResult<()>,
102) -> AnyhowResult<bool> {
103    let mut needs_render = false;
104
105    let async_messages = {
106        let _s = tracing::info_span!("process_async_messages").entered();
107        editor.process_async_messages()
108    };
109    if async_messages {
110        needs_render = true;
111    }
112    let pending_file_opens = {
113        let _s = tracing::info_span!("process_pending_file_opens").entered();
114        editor.process_pending_file_opens()
115    };
116    if pending_file_opens {
117        needs_render = true;
118    }
119    if editor.process_line_scan() {
120        needs_render = true;
121    }
122    let search_scan = {
123        let _s = tracing::info_span!("process_search_scan").entered();
124        editor.process_search_scan()
125    };
126    if search_scan {
127        needs_render = true;
128    }
129    let search_overlay_refresh = {
130        let _s = tracing::info_span!("check_search_overlay_refresh").entered();
131        editor.check_search_overlay_refresh()
132    };
133    if search_overlay_refresh {
134        needs_render = true;
135    }
136    if editor.check_mouse_hover_timer() {
137        needs_render = true;
138    }
139    if editor.active_window().check_semantic_highlight_timer() {
140        needs_render = true;
141    }
142    if editor.check_completion_trigger_timer() {
143        needs_render = true;
144    }
145    editor.active_window_mut().check_diagnostic_pull_timer();
146    if editor.check_warning_log() {
147        needs_render = true;
148    }
149    if editor.poll_stdin_streaming() {
150        needs_render = true;
151    }
152
153    if let Err(e) = editor.auto_recovery_save_dirty_buffers() {
154        tracing::debug!("Auto-recovery-save error: {}", e);
155    }
156    if let Err(e) = editor.auto_save_persistent_buffers() {
157        tracing::debug!("Auto-save (disk) error: {}", e);
158    }
159
160    if editor.take_full_redraw_request() {
161        clear_terminal()?;
162        needs_render = true;
163    }
164
165    Ok(needs_render)
166}
167
168pub(crate) use path_utils::normalize_path;
169
170use self::types::{
171    LspMenuItem, LspMessageEntry, LspProgressInfo, SearchState, TabContextMenu,
172    DEFAULT_BACKGROUND_FILE,
173};
174use crate::config::Config;
175use crate::config_io::DirectoryContext;
176use crate::input::buffer_mode::ModeRegistry;
177use crate::input::command_registry::CommandRegistry;
178use crate::input::keybindings::{Action, KeyContext, KeybindingResolver};
179use crate::input::quick_open::{
180    BufferProvider, CommandProvider, FileProvider, GotoLineProvider, QuickOpenRegistry,
181};
182use crate::model::cursor::Cursors;
183use crate::model::event::{Event, EventLog, LeafId, SplitDirection};
184use crate::model::filesystem::FileSystem;
185use crate::services::async_bridge::{AsyncBridge, AsyncMessage};
186use crate::services::fs::FsManager;
187use crate::services::lsp::manager::LspManager;
188use crate::services::plugins::PluginManager;
189use crate::services::recovery::{RecoveryConfig, RecoveryService};
190use crate::services::time_source::{RealTimeSource, SharedTimeSource};
191use crate::state::EditorState;
192use crate::types::{LspLanguageConfig, LspServerConfig, ProcessLimits};
193use crate::view::file_tree::{FileTree, FileTreeView};
194use crate::view::prompt::PromptType;
195use crate::view::split::{SplitManager, SplitViewState};
196use crate::view::ui::{
197    FileExplorerRenderer, SplitRenderer, StatusBarRenderer, SuggestionsRenderer,
198};
199use crossterm::event::{KeyCode, KeyModifiers};
200use ratatui::{
201    layout::{Constraint, Direction, Layout},
202    Frame,
203};
204use std::collections::HashMap;
205use std::ops::Range;
206use std::path::{Path, PathBuf};
207use std::sync::{Arc, Mutex, RwLock};
208use std::time::Instant;
209
210// Re-export BufferId from event module for backward compatibility
211pub use self::types::{BufferKind, BufferMetadata, HoverTarget};
212pub use self::warning_domains::{
213    GeneralWarningDomain, LspWarningDomain, WarningAction, WarningActionId, WarningDomain,
214    WarningDomainRegistry, WarningLevel, WarningPopupContent,
215};
216pub use crate::model::event::BufferId;
217
218/// Decode a wire-side LSP URI to a host path. Thin wrapper over
219/// [`LspUri::to_host_path`](crate::app::types::LspUri::to_host_path)
220/// that produces a `Result` for call sites that prefer the
221/// error-string form. Editor code that owns a raw `lsp_types::Uri`
222/// from a third-party type (e.g. `lsp_types::Location.uri`) wraps it
223/// via [`LspUri::from_wire`](crate::app::types::LspUri::from_wire)
224/// and then calls this — that's the only path from a wire URI to a
225/// host `PathBuf`, by construction.
226fn lsp_uri_to_host_path(
227    uri: &crate::app::types::LspUri,
228    translation: Option<&crate::services::authority::PathTranslation>,
229) -> Result<PathBuf, String> {
230    uri.to_host_path(translation)
231        .ok_or_else(|| "URI is not a file path".to_string())
232}
233
234/// A pending grammar registration waiting for reload_grammars() to apply
235#[derive(Clone, Debug)]
236pub struct PendingGrammar {
237    /// Language identifier (e.g., "elixir")
238    pub language: String,
239    /// Path to the grammar file (.sublime-syntax or .tmLanguage)
240    pub grammar_path: String,
241    /// File extensions to associate with this grammar
242    pub extensions: Vec<String>,
243}
244
245/// Track an in-flight semantic token range request.
246#[derive(Clone, Debug)]
247pub(crate) struct SemanticTokenRangeRequest {
248    pub(crate) buffer_id: BufferId,
249    pub(crate) version: u64,
250    pub(crate) range: Range<usize>,
251    pub(crate) start_line: usize,
252    pub(crate) end_line: usize,
253}
254
255#[derive(Clone, Copy, Debug)]
256pub(crate) enum SemanticTokensFullRequestKind {
257    Full,
258    FullDelta,
259}
260
261#[derive(Clone, Debug)]
262pub(crate) struct SemanticTokenFullRequest {
263    pub(crate) buffer_id: BufferId,
264    pub(crate) version: u64,
265    pub(crate) kind: SemanticTokensFullRequestKind,
266}
267
268#[derive(Clone, Debug)]
269pub(crate) struct FoldingRangeRequest {
270    pub(crate) buffer_id: BufferId,
271    pub(crate) version: u64,
272}
273
274#[derive(Clone, Debug)]
275pub(crate) struct InlayHintsRequest {
276    pub(crate) buffer_id: BufferId,
277    pub(crate) version: u64,
278}
279
280/// State for the dabbrev cycling session (Alt+/ style).
281///
282/// When the user presses Alt+/ repeatedly, we cycle through candidates
283/// in proximity order without showing a popup. The session is reset when
284/// any other action is taken (typing, moving, etc.).
285#[derive(Debug, Clone)]
286pub struct DabbrevCycleState {
287    /// The original prefix the user typed before the first expansion.
288    pub original_prefix: String,
289    /// Byte position where the prefix starts.
290    pub word_start: usize,
291    /// The list of candidates (ordered by proximity).
292    pub candidates: Vec<String>,
293    /// Current index into `candidates`.
294    pub index: usize,
295}
296
297/// Snapshot of cursor and viewport state used to restore the original position
298/// when a goto-line preview is abandoned (cancel, or the user edits the input
299/// so it no longer targets a line).
300///
301/// Shared between Quick Open's `:N` syntax and the standalone `Goto Line`
302/// prompt — both flows save a snapshot on the first preview jump and restore
303/// it if the user cancels or clears the target.
304///
305/// `last_jump_position` is the byte offset the most recent preview jump put the
306/// cursor at; the restore path only applies the snapshot when the cursor is
307/// still exactly there. If anything else moved the cursor (mouse click, an
308/// async buffer edit shifting positions via `adjust_for_edit`, …) the snapshot
309/// is considered stale and simply dropped. This is the single staleness check
310/// that replaces per-site invalidation across many call paths.
311#[derive(Debug, Clone)]
312pub(crate) struct GotoLinePreviewSnapshot {
313    pub buffer_id: BufferId,
314    pub split_id: LeafId,
315    pub cursor_id: crate::model::event::CursorId,
316    pub position: usize,
317    pub anchor: Option<usize>,
318    pub sticky_column: usize,
319    pub viewport_top_byte: usize,
320    pub viewport_top_view_line_offset: usize,
321    pub viewport_left_column: usize,
322    pub last_jump_position: usize,
323}
324
325/// The main editor struct - manages multiple buffers, clipboard, and rendering
326pub struct Editor {
327    // Buffers moved onto `Window` (Step 0c). Each window owns its
328    // own buffer storage; opening the same file in two windows
329    // produces two independent buffers. Access through
330    // `Editor::buffers()` / `buffers_mut()` (active window) or by
331    // direct `self.windows.get_mut(&id).unwrap().buffers` for
332    // cross-window iteration.
333
334    // NOTE: There is no `active_buffer` field. The active buffer is derived from
335    // `split_manager.active_buffer_id()` to maintain a single source of truth.
336    // Use `self.active_buffer()` to get the active buffer ID.
337    // event_logs moved onto `Window` (Step 0e). Undo logs follow the
338    // buffer storage, so they live alongside the buffer they describe.
339    /// Next buffer ID to assign.
340    ///
341    /// **Use [`Self::alloc_buffer_id`] instead of direct mutation.**
342    /// Direct `self.next_buffer_id += 1` works only on `impl Editor`
343    /// — methods that run on `impl Window` allocate via the shared
344    /// `Arc<BufferIdAllocator>` in `WindowResources`. Keeping this
345    /// counter as the canonical source means a handler on `Editor`
346    /// can still increment locally; the allocator's atomic stays in
347    /// sync because both go through `alloc_buffer_id`.
348    next_buffer_id: usize,
349
350    /// Globally-unique buffer-id allocator shared by `Arc` clone with
351    /// every `Window` (via `WindowResources`). Handlers on
352    /// `impl Window` call `Window::alloc_buffer_id()` which delegates
353    /// here; handlers on `impl Editor` call `Editor::alloc_buffer_id()`
354    /// which does the same plus advances the local `next_buffer_id`
355    /// snapshot. Both routes produce the same monotonic id sequence.
356    pub(crate) buffer_id_alloc: crate::app::window_resources::BufferIdAllocator,
357
358    /// Configuration.
359    ///
360    /// Stored as `Arc<Config>` so that mutations go through `Arc::make_mut`
361    /// (via `config_mut()`), which clone-on-writes when any other holder
362    /// references the same value. `Arc<T>` has no `DerefMut`, so direct
363    /// field assignment through `self.config` is a compile error — every
364    /// mutation must route through the CoW-aware accessor.
365    ///
366    /// Effective value is `base_config_json` + `runtime_overlay` (design
367    /// §3.4): init.ts and plugins may layer per-session writes via
368    /// `editor.setSetting(path, value)`. The overlay is merged into
369    /// `base_config_json`, the result is deserialised into this field,
370    /// and mutations go through `Arc::make_mut`.
371    ///
372    /// **Freshness invariant**: `config_snapshot_anchor` below is set to
373    /// `Arc::clone(&self.config)` on every plugin-snapshot refresh. That
374    /// guarantees the first `Arc::make_mut(&mut self.config)` after each
375    /// refresh *always* CoW-clones (strong count ≥ 2), so `self.config`
376    /// moves to a new pointer and stops being `ptr_eq` with the anchor.
377    config: Arc<Config>,
378
379    /// Clone of `config` captured at the last plugin-snapshot refresh.
380    config_snapshot_anchor: Arc<Config>,
381
382    /// Serialized JSON of `*self.config` as of the last time
383    /// `ptr_eq(&self.config, &self.config_snapshot_anchor)` was false.
384    config_cached_json: Arc<serde_json::Value>,
385
386    /// Cached raw user config (for plugins, avoids re-reading file on every frame).
387    user_config_raw: Arc<serde_json::Value>,
388
389    /// Directory context for editor state paths
390    dir_context: DirectoryContext,
391
392    /// Grammar registry for TextMate syntax highlighting
393    grammar_registry: std::sync::Arc<crate::primitives::grammar::GrammarRegistry>,
394
395    /// Pending grammars registered by plugins, waiting for reload_grammars() to apply
396    pending_grammars: Vec<PendingGrammar>,
397
398    /// Whether a grammar reload has been requested but not yet flushed.
399    /// This allows batching multiple RegisterGrammar+ReloadGrammars sequences
400    /// into a single rebuild.
401    grammar_reload_pending: bool,
402
403    /// Whether a background grammar build is in progress.
404    /// When true, `flush_pending_grammars()` defers work until the build completes.
405    grammar_build_in_progress: bool,
406
407    /// Whether the initial full grammar build (user grammars + language packs)
408    /// still needs to happen. Deferred from construction so that plugin-registered
409    /// grammars from the first event-loop tick are included in a single build.
410    needs_full_grammar_build: bool,
411
412    /// Plugin callback IDs waiting for the grammar build to complete.
413    /// Multiple reloadGrammars() calls may accumulate here; all are resolved
414    /// when the background build finishes.
415    pending_grammar_callbacks: Vec<fresh_core::api::JsCallbackId>,
416
417    /// Active theme
418    /// Active resolved theme, shared with windows via WindowResources.
419    /// Wrapped in `Arc<RwLock<>>` so a theme reload propagates to every
420    /// window without copying.
421    pub(crate) theme: Arc<RwLock<crate::view::theme::Theme>>,
422
423    /// All loaded themes (embedded + user). Held as `Arc` so
424    /// `expanded_menus_cache` can detect a registry swap via `Arc::ptr_eq`.
425    theme_registry: Arc<crate::view::theme::ThemeRegistry>,
426
427    /// Memoised `MenuConfig` with `DynamicSubmenu` items expanded against
428    /// the current theme registry.
429    expanded_menus_cache: crate::view::ui::ExpandedMenusCache,
430
431    /// Shared theme data cache for plugin access (name → JSON value)
432    theme_cache: Arc<RwLock<HashMap<String, serde_json::Value>>>,
433
434    /// Optional ANSI background image
435    ansi_background: Option<crate::primitives::ansi_background::AnsiBackground>,
436
437    /// Source path for the currently loaded ANSI background
438    ansi_background_path: Option<PathBuf>,
439
440    /// Blend amount for the ANSI background (0..1)
441    background_fade: f32,
442
443    /// Keybinding resolver (shared with Quick Open CommandProvider)
444    keybindings: Arc<RwLock<KeybindingResolver>>,
445
446    /// Shared clipboard (handles both internal and system clipboard)
447    clipboard: crate::services::clipboard::Clipboard,
448
449    /// Should the editor quit?
450    should_quit: bool,
451
452    /// Whether the workspace-trust prompt currently on screen was opened
453    /// voluntarily (command palette) rather than as the mandatory open-time
454    /// gate. When `true` its secondary action is "Cancel" (just close); when
455    /// `false` it's "Quit" (exit the editor) and Escape is inert.
456    workspace_trust_prompt_cancellable: bool,
457
458    /// The marker files/dirs that triggered the workspace-trust prompt (e.g.
459    /// `.envrc`, `.venv`, `App.sln`), captured when the prompt is shown so the
460    /// dialog can tell the user why it appeared.
461    workspace_trust_markers: Vec<String>,
462
463    /// Scroll offset (in rows) for the workspace-trust dialog when it's too
464    /// tall for the terminal. Driven by the mouse wheel; clamped in render.
465    workspace_trust_scroll: u16,
466
467    /// Should the client detach (keep server running)?
468    should_detach: bool,
469
470    /// Running in session/server mode (use hardware cursor only, no REVERSED style)
471    session_mode: bool,
472
473    /// Backend does not render a hardware cursor — always use software cursor indicators.
474    software_cursor_only: bool,
475
476    /// Session name for display in status bar (session mode only)
477    session_name: Option<String>,
478
479    /// Pending escape sequences to send to client (session mode only)
480    /// These get prepended to the next render output
481    pending_escape_sequences: Vec<u8>,
482
483    /// If set, the editor should restart with this new working directory
484    /// This is used by Open Folder to do a clean context switch
485    restart_with_dir: Option<PathBuf>,
486
487    // status_message, plugin_status_message, prompt moved onto
488    // `Window` (Step 0k phase 3) — each window has its own chrome,
489    // and the active window's chrome is what renders.
490    /// Last terminal window title written via OSC 2. Used so we only write
491    /// the escape sequence when the title would actually change, rather
492    /// than on every frame.
493    last_window_title: Option<String>,
494
495    // `plugin_errors` moved onto `Window`.
496    /// Terminal dimensions (for creating new buffers)
497    terminal_width: u16,
498    terminal_height: u16,
499
500    // LSP manager moved onto `Window`. Access via
501    // `Editor::lsp()` / `lsp_mut()` — each window has its own
502    // LspManager rooted at its project root.
503    // buffer_metadata moved onto `Window` (Step 0l). Access via
504    // `self.active_window().buffer_metadata` (or
505    // `_mut()`) — each window owns the metadata for its own
506    // buffers, alongside the buffer storage itself.
507    /// Buffer mode registry (for buffer-local keybindings)
508    mode_registry: ModeRegistry,
509
510    /// Tokio runtime for async I/O tasks
511    tokio_runtime: Option<Arc<tokio::runtime::Runtime>>,
512
513    /// Bridge for async messages from tokio tasks to main loop
514    async_bridge: Option<AsyncBridge>,
515
516    // split_manager and split_view_states moved onto `Window`. Access
517    // via `Editor::split_manager()` / `split_manager_mut()` and
518    // `Editor::split_view_states()` / `split_view_states_mut()`.
519    // Each window owns its own split tree + per-leaf view state.
520    // `previous_viewports` moved onto `Window` (per-leaf state — each
521    // window has its own splits, so its own viewport-change tracker).
522    // `scroll_sync_manager` moved onto `Window` — pairs splits, which
523    // are per-window.
524
525    // file_explorer moved onto `Window`. Access via
526    // `Editor::file_explorer()` / `file_explorer_mut()` —
527    // each window has its own tree view.
528    // `preview` (per-window preview-tab tracker) moved onto `Window`.
529    // Each window has its own preview slot.
530
531    // suppress_position_history_once moved onto `Window` (Step 0f).
532    /// Filesystem manager for file explorer
533    fs_manager: Arc<FsManager>,
534
535    /// Single backend slot for "where does the editor act?".
536    ///
537    /// Bundles filesystem, process spawner, terminal wrapper, and
538    /// display label. Replaces the old quartet of `filesystem`,
539    /// `process_spawner`, `terminal_wrapper`, `authority_display_string`
540    /// fields. Always present; the editor boots with `Authority::local()`
541    /// and plugins (or the SSH startup flow) install a different one
542    /// later via `install_authority`. Pointer-equality on the inner
543    /// `Arc`s answers "still the same backend?".
544    authority: crate::services::authority::Authority,
545
546    /// Authority queued by `install_authority`, picked up by `main.rs`
547    /// right before dropping this editor on restart. `None` in the
548    /// steady state. Not durable state — restarts from `main.rs`'s
549    /// restart-dir path leave this `None`, and the main loop carries
550    /// the authority over through its own channel.
551    pending_authority: Option<crate::services::authority::Authority>,
552
553    /// Plugin-supplied override for the Remote Indicator. Takes
554    /// precedence over the authority-derived state at render time.
555    /// Cleared on editor restart (plugins must reassert the state
556    /// after `setAuthority`). See
557    /// `PluginCommand::SetRemoteIndicatorState`.
558    pub remote_indicator_override: Option<crate::view::ui::status_bar::RemoteIndicatorOverride>,
559
560    /// Local filesystem for editor-internal files (log files, status
561    /// log). Stays separate from `authority` because these are the
562    /// editor's own private state — they live on the host disk
563    /// regardless of where the user is editing.
564    local_filesystem: Arc<dyn FileSystem + Send + Sync>,
565
566    // `file_explorer_visible`, `file_explorer_sync_in_progress`,
567    // `file_explorer_width`, `file_explorer_side`,
568    // `pending_file_explorer_show_*`, `file_explorer_decorations`,
569    // `file_explorer_decoration_cache` all moved onto `Window`. The
570    // file-explorer view itself was already per-window since Step 0b;
571    // these chrome flags follow.
572    /// File explorer clipboard for cut/copy/paste of files and directories
573    // `file_explorer_clipboard` moved onto `Window`.
574
575    // `menu_bar_visible`, `menu_bar_auto_shown`, `tab_bar_visible`,
576    // `status_bar_visible`, `prompt_line_visible`, `mouse_enabled`
577    // moved onto `Window` — per-window UI toggles.
578
579    // `same_buffer_scroll_sync` moved onto `Window` — per-window UX
580    // toggle, since the split tree it controls is per-window.
581    /// Mouse cursor position (for GPM software cursor rendering)
582    /// When GPM is active, we need to draw our own cursor since GPM can't
583    /// draw on the alternate screen buffer used by TUI applications.
584    // `mouse_cursor_position`, `gpm_active`, `key_context` moved onto `Window`.
585
586    /// Menu state (active menu, highlighted item)
587    menu_state: crate::view::ui::MenuState,
588
589    /// Menu configuration (built-in menus with i18n support)
590    menus: crate::config::MenuConfig,
591
592    /// Working directory for file explorer (set at initialization).
593    ///
594    /// During the Session migration this field still backs every
595    /// existing read site. New code should prefer
596    /// `self.active_window().root` so the eventual swap to a real
597    /// active-session pointer is a no-op for the call site. See
598    /// `docs/internal/orchestrator-sessions-design.md` Step 1.
599    working_dir: PathBuf,
600
601    /// All editor sessions, keyed by id. Initially holds exactly one
602    /// session (`WindowId(1)`, the "base") rooted at `working_dir`.
603    /// Step 1 of the migration adds the abstraction without yet
604    /// allowing more than one entry.
605    pub(crate) windows: HashMap<fresh_core::WindowId, crate::app::window::Window>,
606
607    /// Id of the currently active session. Always `WindowId(1)` for
608    /// now; multi-session support arrives in a follow-up commit.
609    pub(crate) active_window: fresh_core::WindowId,
610
611    /// Monotonic counter for the next session id. The base session
612    /// uses 1; new sessions take 2, 3, …. Closing a session does
613    /// not free its id (per design, ids are stable within a process).
614    /// Unused until `createWindow` lands in the next migration step.
615    #[allow(dead_code)]
616    pub(crate) next_window_id: u64,
617
618    // LSP request-tracking state (next_lsp_request_id,
619    // pending_*_requests, *_in_flight, completion_items,
620    // dabbrev_state, etc.) all moved onto `Window` in Step 0k.
621    // `completion_service` moved onto `Window` — orchestrates this
622    // window's per-window completion providers (dabbrev, buffer words,
623    // LSP, plugin providers).
624    // `lsp_diagnostic_namespace` moved onto `Window` — overlay
625    // namespace key for diagnostic overlays, which are buffer overlays
626    // and follow buffers onto the window.
627    // `interactive_replace_state` moved onto `Window` — per-window
628    // search-and-replace session state.
629    // `mouse_state` moved onto `Window` — drag targets reference per-window LeafIds.
630    // `tab_context_menu`, `file_explorer_context_menu`, `theme_info_popup`
631    // moved onto `Window`.
632    // `chrome_layout` moved onto `Window` — each window has its own
633    // status bar, menu, prompt overlay, and popups.
634    /// Command registry for dynamic commands
635    command_registry: Arc<RwLock<CommandRegistry>>,
636
637    /// Quick Open registry for unified prompt providers
638    quick_open_registry: QuickOpenRegistry,
639
640    /// Plugin manager (handles both enabled and disabled cases)
641    /// Plugin manager, wrapped in `Arc<RwLock<>>` so windows can fire
642    /// hooks (`run_hook`) via WindowResources without holding an
643    /// `&mut Editor` reference. `&self` methods (run_hook,
644    /// deliver_response, has_hook_handlers, …) take a read lock; the
645    /// few `&mut self` methods (process_commands, check_thread_health,
646    /// test_inject_command) take a write lock.
647    plugin_manager: Arc<RwLock<PluginManager>>,
648
649    // `plugin_dev_workspaces` moved onto `Window` — keyed by `BufferId`,
650    // and buffers are per-window, so the workspace map follows.
651    /// Registry of status-bar tokens contributed by plugins.
652    /// Key: "plugin_name:token_name" (e.g., "git_statusbar:branch"); value: display title.
653    /// Global and lifetime-checked. Per-buffer values live on each `Window`.
654    status_bar_token_registry: Mutex<HashMap<String, String>>,
655
656    /// Registry of plugin-provided config schemas, keyed by plugin name.
657    /// Each value is the validated JSON schema describing
658    /// `plugins.<name>.settings.*`. Populated at startup from
659    /// `<plugin_name>.schema.json` sidecar files discovered next to plugin
660    /// `.ts`/`.js` files; the Settings UI reads this to render a
661    /// per-plugin sub-category under "Plugin Settings".
662    pub(crate) plugin_schemas:
663        std::sync::Arc<std::sync::RwLock<HashMap<String, serde_json::Value>>>,
664
665    // `seen_byte_ranges` moved onto `Window` — keyed by `BufferId`
666    // which lives on `Window`, so the tracker follows the buffers.
667
668    // panel_ids moved onto `Window`. Access via
669    // `Editor::panel_ids()` / `panel_ids_mut()` — those resolve to
670    // the active window's dock occupancy. Each window owns its own
671    // utility-dock; switching windows doesn't share dock state.
672    // `buffer_groups`, `buffer_to_group`, `next_buffer_group_id`
673    // moved onto `Window` — each window has its own buffer groups.
674
675    // grouped_subtrees moved onto `Window` — each window owns its
676    // own buffer-group subtrees (a window with a Live Grep panel
677    // open doesn't share the panel state with sibling windows).
678    /// Background process abort handles for cancellation
679    /// Maps process_id to abort handle
680    background_process_handles: HashMap<u64, tokio::task::AbortHandle>,
681
682    /// Cancellation senders for host-side processes spawned via
683    /// `spawnHostProcess`. Firing the sender (or dropping it) triggers
684    /// an in-task `child.start_kill()` so the process is reaped, not
685    /// just orphaned. Entries are removed when the spawn task sends
686    /// its terminal `PluginProcessOutput`.
687    host_process_handles: HashMap<u64, tokio::sync::oneshot::Sender<()>>,
688    /// FIFO queue of plugin `editor.getNextKey()` callbacks awaiting a
689    /// keypress. While non-empty, the next key arriving in
690    /// `handle_key` is consumed by resolving the front-most callback
691    /// rather than dispatching to mode bindings or other handlers.
692    // `pending_next_key_callbacks`, `key_capture_active`,
693    // `pending_key_capture_buffer` moved onto `Window`.
694    // `lsp_progress`, `lsp_server_statuses`, `lsp_window_messages`,
695    // `lsp_log_messages` moved onto `Window` — each window has its own
696    // LspManager, so the progress/status/message streams describe that
697    // manager's servers, not the editor's.
698
699    // `diagnostic_result_ids` moved onto `Window` — each window has its
700    // own LspManager and therefore its own per-URI result_id stream.
701    // `stored_push_diagnostics`, `stored_pull_diagnostics`,
702    // `stored_diagnostics`, `stored_folding_ranges` moved onto
703    // `Window` — URI-keyed but each URI maps to a buffer in a specific
704    // window's LSP set, so the maps describe one window's diagnostics.
705    /// Event broadcaster for control events (observable by external systems)
706    event_broadcaster: crate::model::control_event::EventBroadcaster,
707
708    // bookmarks moved onto `Window` (Step 0f).
709    /// Macro record/playback subsystem (owns `macros`, `recording`,
710    /// `last_register`, and the `playing` guard flag).
711    // `macros` moved onto `Window`.
712
713    /// Pending plugin action receivers (for async action execution)
714    #[cfg(feature = "plugins")]
715    pending_plugin_actions: Vec<(
716        String,
717        crate::services::plugins::thread::oneshot::Receiver<anyhow::Result<()>>,
718    )>,
719
720    /// Flag set by plugin commands that need a render (e.g., RefreshLines)
721    #[cfg(feature = "plugins")]
722    plugin_render_requested: bool,
723
724    /// Pending chord sequence for multi-key bindings (e.g., C-x C-s in Emacs)
725    /// Stores the keys pressed so far in a chord sequence
726    // `chord_state` moved onto `Window`.
727
728    // (Historical `pending_lsp_confirmation` and `pending_lsp_status_popup`
729    // fields moved onto `Popup::resolver` — each popup carries its own
730    // "how do I confirm?" identity, so `handle_popup_confirm` dispatches
731    // by matching the focused popup's resolver instead of racing through
732    // a precedence cascade of side-channel `Option`s that a second
733    // simultaneously-open popup could steal.)
734    /// Languages the user has interactively dismissed from the LSP popup.
735    ///
736    /// Pending Save-As queue for the "save and quit" flow.
737    ///
738    // `last_auto_revert_poll`, `last_file_tree_poll`, `git_index_resolved`,
739    // `dir_mod_times`, `pending_file_poll_rx`, `pending_dir_poll_rx`
740    // all moved onto `Window` — auto-revert and file-tree change
741    // detection are per-window, paired with the already-per-window
742    // `file_mod_times`.
743    /// File open dialog state (when PromptType::OpenFile is active)
744    // `file_open_state` moved onto `Window`.
745
746    /// Cached layout for file browser (for mouse hit testing)
747    // `file_browser_layout` moved onto `Window`.
748
749    /// Recovery service for auto-recovery-save and crash recovery
750    recovery_service: RecoveryService,
751
752    /// Request a full terminal clear and redraw on the next frame
753    full_redraw_requested: bool,
754
755    /// Request the event loop to suspend the process (SIGTSTP on Unix).
756    /// Consumed by the outer event loop after the current action returns.
757    suspend_requested: bool,
758
759    /// Time source for testable time operations
760    time_source: SharedTimeSource,
761
762    /// Last auto-recovery-save time for rate limiting
763    // `last_auto_recovery_save`, `last_persistent_auto_save` moved onto `Window`.
764
765    /// Active custom contexts for command visibility
766    /// Plugin-defined contexts like "config-editor" that control command availability
767    // `active_custom_contexts` moved onto `Window`.
768
769    /// Plugin-managed global state, isolated per plugin name.
770    /// Outer key is plugin name, inner key is the state key set by the plugin.
771    plugin_global_state: HashMap<String, HashMap<String, serde_json::Value>>,
772    /// Warning log receiver and path (for tracking warnings)
773    warning_log: Option<(std::sync::mpsc::Receiver<()>, PathBuf)>,
774
775    /// Status message log path (for viewing full status history)
776    status_log_path: Option<PathBuf>,
777
778    /// Warning domain registry for extensible warning indicators
779    /// Contains LSP warnings, general warnings, and can be extended by plugins
780    // `warning_domains` moved onto `Window`.
781
782    /// Periodic update checker (checks for new releases every hour)
783    update_checker: Option<crate::services::release_checker::PeriodicUpdateChecker>,
784
785    // Terminal subsystem moved onto `Window` (Step 0d). PTYs and
786    // their backing files belong to the window that spawned them, so
787    // closeWindow joins the threads. Access through methods on Window
788    // (called via `self.windows.get_mut(&id).unwrap().method(...)`),
789    // not via accessors on Editor.
790    /// Plugin-driven filesystem watchers (lazily constructed —
791    /// the underlying notify backend spawns a thread, so it's
792    /// nicer to defer until the first `watchPath` call). See
793    /// `services/file_watcher.rs`.
794    file_watcher_manager: crate::services::file_watcher::FileWatcherManager,
795
796    /// Test-only sink for `path_changed` plugin events. Captured
797    /// by `async_dispatch` whenever a PathChanged AsyncMessage
798    /// arrives, so e2e tests can assert filesystem events
799    /// reached the editor without standing up a JS plugin.
800    /// Production builds never read this.
801    pub(crate) last_path_change_for_test: Option<(u64, std::path::PathBuf, &'static str)>,
802
803    /// Test-only sink for the most-recent `WatchPathRegistered`
804    /// plugin response, keyed by request_id. Used by
805    /// `watch_path` e2e tests to read back the allocated handle.
806    pub(crate) last_watch_response_for_test: Option<(u64, Result<u64, String>)>,
807
808    /// Plugin-driven session preview override. When `Some(sid)`
809    /// and the floating-overlay prompt is open, the overlay's
810    /// preview pane renders the *entire* split tree of session
811    /// `sid` natively — Primitive #1 in
812    /// `docs/internal/orchestrator-sessions-design.md` §
813    /// "Rich Control Room rendering".
814    pub(crate) preview_window_id: Option<fresh_core::WindowId>,
815
816    // terminal_buffers / terminal_backing_files / terminal_log_files
817    // moved onto `Window` (Step 0d).
818    // `ephemeral_terminals` moved onto `Window` — TerminalManager and
819    // its terminals are per-window (Step 0d), so the ephemeral set
820    // follows.
821
822    // `terminal_mode` moved onto `Window`. Per-window because each
823    // window has its own active terminal buffer.
824    /// Whether keyboard capture is enabled in terminal mode.
825    /// When true, ALL keys go to the terminal (except Ctrl+` to toggle).
826    /// When false, UI keybindings (split nav, palette, etc.) are processed first.
827    // `keyboard_capture` moved onto `Window`.
828
829    // `terminal_mode_resume` moved onto `Window` — terminal buffers
830    // are per-window (Step 0d), so the auto-resume set follows.
831    /// Timestamp of the previous mouse click (for multi-click detection)
832    // `previous_click_time`, `previous_click_position`, `click_count` moved onto `Window`.
833
834    /// Settings UI state (when settings modal is open)
835    pub(crate) settings_state: Option<crate::view::settings::SettingsState>,
836
837    /// Calibration wizard state (when calibration modal is open)
838    pub(crate) calibration_wizard: Option<calibration_wizard::CalibrationWizard>,
839
840    // `event_debug` modal state moved to `Window` — each window has its
841    // own debug overlay (the dialog records keystrokes destined for that
842    // window's input pipeline, so it's logically per-window).
843    /// Keybinding editor state (when keybinding editor modal is open)
844    pub(crate) keybinding_editor: Option<keybinding_editor::KeybindingEditor>,
845
846    /// Key translator for input calibration (loaded from config)
847    pub(crate) key_translator: crate::input::key_translator::KeyTranslator,
848
849    /// Terminal color capability (true color, 256, or 16 colors)
850    color_capability: crate::view::color_support::ColorCapability,
851
852    /// Hunks for the Review Diff tool
853    // `review_hunks` moved onto `Window`.
854
855    /// Editor-level popups that float above any buffer regardless of which
856    /// one is active. Plugin notifications (showActionPopup) live here so a
857    /// switch to a virtual buffer (Dashboard, diagnostics panel, …) doesn't
858    /// hide them mid-decision.
859    ///
860    /// Each plugin popup carries its `popup_id` inside its
861    /// `PopupResolver::PluginAction` — no parallel side-channel stack.
862    pub(crate) global_popups: crate::view::popup::PopupManager,
863
864    // composite_buffers + composite_view_states moved onto `Window` —
865    // composite-buffer panels (Live Grep results, Diagnostics list,
866    // References, etc.) belong to the window that opened the panel.
867    /// Pending file opens from CLI arguments (processed after TUI starts)
868    /// This allows CLI files to go through the same code path as interactive file opens,
869    /// ensuring consistent error handling (e.g., encoding confirmation prompts).
870    // `pending_file_opens` moved onto `Window`.
871
872    /// When true, apply hot exit recovery after the next batch of pending file opens
873    // `pending_hot_exit_recovery` moved onto `Window`.
874
875    /// Tracks buffers opened with --wait: maps buffer_id → (wait_id, has_popup)
876    // `wait_tracking` moved onto `Window`.
877    /// Wait IDs that have completed (buffer closed or popup dismissed)
878    // `completed_waits` moved onto `Window`.
879
880    /// Stdin streaming state (if reading from stdin)
881    stdin_stream: stdin_stream::StdinStream,
882
883    /// Incremental line scan state (for non-blocking progress during Go to Line)
884    // `line_scan` moved onto `Window`.
885
886    /// Incremental search scan state (for non-blocking search on large files)
887    // `search_scan` moved onto `Window`.
888
889    /// Viewport top_byte when search overlays were last refreshed.
890    /// Used to detect viewport scrolling so overlays can be updated.
891    // `search_overlay_top_byte` moved onto `Window`.
892
893    /// Frame-buffer animation layer. Applied at the end of `render`; the
894    /// main loop consults `is_active`/`next_deadline` to keep re-rendering
895    /// while animations are running.
896    // `animations` moved onto `Window`.
897
898    /// Hardware-cursor screen position from the previous render pass, paired
899    /// with the active split that owned the cursor at that time. Used to
900    /// detect "jumps" (search, goto-line, click, goto-definition, focus
901    /// change between splits, tab/buffer switch, etc.) and animate the
902    /// cursor moving from its old screen position to its new one. Cross-
903    /// pane jumps animate unconditionally; same-pane jumps animate when
904    /// the cursor moved more than two rows or at least ten columns.
905    pub(crate) previous_cursor_screen_pos: Option<((u16, u16), LeafId)>,
906    /// ID of the most recent cursor-jump animation, kept so successive jumps
907    /// cancel the prior one instead of stacking trail effects.
908    pub(crate) cursor_jump_animation: Option<crate::view::animation::AnimationId>,
909
910    /// Deferred plugin animations targeting a virtual buffer whose
911    /// on-screen Rect wasn't in the cached split layout at command
912    /// dispatch time. Drained at the top of each render pass once
913    /// `split_areas` has been recomputed, so the animation starts on
914    /// the very first frame the buffer actually occupies screen space.
915    pub(crate) pending_vb_animations: Vec<(u64, BufferId, fresh_core::api::PluginAnimationKind)>,
916
917    /// Plugin widget panels mounted via `MountWidgetPanel`.
918    ///
919    /// One entry per active panel. The registry holds the most recent
920    /// `WidgetSpec` per panel so future updates can reconcile against
921    /// it and so a theme change can re-render every panel without
922    /// the originating plugin needing to re-emit. See
923    /// `docs/internal/plugin-widget-library-design.md`.
924    pub(crate) widget_registry: crate::widgets::WidgetRegistry,
925
926    /// Currently-mounted floating widget panel, if any.
927    ///
928    /// At most one floating widget panel is shown at a time — the
929    /// overlay is modal-ish (input is routed to it) and stacking
930    /// floaters would obscure each other without a usable focus
931    /// model. Mounting a second one replaces the first.
932    pub(crate) floating_widget_panel: Option<FloatingWidgetState>,
933}
934
935/// Sentinel `BufferId` registered with the widget registry for the
936/// floating panel — never appears in the editor's buffer table, so
937/// `set_virtual_buffer_content` against it would fail. The mount /
938/// rerender paths special-case this id and paint into the overlay
939/// rect instead.
940pub(crate) const FLOATING_PANEL_BUFFER_ID: BufferId = BufferId(usize::MAX);
941
942/// A widget panel rendered as a centered floating overlay rather
943/// than into a virtual buffer. The panel still lives in the shared
944/// `widget_registry` (so the existing reconcile / widget-command /
945/// mutate paths apply), but its rendered entries are painted into
946/// the overlay rect at draw time instead of being written into a
947/// virtual buffer.
948#[derive(Debug, Clone)]
949pub(crate) struct FloatingWidgetState {
950    pub panel_id: crate::widgets::PanelId,
951    pub width_pct: u8,
952    pub height_pct: u8,
953    /// Most-recently rendered entries. Refreshed on every spec /
954    /// command / mutate; painted into the overlay rect at draw
955    /// time.
956    pub entries: Vec<fresh_core::text_property::TextPropertyEntry>,
957    /// Hardware-cursor target when a `TextInput` is focused.
958    pub focus_cursor: Option<crate::widgets::FocusCursor>,
959    /// Window-embed rectangles reserved by `WindowEmbed`
960    /// widgets in the panel's spec. After the entries paint
961    /// down their (blank) cells, the floating panel render
962    /// walks these and invokes the per-window paint path
963    /// scoped to each rect — giving us a live render of the
964    /// referenced editor window inside the floating overlay.
965    pub embeds: Vec<crate::widgets::EmbedRect>,
966    /// Rows produced by `WidgetSpec::Overlay` children. Painted
967    /// AFTER `entries` and `embeds`, on top of whatever's at
968    /// each `buffer_row`. Used for dropdown completions /
969    /// transient popups that should appear next to a focused
970    /// widget without reflowing the rest of the panel when
971    /// they show or hide.
972    pub overlays: Vec<crate::widgets::OverlayRow>,
973    /// Inner rect (frame interior) of the last draw — used by the
974    /// click hit-test to map terminal coords back to buffer coords.
975    pub last_inner_rect: Option<ratatui::layout::Rect>,
976}
977
978/// A file that should be opened after the TUI starts
979#[derive(Debug, Clone)]
980pub struct PendingFileOpen {
981    /// Path to the file
982    pub path: PathBuf,
983    /// Line number to navigate to (1-indexed, optional)
984    pub line: Option<usize>,
985    /// Column number to navigate to (1-indexed, optional)
986    pub column: Option<usize>,
987    /// End line for range selection (1-indexed, optional)
988    pub end_line: Option<usize>,
989    /// End column for range selection (1-indexed, optional)
990    pub end_column: Option<usize>,
991    /// Hover popup message to show after opening (optional)
992    pub message: Option<String>,
993    /// Wait ID for --wait tracking (if the CLI is blocking until done)
994    pub wait_id: Option<u64>,
995}
996
997impl Editor {
998    /// Load an ANSI background image from a user-provided path
999    fn load_ansi_background(&mut self, input: &str) -> AnyhowResult<()> {
1000        let trimmed = input.trim();
1001
1002        if trimmed.is_empty() {
1003            self.ansi_background = None;
1004            self.ansi_background_path = None;
1005            self.set_status_message(t!("status.background_cleared").to_string());
1006            return Ok(());
1007        }
1008
1009        let input_path = Path::new(trimmed);
1010        let resolved = if input_path.is_absolute() {
1011            input_path.to_path_buf()
1012        } else {
1013            self.working_dir.join(input_path)
1014        };
1015
1016        let canonical = resolved.canonicalize().unwrap_or_else(|_| resolved.clone());
1017
1018        let parsed = crate::primitives::ansi_background::AnsiBackground::from_file(&canonical)?;
1019
1020        self.ansi_background = Some(parsed);
1021        self.ansi_background_path = Some(canonical.clone());
1022        self.set_status_message(
1023            t!(
1024                "view.background_set",
1025                path = canonical.display().to_string()
1026            )
1027            .to_string(),
1028        );
1029
1030        Ok(())
1031    }
1032
1033    /// Total number of open buffers across the workspace. Test
1034    /// support for `EditorTestApi::buffer_count` (Phase 7 of the
1035    /// scenario migration).
1036    #[doc(hidden)]
1037    pub fn buffer_count_for_tests(&self) -> usize {
1038        self.windows
1039            .get(&self.active_window)
1040            .map(|w| &w.buffers)
1041            .expect("active window present")
1042            .len()
1043    }
1044
1045    /// Buffer IDs in stable order (sorted by inner value). Used by
1046    /// `EditorTestApi::buffer_paths` so workspace assertions don't
1047    /// depend on `HashMap` iteration order.
1048    #[doc(hidden)]
1049    pub fn all_buffer_ids_for_tests(&self) -> Vec<BufferId> {
1050        let mut ids: Vec<BufferId> = self
1051            .windows
1052            .get(&self.active_window)
1053            .map(|w| &w.buffers)
1054            .expect("active window present")
1055            .ids();
1056        ids.sort_by_key(|id| id.0);
1057        ids
1058    }
1059
1060    /// Get the currently active buffer state
1061    pub fn active_state(&self) -> &EditorState {
1062        self.windows
1063            .get(&self.active_window)
1064            .map(|w| &w.buffers)
1065            .expect("active window present")
1066            .get(&self.active_buffer())
1067            .unwrap()
1068    }
1069
1070    /// Get the currently active buffer state (mutable)
1071    pub fn active_state_mut(&mut self) -> &mut EditorState {
1072        let __buffer_id = self.active_buffer();
1073        self.windows
1074            .get_mut(&self.active_window)
1075            .map(|w| &mut w.buffers)
1076            .expect("active window present")
1077            .get_mut(&__buffer_id)
1078            .unwrap()
1079    }
1080
1081    /// Get the cursors for the active buffer in the active split.
1082    /// Uses `effective_active_split` so focused buffer-group panels return
1083    /// their own cursors (not the outer split's stale ones).
1084    pub fn active_cursors(&self) -> &Cursors {
1085        let split_id = self.effective_active_split();
1086        &self
1087            .windows
1088            .get(&self.active_window)
1089            .and_then(|w| w.buffers.splits())
1090            .map(|(_, vs)| vs)
1091            .expect("active window must have a populated split layout")
1092            .get(&split_id)
1093            .unwrap()
1094            .cursors
1095    }
1096
1097    /// Get the cursors for the active buffer in the active split (mutable)
1098    pub fn active_cursors_mut(&mut self) -> &mut Cursors {
1099        let split_id = self.effective_active_split();
1100        &mut self
1101            .windows
1102            .get_mut(&self.active_window)
1103            .and_then(|w| w.split_view_states_mut())
1104            .expect("active window must have a populated split layout")
1105            .get_mut(&split_id)
1106            .unwrap()
1107            .cursors
1108    }
1109
1110    /// Set completion items for type-to-filter (for testing)
1111    pub fn set_completion_items(&mut self, items: Vec<lsp_types::CompletionItem>) {
1112        self.active_window_mut().completion_items = Some(items);
1113    }
1114
1115    /// Get the viewport for the active split
1116    pub fn active_viewport(&self) -> &crate::view::viewport::Viewport {
1117        let active_split = self
1118            .windows
1119            .get(&self.active_window)
1120            .and_then(|w| w.buffers.splits())
1121            .map(|(mgr, _)| mgr)
1122            .expect("active window must have a populated split layout")
1123            .active_split();
1124        &self
1125            .windows
1126            .get(&self.active_window)
1127            .and_then(|w| w.buffers.splits())
1128            .map(|(_, vs)| vs)
1129            .expect("active window must have a populated split layout")
1130            .get(&active_split)
1131            .unwrap()
1132            .viewport
1133    }
1134
1135    /// Get the viewport for the active split (mutable)
1136    pub fn active_viewport_mut(&mut self) -> &mut crate::view::viewport::Viewport {
1137        let active_split = self
1138            .windows
1139            .get(&self.active_window)
1140            .and_then(|w| w.buffers.splits())
1141            .map(|(mgr, _)| mgr)
1142            .expect("active window must have a populated split layout")
1143            .active_split();
1144        &mut self
1145            .windows
1146            .get_mut(&self.active_window)
1147            .and_then(|w| w.split_view_states_mut())
1148            .expect("active window must have a populated split layout")
1149            .get_mut(&active_split)
1150            .unwrap()
1151            .viewport
1152    }
1153
1154    /// Get the display name for a buffer (filename or virtual buffer name)
1155    pub fn get_buffer_display_name(&self, buffer_id: BufferId) -> String {
1156        // Check composite buffers first
1157        if let Some(composite) = self.active_window().composite_buffers.get(&buffer_id) {
1158            return composite.name.clone();
1159        }
1160
1161        self.active_window()
1162            .buffer_metadata
1163            .get(&buffer_id)
1164            .map(|m| m.display_name.clone())
1165            .or_else(|| {
1166                self.windows
1167                    .get(&self.active_window)
1168                    .map(|w| &w.buffers)
1169                    .expect("active window present")
1170                    .get(&buffer_id)
1171                    .and_then(|state| {
1172                        state
1173                            .buffer
1174                            .file_path()
1175                            .and_then(|p| p.file_name())
1176                            .and_then(|n| n.to_str())
1177                            .map(|s| s.to_string())
1178                    })
1179            })
1180            .unwrap_or_else(|| "[No Name]".to_string())
1181    }
1182
1183    /// Apply an event to the active buffer with all cross-cutting concerns.
1184    /// This is the centralized method that automatically handles:
1185    /// - Event application to buffer
1186    /// - Plugin hooks (after-insert, after-delete, etc.)
1187    /// - LSP notifications
1188    /// - Any other cross-cutting concerns
1189    ///
1190
1191    /// Get the event log for the active buffer
1192    pub fn active_event_log(&self) -> &EventLog {
1193        self.active_window()
1194            .event_logs
1195            .get(&self.active_buffer())
1196            .unwrap()
1197    }
1198
1199    /// Get the event log for the active buffer (mutable)
1200    pub fn active_event_log_mut(&mut self) -> &mut EventLog {
1201        let buffer_id = self.active_buffer();
1202        self.active_window_mut()
1203            .event_logs
1204            .get_mut(&buffer_id)
1205            .unwrap()
1206    }
1207
1208    /// Register a status-bar token contributed by a plugin.
1209    /// Token key is "plugin_name:token_name".
1210    /// Returns Err if token already registered or inputs are invalid.
1211    pub fn register_status_bar_element(
1212        &self,
1213        plugin_name: &str,
1214        token_name: &str,
1215        title: &str,
1216    ) -> Result<(), String> {
1217        if plugin_name.is_empty() {
1218            return Err("Plugin name cannot be empty".to_string());
1219        }
1220        if token_name.is_empty() {
1221            return Err("Token name cannot be empty".to_string());
1222        }
1223
1224        let key = format!("{}:{}", plugin_name, token_name);
1225        let mut registry = self.status_bar_token_registry.lock().unwrap();
1226
1227        if registry.contains_key(&key) {
1228            return Err(format!("Token '{}' already registered", key));
1229        }
1230
1231        registry.insert(key, title.to_string());
1232        Ok(())
1233    }
1234
1235    /// Set the value for a status-bar token on a specific buffer.
1236    /// Key format: "plugin_name:token_name". The buffer is located across
1237    /// every window — buffers are uniquely owned, so at most one window
1238    /// matches.
1239    pub fn set_status_bar_value(
1240        &mut self,
1241        buffer_id: BufferId,
1242        key: &str,
1243        value: String,
1244    ) -> Result<(), String> {
1245        for window in self.windows.values_mut() {
1246            if window.buffers.contains_key(&buffer_id) {
1247                window
1248                    .status_bar_values
1249                    .entry(buffer_id)
1250                    .or_default()
1251                    .insert(key.to_string(), value);
1252                return Ok(());
1253            }
1254        }
1255        Err(format!("Buffer {:?} not found", buffer_id))
1256    }
1257
1258    /// Get all registered status bar tokens for Settings UI.
1259    /// Returns Vec of (token_key_with_braces, title).
1260    pub fn get_status_bar_elements(&self) -> Vec<(String, String)> {
1261        self.status_bar_token_registry
1262            .lock()
1263            .unwrap()
1264            .iter()
1265            .map(|(k, title)| (format!("{{{}}}", k), title.clone()))
1266            .collect()
1267    }
1268
1269    /// Snapshot of token values for a specific buffer (render path).
1270    pub fn get_status_bar_element_values(&self, buffer_id: BufferId) -> HashMap<String, String> {
1271        for window in self.windows.values() {
1272            if let Some(values) = window.status_bar_values.get(&buffer_id) {
1273                return values.clone();
1274            }
1275        }
1276        HashMap::new()
1277    }
1278
1279    /// Current value of a single status-bar token for the given buffer, if any.
1280    /// Used by the plugin command dispatcher to detect no-op `SetStatusBarValue`
1281    /// commands (i.e. plugins re-publishing an unchanged value on every render).
1282    pub fn current_status_bar_value(&self, buffer_id: BufferId, key: &str) -> Option<&str> {
1283        for window in self.windows.values() {
1284            if let Some(values) = window.status_bar_values.get(&buffer_id) {
1285                if let Some(v) = values.get(key) {
1286                    return Some(v.as_str());
1287                }
1288                return None;
1289            }
1290        }
1291        None
1292    }
1293
1294    /// Remove every registry entry and per-buffer value belonging to a plugin.
1295    /// Called when a plugin is unloaded.
1296    fn remove_plugin_status_bar_elements(&mut self, plugin_name: &str) {
1297        let prefix = format!("{}:", plugin_name);
1298        self.status_bar_token_registry
1299            .lock()
1300            .unwrap()
1301            .retain(|k, _| !k.starts_with(&prefix));
1302        for window in self.windows.values_mut() {
1303            for values in window.status_bar_values.values_mut() {
1304                values.retain(|k, _| !k.starts_with(&prefix));
1305            }
1306        }
1307    }
1308}
1309
1310/// Parse a key string like "RET", "C-n", "M-x", "q" into KeyCode and KeyModifiers
1311///
1312/// Supports:
1313/// - Single characters: "a", "q", etc.
1314/// - Function keys: "F1", "F2", etc.
1315/// - Special keys: "RET", "TAB", "ESC", "SPC", "DEL", "BS"
1316/// - Modifiers: "C-" (Control), "M-" (Alt/Meta), "S-" (Shift)
1317/// - Combinations: "C-n", "M-x", "C-M-s", etc.
1318fn parse_key_string(key_str: &str) -> Option<(KeyCode, KeyModifiers)> {
1319    use crossterm::event::{KeyCode, KeyModifiers};
1320
1321    let mut modifiers = KeyModifiers::NONE;
1322    let mut remaining = key_str;
1323
1324    // Parse modifiers
1325    loop {
1326        if remaining.starts_with("C-") {
1327            modifiers |= KeyModifiers::CONTROL;
1328            remaining = &remaining[2..];
1329        } else if remaining.starts_with("M-") {
1330            modifiers |= KeyModifiers::ALT;
1331            remaining = &remaining[2..];
1332        } else if remaining.starts_with("S-") {
1333            modifiers |= KeyModifiers::SHIFT;
1334            remaining = &remaining[2..];
1335        } else {
1336            break;
1337        }
1338    }
1339
1340    // Parse the key
1341    // Use uppercase for matching special keys, but preserve original for single chars
1342    let upper = remaining.to_uppercase();
1343    let code = match upper.as_str() {
1344        "RET" | "RETURN" | "ENTER" => KeyCode::Enter,
1345        "TAB" => KeyCode::Tab,
1346        "BACKTAB" => KeyCode::BackTab,
1347        "ESC" | "ESCAPE" => KeyCode::Esc,
1348        "SPC" | "SPACE" => KeyCode::Char(' '),
1349        "DEL" | "DELETE" => KeyCode::Delete,
1350        "BS" | "BACKSPACE" => KeyCode::Backspace,
1351        "UP" => KeyCode::Up,
1352        "DOWN" => KeyCode::Down,
1353        "LEFT" => KeyCode::Left,
1354        "RIGHT" => KeyCode::Right,
1355        "HOME" => KeyCode::Home,
1356        "END" => KeyCode::End,
1357        "PAGEUP" | "PGUP" => KeyCode::PageUp,
1358        "PAGEDOWN" | "PGDN" => KeyCode::PageDown,
1359        s if s.starts_with('F') && s.len() > 1 => {
1360            // Function key (F1-F12)
1361            if let Ok(n) = s[1..].parse::<u8>() {
1362                KeyCode::F(n)
1363            } else {
1364                return None;
1365            }
1366        }
1367        _ if remaining.len() == 1 => {
1368            // Single character - use ORIGINAL remaining, not uppercased
1369            // For uppercase letters, add SHIFT modifier so 'J' != 'j'
1370            let c = remaining.chars().next()?;
1371            if c.is_ascii_uppercase() {
1372                modifiers |= KeyModifiers::SHIFT;
1373            }
1374            KeyCode::Char(c.to_ascii_lowercase())
1375        }
1376        _ => return None,
1377    };
1378
1379    // Plugins commonly spell Shift+Tab as "S-Tab"; terminals deliver
1380    // BackTab and the lookup-side `normalize_key` strips the redundant
1381    // SHIFT. Normalize on the binding side too so "S-Tab" and "BackTab"
1382    // both register as `(BackTab, NONE)` and match.
1383    if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) {
1384        return Some((KeyCode::BackTab, modifiers.difference(KeyModifiers::SHIFT)));
1385    }
1386
1387    Some((code, modifiers))
1388}
1389
1390#[cfg(test)]
1391mod tests {
1392    use super::*;
1393    use lsp_types::{Position, Range as LspRange, TextDocumentContentChangeEvent};
1394    use tempfile::TempDir;
1395
1396    /// Create a test DirectoryContext with temp directories
1397    fn test_dir_context() -> (DirectoryContext, TempDir) {
1398        let temp_dir = TempDir::new().unwrap();
1399        let dir_context = DirectoryContext::for_testing(temp_dir.path());
1400        (dir_context, temp_dir)
1401    }
1402
1403    /// Create a test filesystem
1404    fn test_filesystem() -> Arc<dyn FileSystem + Send + Sync> {
1405        Arc::new(crate::model::filesystem::StdFileSystem)
1406    }
1407
1408    #[test]
1409    fn parse_key_string_shift_tab_normalizes_to_backtab() {
1410        use crossterm::event::{KeyCode, KeyModifiers};
1411        // Plugins write "S-Tab" in their defineMode binding tables; the
1412        // terminal delivers BackTab (with SHIFT stripped by normalize_key
1413        // on lookup). Without this normalization, the binding never
1414        // matches.
1415        assert_eq!(
1416            parse_key_string("S-Tab"),
1417            Some((KeyCode::BackTab, KeyModifiers::NONE)),
1418        );
1419        assert_eq!(
1420            parse_key_string("BackTab"),
1421            Some((KeyCode::BackTab, KeyModifiers::NONE)),
1422        );
1423        // Plain Tab is unaffected.
1424        assert_eq!(
1425            parse_key_string("Tab"),
1426            Some((KeyCode::Tab, KeyModifiers::NONE)),
1427        );
1428    }
1429
1430    #[test]
1431    fn test_editor_new() {
1432        let config = Config::default();
1433        let (dir_context, _temp) = test_dir_context();
1434        let editor = Editor::new(
1435            config,
1436            80,
1437            24,
1438            dir_context,
1439            crate::view::color_support::ColorCapability::TrueColor,
1440            test_filesystem(),
1441        )
1442        .unwrap();
1443
1444        assert_eq!(editor.buffers().len(), 1);
1445        assert!(!editor.should_quit());
1446    }
1447
1448    #[test]
1449    fn test_new_buffer() {
1450        let config = Config::default();
1451        let (dir_context, _temp) = test_dir_context();
1452        let mut editor = Editor::new(
1453            config,
1454            80,
1455            24,
1456            dir_context,
1457            crate::view::color_support::ColorCapability::TrueColor,
1458            test_filesystem(),
1459        )
1460        .unwrap();
1461
1462        let id = editor.new_buffer();
1463        assert_eq!(editor.buffers().len(), 2);
1464        assert_eq!(editor.active_buffer(), id);
1465    }
1466
1467    #[test]
1468    #[ignore]
1469    fn test_clipboard() {
1470        let config = Config::default();
1471        let (dir_context, _temp) = test_dir_context();
1472        let mut editor = Editor::new(
1473            config,
1474            80,
1475            24,
1476            dir_context,
1477            crate::view::color_support::ColorCapability::TrueColor,
1478            test_filesystem(),
1479        )
1480        .unwrap();
1481
1482        // Manually set clipboard (using internal to avoid system clipboard in tests)
1483        editor.clipboard.set_internal("test".to_string());
1484
1485        // Paste should work
1486        editor.paste();
1487
1488        let content = editor.active_state().buffer.to_string().unwrap();
1489        assert_eq!(content, "test");
1490    }
1491
1492    #[test]
1493    fn test_action_to_events_insert_char() {
1494        let config = Config::default();
1495        let (dir_context, _temp) = test_dir_context();
1496        let mut editor = Editor::new(
1497            config,
1498            80,
1499            24,
1500            dir_context,
1501            crate::view::color_support::ColorCapability::TrueColor,
1502            test_filesystem(),
1503        )
1504        .unwrap();
1505
1506        let events = editor
1507            .active_window_mut()
1508            .action_to_events(Action::InsertChar('a'));
1509        assert!(events.is_some());
1510
1511        let events = events.unwrap();
1512        assert_eq!(events.len(), 1);
1513
1514        match &events[0] {
1515            Event::Insert { position, text, .. } => {
1516                assert_eq!(*position, 0);
1517                assert_eq!(text, "a");
1518            }
1519            _ => panic!("Expected Insert event"),
1520        }
1521    }
1522
1523    #[test]
1524    fn test_action_to_events_move_right() {
1525        let config = Config::default();
1526        let (dir_context, _temp) = test_dir_context();
1527        let mut editor = Editor::new(
1528            config,
1529            80,
1530            24,
1531            dir_context,
1532            crate::view::color_support::ColorCapability::TrueColor,
1533            test_filesystem(),
1534        )
1535        .unwrap();
1536
1537        // Insert some text first
1538        let cursor_id = editor.active_cursors().primary_id();
1539        editor.apply_event_to_active_buffer(&Event::Insert {
1540            position: 0,
1541            text: "hello".to_string(),
1542            cursor_id,
1543        });
1544
1545        let events = editor
1546            .active_window_mut()
1547            .action_to_events(Action::MoveRight);
1548        assert!(events.is_some());
1549
1550        let events = events.unwrap();
1551        assert_eq!(events.len(), 1);
1552
1553        match &events[0] {
1554            Event::MoveCursor {
1555                new_position,
1556                new_anchor,
1557                ..
1558            } => {
1559                // Cursor was at 5 (end of "hello"), stays at 5 (can't move beyond end)
1560                assert_eq!(*new_position, 5);
1561                assert_eq!(*new_anchor, None); // No selection
1562            }
1563            _ => panic!("Expected MoveCursor event"),
1564        }
1565    }
1566
1567    #[test]
1568    fn test_action_to_events_move_up_down() {
1569        let config = Config::default();
1570        let (dir_context, _temp) = test_dir_context();
1571        let mut editor = Editor::new(
1572            config,
1573            80,
1574            24,
1575            dir_context,
1576            crate::view::color_support::ColorCapability::TrueColor,
1577            test_filesystem(),
1578        )
1579        .unwrap();
1580
1581        // Insert multi-line text
1582        let cursor_id = editor.active_cursors().primary_id();
1583        editor.apply_event_to_active_buffer(&Event::Insert {
1584            position: 0,
1585            text: "line1\nline2\nline3".to_string(),
1586            cursor_id,
1587        });
1588
1589        // Move cursor to start of line 2
1590        editor.apply_event_to_active_buffer(&Event::MoveCursor {
1591            cursor_id,
1592            old_position: 0, // TODO: Get actual old position
1593            new_position: 6,
1594            old_anchor: None, // TODO: Get actual old anchor
1595            new_anchor: None,
1596            old_sticky_column: 0,
1597            new_sticky_column: 0,
1598        });
1599
1600        // Test move up
1601        let events = editor.active_window_mut().action_to_events(Action::MoveUp);
1602        assert!(events.is_some());
1603        let events = events.unwrap();
1604        assert_eq!(events.len(), 1);
1605
1606        match &events[0] {
1607            Event::MoveCursor { new_position, .. } => {
1608                assert_eq!(*new_position, 0); // Should be at start of line 1
1609            }
1610            _ => panic!("Expected MoveCursor event"),
1611        }
1612    }
1613
1614    #[test]
1615    fn test_action_to_events_insert_newline() {
1616        let config = Config::default();
1617        let (dir_context, _temp) = test_dir_context();
1618        let mut editor = Editor::new(
1619            config,
1620            80,
1621            24,
1622            dir_context,
1623            crate::view::color_support::ColorCapability::TrueColor,
1624            test_filesystem(),
1625        )
1626        .unwrap();
1627
1628        let events = editor
1629            .active_window_mut()
1630            .action_to_events(Action::InsertNewline);
1631        assert!(events.is_some());
1632
1633        let events = events.unwrap();
1634        assert_eq!(events.len(), 1);
1635
1636        match &events[0] {
1637            Event::Insert { text, .. } => {
1638                assert_eq!(text, "\n");
1639            }
1640            _ => panic!("Expected Insert event"),
1641        }
1642    }
1643
1644    #[test]
1645    fn test_action_to_events_unimplemented() {
1646        let config = Config::default();
1647        let (dir_context, _temp) = test_dir_context();
1648        let mut editor = Editor::new(
1649            config,
1650            80,
1651            24,
1652            dir_context,
1653            crate::view::color_support::ColorCapability::TrueColor,
1654            test_filesystem(),
1655        )
1656        .unwrap();
1657
1658        // These actions should return None (not yet implemented)
1659        assert!(editor
1660            .active_window_mut()
1661            .action_to_events(Action::Save)
1662            .is_none());
1663        assert!(editor
1664            .active_window_mut()
1665            .action_to_events(Action::Quit)
1666            .is_none());
1667        assert!(editor
1668            .active_window_mut()
1669            .action_to_events(Action::Undo)
1670            .is_none());
1671    }
1672
1673    #[test]
1674    fn test_action_to_events_delete_backward() {
1675        let config = Config::default();
1676        let (dir_context, _temp) = test_dir_context();
1677        let mut editor = Editor::new(
1678            config,
1679            80,
1680            24,
1681            dir_context,
1682            crate::view::color_support::ColorCapability::TrueColor,
1683            test_filesystem(),
1684        )
1685        .unwrap();
1686
1687        // Insert some text first
1688        let cursor_id = editor.active_cursors().primary_id();
1689        editor.apply_event_to_active_buffer(&Event::Insert {
1690            position: 0,
1691            text: "hello".to_string(),
1692            cursor_id,
1693        });
1694
1695        let events = editor
1696            .active_window_mut()
1697            .action_to_events(Action::DeleteBackward);
1698        assert!(events.is_some());
1699
1700        let events = events.unwrap();
1701        assert_eq!(events.len(), 1);
1702
1703        match &events[0] {
1704            Event::Delete {
1705                range,
1706                deleted_text,
1707                ..
1708            } => {
1709                assert_eq!(range.clone(), 4..5); // Delete 'o'
1710                assert_eq!(deleted_text, "o");
1711            }
1712            _ => panic!("Expected Delete event"),
1713        }
1714    }
1715
1716    #[test]
1717    fn test_action_to_events_delete_forward() {
1718        let config = Config::default();
1719        let (dir_context, _temp) = test_dir_context();
1720        let mut editor = Editor::new(
1721            config,
1722            80,
1723            24,
1724            dir_context,
1725            crate::view::color_support::ColorCapability::TrueColor,
1726            test_filesystem(),
1727        )
1728        .unwrap();
1729
1730        // Insert some text first
1731        let cursor_id = editor.active_cursors().primary_id();
1732        editor.apply_event_to_active_buffer(&Event::Insert {
1733            position: 0,
1734            text: "hello".to_string(),
1735            cursor_id,
1736        });
1737
1738        // Move cursor to position 0
1739        editor.apply_event_to_active_buffer(&Event::MoveCursor {
1740            cursor_id,
1741            old_position: 0, // TODO: Get actual old position
1742            new_position: 0,
1743            old_anchor: None, // TODO: Get actual old anchor
1744            new_anchor: None,
1745            old_sticky_column: 0,
1746            new_sticky_column: 0,
1747        });
1748
1749        let events = editor
1750            .active_window_mut()
1751            .action_to_events(Action::DeleteForward);
1752        assert!(events.is_some());
1753
1754        let events = events.unwrap();
1755        assert_eq!(events.len(), 1);
1756
1757        match &events[0] {
1758            Event::Delete {
1759                range,
1760                deleted_text,
1761                ..
1762            } => {
1763                assert_eq!(range.clone(), 0..1); // Delete 'h'
1764                assert_eq!(deleted_text, "h");
1765            }
1766            _ => panic!("Expected Delete event"),
1767        }
1768    }
1769
1770    #[test]
1771    fn test_action_to_events_select_right() {
1772        let config = Config::default();
1773        let (dir_context, _temp) = test_dir_context();
1774        let mut editor = Editor::new(
1775            config,
1776            80,
1777            24,
1778            dir_context,
1779            crate::view::color_support::ColorCapability::TrueColor,
1780            test_filesystem(),
1781        )
1782        .unwrap();
1783
1784        // Insert some text first
1785        let cursor_id = editor.active_cursors().primary_id();
1786        editor.apply_event_to_active_buffer(&Event::Insert {
1787            position: 0,
1788            text: "hello".to_string(),
1789            cursor_id,
1790        });
1791
1792        // Move cursor to position 0
1793        editor.apply_event_to_active_buffer(&Event::MoveCursor {
1794            cursor_id,
1795            old_position: 0, // TODO: Get actual old position
1796            new_position: 0,
1797            old_anchor: None, // TODO: Get actual old anchor
1798            new_anchor: None,
1799            old_sticky_column: 0,
1800            new_sticky_column: 0,
1801        });
1802
1803        let events = editor
1804            .active_window_mut()
1805            .action_to_events(Action::SelectRight);
1806        assert!(events.is_some());
1807
1808        let events = events.unwrap();
1809        assert_eq!(events.len(), 1);
1810
1811        match &events[0] {
1812            Event::MoveCursor {
1813                new_position,
1814                new_anchor,
1815                ..
1816            } => {
1817                assert_eq!(*new_position, 1); // Moved to position 1
1818                assert_eq!(*new_anchor, Some(0)); // Anchor at start
1819            }
1820            _ => panic!("Expected MoveCursor event"),
1821        }
1822    }
1823
1824    #[test]
1825    fn test_action_to_events_select_all() {
1826        let config = Config::default();
1827        let (dir_context, _temp) = test_dir_context();
1828        let mut editor = Editor::new(
1829            config,
1830            80,
1831            24,
1832            dir_context,
1833            crate::view::color_support::ColorCapability::TrueColor,
1834            test_filesystem(),
1835        )
1836        .unwrap();
1837
1838        // Insert some text first
1839        let cursor_id = editor.active_cursors().primary_id();
1840        editor.apply_event_to_active_buffer(&Event::Insert {
1841            position: 0,
1842            text: "hello world".to_string(),
1843            cursor_id,
1844        });
1845
1846        let events = editor
1847            .active_window_mut()
1848            .action_to_events(Action::SelectAll);
1849        assert!(events.is_some());
1850
1851        let events = events.unwrap();
1852        assert_eq!(events.len(), 1);
1853
1854        match &events[0] {
1855            Event::MoveCursor {
1856                new_position,
1857                new_anchor,
1858                ..
1859            } => {
1860                assert_eq!(*new_position, 11); // At end of buffer
1861                assert_eq!(*new_anchor, Some(0)); // Anchor at start
1862            }
1863            _ => panic!("Expected MoveCursor event"),
1864        }
1865    }
1866
1867    #[test]
1868    fn test_action_to_events_document_nav() {
1869        let config = Config::default();
1870        let (dir_context, _temp) = test_dir_context();
1871        let mut editor = Editor::new(
1872            config,
1873            80,
1874            24,
1875            dir_context,
1876            crate::view::color_support::ColorCapability::TrueColor,
1877            test_filesystem(),
1878        )
1879        .unwrap();
1880
1881        // Insert multi-line text
1882        let cursor_id = editor.active_cursors().primary_id();
1883        editor.apply_event_to_active_buffer(&Event::Insert {
1884            position: 0,
1885            text: "line1\nline2\nline3".to_string(),
1886            cursor_id,
1887        });
1888
1889        // Test MoveDocumentStart
1890        let events = editor
1891            .active_window_mut()
1892            .action_to_events(Action::MoveDocumentStart);
1893        assert!(events.is_some());
1894        let events = events.unwrap();
1895        match &events[0] {
1896            Event::MoveCursor { new_position, .. } => {
1897                assert_eq!(*new_position, 0);
1898            }
1899            _ => panic!("Expected MoveCursor event"),
1900        }
1901
1902        // Test MoveDocumentEnd
1903        let events = editor
1904            .active_window_mut()
1905            .action_to_events(Action::MoveDocumentEnd);
1906        assert!(events.is_some());
1907        let events = events.unwrap();
1908        match &events[0] {
1909            Event::MoveCursor { new_position, .. } => {
1910                assert_eq!(*new_position, 17); // End of buffer
1911            }
1912            _ => panic!("Expected MoveCursor event"),
1913        }
1914    }
1915
1916    #[test]
1917    fn test_action_to_events_remove_secondary_cursors() {
1918        use crate::model::event::CursorId;
1919
1920        let config = Config::default();
1921        let (dir_context, _temp) = test_dir_context();
1922        let mut editor = Editor::new(
1923            config,
1924            80,
1925            24,
1926            dir_context,
1927            crate::view::color_support::ColorCapability::TrueColor,
1928            test_filesystem(),
1929        )
1930        .unwrap();
1931
1932        // Insert some text first to have positions to place cursors
1933        let cursor_id = editor.active_cursors().primary_id();
1934        editor.apply_event_to_active_buffer(&Event::Insert {
1935            position: 0,
1936            text: "hello world test".to_string(),
1937            cursor_id,
1938        });
1939
1940        // Add secondary cursors at different positions to avoid normalization merging
1941        editor.apply_event_to_active_buffer(&Event::AddCursor {
1942            cursor_id: CursorId(1),
1943            position: 5,
1944            anchor: None,
1945        });
1946        editor.apply_event_to_active_buffer(&Event::AddCursor {
1947            cursor_id: CursorId(2),
1948            position: 10,
1949            anchor: None,
1950        });
1951
1952        assert_eq!(editor.active_cursors().count(), 3);
1953
1954        // Find the first cursor ID (the one that will be kept)
1955        let first_id = editor
1956            .active_cursors()
1957            .iter()
1958            .map(|(id, _)| id)
1959            .min_by_key(|id| id.0)
1960            .expect("Should have at least one cursor");
1961
1962        // RemoveSecondaryCursors should generate RemoveCursor events
1963        let events = editor
1964            .active_window_mut()
1965            .action_to_events(Action::RemoveSecondaryCursors);
1966        assert!(events.is_some());
1967
1968        let events = events.unwrap();
1969        // Should have RemoveCursor events for the two secondary cursors
1970        // Plus ClearAnchor events for all cursors (to clear Emacs mark mode)
1971        let remove_cursor_events: Vec<_> = events
1972            .iter()
1973            .filter_map(|e| match e {
1974                Event::RemoveCursor { cursor_id, .. } => Some(*cursor_id),
1975                _ => None,
1976            })
1977            .collect();
1978
1979        // Should have 2 RemoveCursor events (one for each secondary cursor)
1980        assert_eq!(remove_cursor_events.len(), 2);
1981
1982        for cursor_id in &remove_cursor_events {
1983            // Should not be the first cursor (the one we're keeping)
1984            assert_ne!(*cursor_id, first_id);
1985        }
1986    }
1987
1988    #[test]
1989    fn test_action_to_events_scroll() {
1990        let config = Config::default();
1991        let (dir_context, _temp) = test_dir_context();
1992        let mut editor = Editor::new(
1993            config,
1994            80,
1995            24,
1996            dir_context,
1997            crate::view::color_support::ColorCapability::TrueColor,
1998            test_filesystem(),
1999        )
2000        .unwrap();
2001
2002        // Test ScrollUp
2003        let events = editor
2004            .active_window_mut()
2005            .action_to_events(Action::ScrollUp);
2006        assert!(events.is_some());
2007        let events = events.unwrap();
2008        assert_eq!(events.len(), 1);
2009        match &events[0] {
2010            Event::Scroll { line_offset } => {
2011                assert_eq!(*line_offset, -1);
2012            }
2013            _ => panic!("Expected Scroll event"),
2014        }
2015
2016        // Test ScrollDown
2017        let events = editor
2018            .active_window_mut()
2019            .action_to_events(Action::ScrollDown);
2020        assert!(events.is_some());
2021        let events = events.unwrap();
2022        assert_eq!(events.len(), 1);
2023        match &events[0] {
2024            Event::Scroll { line_offset } => {
2025                assert_eq!(*line_offset, 1);
2026            }
2027            _ => panic!("Expected Scroll event"),
2028        }
2029    }
2030
2031    #[test]
2032    fn test_action_to_events_none() {
2033        let config = Config::default();
2034        let (dir_context, _temp) = test_dir_context();
2035        let mut editor = Editor::new(
2036            config,
2037            80,
2038            24,
2039            dir_context,
2040            crate::view::color_support::ColorCapability::TrueColor,
2041            test_filesystem(),
2042        )
2043        .unwrap();
2044
2045        // None action should return None
2046        let events = editor.active_window_mut().action_to_events(Action::None);
2047        assert!(events.is_none());
2048    }
2049
2050    #[test]
2051    fn test_lsp_incremental_insert_generates_correct_range() {
2052        // Test that insert events generate correct incremental LSP changes
2053        // with zero-width ranges at the insertion point
2054        use crate::model::buffer::Buffer;
2055
2056        let buffer = Buffer::from_str_test("hello\nworld");
2057
2058        // Insert "NEW" at position 0 (before "hello")
2059        // Expected LSP range: line 0, char 0 to line 0, char 0 (zero-width)
2060        let position = 0;
2061        let (line, character) = buffer.position_to_lsp_position(position);
2062
2063        assert_eq!(line, 0, "Insertion at start should be line 0");
2064        assert_eq!(character, 0, "Insertion at start should be char 0");
2065
2066        // Create the range as we do in notify_lsp_change
2067        let lsp_pos = Position::new(line as u32, character as u32);
2068        let lsp_range = LspRange::new(lsp_pos, lsp_pos);
2069
2070        assert_eq!(lsp_range.start.line, 0);
2071        assert_eq!(lsp_range.start.character, 0);
2072        assert_eq!(lsp_range.end.line, 0);
2073        assert_eq!(lsp_range.end.character, 0);
2074        assert_eq!(
2075            lsp_range.start, lsp_range.end,
2076            "Insert should have zero-width range"
2077        );
2078
2079        // Test insertion at middle of first line (position 3, after "hel")
2080        let position = 3;
2081        let (line, character) = buffer.position_to_lsp_position(position);
2082
2083        assert_eq!(line, 0);
2084        assert_eq!(character, 3);
2085
2086        // Test insertion at start of second line (position 6, after "hello\n")
2087        let position = 6;
2088        let (line, character) = buffer.position_to_lsp_position(position);
2089
2090        assert_eq!(line, 1, "Position after newline should be line 1");
2091        assert_eq!(character, 0, "Position at start of line 2 should be char 0");
2092    }
2093
2094    #[test]
2095    fn test_lsp_incremental_delete_generates_correct_range() {
2096        // Test that delete events generate correct incremental LSP changes
2097        // with proper start/end ranges
2098        use crate::model::buffer::Buffer;
2099
2100        let buffer = Buffer::from_str_test("hello\nworld");
2101
2102        // Delete "ello" (positions 1-5 on line 0)
2103        let range_start = 1;
2104        let range_end = 5;
2105
2106        let (start_line, start_char) = buffer.position_to_lsp_position(range_start);
2107        let (end_line, end_char) = buffer.position_to_lsp_position(range_end);
2108
2109        assert_eq!(start_line, 0);
2110        assert_eq!(start_char, 1);
2111        assert_eq!(end_line, 0);
2112        assert_eq!(end_char, 5);
2113
2114        let lsp_range = LspRange::new(
2115            Position::new(start_line as u32, start_char as u32),
2116            Position::new(end_line as u32, end_char as u32),
2117        );
2118
2119        assert_eq!(lsp_range.start.line, 0);
2120        assert_eq!(lsp_range.start.character, 1);
2121        assert_eq!(lsp_range.end.line, 0);
2122        assert_eq!(lsp_range.end.character, 5);
2123        assert_ne!(
2124            lsp_range.start, lsp_range.end,
2125            "Delete should have non-zero range"
2126        );
2127
2128        // Test deletion across lines (delete "o\nw" - positions 4-8)
2129        let range_start = 4;
2130        let range_end = 8;
2131
2132        let (start_line, start_char) = buffer.position_to_lsp_position(range_start);
2133        let (end_line, end_char) = buffer.position_to_lsp_position(range_end);
2134
2135        assert_eq!(start_line, 0, "Delete start on line 0");
2136        assert_eq!(start_char, 4, "Delete start at char 4");
2137        assert_eq!(end_line, 1, "Delete end on line 1");
2138        assert_eq!(end_char, 2, "Delete end at char 2 of line 1");
2139    }
2140
2141    #[test]
2142    fn test_lsp_incremental_utf16_encoding() {
2143        // Test that position_to_lsp_position correctly handles UTF-16 encoding
2144        // LSP uses UTF-16 code units, not byte positions
2145        use crate::model::buffer::Buffer;
2146
2147        // Test with emoji (4 bytes in UTF-8, 2 code units in UTF-16)
2148        let buffer = Buffer::from_str_test("😀hello");
2149
2150        // Position 4 is after the emoji (4 bytes)
2151        let (line, character) = buffer.position_to_lsp_position(4);
2152
2153        assert_eq!(line, 0);
2154        assert_eq!(character, 2, "Emoji should count as 2 UTF-16 code units");
2155
2156        // Position 9 is after "😀hell" (4 bytes emoji + 5 bytes text)
2157        let (line, character) = buffer.position_to_lsp_position(9);
2158
2159        assert_eq!(line, 0);
2160        assert_eq!(
2161            character, 7,
2162            "Should be 2 (emoji) + 5 (text) = 7 UTF-16 code units"
2163        );
2164
2165        // Test with multi-byte character (é is 2 bytes in UTF-8, 1 code unit in UTF-16)
2166        let buffer = Buffer::from_str_test("café");
2167
2168        // Position 3 is after "caf" (3 bytes)
2169        let (line, character) = buffer.position_to_lsp_position(3);
2170
2171        assert_eq!(line, 0);
2172        assert_eq!(character, 3);
2173
2174        // Position 5 is after "café" (3 + 2 bytes)
2175        let (line, character) = buffer.position_to_lsp_position(5);
2176
2177        assert_eq!(line, 0);
2178        assert_eq!(character, 4, "é should count as 1 UTF-16 code unit");
2179    }
2180
2181    #[test]
2182    fn test_lsp_content_change_event_structure() {
2183        // Test that we can create TextDocumentContentChangeEvent for incremental updates
2184
2185        // Incremental insert
2186        let insert_change = TextDocumentContentChangeEvent {
2187            range: Some(LspRange::new(Position::new(0, 5), Position::new(0, 5))),
2188            range_length: None,
2189            text: "NEW".to_string(),
2190        };
2191
2192        assert!(insert_change.range.is_some());
2193        assert_eq!(insert_change.text, "NEW");
2194        let range = insert_change.range.unwrap();
2195        assert_eq!(
2196            range.start, range.end,
2197            "Insert should have zero-width range"
2198        );
2199
2200        // Incremental delete
2201        let delete_change = TextDocumentContentChangeEvent {
2202            range: Some(LspRange::new(Position::new(0, 2), Position::new(0, 7))),
2203            range_length: None,
2204            text: String::new(),
2205        };
2206
2207        assert!(delete_change.range.is_some());
2208        assert_eq!(delete_change.text, "");
2209        let range = delete_change.range.unwrap();
2210        assert_ne!(range.start, range.end, "Delete should have non-zero range");
2211        assert_eq!(range.start.line, 0);
2212        assert_eq!(range.start.character, 2);
2213        assert_eq!(range.end.line, 0);
2214        assert_eq!(range.end.character, 7);
2215    }
2216
2217    #[test]
2218    fn test_goto_matching_bracket_forward() {
2219        let config = Config::default();
2220        let (dir_context, _temp) = test_dir_context();
2221        let mut editor = Editor::new(
2222            config,
2223            80,
2224            24,
2225            dir_context,
2226            crate::view::color_support::ColorCapability::TrueColor,
2227            test_filesystem(),
2228        )
2229        .unwrap();
2230
2231        // Insert text with brackets
2232        let cursor_id = editor.active_cursors().primary_id();
2233        editor.apply_event_to_active_buffer(&Event::Insert {
2234            position: 0,
2235            text: "fn main() { let x = (1 + 2); }".to_string(),
2236            cursor_id,
2237        });
2238
2239        // Move cursor to opening brace '{'
2240        editor.apply_event_to_active_buffer(&Event::MoveCursor {
2241            cursor_id,
2242            old_position: 31,
2243            new_position: 10,
2244            old_anchor: None,
2245            new_anchor: None,
2246            old_sticky_column: 0,
2247            new_sticky_column: 0,
2248        });
2249
2250        assert_eq!(editor.active_cursors().primary().position, 10);
2251
2252        // Call goto_matching_bracket
2253        editor.goto_matching_bracket();
2254
2255        // Should move to closing brace '}' at position 29
2256        // "fn main() { let x = (1 + 2); }"
2257        //            ^                   ^
2258        //           10                  29
2259        assert_eq!(editor.active_cursors().primary().position, 29);
2260    }
2261
2262    #[test]
2263    fn test_goto_matching_bracket_backward() {
2264        let config = Config::default();
2265        let (dir_context, _temp) = test_dir_context();
2266        let mut editor = Editor::new(
2267            config,
2268            80,
2269            24,
2270            dir_context,
2271            crate::view::color_support::ColorCapability::TrueColor,
2272            test_filesystem(),
2273        )
2274        .unwrap();
2275
2276        // Insert text with brackets
2277        let cursor_id = editor.active_cursors().primary_id();
2278        editor.apply_event_to_active_buffer(&Event::Insert {
2279            position: 0,
2280            text: "fn main() { let x = (1 + 2); }".to_string(),
2281            cursor_id,
2282        });
2283
2284        // Move cursor to closing paren ')'
2285        editor.apply_event_to_active_buffer(&Event::MoveCursor {
2286            cursor_id,
2287            old_position: 31,
2288            new_position: 26,
2289            old_anchor: None,
2290            new_anchor: None,
2291            old_sticky_column: 0,
2292            new_sticky_column: 0,
2293        });
2294
2295        // Call goto_matching_bracket
2296        editor.goto_matching_bracket();
2297
2298        // Should move to opening paren '('
2299        assert_eq!(editor.active_cursors().primary().position, 20);
2300    }
2301
2302    #[test]
2303    fn test_goto_matching_bracket_nested() {
2304        let config = Config::default();
2305        let (dir_context, _temp) = test_dir_context();
2306        let mut editor = Editor::new(
2307            config,
2308            80,
2309            24,
2310            dir_context,
2311            crate::view::color_support::ColorCapability::TrueColor,
2312            test_filesystem(),
2313        )
2314        .unwrap();
2315
2316        // Insert text with nested brackets
2317        let cursor_id = editor.active_cursors().primary_id();
2318        editor.apply_event_to_active_buffer(&Event::Insert {
2319            position: 0,
2320            text: "{a{b{c}d}e}".to_string(),
2321            cursor_id,
2322        });
2323
2324        // Move cursor to first '{'
2325        editor.apply_event_to_active_buffer(&Event::MoveCursor {
2326            cursor_id,
2327            old_position: 11,
2328            new_position: 0,
2329            old_anchor: None,
2330            new_anchor: None,
2331            old_sticky_column: 0,
2332            new_sticky_column: 0,
2333        });
2334
2335        // Call goto_matching_bracket
2336        editor.goto_matching_bracket();
2337
2338        // Should jump to last '}'
2339        assert_eq!(editor.active_cursors().primary().position, 10);
2340    }
2341
2342    #[test]
2343    fn test_search_case_sensitive() {
2344        let config = Config::default();
2345        let (dir_context, _temp) = test_dir_context();
2346        let mut editor = Editor::new(
2347            config,
2348            80,
2349            24,
2350            dir_context,
2351            crate::view::color_support::ColorCapability::TrueColor,
2352            test_filesystem(),
2353        )
2354        .unwrap();
2355
2356        // Insert text
2357        let cursor_id = editor.active_cursors().primary_id();
2358        editor.apply_event_to_active_buffer(&Event::Insert {
2359            position: 0,
2360            text: "Hello hello HELLO".to_string(),
2361            cursor_id,
2362        });
2363
2364        // Test case-insensitive search (default)
2365        editor.active_window_mut().search_case_sensitive = false;
2366        editor.perform_search("hello");
2367
2368        let search_state = editor.active_window().search_state.as_ref().unwrap();
2369        assert_eq!(
2370            search_state.matches.len(),
2371            3,
2372            "Should find all 3 matches case-insensitively"
2373        );
2374
2375        // Test case-sensitive search
2376        editor.active_window_mut().search_case_sensitive = true;
2377        editor.perform_search("hello");
2378
2379        let search_state = editor.active_window().search_state.as_ref().unwrap();
2380        assert_eq!(
2381            search_state.matches.len(),
2382            1,
2383            "Should find only 1 exact match"
2384        );
2385        assert_eq!(
2386            search_state.matches[0], 6,
2387            "Should find 'hello' at position 6"
2388        );
2389    }
2390
2391    #[test]
2392    fn test_search_whole_word() {
2393        let config = Config::default();
2394        let (dir_context, _temp) = test_dir_context();
2395        let mut editor = Editor::new(
2396            config,
2397            80,
2398            24,
2399            dir_context,
2400            crate::view::color_support::ColorCapability::TrueColor,
2401            test_filesystem(),
2402        )
2403        .unwrap();
2404
2405        // Insert text
2406        let cursor_id = editor.active_cursors().primary_id();
2407        editor.apply_event_to_active_buffer(&Event::Insert {
2408            position: 0,
2409            text: "test testing tested attest test".to_string(),
2410            cursor_id,
2411        });
2412
2413        // Test partial word match (default)
2414        editor.active_window_mut().search_whole_word = false;
2415        editor.active_window_mut().search_case_sensitive = true;
2416        editor.perform_search("test");
2417
2418        let search_state = editor.active_window().search_state.as_ref().unwrap();
2419        assert_eq!(
2420            search_state.matches.len(),
2421            5,
2422            "Should find 'test' in all occurrences"
2423        );
2424
2425        // Test whole word match
2426        editor.active_window_mut().search_whole_word = true;
2427        editor.perform_search("test");
2428
2429        let search_state = editor.active_window().search_state.as_ref().unwrap();
2430        assert_eq!(
2431            search_state.matches.len(),
2432            2,
2433            "Should find only whole word 'test'"
2434        );
2435        assert_eq!(search_state.matches[0], 0, "First match at position 0");
2436        assert_eq!(search_state.matches[1], 27, "Second match at position 27");
2437    }
2438
2439    #[test]
2440    fn test_search_scan_completes_when_capped() {
2441        // Regression test: when the incremental search scan hits MAX_MATCHES
2442        // early (e.g. at 15% of the file), the scan's `capped` flag is set to
2443        // true and the batch loop breaks.  The completion check in
2444        // process_search_scan() must also consider `capped` — otherwise the
2445        // scan gets stuck in an infinite loop showing "Searching... 15%".
2446        let config = Config::default();
2447        let (dir_context, _temp) = test_dir_context();
2448        let mut editor = Editor::new(
2449            config,
2450            80,
2451            24,
2452            dir_context,
2453            crate::view::color_support::ColorCapability::TrueColor,
2454            test_filesystem(),
2455        )
2456        .unwrap();
2457
2458        // Manually create a search scan state that is already capped but not
2459        // at the last chunk (simulating early cap at ~15%).
2460        let buffer_id = editor.active_buffer();
2461        let regex = regex::bytes::Regex::new("test").unwrap();
2462        let fake_chunks = vec![
2463            crate::model::buffer::LineScanChunk {
2464                leaf_index: 0,
2465                byte_len: 100,
2466                already_known: true,
2467            },
2468            crate::model::buffer::LineScanChunk {
2469                leaf_index: 1,
2470                byte_len: 100,
2471                already_known: true,
2472            },
2473        ];
2474
2475        let chunked = crate::model::buffer::ChunkedSearchState {
2476            chunks: fake_chunks,
2477            next_chunk: 1, // Only processed 1 of 2 chunks
2478            next_doc_offset: 100,
2479            total_bytes: 200,
2480            scanned_bytes: 100,
2481            regex,
2482            matches: vec![
2483                crate::model::buffer::SearchMatch {
2484                    byte_offset: 10,
2485                    length: 4,
2486                    line: 1,
2487                    column: 11,
2488                    context: String::new(),
2489                },
2490                crate::model::buffer::SearchMatch {
2491                    byte_offset: 50,
2492                    length: 4,
2493                    line: 1,
2494                    column: 51,
2495                    context: String::new(),
2496                },
2497            ],
2498            overlap_tail: Vec::new(),
2499            overlap_doc_offset: 0,
2500            max_matches: 10_000,
2501            capped: true, // Capped early — this is the key condition
2502            query_len: 4,
2503            running_line: 1,
2504        };
2505
2506        editor.active_window_mut().search_scan.start(
2507            buffer_id,
2508            Vec::new(),
2509            chunked,
2510            "test".to_string(),
2511            None,
2512            false,
2513            false,
2514            false,
2515        );
2516
2517        // process_search_scan should finalize the search (not loop forever)
2518        let result = editor.process_search_scan();
2519        assert!(
2520            result,
2521            "process_search_scan should return true (needs render)"
2522        );
2523
2524        // The scan state should be consumed (drained)
2525        assert_eq!(
2526            editor.active_window().search_scan.buffer_id(),
2527            None,
2528            "search_scan should be drained after capped scan completes"
2529        );
2530
2531        // Search state should be set with the accumulated matches
2532        let search_state = editor
2533            .active_window()
2534            .search_state
2535            .as_ref()
2536            .expect("search_state should be set after scan finishes");
2537        assert_eq!(search_state.matches.len(), 2, "Should have 2 matches");
2538        assert_eq!(search_state.query, "test");
2539        assert!(
2540            search_state.capped,
2541            "search_state should be marked as capped"
2542        );
2543    }
2544
2545    #[test]
2546    fn test_bookmarks() {
2547        let config = Config::default();
2548        let (dir_context, _temp) = test_dir_context();
2549        let mut editor = Editor::new(
2550            config,
2551            80,
2552            24,
2553            dir_context,
2554            crate::view::color_support::ColorCapability::TrueColor,
2555            test_filesystem(),
2556        )
2557        .unwrap();
2558
2559        // Insert text
2560        let cursor_id = editor.active_cursors().primary_id();
2561        editor.apply_event_to_active_buffer(&Event::Insert {
2562            position: 0,
2563            text: "Line 1\nLine 2\nLine 3".to_string(),
2564            cursor_id,
2565        });
2566
2567        // Move cursor to line 2 start (position 7)
2568        editor.apply_event_to_active_buffer(&Event::MoveCursor {
2569            cursor_id,
2570            old_position: 21,
2571            new_position: 7,
2572            old_anchor: None,
2573            new_anchor: None,
2574            old_sticky_column: 0,
2575            new_sticky_column: 0,
2576        });
2577
2578        // Set bookmark '1'
2579        editor.active_window_mut().set_bookmark('1');
2580        assert_eq!(
2581            editor
2582                .active_window()
2583                .bookmarks
2584                .get('1')
2585                .map(|b| b.position),
2586            Some(7)
2587        );
2588
2589        // Move cursor elsewhere
2590        editor.apply_event_to_active_buffer(&Event::MoveCursor {
2591            cursor_id,
2592            old_position: 7,
2593            new_position: 14,
2594            old_anchor: None,
2595            new_anchor: None,
2596            old_sticky_column: 0,
2597            new_sticky_column: 0,
2598        });
2599
2600        // Jump back to bookmark
2601        editor.jump_to_bookmark('1');
2602        assert_eq!(editor.active_cursors().primary().position, 7);
2603
2604        // Clear bookmark
2605        editor.active_window_mut().clear_bookmark('1');
2606        assert_eq!(editor.active_window().bookmarks.get('1'), None);
2607    }
2608
2609    #[test]
2610    fn test_action_enum_new_variants() {
2611        // Test that new actions can be parsed from strings
2612        use serde_json::json;
2613
2614        let args = HashMap::new();
2615        assert_eq!(
2616            Action::from_str("smart_home", &args),
2617            Some(Action::SmartHome)
2618        );
2619        assert_eq!(
2620            Action::from_str("dedent_selection", &args),
2621            Some(Action::DedentSelection)
2622        );
2623        assert_eq!(
2624            Action::from_str("toggle_comment", &args),
2625            Some(Action::ToggleComment)
2626        );
2627        assert_eq!(
2628            Action::from_str("goto_matching_bracket", &args),
2629            Some(Action::GoToMatchingBracket)
2630        );
2631        assert_eq!(
2632            Action::from_str("list_bookmarks", &args),
2633            Some(Action::ListBookmarks)
2634        );
2635        assert_eq!(
2636            Action::from_str("toggle_search_case_sensitive", &args),
2637            Some(Action::ToggleSearchCaseSensitive)
2638        );
2639        assert_eq!(
2640            Action::from_str("toggle_search_whole_word", &args),
2641            Some(Action::ToggleSearchWholeWord)
2642        );
2643
2644        // Test bookmark actions with arguments
2645        let mut args_with_char = HashMap::new();
2646        args_with_char.insert("char".to_string(), json!("5"));
2647        assert_eq!(
2648            Action::from_str("set_bookmark", &args_with_char),
2649            Some(Action::SetBookmark('5'))
2650        );
2651        assert_eq!(
2652            Action::from_str("jump_to_bookmark", &args_with_char),
2653            Some(Action::JumpToBookmark('5'))
2654        );
2655        assert_eq!(
2656            Action::from_str("clear_bookmark", &args_with_char),
2657            Some(Action::ClearBookmark('5'))
2658        );
2659    }
2660
2661    #[test]
2662    fn test_keybinding_new_defaults() {
2663        use crossterm::event::{KeyEvent, KeyEventKind, KeyEventState};
2664
2665        // Test that new keybindings are properly registered in the "default" keymap
2666        // Note: We explicitly use "default" keymap, not Config::default() which uses
2667        // platform-specific keymaps (e.g., "macos" on macOS has different bindings)
2668        let mut config = Config::default();
2669        config.active_keybinding_map = crate::config::KeybindingMapName("default".to_string());
2670        let resolver = KeybindingResolver::new(&config);
2671
2672        // Test Ctrl+/ is ToggleComment (not CommandPalette)
2673        let event = KeyEvent {
2674            code: KeyCode::Char('/'),
2675            modifiers: KeyModifiers::CONTROL,
2676            kind: KeyEventKind::Press,
2677            state: KeyEventState::NONE,
2678        };
2679        let action = resolver.resolve(&event, KeyContext::Normal);
2680        assert_eq!(action, Action::ToggleComment);
2681
2682        // Test Ctrl+] is GoToMatchingBracket
2683        let event = KeyEvent {
2684            code: KeyCode::Char(']'),
2685            modifiers: KeyModifiers::CONTROL,
2686            kind: KeyEventKind::Press,
2687            state: KeyEventState::NONE,
2688        };
2689        let action = resolver.resolve(&event, KeyContext::Normal);
2690        assert_eq!(action, Action::GoToMatchingBracket);
2691
2692        // Test Shift+Tab is DedentSelection
2693        let event = KeyEvent {
2694            code: KeyCode::Tab,
2695            modifiers: KeyModifiers::SHIFT,
2696            kind: KeyEventKind::Press,
2697            state: KeyEventState::NONE,
2698        };
2699        let action = resolver.resolve(&event, KeyContext::Normal);
2700        assert_eq!(action, Action::DedentSelection);
2701
2702        // Test Ctrl+G is GotoLine
2703        let event = KeyEvent {
2704            code: KeyCode::Char('g'),
2705            modifiers: KeyModifiers::CONTROL,
2706            kind: KeyEventKind::Press,
2707            state: KeyEventState::NONE,
2708        };
2709        let action = resolver.resolve(&event, KeyContext::Normal);
2710        assert_eq!(action, Action::GotoLine);
2711
2712        // Test bookmark keybindings
2713        let event = KeyEvent {
2714            code: KeyCode::Char('5'),
2715            modifiers: KeyModifiers::CONTROL | KeyModifiers::SHIFT,
2716            kind: KeyEventKind::Press,
2717            state: KeyEventState::NONE,
2718        };
2719        let action = resolver.resolve(&event, KeyContext::Normal);
2720        assert_eq!(action, Action::SetBookmark('5'));
2721
2722        let event = KeyEvent {
2723            code: KeyCode::Char('5'),
2724            modifiers: KeyModifiers::ALT,
2725            kind: KeyEventKind::Press,
2726            state: KeyEventState::NONE,
2727        };
2728        let action = resolver.resolve(&event, KeyContext::Normal);
2729        assert_eq!(action, Action::JumpToBookmark('5'));
2730    }
2731
2732    /// This test demonstrates the bug where LSP didChange notifications contain
2733    /// incorrect positions because they're calculated from the already-modified buffer.
2734    ///
2735    /// When applying LSP rename edits:
2736    /// 1. apply_events_to_buffer_as_bulk_edit() applies the edits to the buffer
2737    /// 2. Then calls notify_lsp_change() which calls collect_lsp_changes()
2738    /// 3. collect_lsp_changes() converts byte positions to LSP positions using
2739    ///    the CURRENT buffer state
2740    ///
2741    /// But the byte positions in the events are relative to the ORIGINAL buffer,
2742    /// not the modified one! This causes LSP to receive wrong positions.
2743    #[test]
2744    fn test_lsp_rename_didchange_positions_bug() {
2745        use crate::model::buffer::Buffer;
2746
2747        let config = Config::default();
2748        let (dir_context, _temp) = test_dir_context();
2749        let mut editor = Editor::new(
2750            config,
2751            80,
2752            24,
2753            dir_context,
2754            crate::view::color_support::ColorCapability::TrueColor,
2755            test_filesystem(),
2756        )
2757        .unwrap();
2758
2759        // Set buffer content: "fn foo(val: i32) {\n    val + 1\n}\n"
2760        // Line 0: positions 0-19 (includes newline)
2761        // Line 1: positions 19-31 (includes newline)
2762        let initial = "fn foo(val: i32) {\n    val + 1\n}\n";
2763        editor.active_state_mut().buffer =
2764            Buffer::from_str(initial, 1024 * 1024, test_filesystem());
2765
2766        // Simulate LSP rename batch: rename "val" to "value" in two places
2767        // This is applied in reverse order to preserve positions:
2768        // 1. Delete "val" at position 23 (line 1, char 4), insert "value"
2769        // 2. Delete "val" at position 7 (line 0, char 7), insert "value"
2770        let cursor_id = editor.active_cursors().primary_id();
2771
2772        let batch = Event::Batch {
2773            events: vec![
2774                // Second occurrence first (reverse order for position preservation)
2775                Event::Delete {
2776                    range: 23..26, // "val" on line 1
2777                    deleted_text: "val".to_string(),
2778                    cursor_id,
2779                },
2780                Event::Insert {
2781                    position: 23,
2782                    text: "value".to_string(),
2783                    cursor_id,
2784                },
2785                // First occurrence second
2786                Event::Delete {
2787                    range: 7..10, // "val" on line 0
2788                    deleted_text: "val".to_string(),
2789                    cursor_id,
2790                },
2791                Event::Insert {
2792                    position: 7,
2793                    text: "value".to_string(),
2794                    cursor_id,
2795                },
2796            ],
2797            description: "LSP Rename".to_string(),
2798        };
2799
2800        // CORRECT: Calculate LSP positions BEFORE applying batch
2801        let lsp_changes_before = editor.active_window().collect_lsp_changes(&batch);
2802
2803        // Now apply the batch (this is what apply_events_to_buffer_as_bulk_edit does)
2804        editor.apply_event_to_active_buffer(&batch);
2805
2806        // BUG DEMONSTRATION: Calculate LSP positions AFTER applying batch
2807        // This is what happens when notify_lsp_change is called after state.apply()
2808        let lsp_changes_after = editor.active_window().collect_lsp_changes(&batch);
2809
2810        // Verify buffer was correctly modified
2811        let final_content = editor.active_state().buffer.to_string().unwrap();
2812        assert_eq!(
2813            final_content, "fn foo(value: i32) {\n    value + 1\n}\n",
2814            "Buffer should have 'value' in both places"
2815        );
2816
2817        // The CORRECT positions (before applying batch):
2818        // - Delete at 23..26 should be line 1, char 4-7 (in original buffer)
2819        // - Insert at 23 should be line 1, char 4 (in original buffer)
2820        // - Delete at 7..10 should be line 0, char 7-10 (in original buffer)
2821        // - Insert at 7 should be line 0, char 7 (in original buffer)
2822        assert_eq!(lsp_changes_before.len(), 4, "Should have 4 changes");
2823
2824        let first_delete = &lsp_changes_before[0];
2825        let first_del_range = first_delete.range.unwrap();
2826        assert_eq!(
2827            first_del_range.start.line, 1,
2828            "First delete should be on line 1 (BEFORE)"
2829        );
2830        assert_eq!(
2831            first_del_range.start.character, 4,
2832            "First delete start should be at char 4 (BEFORE)"
2833        );
2834
2835        // The INCORRECT positions (after applying batch):
2836        // Since the buffer has changed, position 23 now points to different text!
2837        // Original buffer position 23 was start of "val" on line 1
2838        // But after rename, the buffer is "fn foo(value: i32) {\n    value + 1\n}\n"
2839        // Position 23 in new buffer is 'l' in "value" (line 1, offset into "value")
2840        assert_eq!(lsp_changes_after.len(), 4, "Should have 4 changes");
2841
2842        let first_delete_after = &lsp_changes_after[0];
2843        let first_del_range_after = first_delete_after.range.unwrap();
2844
2845        // THIS IS THE BUG: The positions are WRONG when calculated from modified buffer
2846        // The first delete's range.end position will be wrong because the buffer changed
2847        eprintln!("BEFORE modification:");
2848        eprintln!(
2849            "  Delete at line {}, char {}-{}",
2850            first_del_range.start.line,
2851            first_del_range.start.character,
2852            first_del_range.end.character
2853        );
2854        eprintln!("AFTER modification:");
2855        eprintln!(
2856            "  Delete at line {}, char {}-{}",
2857            first_del_range_after.start.line,
2858            first_del_range_after.start.character,
2859            first_del_range_after.end.character
2860        );
2861
2862        // The bug causes the position calculation to be wrong.
2863        // After applying the batch, position 23..26 in the modified buffer
2864        // is different from what it was in the original buffer.
2865        //
2866        // Modified buffer: "fn foo(value: i32) {\n    value + 1\n}\n"
2867        // Position 23 = 'l' in second "value"
2868        // Position 26 = 'e' in second "value"
2869        // This maps to line 1, char 2-5 (wrong!)
2870        //
2871        // Original buffer: "fn foo(val: i32) {\n    val + 1\n}\n"
2872        // Position 23 = 'v' in "val"
2873        // Position 26 = ' ' after "val"
2874        // This maps to line 1, char 4-7 (correct!)
2875
2876        // The positions are different! This demonstrates the bug.
2877        // Note: Due to how the batch is applied (all operations at once),
2878        // the exact positions may vary, but they will definitely be wrong.
2879        assert_ne!(
2880            first_del_range_after.end.character, first_del_range.end.character,
2881            "BUG CONFIRMED: LSP positions are different when calculated after buffer modification!"
2882        );
2883
2884        eprintln!("\n=== BUG DEMONSTRATED ===");
2885        eprintln!("When collect_lsp_changes() is called AFTER buffer modification,");
2886        eprintln!("the positions are WRONG because they're calculated from the");
2887        eprintln!("modified buffer, not the original buffer.");
2888        eprintln!("This causes the second rename to fail with 'content modified' error.");
2889        eprintln!("========================\n");
2890    }
2891
2892    #[test]
2893    fn test_lsp_rename_preserves_cursor_position() {
2894        use crate::model::buffer::Buffer;
2895
2896        let config = Config::default();
2897        let (dir_context, _temp) = test_dir_context();
2898        let mut editor = Editor::new(
2899            config,
2900            80,
2901            24,
2902            dir_context,
2903            crate::view::color_support::ColorCapability::TrueColor,
2904            test_filesystem(),
2905        )
2906        .unwrap();
2907
2908        // Set buffer content: "fn foo(val: i32) {\n    val + 1\n}\n"
2909        // Line 0: positions 0-19 (includes newline)
2910        // Line 1: positions 19-31 (includes newline)
2911        let initial = "fn foo(val: i32) {\n    val + 1\n}\n";
2912        editor.active_state_mut().buffer =
2913            Buffer::from_str(initial, 1024 * 1024, test_filesystem());
2914
2915        // Position cursor at the second "val" (position 23 = 'v' of "val" on line 1)
2916        let original_cursor_pos = 23;
2917        editor.active_cursors_mut().primary_mut().position = original_cursor_pos;
2918
2919        // Verify cursor is at the right position
2920        let buffer_text = editor.active_state().buffer.to_string().unwrap();
2921        let text_at_cursor = buffer_text[original_cursor_pos..original_cursor_pos + 3].to_string();
2922        assert_eq!(text_at_cursor, "val", "Cursor should be at 'val'");
2923
2924        // Simulate LSP rename batch: rename "val" to "value" in two places
2925        // Applied in reverse order (from end of file to start)
2926        let cursor_id = editor.active_cursors().primary_id();
2927        let buffer_id = editor.active_buffer();
2928
2929        let events = vec![
2930            // Second occurrence first (at position 23, line 1)
2931            Event::Delete {
2932                range: 23..26, // "val" on line 1
2933                deleted_text: "val".to_string(),
2934                cursor_id,
2935            },
2936            Event::Insert {
2937                position: 23,
2938                text: "value".to_string(),
2939                cursor_id,
2940            },
2941            // First occurrence second (at position 7, line 0)
2942            Event::Delete {
2943                range: 7..10, // "val" on line 0
2944                deleted_text: "val".to_string(),
2945                cursor_id,
2946            },
2947            Event::Insert {
2948                position: 7,
2949                text: "value".to_string(),
2950                cursor_id,
2951            },
2952        ];
2953
2954        // Apply the rename using bulk edit (this should preserve cursor position)
2955        editor
2956            .apply_events_to_buffer_as_bulk_edit(buffer_id, events, "LSP Rename".to_string())
2957            .unwrap();
2958
2959        // Verify buffer was correctly modified
2960        let final_content = editor.active_state().buffer.to_string().unwrap();
2961        assert_eq!(
2962            final_content, "fn foo(value: i32) {\n    value + 1\n}\n",
2963            "Buffer should have 'value' in both places"
2964        );
2965
2966        // The cursor was originally at position 23 (start of "val" on line 1).
2967        // After renaming:
2968        // - The first "val" (at pos 7-10) was replaced with "value" (5 chars instead of 3)
2969        //   This adds 2 bytes before the cursor.
2970        // - The second "val" at the cursor position was replaced.
2971        //
2972        // Expected cursor position: 23 + 2 = 25 (start of "value" on line 1)
2973        let final_cursor_pos = editor.active_cursors().primary().position;
2974        let expected_cursor_pos = 25; // original 23 + 2 (delta from first rename)
2975
2976        assert_eq!(
2977            final_cursor_pos, expected_cursor_pos,
2978            "Cursor should be at position {} (start of 'value' on line 1), but was at {}. \
2979             Original pos: {}, expected adjustment: +2 for first rename",
2980            expected_cursor_pos, final_cursor_pos, original_cursor_pos
2981        );
2982
2983        // Verify cursor is at start of the renamed symbol
2984        let text_at_new_cursor = &final_content[final_cursor_pos..final_cursor_pos + 5];
2985        assert_eq!(
2986            text_at_new_cursor, "value",
2987            "Cursor should be at the start of 'value' after rename"
2988        );
2989    }
2990
2991    #[test]
2992    fn test_lsp_rename_twice_consecutive() {
2993        // This test reproduces the bug where the second rename fails because
2994        // LSP positions are calculated incorrectly after the first rename.
2995        use crate::model::buffer::Buffer;
2996
2997        let config = Config::default();
2998        let (dir_context, _temp) = test_dir_context();
2999        let mut editor = Editor::new(
3000            config,
3001            80,
3002            24,
3003            dir_context,
3004            crate::view::color_support::ColorCapability::TrueColor,
3005            test_filesystem(),
3006        )
3007        .unwrap();
3008
3009        // Initial content: "fn foo(val: i32) {\n    val + 1\n}\n"
3010        let initial = "fn foo(val: i32) {\n    val + 1\n}\n";
3011        editor.active_state_mut().buffer =
3012            Buffer::from_str(initial, 1024 * 1024, test_filesystem());
3013
3014        let cursor_id = editor.active_cursors().primary_id();
3015        let buffer_id = editor.active_buffer();
3016
3017        // === FIRST RENAME: "val" -> "value" ===
3018        // Create events for first rename (applied in reverse order)
3019        let events1 = vec![
3020            // Second occurrence first (at position 23, line 1, char 4)
3021            Event::Delete {
3022                range: 23..26,
3023                deleted_text: "val".to_string(),
3024                cursor_id,
3025            },
3026            Event::Insert {
3027                position: 23,
3028                text: "value".to_string(),
3029                cursor_id,
3030            },
3031            // First occurrence (at position 7, line 0, char 7)
3032            Event::Delete {
3033                range: 7..10,
3034                deleted_text: "val".to_string(),
3035                cursor_id,
3036            },
3037            Event::Insert {
3038                position: 7,
3039                text: "value".to_string(),
3040                cursor_id,
3041            },
3042        ];
3043
3044        // Create batch for LSP change verification
3045        let batch1 = Event::Batch {
3046            events: events1.clone(),
3047            description: "LSP Rename 1".to_string(),
3048        };
3049
3050        // Collect LSP changes BEFORE applying (this is the fix)
3051        let lsp_changes1 = editor.active_window().collect_lsp_changes(&batch1);
3052
3053        // Verify first rename LSP positions are correct
3054        assert_eq!(
3055            lsp_changes1.len(),
3056            4,
3057            "First rename should have 4 LSP changes"
3058        );
3059
3060        // First delete should be at line 1, char 4-7 (second "val")
3061        let first_del = &lsp_changes1[0];
3062        let first_del_range = first_del.range.unwrap();
3063        assert_eq!(first_del_range.start.line, 1, "First delete line");
3064        assert_eq!(
3065            first_del_range.start.character, 4,
3066            "First delete start char"
3067        );
3068        assert_eq!(first_del_range.end.character, 7, "First delete end char");
3069
3070        // Apply first rename using bulk edit
3071        editor
3072            .apply_events_to_buffer_as_bulk_edit(buffer_id, events1, "LSP Rename 1".to_string())
3073            .unwrap();
3074
3075        // Verify buffer after first rename
3076        let after_first = editor.active_state().buffer.to_string().unwrap();
3077        assert_eq!(
3078            after_first, "fn foo(value: i32) {\n    value + 1\n}\n",
3079            "After first rename"
3080        );
3081
3082        // === SECOND RENAME: "value" -> "x" ===
3083        // Now "value" is at:
3084        // - Line 0, char 7-12 (positions 7-12 in buffer)
3085        // - Line 1, char 4-9 (positions 25-30 in buffer, because line 0 grew by 2)
3086        //
3087        // Buffer: "fn foo(value: i32) {\n    value + 1\n}\n"
3088        //          0123456789...
3089
3090        // Create events for second rename
3091        let events2 = vec![
3092            // Second occurrence first (at position 25, line 1, char 4)
3093            Event::Delete {
3094                range: 25..30,
3095                deleted_text: "value".to_string(),
3096                cursor_id,
3097            },
3098            Event::Insert {
3099                position: 25,
3100                text: "x".to_string(),
3101                cursor_id,
3102            },
3103            // First occurrence (at position 7, line 0, char 7)
3104            Event::Delete {
3105                range: 7..12,
3106                deleted_text: "value".to_string(),
3107                cursor_id,
3108            },
3109            Event::Insert {
3110                position: 7,
3111                text: "x".to_string(),
3112                cursor_id,
3113            },
3114        ];
3115
3116        // Create batch for LSP change verification
3117        let batch2 = Event::Batch {
3118            events: events2.clone(),
3119            description: "LSP Rename 2".to_string(),
3120        };
3121
3122        // Collect LSP changes BEFORE applying (this is the fix)
3123        let lsp_changes2 = editor.active_window().collect_lsp_changes(&batch2);
3124
3125        // Verify second rename LSP positions are correct
3126        // THIS IS WHERE THE BUG WOULD MANIFEST - if positions are wrong,
3127        // the LSP server would report "No references found at position"
3128        assert_eq!(
3129            lsp_changes2.len(),
3130            4,
3131            "Second rename should have 4 LSP changes"
3132        );
3133
3134        // First delete should be at line 1, char 4-9 (second "value")
3135        let second_first_del = &lsp_changes2[0];
3136        let second_first_del_range = second_first_del.range.unwrap();
3137        assert_eq!(
3138            second_first_del_range.start.line, 1,
3139            "Second rename first delete should be on line 1"
3140        );
3141        assert_eq!(
3142            second_first_del_range.start.character, 4,
3143            "Second rename first delete start should be at char 4"
3144        );
3145        assert_eq!(
3146            second_first_del_range.end.character, 9,
3147            "Second rename first delete end should be at char 9 (4 + 5 for 'value')"
3148        );
3149
3150        // Third delete should be at line 0, char 7-12 (first "value")
3151        let second_third_del = &lsp_changes2[2];
3152        let second_third_del_range = second_third_del.range.unwrap();
3153        assert_eq!(
3154            second_third_del_range.start.line, 0,
3155            "Second rename third delete should be on line 0"
3156        );
3157        assert_eq!(
3158            second_third_del_range.start.character, 7,
3159            "Second rename third delete start should be at char 7"
3160        );
3161        assert_eq!(
3162            second_third_del_range.end.character, 12,
3163            "Second rename third delete end should be at char 12 (7 + 5 for 'value')"
3164        );
3165
3166        // Apply second rename using bulk edit
3167        editor
3168            .apply_events_to_buffer_as_bulk_edit(buffer_id, events2, "LSP Rename 2".to_string())
3169            .unwrap();
3170
3171        // Verify buffer after second rename
3172        let after_second = editor.active_state().buffer.to_string().unwrap();
3173        assert_eq!(
3174            after_second, "fn foo(x: i32) {\n    x + 1\n}\n",
3175            "After second rename"
3176        );
3177    }
3178
3179    #[test]
3180    fn test_ensure_active_tab_visible_static_offset() {
3181        let config = Config::default();
3182        let (dir_context, _temp) = test_dir_context();
3183        let mut editor = Editor::new(
3184            config,
3185            80,
3186            24,
3187            dir_context,
3188            crate::view::color_support::ColorCapability::TrueColor,
3189            test_filesystem(),
3190        )
3191        .unwrap();
3192        let split_id = editor.split_manager().active_split();
3193
3194        // Create three buffers with long names to force scrolling.
3195        let buf1 = editor.new_buffer();
3196        editor
3197            .buffers_mut()
3198            .get_mut(&buf1)
3199            .unwrap()
3200            .buffer
3201            .rename_file_path(std::path::PathBuf::from("aaa_long_name_01.txt"));
3202        let buf2 = editor.new_buffer();
3203        editor
3204            .buffers_mut()
3205            .get_mut(&buf2)
3206            .unwrap()
3207            .buffer
3208            .rename_file_path(std::path::PathBuf::from("bbb_long_name_02.txt"));
3209        let buf3 = editor.new_buffer();
3210        editor
3211            .buffers_mut()
3212            .get_mut(&buf3)
3213            .unwrap()
3214            .buffer
3215            .rename_file_path(std::path::PathBuf::from("ccc_long_name_03.txt"));
3216
3217        {
3218            use crate::view::split::TabTarget;
3219            let view_state = editor.split_view_states_mut().get_mut(&split_id).unwrap();
3220            view_state.open_buffers = vec![
3221                TabTarget::Buffer(buf1),
3222                TabTarget::Buffer(buf2),
3223                TabTarget::Buffer(buf3),
3224            ];
3225            view_state.tab_scroll_offset = 50;
3226        }
3227
3228        // Force active buffer to first tab and ensure helper brings it into view.
3229        // Note: available_width must be >= tab width (2 + name_len) for offset to be 0
3230        // Tab width = 2 + 20 (name length) = 22, so we need at least 22
3231        editor
3232            .active_window_mut()
3233            .ensure_active_tab_visible(split_id, buf1, 25);
3234        assert_eq!(
3235            editor
3236                .split_view_states()
3237                .get(&split_id)
3238                .unwrap()
3239                .tab_scroll_offset,
3240            0
3241        );
3242
3243        // Now make the last tab active and ensure offset moves forward but stays bounded.
3244        editor
3245            .active_window_mut()
3246            .ensure_active_tab_visible(split_id, buf3, 25);
3247        let view_state = editor.split_view_states().get(&split_id).unwrap();
3248        assert!(view_state.tab_scroll_offset > 0);
3249        let buffer_ids: Vec<_> = view_state.buffer_tab_ids_vec();
3250        let total_width: usize = buffer_ids
3251            .iter()
3252            .enumerate()
3253            .map(|(idx, id)| {
3254                let state = editor.buffers().get(id).unwrap();
3255                let name_len = state
3256                    .buffer
3257                    .file_path()
3258                    .and_then(|p| p.file_name())
3259                    .and_then(|n| n.to_str())
3260                    .map(|s| s.chars().count())
3261                    .unwrap_or(0);
3262                let tab_width = 2 + name_len;
3263                if idx < buffer_ids.len() - 1 {
3264                    tab_width + 1 // separator
3265                } else {
3266                    tab_width
3267                }
3268            })
3269            .sum();
3270        assert!(view_state.tab_scroll_offset <= total_width);
3271    }
3272}