From c7273d754cb9cbefafbb729c8b3de32821de6769 Mon Sep 17 00:00:00 2001 From: Matt Paras Date: Thu, 29 May 2025 21:34:47 -0700 Subject: [PATCH] add document saved hook --- helix-term/src/commands/engine/steel.rs | 46 ++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands/engine/steel.rs b/helix-term/src/commands/engine/steel.rs index cb12867da..a628797fa 100644 --- a/helix-term/src/commands/engine/steel.rs +++ b/helix-term/src/commands/engine/steel.rs @@ -21,7 +21,7 @@ use helix_view::{ GutterConfig, IndentGuidesConfig, LineEndingConfig, LineNumber, LspConfig, SearchConfig, SmartTabConfig, StatusLineConfig, TerminalConfig, WhitespaceConfig, }, - events::{DocumentDidOpen, DocumentFocusLost, SelectionDidChange}, + events::{DocumentDidOpen, DocumentFocusLost, DocumentSaved, SelectionDidChange}, extension::document_id_to_usize, graphics::CursorKind, input::KeyEvent, @@ -3122,6 +3122,50 @@ fn register_hook(event_kind: String, callback_fn: SteelVal) -> steel::UnRecovera Ok(SteelVal::Void).into() } + "document-saved" => { + // TODO: Share this code with the above since most of it is + // exactly the same + register_hook!(move |event: &mut DocumentSaved<'_>| { + let cloned_func = rooted.value().clone(); + let doc_id = event.doc; + + let callback = move |editor: &mut Editor, + _compositor: &mut Compositor, + jobs: &mut job::Jobs| { + let mut ctx = Context { + register: None, + count: None, + editor, + callback: Vec::new(), + on_next_key_callback: None, + jobs, + }; + enter_engine(|guard| { + if let Err(e) = guard + .with_mut_reference::(&mut ctx) + .consume(move |engine, args| { + let context = args[0].clone(); + engine.update_value("*helix.cx*", context); + // TODO: Reuse this allocation if possible + let mut args = [doc_id.into_steelval().unwrap()]; + engine.call_function_with_args_from_mut_slice( + cloned_func.clone(), + &mut args, + ) + }) + { + present_error_inside_engine_context(&mut ctx, guard, e); + } + }) + }; + job::dispatch_blocking_jobs(callback); + + Ok(()) + }); + + Ok(SteelVal::Void).into() + } + _ => steelerr!(Generic => "Unable to register hook: Unknown event type: {}", event_kind) .into(), }