diff --git a/helix-term/src/handlers.rs b/helix-term/src/handlers.rs index b41c8ea83..41d85f789 100644 --- a/helix-term/src/handlers.rs +++ b/helix-term/src/handlers.rs @@ -13,7 +13,7 @@ pub use helix_view::handlers::Handlers; use self::document_colors::DocumentColorsHandler; -mod auto_reload; +pub(super) mod auto_reload; mod auto_save; pub mod completion; mod diagnostics; diff --git a/helix-term/src/handlers/auto_reload.rs b/helix-term/src/handlers/auto_reload.rs index 3763aa1b0..f111bc1d7 100644 --- a/helix-term/src/handlers/auto_reload.rs +++ b/helix-term/src/handlers/auto_reload.rs @@ -76,16 +76,8 @@ impl helix_event::AsyncHook for AutoReloadHandler { /// Requests a reload if any documents have been modified externally. fn prompt_to_reload_if_needed(editor: &mut Editor, compositor: &mut Compositor) { - let modified_docs = editor - .documents() - // Filter out documents that have unsaved changes. - .filter(|doc| !doc.is_modified()) - // Get the documents that have been modified externally. - .filter(has_document_been_externally_modified) - .count(); - // If there are no externally modified documents, we can do nothing. - if modified_docs == 0 { + if count_externally_modified_documents(editor.documents()) == 0 { // Reset the debounce timer to allow for the next check. let config = editor.config.load(); if config.auto_reload.periodic.enable { @@ -132,21 +124,27 @@ fn prompt_to_reload_if_needed(editor: &mut Editor, compositor: &mut Compositor) compositor.push(Box::new(prompt)); } -fn has_document_been_externally_modified(doc: &&Document) -> bool { - let last_saved_time = doc.get_last_saved_time(); - let Some(path) = doc.path() else { - return false; - }; +pub fn count_externally_modified_documents<'a>(docs: impl Iterator) -> usize { + docs // Filter out documents that have unsaved changes. + .filter(|doc| !doc.is_modified()) + // Get the documents that have been modified externally. + .filter(|doc| { + let last_saved_time = doc.get_last_saved_time(); + let Some(path) = doc.path() else { + return false; + }; - // Check if the file has been modified externally - if let Ok(metadata) = fs::metadata(path) { - if let Ok(modified_time) = metadata.modified() { - if modified_time > last_saved_time { - return true; + // Check if the file has been modified externally + if let Ok(metadata) = fs::metadata(path) { + if let Ok(modified_time) = metadata.modified() { + if modified_time > last_saved_time { + return true; + } + } } - } - } - false + false + }) + .count() } pub(super) fn register_hooks(handlers: &Handlers) { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 60d65c33b..bbf7d55e7 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -1469,10 +1469,22 @@ impl Component for EditorView { Event::IdleTimeout => self.handle_idle_timeout(&mut cx), Event::FocusGained => { if context.editor.config().auto_reload.focus_gained { - helix_event::send_blocking( - &context.editor.handlers.auto_reload, - helix_view::handlers::AutoReloadEvent::EditorFocused, - ); + if crate::handlers::auto_reload::count_externally_modified_documents( + context.editor.documents(), + ) > 0 + { + if let Err(e) = commands::typed::reload_all( + context, + helix_core::command_line::Args::default(), + super::PromptEvent::Validate, + ) { + context.editor.set_error(format!("{}", e)); + } else { + context + .editor + .set_status("Reloaded files due to external changes"); + } + } } self.terminal_focused = true; EventResult::Consumed(None)