Skip to main content

fresh/app/
file_operations.rs

1//! File operations for the Editor.
2//!
3//! This module contains file I/O and watching operations:
4//! - Saving buffers
5//! - Reverting to saved version
6//! - Auto-revert and file change polling
7//! - LSP file notifications (open, change)
8//! - File modification time tracking
9//! - Save conflict detection
10
11use crate::model::buffer::SudoSaveRequired;
12use crate::model::filesystem::FileSystem;
13use crate::view::file_tree::FileTreeView;
14use crate::view::prompt::PromptType;
15use std::path::{Path, PathBuf};
16
17use lsp_types::TextDocumentContentChangeEvent;
18use rust_i18n::t;
19
20use crate::model::event::{BufferId, EventLog};
21use crate::services::lsp::manager::LspSpawnResult;
22use crate::state::EditorState;
23
24use super::{BufferMetadata, Editor};
25
26impl Editor {
27    /// Save the active buffer
28    pub fn save(&mut self) -> anyhow::Result<()> {
29        // Fail fast if remote connection is down
30        if !self.authority.filesystem.is_remote_connected() {
31            anyhow::bail!(
32                "Cannot save: remote connection lost ({})",
33                self.authority
34                    .filesystem
35                    .remote_connection_info()
36                    .unwrap_or("unknown host")
37            );
38        }
39
40        let path = self
41            .active_state()
42            .buffer
43            .file_path()
44            .map(|p| p.to_path_buf());
45
46        match self.active_state_mut().buffer.save() {
47            Ok(()) => self.finalize_save(path),
48            Err(e) => {
49                if let Some(sudo_info) = e.downcast_ref::<SudoSaveRequired>() {
50                    let info = sudo_info.clone();
51                    self.start_prompt(
52                        t!("prompt.sudo_save_confirm").to_string(),
53                        PromptType::ConfirmSudoSave { info },
54                    );
55                    Ok(())
56                } else if let Some(path) = path {
57                    // Check if failure is due to non-existent parent directory
58                    let is_not_found = e
59                        .downcast_ref::<std::io::Error>()
60                        .is_some_and(|io_err| io_err.kind() == std::io::ErrorKind::NotFound);
61                    if is_not_found {
62                        if let Some(parent) = path.parent() {
63                            if !self.authority.filesystem.exists(parent) {
64                                let dir_name = parent
65                                    .strip_prefix(self.working_dir())
66                                    .unwrap_or(parent)
67                                    .display()
68                                    .to_string();
69                                self.start_prompt(
70                                    t!("buffer.create_directory_confirm", name = &dir_name)
71                                        .to_string(),
72                                    PromptType::ConfirmCreateDirectory { path },
73                                );
74                                return Ok(());
75                            }
76                        }
77                    }
78                    Err(e)
79                } else {
80                    Err(e)
81                }
82            }
83        }
84    }
85
86    /// Internal helper to finalize save state (mark as saved, notify LSP, etc.)
87    pub(crate) fn finalize_save(&mut self, path: Option<PathBuf>) -> anyhow::Result<()> {
88        let buffer_id = self.active_buffer();
89        self.finalize_save_buffer(buffer_id, path, false)
90    }
91
92    /// Internal helper to finalize save state for a specific buffer
93    pub(crate) fn finalize_save_buffer(
94        &mut self,
95        buffer_id: BufferId,
96        path: Option<PathBuf>,
97        silent: bool,
98    ) -> anyhow::Result<()> {
99        // Auto-detect language if it's currently "text" and we have a path
100        if let Some(ref p) = path {
101            if let Some(state) = self
102                .windows
103                .get_mut(&self.active_window)
104                .map(|w| &mut w.buffers)
105                .expect("active window present")
106                .get_mut(&buffer_id)
107            {
108                if state.language == "text" {
109                    let first_line = state.buffer.first_line_lossy();
110                    let detected =
111                        crate::primitives::detected_language::DetectedLanguage::from_path(
112                            p,
113                            first_line.as_deref(),
114                            &self.grammar_registry,
115                            &self.config.languages,
116                        );
117                    state.apply_language(detected);
118                }
119            }
120        }
121
122        if !silent {
123            self.active_window_mut().status_message = Some(t!("status.file_saved").to_string());
124        }
125
126        // Mark the event log position as saved (for undo modified tracking)
127        if let Some(event_log) = self.active_window_mut().event_logs.get_mut(&buffer_id) {
128            event_log.mark_saved();
129        }
130
131        // Update file modification time after save
132        if let Some(ref p) = path {
133            if let Ok(metadata) = self.authority.filesystem.metadata(p) {
134                if let Some(mtime) = metadata.modified {
135                    self.file_mod_times_mut().insert(p.clone(), mtime);
136                }
137            }
138        }
139
140        // Reload .gitignore in the file explorer when the user saves one.
141        // Otherwise the tree keeps filtering by the old rules until restart.
142        if let Some(ref p) = path {
143            if p.file_name().and_then(|n| n.to_str()) == Some(".gitignore") {
144                if let Some(parent) = p.parent() {
145                    let parent = parent.to_path_buf();
146                    let fs = self.authority.filesystem.clone();
147                    if let Some(explorer) = self.file_explorer_mut().as_mut() {
148                        load_gitignore_via_fs(fs.as_ref(), explorer, &parent);
149                    }
150                }
151            }
152        }
153
154        // Notify LSP of save
155        self.active_window_mut().notify_lsp_save_buffer(buffer_id);
156
157        // Delete recovery file (buffer is now saved)
158        if let Err(e) = self.delete_buffer_recovery(buffer_id) {
159            tracing::warn!("Failed to delete recovery file: {}", e);
160        }
161
162        // Emit control event
163        if let Some(ref p) = path {
164            self.emit_event(
165                crate::model::control_event::events::FILE_SAVED.name,
166                serde_json::json!({
167                    "path": p.display().to_string()
168                }),
169            );
170        }
171
172        // Fire AfterFileSave hook for plugins
173        if let Some(ref p) = path {
174            self.plugin_manager.read().unwrap().run_hook(
175                "after_file_save",
176                crate::services::plugins::hooks::HookArgs::AfterFileSave {
177                    buffer_id,
178                    path: p.clone(),
179                },
180            );
181        }
182
183        // Run on-save actions (formatters, linters, etc.)
184        // Note: run_on_save_actions also assumes active_buffer internally.
185        // We might need to refactor it too if we want auto-save to trigger formatters.
186        // For now, let's just do it for active buffer or skip for silent auto-saves.
187
188        if !silent {
189            match self.run_on_save_actions() {
190                Ok(true) => {
191                    // Actions ran successfully - if status_message was set by run_on_save_actions
192                    // (e.g., for missing optional formatters), keep it. Otherwise update status.
193                    if self.active_window_mut().status_message.as_deref()
194                        == Some(&t!("status.file_saved"))
195                    {
196                        self.active_window_mut().status_message =
197                            Some(t!("status.file_saved_with_actions").to_string());
198                    }
199                    // else: keep the message set by run_on_save_actions (e.g., missing formatter)
200                }
201                Ok(false) => {
202                    // No actions configured, keep original status
203                }
204                Err(e) => {
205                    // Action failed, show error but don't fail the save
206                    self.active_window_mut().status_message = Some(e);
207                }
208            }
209        }
210
211        Ok(())
212    }
213
214    /// Auto-save all modified buffers to their original files on disk
215    /// Returns the number of buffers saved
216    pub fn auto_save_persistent_buffers(&mut self) -> anyhow::Result<usize> {
217        if !self.config.editor.auto_save_enabled {
218            return Ok(0);
219        }
220
221        // Check if enough time has passed since last auto-save
222        let interval =
223            std::time::Duration::from_secs(self.config.editor.auto_save_interval_secs as u64);
224        if self
225            .time_source
226            .elapsed_since(self.active_window().last_persistent_auto_save)
227            < interval
228        {
229            return Ok(0);
230        }
231
232        self.active_window_mut().last_persistent_auto_save = self.time_source.now();
233
234        // Collect info for modified buffers that have a file path
235        let mut to_save = Vec::new();
236        for (id, state) in self
237            .windows
238            .get(&self.active_window)
239            .map(|w| &w.buffers)
240            .expect("active window present")
241        {
242            if state.buffer.is_modified() {
243                if let Some(path) = state.buffer.file_path() {
244                    to_save.push((*id, path.to_path_buf()));
245                }
246            }
247        }
248
249        let mut count = 0;
250        for (id, path) in to_save {
251            if let Some(state) = self
252                .windows
253                .get_mut(&self.active_window)
254                .map(|w| &mut w.buffers)
255                .expect("active window present")
256                .get_mut(&id)
257            {
258                match state.buffer.save() {
259                    Ok(()) => {
260                        self.finalize_save_buffer(id, Some(path), true)?;
261                        count += 1;
262                    }
263                    Err(e) => {
264                        // Skip if sudo is required (auto-save can't handle prompts)
265                        if e.downcast_ref::<SudoSaveRequired>().is_some() {
266                            tracing::debug!(
267                                "Auto-save skipped for {:?} (sudo required)",
268                                path.display()
269                            );
270                        } else {
271                            tracing::warn!("Auto-save failed for {:?}: {}", path.display(), e);
272                        }
273                    }
274                }
275            }
276        }
277
278        Ok(count)
279    }
280
281    /// Collect ids of modified unnamed (no on-disk path) buffers in tab order.
282    ///
283    /// Used by the "save and quit" flow to walk a Save-As prompt over each
284    /// unnamed buffer before the editor actually exits.
285    pub(crate) fn collect_unnamed_modified_buffers(&self) -> Vec<BufferId> {
286        let mut out = Vec::new();
287        for (id, state) in self
288            .windows
289            .get(&self.active_window)
290            .map(|w| &w.buffers)
291            .expect("active window present")
292        {
293            if !state.buffer.is_modified() {
294                continue;
295            }
296            let is_unnamed = state
297                .buffer
298                .file_path()
299                .map(|p| p.as_os_str().is_empty())
300                .unwrap_or(true);
301            if is_unnamed {
302                out.push(*id);
303            }
304        }
305        out
306    }
307
308    /// Pop the next id from `pending_quit_unnamed_save` and start a Save-As
309    /// prompt for it. Returns true when a prompt was opened (and the editor
310    /// must keep running until the user finishes the chain).
311    pub(crate) fn start_next_quit_save_as(&mut self) -> bool {
312        while let Some(buffer_id) = self
313            .active_window_mut()
314            .pending_quit_unnamed_save
315            .first()
316            .copied()
317        {
318            // Skip ids that vanished or were already saved out from under us.
319            let still_dirty_unnamed = self
320                .buffers()
321                .get(&buffer_id)
322                .map(|s| {
323                    s.buffer.is_modified()
324                        && s.buffer
325                            .file_path()
326                            .map(|p| p.as_os_str().is_empty())
327                            .unwrap_or(true)
328                })
329                .unwrap_or(false);
330            if !still_dirty_unnamed {
331                self.active_window_mut().pending_quit_unnamed_save.remove(0);
332                continue;
333            }
334
335            self.set_active_buffer(buffer_id);
336            self.start_prompt(
337                t!("file.save_as_prompt").to_string(),
338                PromptType::SaveFileAs,
339            );
340            return true;
341        }
342        false
343    }
344
345    /// Save all modified file-backed buffers to disk (called on exit when auto_save is enabled).
346    /// Unlike `auto_save_persistent_buffers`, this skips the interval check and only saves
347    /// named file-backed buffers (not unnamed buffers).
348    pub fn save_all_on_exit(&mut self) -> anyhow::Result<usize> {
349        let mut to_save = Vec::new();
350        for (id, state) in self
351            .windows
352            .get(&self.active_window)
353            .map(|w| &w.buffers)
354            .expect("active window present")
355        {
356            if state.buffer.is_modified() {
357                if let Some(path) = state.buffer.file_path() {
358                    if !path.as_os_str().is_empty() {
359                        to_save.push((*id, path.to_path_buf()));
360                    }
361                }
362            }
363        }
364
365        let mut count = 0;
366        for (id, path) in to_save {
367            if let Some(state) = self
368                .windows
369                .get_mut(&self.active_window)
370                .map(|w| &mut w.buffers)
371                .expect("active window present")
372                .get_mut(&id)
373            {
374                match state.buffer.save() {
375                    Ok(()) => {
376                        self.finalize_save_buffer(id, Some(path), true)?;
377                        count += 1;
378                    }
379                    Err(e) => {
380                        if e.downcast_ref::<SudoSaveRequired>().is_some() {
381                            tracing::debug!(
382                                "Auto-save on exit skipped for {} (sudo required)",
383                                path.display()
384                            );
385                        } else {
386                            tracing::warn!(
387                                "Auto-save on exit failed for {}: {}",
388                                path.display(),
389                                e
390                            );
391                        }
392                    }
393                }
394            }
395        }
396
397        Ok(count)
398    }
399
400    /// Revert the active buffer to the last saved version on disk
401    /// Returns Ok(true) if reverted, Ok(false) if no file path, Err on failure
402    pub fn revert_file(&mut self) -> anyhow::Result<bool> {
403        let path = match self.active_state().buffer.file_path() {
404            Some(p) => p.to_path_buf(),
405            None => {
406                self.active_window_mut().status_message =
407                    Some(t!("status.no_file_to_revert").to_string());
408                return Ok(false);
409            }
410        };
411
412        if !path.exists() {
413            self.active_window_mut().status_message =
414                Some(t!("status.file_not_exists", path = path.display().to_string()).to_string());
415            return Ok(false);
416        }
417
418        // Save scroll position (from SplitViewState) and cursor positions before reloading
419        let active_split = self
420            .windows
421            .get(&self.active_window)
422            .and_then(|w| w.buffers.splits())
423            .map(|(mgr, _)| mgr)
424            .expect("active window must have a populated split layout")
425            .active_split();
426        let (old_top_byte, old_left_column) = self
427            .windows
428            .get(&self.active_window)
429            .and_then(|w| w.buffers.splits())
430            .map(|(_, vs)| vs)
431            .expect("active window must have a populated split layout")
432            .get(&active_split)
433            .map(|vs| (vs.viewport.top_byte, vs.viewport.left_column))
434            .unwrap_or((0, 0));
435        let old_cursors = self.active_cursors().clone();
436
437        // Preserve user settings before reloading
438        let old_buffer_settings = self.active_state().buffer_settings.clone();
439        let old_editing_disabled = self.active_state().editing_disabled;
440
441        // Load the file content fresh from disk
442        let mut new_state = EditorState::from_file_with_languages(
443            &path,
444            self.terminal_width,
445            self.terminal_height,
446            self.config.editor.large_file_threshold_bytes as usize,
447            &self.grammar_registry,
448            &self.config.languages,
449            std::sync::Arc::clone(&self.authority.filesystem),
450        )?;
451
452        // Restore cursor positions (clamped to valid range for new file size)
453        let new_file_size = new_state.buffer.len();
454        let mut restored_cursors = old_cursors;
455        restored_cursors.map(|cursor| {
456            cursor.position = cursor.position.min(new_file_size);
457            // Clear selection since the content may have changed
458            cursor.clear_selection();
459        });
460        // Restore user settings (tab size, indentation, etc.)
461        new_state.buffer_settings = old_buffer_settings;
462        new_state.editing_disabled = old_editing_disabled;
463        // Line number visibility is in per-split BufferViewState (survives buffer replacement)
464
465        // Replace the current buffer with the new state
466        let buffer_id = self.active_buffer();
467        if let Some(state) = self
468            .windows
469            .get_mut(&self.active_window)
470            .map(|w| &mut w.buffers)
471            .expect("active window present")
472            .get_mut(&buffer_id)
473        {
474            *state = new_state;
475            // Note: line_wrap_enabled is now in SplitViewState.viewport
476        }
477
478        // Restore cursor positions in SplitViewState (clamped to valid range for new file size)
479        let active_split = self
480            .windows
481            .get(&self.active_window)
482            .and_then(|w| w.buffers.splits())
483            .map(|(mgr, _)| mgr)
484            .expect("active window must have a populated split layout")
485            .active_split();
486        if let Some(view_state) = self
487            .windows
488            .get_mut(&self.active_window)
489            .and_then(|w| w.split_view_states_mut())
490            .expect("active window must have a populated split layout")
491            .get_mut(&active_split)
492        {
493            view_state.cursors = restored_cursors;
494        }
495
496        // Restore scroll position in SplitViewState (clamped to valid range for new file size)
497        if let Some(view_state) = self
498            .windows
499            .get_mut(&self.active_window)
500            .and_then(|w| w.split_view_states_mut())
501            .expect("active window must have a populated split layout")
502            .get_mut(&active_split)
503        {
504            view_state.viewport.top_byte = old_top_byte.min(new_file_size);
505            view_state.viewport.left_column = old_left_column;
506        }
507
508        // Clear the undo/redo history for this buffer
509        if let Some(event_log) = self.active_window_mut().event_logs.get_mut(&buffer_id) {
510            *event_log = EventLog::new();
511        }
512
513        // Clear seen_byte_ranges so plugins get notified of all visible lines
514        self.active_window_mut().seen_byte_ranges.remove(&buffer_id);
515
516        // Update the file modification time
517        if let Ok(metadata) = self.authority.filesystem.metadata(&path) {
518            if let Some(mtime) = metadata.modified {
519                self.file_mod_times_mut().insert(path.clone(), mtime);
520            }
521        }
522
523        // Notify LSP that the file was changed
524        self.notify_lsp_file_changed(&path);
525
526        self.active_window_mut().status_message = Some(t!("status.reverted").to_string());
527        Ok(true)
528    }
529
530    /// Toggle auto-revert mode
531    pub fn toggle_auto_revert(&mut self) {
532        self.active_window_mut().auto_revert_enabled = !self.active_window().auto_revert_enabled;
533
534        if self.active_window().auto_revert_enabled {
535            self.active_window_mut().status_message =
536                Some(t!("status.auto_revert_enabled").to_string());
537        } else {
538            self.active_window_mut().status_message =
539                Some(t!("status.auto_revert_disabled").to_string());
540        }
541    }
542
543    /// Poll for file changes (called from main loop)
544    ///
545    /// Checks modification times of open files to detect external changes.
546    /// Returns true if any file was changed (requires re-render).
547    ///
548    /// To avoid blocking the event loop, metadata checks run on a background
549    /// thread. This method launches a poll if the interval has elapsed and no
550    /// poll is already in flight, then checks for results from a prior poll.
551    pub fn poll_file_changes(&mut self) -> bool {
552        // Skip if auto-revert is disabled
553        if !self.active_window().auto_revert_enabled {
554            return false;
555        }
556
557        // Check for results from a previous background poll
558        let mut any_changed = false;
559        if let Some(ref rx) = self.active_window_mut().pending_file_poll_rx {
560            match rx.try_recv() {
561                Ok(results) => {
562                    self.active_window_mut().pending_file_poll_rx = None;
563                    any_changed = self.process_file_poll_results(results);
564                }
565                Err(std::sync::mpsc::TryRecvError::Empty) => {
566                    // Still in progress — don't block, don't start another
567                    return false;
568                }
569                Err(std::sync::mpsc::TryRecvError::Disconnected) => {
570                    // Background task panicked or was dropped
571                    self.active_window_mut().pending_file_poll_rx = None;
572                }
573            }
574        }
575
576        // Check poll interval
577        let poll_interval =
578            std::time::Duration::from_millis(self.config.editor.auto_revert_poll_interval_ms);
579        let last_poll = self.active_window().last_auto_revert_poll;
580        let elapsed = self.time_source.elapsed_since(last_poll);
581        tracing::trace!(
582            "poll_file_changes: elapsed={:?}, poll_interval={:?}",
583            elapsed,
584            poll_interval
585        );
586        if elapsed < poll_interval {
587            return any_changed;
588        }
589        self.active_window_mut().last_auto_revert_poll = self.time_source.now();
590
591        // Collect paths of open files that need checking
592        let files_to_check: Vec<PathBuf> = self.buffers().paths();
593
594        if files_to_check.is_empty() {
595            return any_changed;
596        }
597
598        // Spawn background metadata checks
599        let (tx, rx) = std::sync::mpsc::channel();
600        let fs = self.authority.filesystem.clone();
601        std::thread::Builder::new()
602            .name("poll-file-changes".to_string())
603            .spawn(move || {
604                let results: Vec<(PathBuf, Option<std::time::SystemTime>)> = files_to_check
605                    .into_iter()
606                    .map(|path| {
607                        let mtime = fs.metadata(&path).ok().and_then(|m| m.modified);
608                        (path, mtime)
609                    })
610                    .collect();
611                // Receiver may have been dropped if auto-revert was disabled
612                // or the editor is shutting down — that's fine.
613                if tx.send(results).is_err() {}
614            })
615            .ok();
616        self.active_window_mut().pending_file_poll_rx = Some(rx);
617
618        any_changed
619    }
620
621    /// Process results from a background file poll
622    fn process_file_poll_results(
623        &mut self,
624        results: Vec<(PathBuf, Option<std::time::SystemTime>)>,
625    ) -> bool {
626        let mut any_changed = false;
627        for (path, mtime_opt) in results {
628            let Some(current_mtime) = mtime_opt else {
629                continue;
630            };
631
632            if let Some(&stored_mtime) = self.file_mod_times().get(&path) {
633                if current_mtime != stored_mtime {
634                    let path_str = path.display().to_string();
635                    if self.handle_async_file_changed(path_str) {
636                        any_changed = true;
637                    }
638                }
639            } else {
640                // First time seeing this file, record its mtime
641                self.file_mod_times_mut().insert(path, current_mtime);
642            }
643        }
644        any_changed
645    }
646
647    /// Poll for file tree changes (called from main loop)
648    ///
649    /// Checks modification times of expanded directories to detect new/deleted files.
650    /// Returns true if any directory was refreshed (requires re-render).
651    ///
652    /// Like poll_file_changes, metadata checks run on a background thread to
653    /// avoid blocking the event loop.
654    pub fn poll_file_tree_changes(&mut self) -> bool {
655        use crate::view::file_tree::NodeId;
656
657        // Check for results from a previous background poll
658        let mut any_refreshed = false;
659        let mut dir_poll_pending = false;
660        if let Some(ref rx) = self.active_window_mut().pending_dir_poll_rx {
661            match rx.try_recv() {
662                Ok((dir_results, git_index_mtime)) => {
663                    self.active_window_mut().pending_dir_poll_rx = None;
664                    any_refreshed = self.process_dir_poll_results(dir_results, git_index_mtime);
665                }
666                Err(std::sync::mpsc::TryRecvError::Empty) => {
667                    dir_poll_pending = true;
668                }
669                Err(std::sync::mpsc::TryRecvError::Disconnected) => {
670                    self.active_window_mut().pending_dir_poll_rx = None;
671                }
672            }
673        }
674
675        // Check poll interval
676        let poll_interval =
677            std::time::Duration::from_millis(self.config.editor.file_tree_poll_interval_ms);
678        let last_tree_poll = self.active_window().last_file_tree_poll;
679        if self.time_source.elapsed_since(last_tree_poll) < poll_interval {
680            return any_refreshed;
681        }
682        self.active_window_mut().last_file_tree_poll = self.time_source.now();
683
684        // Re-stat every loaded .gitignore and reload/drop as needed, so
685        // external edits (git pull, sed, another editor) and deletions take
686        // effect without a restart. In-editor saves already reload eagerly
687        // via finalize_save_buffer. Sync I/O here — a handful of small files
688        // and all access goes through the filesystem authority.
689        if self.sync_gitignores_from_disk() {
690            any_refreshed = true;
691        }
692
693        // If a previous dir-poll is still in flight, don't stack another.
694        if dir_poll_pending {
695            return any_refreshed;
696        }
697
698        // Resolve the git index path once (first poll only). This uses the
699        // ProcessSpawner which may block briefly on the first call, but only
700        // happens once per session.
701        if !self.active_window_mut().git_index_resolved {
702            self.active_window_mut().git_index_resolved = true;
703            if let Some(path) = self.resolve_git_index() {
704                if let Ok(meta) = self.authority.filesystem.metadata(&path) {
705                    if let Some(mtime) = meta.modified {
706                        self.active_window_mut().dir_mod_times.insert(path, mtime);
707                    }
708                }
709            }
710        }
711
712        // Get file explorer reference
713        let Some(explorer) = self.file_explorer() else {
714            return any_refreshed;
715        };
716
717        // Collect expanded directories (node_id, path)
718        let expanded_dirs: Vec<(NodeId, PathBuf)> = explorer
719            .tree()
720            .all_nodes()
721            .filter(|node| node.is_dir() && node.is_expanded())
722            .map(|node| (node.id, node.entry.path.clone()))
723            .collect();
724
725        // Find the git index path to include in the background metadata check
726        let git_index_path: Option<PathBuf> = self
727            .active_window()
728            .dir_mod_times
729            .keys()
730            .find(|p| p.ends_with(".git/index") || p.ends_with(".git\\index"))
731            .cloned();
732
733        if expanded_dirs.is_empty() && git_index_path.is_none() {
734            return any_refreshed;
735        }
736
737        // Spawn background metadata checks (directories + git index)
738        let (tx, rx) = std::sync::mpsc::channel();
739        let fs = self.authority.filesystem.clone();
740        std::thread::Builder::new()
741            .name("poll-dir-changes".to_string())
742            .spawn(move || {
743                let results: Vec<(NodeId, PathBuf, Option<std::time::SystemTime>)> = expanded_dirs
744                    .into_iter()
745                    .map(|(node_id, path)| {
746                        let mtime = fs.metadata(&path).ok().and_then(|m| m.modified);
747                        (node_id, path, mtime)
748                    })
749                    .collect();
750
751                // Also check git index mtime in the same background thread
752                let git_index_mtime = git_index_path.and_then(|path| {
753                    let mtime = fs.metadata(&path).ok().and_then(|m| m.modified);
754                    Some((path, mtime?))
755                });
756
757                // Receiver may have been dropped during shutdown — that's fine.
758                if tx.send((results, git_index_mtime)).is_err() {}
759            })
760            .ok();
761        self.active_window_mut().pending_dir_poll_rx = Some(rx);
762
763        any_refreshed
764    }
765
766    /// Process results from a background directory poll
767    fn process_dir_poll_results(
768        &mut self,
769        results: Vec<(
770            crate::view::file_tree::NodeId,
771            PathBuf,
772            Option<std::time::SystemTime>,
773        )>,
774        git_index_mtime: Option<(PathBuf, std::time::SystemTime)>,
775    ) -> bool {
776        let mut dirs_to_refresh: Vec<(crate::view::file_tree::NodeId, PathBuf)> = Vec::new();
777
778        for (node_id, path, mtime_opt) in results {
779            let Some(current_mtime) = mtime_opt else {
780                continue;
781            };
782
783            if let Some(&stored_mtime) = self.active_window_mut().dir_mod_times.get(&path) {
784                if current_mtime != stored_mtime {
785                    self.active_window_mut()
786                        .dir_mod_times
787                        .insert(path.clone(), current_mtime);
788                    dirs_to_refresh.push((node_id, path.clone()));
789                    tracing::debug!("Directory changed: {:?}", path);
790                }
791            } else {
792                self.active_window_mut()
793                    .dir_mod_times
794                    .insert(path, current_mtime);
795            }
796        }
797
798        // Check if .git/index mtime changed (detected in background thread)
799        let git_index_changed = if let Some((path, current_mtime)) = git_index_mtime {
800            if let Some(&stored_mtime) = self.active_window_mut().dir_mod_times.get(&path) {
801                if current_mtime != stored_mtime {
802                    self.active_window_mut()
803                        .dir_mod_times
804                        .insert(path, current_mtime);
805                    self.plugin_manager.read().unwrap().run_hook(
806                        "focus_gained",
807                        crate::services::plugins::hooks::HookArgs::FocusGained {},
808                    );
809                    true
810                } else {
811                    false
812                }
813            } else {
814                false
815            }
816        } else {
817            false
818        };
819
820        if dirs_to_refresh.is_empty() && !git_index_changed {
821            return false;
822        }
823
824        // Refresh each changed directory and (re)load its .gitignore. A new
825        // .gitignore file inside an expanded dir bumps the dir's mtime so we
826        // land here; reload_expanded_node re-lists entries but doesn't parse
827        // rules — load_gitignore_via_fs handles the rules side.
828        let refreshed_dirs: Vec<PathBuf> = dirs_to_refresh.iter().map(|(_, p)| p.clone()).collect();
829        self.refresh_file_tree_dirs(&refreshed_dirs);
830        let fs = self.authority.filesystem.clone();
831        if let Some(explorer) = self.file_explorer_mut().as_mut() {
832            for dir in refreshed_dirs {
833                load_gitignore_via_fs(fs.as_ref(), explorer, &dir);
834            }
835        }
836
837        true
838    }
839
840    /// Re-read the given directories in the file explorer, preserving
841    /// descendant expansion state and the cursor's path.
842    ///
843    /// Why reload_expanded_node and not refresh_node: refresh_node
844    /// collapses the directory and re-expands it, which recycles every
845    /// descendant NodeId and drops their expansion state. That's fatal
846    /// for this code path, which runs unprompted from a background timer:
847    /// after a cut+paste into the workspace root, the source parent's
848    /// mtime changes, we land here seconds later, and refresh_node would
849    /// collapse a user-expanded subtree and invalidate the cursor
850    /// NodeId (after which Up/Down become no-ops because
851    /// select_next/select_prev can't find the current id in the visible
852    /// list).
853    ///
854    /// We also snapshot the cursor path before the reload and
855    /// re-resolve it afterwards. reload_expanded_node still recycles
856    /// ids under the refreshed root, so the old id is gone; the path
857    /// survives unless the underlying file was deleted, in which case
858    /// we fall back to the root so the cursor stays live and visible
859    /// (a stale id is effectively no cursor at all).
860    ///
861    /// Exposed as `pub` so tests can drive the refresh path directly
862    /// without relying on filesystem mtime detection, which is too
863    /// environment-sensitive (especially across CI filesystems).
864    pub fn refresh_file_tree_dirs(&mut self, paths: &[PathBuf]) {
865        let active_id = self.active_window;
866        let (Some(runtime), Some(explorer)) = (
867            self.tokio_runtime.as_ref(),
868            self.windows
869                .get_mut(&active_id)
870                .and_then(|w| w.file_explorer.as_mut()),
871        ) else {
872            return;
873        };
874        let cursor_path: Option<PathBuf> = explorer.get_selected_entry().map(|e| e.path.clone());
875        // Re-resolve node ids by path at each step: an earlier
876        // reload_expanded_node in this loop may have recycled ids under
877        // its subtree, so any ids captured before this call can be
878        // stale.
879        for path in paths {
880            let Some(id_now) = explorer.tree().get_node_by_path(path).map(|n| n.id) else {
881                continue;
882            };
883            let tree = explorer.tree_mut();
884            if let Err(e) = runtime.block_on(tree.reload_expanded_node(id_now)) {
885                tracing::warn!("Failed to refresh directory {:?}: {}", path, e);
886            }
887        }
888        if let Some(path) = cursor_path {
889            if explorer.tree().get_node_by_path(&path).is_some() {
890                explorer.navigate_to_path(&path);
891            } else {
892                let root_id = explorer.tree().root_id();
893                explorer.set_selected(Some(root_id));
894            }
895        }
896    }
897
898    /// Re-stat every loaded .gitignore via the filesystem authority and
899    /// reload or drop as needed. Returns true if anything changed.
900    fn sync_gitignores_from_disk(&mut self) -> bool {
901        let fs = self.authority.filesystem.clone();
902        let Some(explorer) = self.file_explorer_mut() else {
903            return false;
904        };
905        let dirs = explorer.ignore_patterns().loaded_gitignore_dirs();
906        let mut changed = false;
907        for dir in dirs {
908            let gitignore_path = dir.join(".gitignore");
909            match fs.metadata(&gitignore_path) {
910                Err(_) => {
911                    explorer.ignore_patterns_mut().remove_gitignore(&dir);
912                    changed = true;
913                }
914                Ok(meta) => {
915                    let stored = explorer.ignore_patterns().stored_gitignore_mtime(&dir);
916                    if stored != meta.modified {
917                        load_gitignore_via_fs(fs.as_ref(), explorer, &dir);
918                        changed = true;
919                    }
920                }
921            }
922        }
923        changed
924    }
925
926    /// Resolve the path to `.git/index` via `git rev-parse --git-dir`.
927    /// Uses the `ProcessSpawner` so it works transparently on both local
928    /// and remote (SSH) filesystems.
929    fn resolve_git_index(&self) -> Option<PathBuf> {
930        let spawner = &self.authority.process_spawner;
931        let cwd = self.working_dir().to_string_lossy().to_string();
932
933        // ProcessSpawner is async — run it on the tokio runtime if available,
934        // otherwise fall back to blocking (should only happen in tests without
935        // a runtime).
936        let result = if let Some(ref rt) = self.tokio_runtime {
937            rt.block_on(spawner.spawn(
938                "git".to_string(),
939                vec!["rev-parse".to_string(), "--git-dir".to_string()],
940                Some(cwd),
941            ))
942        } else {
943            // No runtime — can't run async spawner. This shouldn't happen
944            // in production but can in minimal test setups.
945            return None;
946        };
947
948        let output = result.ok()?;
949        if output.exit_code != 0 {
950            return None;
951        }
952        let git_dir = output.stdout.trim();
953        let git_dir_path = if std::path::Path::new(git_dir).is_absolute() {
954            PathBuf::from(git_dir)
955        } else {
956            self.working_dir().join(git_dir)
957        };
958        Some(git_dir_path.join("index"))
959    }
960
961    /// Notify LSP server about a newly opened file
962    /// Handles language detection, spawning LSP clients, and sending didOpen notifications
963    ///
964    /// Thin delegator: the LSP-notify core lives on `impl Window` (it
965    /// reads only the window's own `lsp` / `buffers` /
966    /// `diagnostic_result_ids` + `self.resources`). Editor callers
967    /// forward to the active window.
968    pub(crate) fn notify_lsp_file_opened(
969        &mut self,
970        path: &Path,
971        buffer_id: BufferId,
972        metadata: &mut BufferMetadata,
973    ) {
974        self.active_window_mut()
975            .notify_lsp_file_opened(path, buffer_id, metadata);
976    }
977
978    /// Record a file's modification time (called when opening files)
979    /// This is used by the polling-based auto-revert to detect external changes.
980    ///
981    /// Thin delegator: `watch_file` is window-local (records into the
982    /// active window's `file_mod_times`); forwards to the active window.
983    pub(crate) fn watch_file(&mut self, path: &Path) {
984        self.active_window_mut().watch_file(path);
985    }
986
987    /// Notify LSP that a file's contents changed (e.g., after revert)
988    pub(crate) fn notify_lsp_file_changed(&mut self, path: &Path) {
989        use crate::services::lsp::manager::LspSpawnResult;
990
991        let Some(lsp_uri) = super::types::file_path_to_lsp_uri_with_translation(
992            path,
993            self.authority.path_translation.as_ref(),
994        ) else {
995            return;
996        };
997
998        // Find the buffer ID, content, and language for this path
999        let Some((buffer_id, content, language)) = self
1000            .buffers()
1001            .iter()
1002            .find(|(_, s)| s.buffer.file_path() == Some(path))
1003            .and_then(|(id, state)| {
1004                state
1005                    .buffer
1006                    .to_string()
1007                    .map(|t| (*id, t, state.language.clone()))
1008            })
1009        else {
1010            return;
1011        };
1012
1013        // Check if we can spawn LSP (respects auto_start setting)
1014        let spawn_result = {
1015            let __active_id = self.active_window;
1016            let Some(lsp) = self.windows.get_mut(&__active_id).map(|w| &mut w.lsp) else {
1017                return;
1018            };
1019            lsp.try_spawn(&language, Some(path))
1020        };
1021
1022        // Only proceed if spawned successfully (or already running)
1023        if spawn_result != LspSpawnResult::Spawned {
1024            return;
1025        }
1026
1027        // Send didOpen to any handles that haven't received it yet
1028        {
1029            let opened_with = self
1030                .active_window()
1031                .buffer_metadata
1032                .get(&buffer_id)
1033                .map(|m| m.lsp_opened_with.clone())
1034                .unwrap_or_default();
1035
1036            let __active_id = self.active_window;
1037
1038            if let Some(lsp) = self.windows.get_mut(&__active_id).map(|w| &mut w.lsp) {
1039                for sh in lsp.get_handles_mut(&language) {
1040                    if opened_with.contains(&sh.handle.id()) {
1041                        continue;
1042                    }
1043                    if let Err(e) =
1044                        sh.handle
1045                            .did_open(lsp_uri.clone(), content.clone(), language.clone())
1046                    {
1047                        tracing::warn!(
1048                            "Failed to send didOpen to LSP '{}' before didChange: {}",
1049                            sh.name,
1050                            e
1051                        );
1052                    } else {
1053                        tracing::debug!(
1054                            "Sent didOpen for {} to LSP '{}' before file change notification",
1055                            lsp_uri.as_str(),
1056                            sh.name
1057                        );
1058                    }
1059                }
1060            }
1061
1062            // Mark all handles as opened
1063            let active_id = self.active_window;
1064            if let Some(__win) = self.windows.get_mut(&active_id) {
1065                if let Some(metadata) = __win.buffer_metadata.get_mut(&buffer_id) {
1066                    for sh in __win.lsp.get_handles(&language) {
1067                        metadata.lsp_opened_with.insert(sh.handle.id());
1068                    }
1069                }
1070            }
1071        }
1072
1073        // Use full document sync - broadcast to all handles
1074        let __active_id = self.active_window;
1075        if let Some(lsp) = self.windows.get_mut(&__active_id).map(|w| &mut w.lsp) {
1076            let content_change = TextDocumentContentChangeEvent {
1077                range: None, // None means full document replacement
1078                range_length: None,
1079                text: content,
1080            };
1081            for sh in lsp.get_handles_mut(&language) {
1082                if let Err(e) = sh
1083                    .handle
1084                    .did_change(lsp_uri.clone(), vec![content_change.clone()])
1085                {
1086                    tracing::warn!("Failed to notify LSP '{}' of file change: {}", sh.name, e);
1087                }
1088            }
1089        }
1090    }
1091
1092    /// Revert a specific buffer by ID without affecting the active viewport.
1093    ///
1094    /// This is used for auto-reverting background buffers that aren't currently
1095    /// visible in the active split. It reloads the buffer content and updates
1096    /// cursors (clamped to valid positions), but does NOT touch any viewport state.
1097    pub(crate) fn revert_buffer_by_id(
1098        &mut self,
1099        buffer_id: BufferId,
1100        path: &Path,
1101    ) -> anyhow::Result<()> {
1102        // Preserve user settings before reloading
1103        // TODO: Consider moving line numbers to SplitViewState (per-view setting)
1104        // Get cursors from split view states for this buffer (find any split showing it)
1105        let old_cursors = self
1106            .windows
1107            .get(&self.active_window)
1108            .and_then(|w| w.buffers.splits())
1109            .map(|(_, vs)| vs)
1110            .expect("active window must have a populated split layout")
1111            .values()
1112            .find_map(|vs| {
1113                if vs.keyed_states.contains_key(&buffer_id) {
1114                    vs.keyed_states.get(&buffer_id).map(|bs| bs.cursors.clone())
1115                } else {
1116                    None
1117                }
1118            })
1119            .unwrap_or_default();
1120        let (old_buffer_settings, old_editing_disabled) = self
1121            .buffers()
1122            .get(&buffer_id)
1123            .map(|s| (s.buffer_settings.clone(), s.editing_disabled))
1124            .unwrap_or_default();
1125
1126        // Load the file content fresh from disk
1127        let mut new_state = EditorState::from_file_with_languages(
1128            path,
1129            self.terminal_width,
1130            self.terminal_height,
1131            self.config.editor.large_file_threshold_bytes as usize,
1132            &self.grammar_registry,
1133            &self.config.languages,
1134            std::sync::Arc::clone(&self.authority.filesystem),
1135        )?;
1136
1137        // Get the new file size for clamping
1138        let new_file_size = new_state.buffer.len();
1139
1140        // Restore cursor positions (clamped to valid range for new file size)
1141        let mut restored_cursors = old_cursors;
1142        restored_cursors.map(|cursor| {
1143            cursor.position = cursor.position.min(new_file_size);
1144            cursor.clear_selection();
1145        });
1146        // Restore user settings (tab size, indentation, etc.)
1147        new_state.buffer_settings = old_buffer_settings;
1148        new_state.editing_disabled = old_editing_disabled;
1149        // Line number visibility is in per-split BufferViewState (survives buffer replacement)
1150
1151        // Replace the buffer content
1152        if let Some(state) = self
1153            .windows
1154            .get_mut(&self.active_window)
1155            .map(|w| &mut w.buffers)
1156            .expect("active window present")
1157            .get_mut(&buffer_id)
1158        {
1159            *state = new_state;
1160        }
1161
1162        // Restore cursors in any split view states that have this buffer
1163        for vs in self
1164            .windows
1165            .get_mut(&self.active_window)
1166            .and_then(|w| w.split_view_states_mut())
1167            .expect("active window must have a populated split layout")
1168            .values_mut()
1169        {
1170            if let Some(buf_state) = vs.keyed_states.get_mut(&buffer_id) {
1171                buf_state.cursors = restored_cursors.clone();
1172            }
1173        }
1174
1175        // Clear the undo/redo history for this buffer
1176        if let Some(event_log) = self.active_window_mut().event_logs.get_mut(&buffer_id) {
1177            *event_log = EventLog::new();
1178        }
1179
1180        // Clear seen_byte_ranges so plugins get notified of all visible lines
1181        self.active_window_mut().seen_byte_ranges.remove(&buffer_id);
1182
1183        // Update the file modification time
1184        if let Ok(metadata) = self.authority.filesystem.metadata(path) {
1185            if let Some(mtime) = metadata.modified {
1186                self.file_mod_times_mut().insert(path.to_path_buf(), mtime);
1187            }
1188        }
1189
1190        // Notify LSP that the file was changed
1191        self.notify_lsp_file_changed(path);
1192
1193        Ok(())
1194    }
1195
1196    /// Handle a file change notification (from file watcher)
1197    pub fn handle_file_changed(&mut self, changed_path: &str) {
1198        let path = PathBuf::from(changed_path);
1199
1200        // Find buffers that have this file open
1201        let buffer_ids: Vec<BufferId> = self
1202            .buffers()
1203            .iter()
1204            .filter(|(_, state)| state.buffer.file_path() == Some(&path))
1205            .map(|(id, _)| *id)
1206            .collect();
1207
1208        if buffer_ids.is_empty() {
1209            return;
1210        }
1211
1212        for buffer_id in buffer_ids {
1213            // Skip terminal buffers - they manage their own content via PTY streaming
1214            // and should not be auto-reverted (which would reset editing_disabled and line_numbers)
1215            if self
1216                .active_window()
1217                .terminal_buffers
1218                .contains_key(&buffer_id)
1219            {
1220                continue;
1221            }
1222
1223            // Skip buffers that have opted out of auto-revert. The
1224            // typical caller is `openFileStreaming`, which manages
1225            // appends itself via `extend_streaming`; an auto-revert
1226            // here would race with those appends, wiping the piece
1227            // tree mid-stream and snapping the cursor back to byte 0
1228            // on every kernel write notification.
1229            if let Some(meta) = self.active_window().buffer_metadata.get(&buffer_id) {
1230                if !meta.auto_revert_enabled {
1231                    continue;
1232                }
1233            }
1234
1235            let state = match self
1236                .windows
1237                .get(&self.active_window)
1238                .map(|w| &w.buffers)
1239                .expect("active window present")
1240                .get(&buffer_id)
1241            {
1242                Some(s) => s,
1243                None => continue,
1244            };
1245
1246            // Check if the file actually changed (compare mod times)
1247            // We use optimistic concurrency: check mtime, and if we decide to revert,
1248            // re-check to handle the race where a save completed between our checks.
1249            let current_mtime = match self
1250                .authority
1251                .filesystem
1252                .metadata(&path)
1253                .ok()
1254                .and_then(|m| m.modified)
1255            {
1256                Some(mtime) => mtime,
1257                None => continue, // Can't read file, skip
1258            };
1259
1260            let dominated_by_stored = self
1261                .file_mod_times()
1262                .get(&path)
1263                .map(|stored| current_mtime <= *stored)
1264                .unwrap_or(false);
1265
1266            if dominated_by_stored {
1267                continue;
1268            }
1269
1270            // If buffer has local modifications, show a warning (don't auto-revert)
1271            if state.buffer.is_modified() {
1272                self.active_window_mut().status_message = Some(format!(
1273                    "File {} changed on disk (buffer has unsaved changes)",
1274                    path.display()
1275                ));
1276                continue;
1277            }
1278
1279            // Auto-revert if enabled and buffer is not modified
1280            if self.active_window().auto_revert_enabled {
1281                // Optimistic concurrency: re-check mtime before reverting.
1282                // A save may have completed between our first check and now,
1283                // updating file_mod_times. If so, skip the revert.
1284                let still_needs_revert = self
1285                    .file_mod_times()
1286                    .get(&path)
1287                    .map(|stored| current_mtime > *stored)
1288                    .unwrap_or(true);
1289
1290                if !still_needs_revert {
1291                    continue;
1292                }
1293
1294                // Check if this buffer is currently displayed in the active split
1295                let is_active_buffer = buffer_id == self.active_buffer();
1296
1297                if is_active_buffer {
1298                    // Use revert_file() which preserves viewport for active buffer
1299                    if let Err(e) = self.revert_file() {
1300                        tracing::error!("Failed to auto-revert file {:?}: {}", path, e);
1301                    } else {
1302                        tracing::info!("Auto-reverted file: {:?}", path);
1303                    }
1304                } else {
1305                    // Use revert_buffer_by_id() which doesn't touch any viewport
1306                    // This prevents corrupting the active split's viewport state
1307                    if let Err(e) = self.revert_buffer_by_id(buffer_id, &path) {
1308                        tracing::error!("Failed to auto-revert background file {:?}: {}", path, e);
1309                    } else {
1310                        tracing::info!("Auto-reverted file: {:?}", path);
1311                    }
1312                }
1313
1314                // Update the modification time tracking for this file
1315                self.watch_file(&path);
1316            }
1317        }
1318    }
1319
1320    /// Check if saving would overwrite changes made by another process
1321    /// Returns Some(current_mtime) if there's a conflict, None otherwise
1322    pub fn check_save_conflict(&self) -> Option<std::time::SystemTime> {
1323        let path = self.active_state().buffer.file_path()?;
1324
1325        // Get current file modification time
1326        let current_mtime = self
1327            .authority
1328            .filesystem
1329            .metadata(path)
1330            .ok()
1331            .and_then(|m| m.modified)?;
1332
1333        // Compare with our recorded modification time
1334        match self.file_mod_times().get(path) {
1335            Some(recorded_mtime) if current_mtime > *recorded_mtime => {
1336                // File was modified externally since we last loaded/saved it
1337                Some(current_mtime)
1338            }
1339            _ => None,
1340        }
1341    }
1342}
1343
1344/// Stat and read `dir/.gitignore` via the filesystem authority and install
1345/// the result on `explorer`. No-op (with a warn-level log on unexpected
1346/// errors) when the file doesn't exist. Shared by the init, expand, save,
1347/// and poll paths so everything routes through the same authority.
1348pub(crate) fn load_gitignore_via_fs(fs: &dyn FileSystem, explorer: &mut FileTreeView, dir: &Path) {
1349    let gitignore_path = dir.join(".gitignore");
1350    let meta = match fs.metadata(&gitignore_path) {
1351        Ok(m) => m,
1352        Err(_) => return,
1353    };
1354    let bytes = match fs.read_file(&gitignore_path) {
1355        Ok(b) => b,
1356        Err(e) => {
1357            tracing::warn!("Failed to read {:?}: {}", gitignore_path, e);
1358            return;
1359        }
1360    };
1361    explorer.load_gitignore_from_bytes(dir, &bytes, meta.modified);
1362}
1363
1364impl crate::app::window::Window {
1365    /// Notify this window's LSP servers about a newly opened file.
1366    ///
1367    /// Window-scoped: reads only the window's own `lsp`, `buffers`,
1368    /// `diagnostic_result_ids`, and `self.resources.{authority,config}`.
1369    /// Handles language detection, spawning LSP clients, and sending
1370    /// didOpen notifications + follow-up pull-diagnostics / inlay-hints.
1371    pub(crate) fn notify_lsp_file_opened(
1372        &mut self,
1373        path: &Path,
1374        buffer_id: BufferId,
1375        metadata: &mut BufferMetadata,
1376    ) {
1377        // Get language from buffer state
1378        let Some(language) = self.buffers.get(&buffer_id).map(|s| s.language.clone()) else {
1379            tracing::debug!("No buffer state for file: {}", path.display());
1380            return;
1381        };
1382
1383        let Some(uri) = metadata.file_uri().cloned() else {
1384            tracing::warn!(
1385                "No URI in metadata for file: {} (failed to compute absolute path)",
1386                path.display()
1387            );
1388            return;
1389        };
1390
1391        // Check file size
1392        let file_size = self
1393            .resources
1394            .authority
1395            .filesystem
1396            .metadata(path)
1397            .ok()
1398            .map(|m| m.size)
1399            .unwrap_or(0);
1400        if file_size > self.resources.config.editor.large_file_threshold_bytes {
1401            let reason = format!("File too large ({} bytes)", file_size);
1402            tracing::debug!(
1403                "Skipping LSP for large file: {} ({})",
1404                path.display(),
1405                reason
1406            );
1407            metadata.disable_lsp(reason);
1408            return;
1409        }
1410
1411        // Get text before borrowing lsp
1412        let text = match self
1413            .buffers
1414            .get(&buffer_id)
1415            .and_then(|state| state.buffer.to_string())
1416        {
1417            Some(t) => t,
1418            None => {
1419                tracing::debug!("Buffer not fully loaded for LSP notification");
1420                return;
1421            }
1422        };
1423
1424        let enable_inlay_hints = self.resources.config.editor.enable_inlay_hints;
1425        let previous_result_id = self.diagnostic_result_ids.get(uri.as_str()).cloned();
1426
1427        // Get buffer line count and version for inlay hints
1428        let (last_line, last_char, buffer_version) = self
1429            .buffers
1430            .get(&buffer_id)
1431            .map(|state| {
1432                let line_count = state.buffer.line_count().unwrap_or(1000);
1433                (
1434                    line_count.saturating_sub(1) as u32,
1435                    10000u32,
1436                    state.buffer.version(),
1437                )
1438            })
1439            .unwrap_or((999, 10000, 0));
1440
1441        // Now borrow lsp and do all LSP operations
1442        let lsp = &mut self.lsp;
1443        let __next_id = &mut self.next_lsp_request_id;
1444
1445        tracing::debug!("LSP manager available for file: {}", path.display());
1446        tracing::debug!(
1447            "Detected language: {} for file: {}",
1448            language,
1449            path.display()
1450        );
1451        tracing::debug!("Using URI from metadata: {}", uri.as_str());
1452        tracing::debug!("Attempting to spawn LSP client for language: {}", language);
1453
1454        match lsp.try_spawn(&language, Some(path)) {
1455            LspSpawnResult::Spawned => {
1456                // Send didOpen to ALL server handles for this language,
1457                // not just the first one.  With multiple servers configured
1458                // (e.g. error-server + warning-server) each needs to know
1459                // about the open document.
1460                for sh in lsp.get_handles_mut(&language) {
1461                    tracing::info!("Sending didOpen to LSP '{}' for: {}", sh.name, uri.as_str());
1462                    if let Err(e) =
1463                        sh.handle
1464                            .did_open(uri.as_uri().clone(), text.clone(), language.clone())
1465                    {
1466                        tracing::warn!("Failed to send didOpen to LSP '{}': {}", sh.name, e);
1467                    } else {
1468                        metadata.lsp_opened_with.insert(sh.handle.id());
1469                    }
1470                }
1471
1472                // Route each follow-up request through capability-aware
1473                // routing so we never send an optional method to a server
1474                // that didn't advertise it. On a cold spawn the capability
1475                // check returns `None` (capabilities aren't known until the
1476                // `initialize` response arrives); the `LspInitialized`
1477                // handler replays these requests once capabilities land.
1478                if let Some(sh) =
1479                    lsp.handle_for_feature_mut(&language, crate::types::LspFeature::Diagnostics)
1480                {
1481                    let request_id = {
1482                        let id = *__next_id;
1483                        *__next_id += 1;
1484                        id
1485                    };
1486                    if let Err(e) = sh.handle.document_diagnostic(
1487                        request_id,
1488                        uri.as_uri().clone(),
1489                        previous_result_id,
1490                    ) {
1491                        tracing::debug!("Failed to request pull diagnostics: {}", e);
1492                    } else {
1493                        tracing::info!(
1494                            "Requested pull diagnostics for {} (request_id={})",
1495                            uri.as_str(),
1496                            request_id
1497                        );
1498                    }
1499                }
1500
1501                if enable_inlay_hints {
1502                    if let Some(sh) =
1503                        lsp.handle_for_feature_mut(&language, crate::types::LspFeature::InlayHints)
1504                    {
1505                        let request_id = {
1506                            let id = *__next_id;
1507                            *__next_id += 1;
1508                            id
1509                        };
1510
1511                        if let Err(e) = sh.handle.inlay_hints(
1512                            request_id,
1513                            uri.as_uri().clone(),
1514                            0,
1515                            0,
1516                            last_line,
1517                            last_char,
1518                        ) {
1519                            tracing::debug!("Failed to request inlay hints: {}", e);
1520                        } else {
1521                            self.pending_inlay_hints_requests.insert(
1522                                request_id,
1523                                super::InlayHintsRequest {
1524                                    buffer_id,
1525                                    version: buffer_version,
1526                                },
1527                            );
1528                            tracing::info!(
1529                                "Requested inlay hints for {} (request_id={})",
1530                                uri.as_str(),
1531                                request_id
1532                            );
1533                        }
1534                    }
1535                }
1536
1537                // Schedule folding range refresh
1538                self.schedule_folding_ranges_refresh(buffer_id);
1539            }
1540            LspSpawnResult::NotAutoStart => {
1541                tracing::debug!(
1542                    "LSP for {} not auto-starting (auto_start=false). Click the LSP indicator to start manually.",
1543                    language
1544                );
1545            }
1546            LspSpawnResult::NotConfigured => {
1547                tracing::debug!("No LSP server configured for language: {}", language);
1548            }
1549            LspSpawnResult::Disabled => {
1550                tracing::debug!("LSP disabled in config for language: {}", language);
1551            }
1552            LspSpawnResult::Failed => {
1553                tracing::warn!("Failed to spawn LSP client for language: {}", language);
1554            }
1555        }
1556    }
1557
1558    /// Record a file's modification time (called when opening files).
1559    /// Window-local: records into this window's own `file_mod_times`.
1560    pub(crate) fn watch_file(&mut self, path: &Path) {
1561        if let Ok(metadata) = self.resources.authority.filesystem.metadata(path) {
1562            if let Some(mtime) = metadata.modified {
1563                self.file_mod_times.insert(path.to_path_buf(), mtime);
1564            }
1565        }
1566    }
1567}