Debian Codegraph @

helix-term/src/handlers/auto_save.rs

raw symbols SCIP
1 use std::{
2 sync::{
3 atomic::{self, AtomicBool},
4 Arc,
5 },
6 time::Duration,
7 };
8
9 use anyhow::Ok;
10 use arc_swap::access::Access;
11
12 use helix_event::{register_hook, send_blocking};
13 use helix_view::{
14 document::Mode,
15 events::DocumentDidChange,
16 handlers::{AutoSaveEvent, Handlers},
17 Editor,
18 };
19 use tokio::time::Instant;
20
21 use crate::{
22 commands, compositor,
23 events::OnModeSwitch,
24 job::{self, Jobs},
25 };
26
27 #[derive(Debug)]
28 pub(super) struct AutoSaveHandler {
29 save_pending: Arc<AtomicBool>,
30 }
31
32 impl AutoSaveHandler {
33 pub fn new() -> AutoSaveHandler {
34 AutoSaveHandler {
35 save_pending: Default::default(),
36 }
37 }
38 }
39
40 impl helix_event::AsyncHook for AutoSaveHandler {
41 type Event = AutoSaveEvent;
42
43 fn handle_event(
44 &mut self,
45 event: Self::Event,
46 existing_debounce: Option<tokio::time::Instant>,
47 ) -> Option<Instant> {
48 match event {
49 Self::Event::DocumentChanged { save_after } => {
50 Some(Instant::now() + Duration::from_millis(save_after))
51 }
52 Self::Event::LeftInsertMode => {
53 if existing_debounce.is_some() {
54 // If the change happened more recently than the debounce, let the
55 // debounce run down before saving.
56 existing_debounce
57 } else {
58 // Otherwise if there is a save pending, save immediately.
59 if self.save_pending.load(atomic::Ordering::Relaxed) {
60 self.finish_debounce();
61 }
62 None
63 }
64 }
65 }
66 }
67
68 fn finish_debounce(&mut self) {
69 let save_pending = self.save_pending.clone();
70 job::dispatch_blocking(move |editor, _| {
71 if editor.mode() == Mode::Insert {
72 // Avoid saving while in insert mode since this mixes up
73 // the modification indicator and prevents future saves.
74 save_pending.store(true, atomic::Ordering::Relaxed);
75 } else {
76 request_auto_save(editor);
77 save_pending.store(false, atomic::Ordering::Relaxed);
78 }
79 })
80 }
81 }
82
83 fn request_auto_save(editor: &mut Editor) {
84 let context = &mut compositor::Context {
85 editor,
86 scroll: Some(0),
87 jobs: &mut Jobs::new(),
88 };
89
90 let options = commands::WriteAllOptions {
91 force: false,
92 write_scratch: false,
93 auto_format: false,
94 code_actions: false,
95 };
96
97 if let Err(e) = commands::typed::write_all_impl(context, options) {
98 context.editor.set_error(format!("{}", e));
99 }
100 }
101
102 pub(super) fn register_hooks(handlers: &Handlers) {
103 let tx = handlers.auto_save.clone();
104 register_hook!(move |event: &mut DocumentDidChange<'_>| {
105 let config = event.doc.config.load();
106 if config.auto_save.after_delay.enable {
107 send_blocking(
108 &tx,
109 AutoSaveEvent::DocumentChanged {
110 save_after: config.auto_save.after_delay.timeout,
111 },
112 );
113 }
114 Ok(())
115 });
116
117 let tx = handlers.auto_save.clone();
118 register_hook!(move |event: &mut OnModeSwitch<'_, '_>| {
119 if event.old_mode == Mode::Insert {
120 send_blocking(&tx, AutoSaveEvent::LeftInsertMode)
121 }
122 Ok(())
123 });
124 }