-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Miguel Perez <[email protected]>
- Loading branch information
1 parent
a1cda3c
commit 265608a
Showing
6 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters