mirror of https://github.com/helix-editor/helix
fix: auto-reloading on focus
Open question: should we prompt users here too? currently don'tpull/13963/head
parent
bf02b4dbea
commit
1d27b50318
|
@ -13,7 +13,7 @@ pub use helix_view::handlers::Handlers;
|
||||||
|
|
||||||
use self::document_colors::DocumentColorsHandler;
|
use self::document_colors::DocumentColorsHandler;
|
||||||
|
|
||||||
mod auto_reload;
|
pub(super) mod auto_reload;
|
||||||
mod auto_save;
|
mod auto_save;
|
||||||
pub mod completion;
|
pub mod completion;
|
||||||
mod diagnostics;
|
mod diagnostics;
|
||||||
|
|
|
@ -76,16 +76,8 @@ impl helix_event::AsyncHook for AutoReloadHandler {
|
||||||
|
|
||||||
/// Requests a reload if any documents have been modified externally.
|
/// Requests a reload if any documents have been modified externally.
|
||||||
fn prompt_to_reload_if_needed(editor: &mut Editor, compositor: &mut Compositor) {
|
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 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.
|
// Reset the debounce timer to allow for the next check.
|
||||||
let config = editor.config.load();
|
let config = editor.config.load();
|
||||||
if config.auto_reload.periodic.enable {
|
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));
|
compositor.push(Box::new(prompt));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_document_been_externally_modified(doc: &&Document) -> bool {
|
pub fn count_externally_modified_documents<'a>(docs: impl Iterator<Item = &'a Document>) -> usize {
|
||||||
let last_saved_time = doc.get_last_saved_time();
|
docs // Filter out documents that have unsaved changes.
|
||||||
let Some(path) = doc.path() else {
|
.filter(|doc| !doc.is_modified())
|
||||||
return false;
|
// 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
|
// Check if the file has been modified externally
|
||||||
if let Ok(metadata) = fs::metadata(path) {
|
if let Ok(metadata) = fs::metadata(path) {
|
||||||
if let Ok(modified_time) = metadata.modified() {
|
if let Ok(modified_time) = metadata.modified() {
|
||||||
if modified_time > last_saved_time {
|
if modified_time > last_saved_time {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
false
|
||||||
}
|
})
|
||||||
false
|
.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn register_hooks(handlers: &Handlers) {
|
pub(super) fn register_hooks(handlers: &Handlers) {
|
||||||
|
|
|
@ -1469,10 +1469,22 @@ impl Component for EditorView {
|
||||||
Event::IdleTimeout => self.handle_idle_timeout(&mut cx),
|
Event::IdleTimeout => self.handle_idle_timeout(&mut cx),
|
||||||
Event::FocusGained => {
|
Event::FocusGained => {
|
||||||
if context.editor.config().auto_reload.focus_gained {
|
if context.editor.config().auto_reload.focus_gained {
|
||||||
helix_event::send_blocking(
|
if crate::handlers::auto_reload::count_externally_modified_documents(
|
||||||
&context.editor.handlers.auto_reload,
|
context.editor.documents(),
|
||||||
helix_view::handlers::AutoReloadEvent::EditorFocused,
|
) > 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;
|
self.terminal_focused = true;
|
||||||
EventResult::Consumed(None)
|
EventResult::Consumed(None)
|
||||||
|
|
Loading…
Reference in New Issue