Skip to content

Commit

Permalink
Revert "Delay auto-save until exiting insert mode (#11047)"
Browse files Browse the repository at this point in the history
This reverts commit dca952c.
  • Loading branch information
the-mikedavis committed Jun 30, 2024
1 parent f0f0f31 commit 1b5cb18
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 73 deletions.
76 changes: 10 additions & 66 deletions helix-term/src/handlers/auto_save.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,39 @@
use std::{
sync::{
atomic::{self, AtomicBool},
Arc,
},
time::Duration,
};
use std::time::Duration;

use anyhow::Ok;
use arc_swap::access::Access;

use helix_event::{register_hook, send_blocking};
use helix_view::{
document::Mode,
events::DocumentDidChange,
handlers::{AutoSaveEvent, Handlers},
Editor,
};
use helix_view::{events::DocumentDidChange, handlers::Handlers, Editor};
use tokio::time::Instant;

use crate::{
commands, compositor,
events::OnModeSwitch,
job::{self, Jobs},
};

#[derive(Debug)]
pub(super) struct AutoSaveHandler {
save_pending: Arc<AtomicBool>,
}
pub(super) struct AutoSaveHandler;

impl AutoSaveHandler {
pub fn new() -> AutoSaveHandler {
AutoSaveHandler {
save_pending: Default::default(),
}
AutoSaveHandler
}
}

impl helix_event::AsyncHook for AutoSaveHandler {
type Event = AutoSaveEvent;
type Event = u64;

fn handle_event(
&mut self,
event: Self::Event,
existing_debounce: Option<tokio::time::Instant>,
timeout: Self::Event,
_: Option<tokio::time::Instant>,
) -> Option<Instant> {
match event {
Self::Event::DocumentChanged { save_after } => {
Some(Instant::now() + Duration::from_millis(save_after))
}
Self::Event::LeftInsertMode => {
if existing_debounce.is_some() {
// If the change happened more recently than the debounce, let the
// debounce run down before saving.
existing_debounce
} else {
// Otherwise if there is a save pending, save immediately.
if self.save_pending.load(atomic::Ordering::Relaxed) {
self.finish_debounce();
}
None
}
}
}
Some(Instant::now() + Duration::from_millis(timeout))
}

fn finish_debounce(&mut self) {
let save_pending = self.save_pending.clone();
job::dispatch_blocking(move |editor, _| {
if editor.mode() == Mode::Insert {
// Avoid saving while in insert mode since this mixes up
// the modification indicator and prevents future saves.
save_pending.store(true, atomic::Ordering::Relaxed);
} else {
request_auto_save(editor);
save_pending.store(false, atomic::Ordering::Relaxed);
}
})
job::dispatch_blocking(move |editor, _| request_auto_save(editor))
}
}

Expand All @@ -97,20 +54,7 @@ pub(super) fn register_hooks(handlers: &Handlers) {
register_hook!(move |event: &mut DocumentDidChange<'_>| {
let config = event.doc.config.load();
if config.auto_save.after_delay.enable {
send_blocking(
&tx,
AutoSaveEvent::DocumentChanged {
save_after: config.auto_save.after_delay.timeout,
},
);
}
Ok(())
});

let tx = handlers.auto_save.clone();
register_hook!(move |event: &mut OnModeSwitch<'_, '_>| {
if event.old_mode == Mode::Insert {
send_blocking(&tx, AutoSaveEvent::LeftInsertMode)
send_blocking(&tx, config.auto_save.after_delay.timeout);
}
Ok(())
});
Expand Down
8 changes: 1 addition & 7 deletions helix-view/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ use crate::{DocumentId, Editor, ViewId};
pub mod dap;
pub mod lsp;

#[derive(Debug)]
pub enum AutoSaveEvent {
DocumentChanged { save_after: u64 },
LeftInsertMode,
}

pub struct Handlers {
// only public because most of the actual implementation is in helix-term right now :/
pub completions: Sender<lsp::CompletionEvent>,
pub signature_hints: Sender<lsp::SignatureHelpEvent>,
pub auto_save: Sender<AutoSaveEvent>,
pub auto_save: Sender<u64>,
}

impl Handlers {
Expand Down

0 comments on commit 1b5cb18

Please sign in to comment.