mirror of https://github.com/helix-editor/helix
Auto Save All Buffers After A Delay (#10899)
* auto save after delay * configable * clearer names * init * working with some odd behaviour * working with greater consistency * Apply reviewer suggestions - Remove unneccessary field - Remove blocking save * Improve auto-save configuration Auto save can be configured to trigger on focus loss: ```toml auto-save.focus-lost = true|false ``` and after a time delay (in milli seconds) since last keypress: ```toml auto-save.after-delay.enable = true|false auto-save.after-delay.timeout = [0, u64::MAX] # default: 3000 ``` * Remove boilerplate and unnecessary types * Remove more useless types * Update docs for auto-save.after-delay * Fix wording of (doc) comments relating to auto-save * book: Move auto-save descriptions to separate section --------- Co-authored-by: Miguel Perez <miguelvojito@gmail.com> Co-authored-by: Miguel Perez <perezoji@cs.fsu.edu>pull/10923/head
parent
a1cda3c19e
commit
265608a3d8
@ -0,0 +1,61 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Ok;
|
||||
use arc_swap::access::Access;
|
||||
|
||||
use helix_event::{register_hook, send_blocking};
|
||||
use helix_view::{events::DocumentDidChange, handlers::Handlers, Editor};
|
||||
use tokio::time::Instant;
|
||||
|
||||
use crate::{
|
||||
commands, compositor,
|
||||
job::{self, Jobs},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct AutoSaveHandler;
|
||||
|
||||
impl AutoSaveHandler {
|
||||
pub fn new() -> AutoSaveHandler {
|
||||
AutoSaveHandler
|
||||
}
|
||||
}
|
||||
|
||||
impl helix_event::AsyncHook for AutoSaveHandler {
|
||||
type Event = u64;
|
||||
|
||||
fn handle_event(
|
||||
&mut self,
|
||||
timeout: Self::Event,
|
||||
_: Option<tokio::time::Instant>,
|
||||
) -> Option<Instant> {
|
||||
Some(Instant::now() + Duration::from_millis(timeout))
|
||||
}
|
||||
|
||||
fn finish_debounce(&mut self) {
|
||||
job::dispatch_blocking(move |editor, _| request_auto_save(editor))
|
||||
}
|
||||
}
|
||||
|
||||
fn request_auto_save(editor: &mut Editor) {
|
||||
let context = &mut compositor::Context {
|
||||
editor,
|
||||
scroll: Some(0),
|
||||
jobs: &mut Jobs::new(),
|
||||
};
|
||||
|
||||
if let Err(e) = commands::typed::write_all_impl(context, false, false) {
|
||||
context.editor.set_error(format!("{}", e));
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn register_hooks(handlers: &Handlers) {
|
||||
let tx = handlers.auto_save.clone();
|
||||
register_hook!(move |event: &mut DocumentDidChange<'_>| {
|
||||
let config = event.doc.config.load();
|
||||
if config.auto_save.after_delay.enable {
|
||||
send_blocking(&tx, config.auto_save.after_delay.timeout);
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue