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