From 20162a426b991f1a07f347f8180480871d15a27a Mon Sep 17 00:00:00 2001 From: unrelentingtech Date: Mon, 2 May 2022 17:31:07 +0300 Subject: feat(commands): make it possible to disable format-on-save via the 'auto-format' option (#2321) --- helix-term/src/commands/typed.rs | 54 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index ec86e446..68aa7703 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -195,6 +195,7 @@ fn write_impl( path: Option<&Cow>, force: bool, ) -> anyhow::Result<()> { + let auto_format = cx.editor.config().auto_format; let jobs = &mut cx.jobs; let doc = doc_mut!(cx.editor); @@ -205,17 +206,21 @@ fn write_impl( if doc.path().is_none() { bail!("cannot write a buffer without a filename"); } - let fmt = doc.auto_format().map(|fmt| { - let shared = fmt.shared(); - let callback = make_format_callback( - doc.id(), - doc.version(), - Modified::SetUnmodified, - shared.clone(), - ); - jobs.callback(callback); - shared - }); + let fmt = if auto_format { + doc.auto_format().map(|fmt| { + let shared = fmt.shared(); + let callback = make_format_callback( + doc.id(), + doc.version(), + Modified::SetUnmodified, + shared.clone(), + ); + jobs.callback(callback); + shared + }) + } else { + None + }; let future = doc.format_and_save(fmt, force); cx.jobs.add(Job::new(future).wait_before_exiting()); @@ -454,6 +459,7 @@ fn write_all_impl( force: bool, ) -> anyhow::Result<()> { let mut errors = String::new(); + let auto_format = cx.editor.config().auto_format; let jobs = &mut cx.jobs; // save all documents for doc in &mut cx.editor.documents.values_mut() { @@ -466,17 +472,21 @@ fn write_all_impl( continue; } - let fmt = doc.auto_format().map(|fmt| { - let shared = fmt.shared(); - let callback = make_format_callback( - doc.id(), - doc.version(), - Modified::SetUnmodified, - shared.clone(), - ); - jobs.callback(callback); - shared - }); + let fmt = if auto_format { + doc.auto_format().map(|fmt| { + let shared = fmt.shared(); + let callback = make_format_callback( + doc.id(), + doc.version(), + Modified::SetUnmodified, + shared.clone(), + ); + jobs.callback(callback); + shared + }) + } else { + None + }; let future = doc.format_and_save(fmt, force); jobs.add(Job::new(future).wait_before_exiting()); } -- cgit v1.2.3-70-g09d2