From df752bbd451b7b875086ebc6f82838bc2aa73882 Mon Sep 17 00:00:00 2001 From: Harishankar G <67601573+voiceroy@users.noreply.github.com> Date: Wed, 12 Feb 2025 23:41:01 +0530 Subject: [PATCH] Prevent auto-format in auto-save (#12817) Co-authored-by: Michael Davis --- helix-term/src/commands/typed.rs | 56 ++++++++++++++++++++++------ helix-term/src/handlers/auto_save.rs | 8 +++- helix-term/src/ui/editor.rs | 7 +++- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 9c48bf723..c5c53acd5 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -678,10 +678,16 @@ pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> Ok(()) } +#[derive(Debug, Clone, Copy)] +pub struct WriteAllOptions { + pub force: bool, + pub write_scratch: bool, + pub auto_format: bool, +} + pub fn write_all_impl( cx: &mut compositor::Context, - force: bool, - write_scratch: bool, + options: WriteAllOptions, ) -> anyhow::Result<()> { let mut errors: Vec<&'static str> = Vec::new(); let config = cx.editor.config(); @@ -699,7 +705,7 @@ pub fn write_all_impl( return None; } if doc.path().is_none() { - if write_scratch { + if options.write_scratch { errors.push("cannot write a buffer without a filename"); } return None; @@ -722,14 +728,14 @@ pub fn write_all_impl( // Save an undo checkpoint for any outstanding changes. doc.append_changes_to_history(view); - let fmt = if config.auto_format { + let fmt = if options.auto_format && config.auto_format { doc.auto_format().map(|fmt| { let callback = make_format_callback( doc_id, doc.version(), target_view, fmt, - Some((None, force)), + Some((None, options.force)), ); jobs.add(Job::with_callback(callback).wait_before_exiting()); }) @@ -738,11 +744,11 @@ pub fn write_all_impl( }; if fmt.is_none() { - cx.editor.save::(doc_id, None, force)?; + cx.editor.save::(doc_id, None, options.force)?; } } - if !errors.is_empty() && !force { + if !errors.is_empty() && !options.force { bail!("{:?}", errors); } @@ -758,7 +764,14 @@ fn write_all( return Ok(()); } - write_all_impl(cx, false, true) + write_all_impl( + cx, + WriteAllOptions { + force: false, + write_scratch: true, + auto_format: true, + }, + ) } fn force_write_all( @@ -770,7 +783,14 @@ fn force_write_all( return Ok(()); } - write_all_impl(cx, true, true) + write_all_impl( + cx, + WriteAllOptions { + force: true, + write_scratch: true, + auto_format: true, + }, + ) } fn write_all_quit( @@ -781,7 +801,14 @@ fn write_all_quit( if event != PromptEvent::Validate { return Ok(()); } - write_all_impl(cx, false, true)?; + write_all_impl( + cx, + WriteAllOptions { + force: false, + write_scratch: true, + auto_format: true, + }, + )?; quit_all_impl(cx, false) } @@ -793,7 +820,14 @@ fn force_write_all_quit( if event != PromptEvent::Validate { return Ok(()); } - let _ = write_all_impl(cx, true, true); + let _ = write_all_impl( + cx, + WriteAllOptions { + force: true, + write_scratch: true, + auto_format: true, + }, + ); quit_all_impl(cx, true) } diff --git a/helix-term/src/handlers/auto_save.rs b/helix-term/src/handlers/auto_save.rs index 4e154df80..47e2ecfdf 100644 --- a/helix-term/src/handlers/auto_save.rs +++ b/helix-term/src/handlers/auto_save.rs @@ -87,7 +87,13 @@ fn request_auto_save(editor: &mut Editor) { jobs: &mut Jobs::new(), }; - if let Err(e) = commands::typed::write_all_impl(context, false, false) { + let options = commands::WriteAllOptions { + force: false, + write_scratch: false, + auto_format: false, + }; + + if let Err(e) = commands::typed::write_all_impl(context, options) { context.editor.set_error(format!("{}", e)); } } diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 6fecd512b..d8500ed4d 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -1522,7 +1522,12 @@ impl Component for EditorView { } Event::FocusLost => { if context.editor.config().auto_save.focus_lost { - if let Err(e) = commands::typed::write_all_impl(context, false, false) { + let options = commands::WriteAllOptions { + force: false, + write_scratch: false, + auto_format: false, + }; + if let Err(e) = commands::typed::write_all_impl(context, options) { context.editor.set_error(format!("{}", e)); } }