From 20162a426b991f1a07f347f8180480871d15a27a Mon Sep 17 00:00:00 2001 From: unrelentingtech Date: Mon, 2 May 2022 17:31:07 +0300 Subject: [PATCH] feat(commands): make it possible to disable format-on-save via the 'auto-format' option (#2321) --- book/src/configuration.md | 1 + helix-term/src/commands/typed.rs | 54 +++++++++++++++++++------------- helix-view/src/editor.rs | 3 ++ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index 913897d62..94134ed4d 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -39,6 +39,7 @@ hidden = false | `line-number` | Line number display: `absolute` simply shows each line's number, while `relative` shows the distance from the current line. When unfocused or in insert mode, `relative` will still show absolute line numbers. | `absolute` | | `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers`, note that `diagnostics` also includes other features like breakpoints | `["diagnostics", "line-numbers"]` | | `auto-completion` | Enable automatic pop up of auto-completion. | `true` | +| `auto-format` | Enable automatic formatting on save. | `true` | | `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` | | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` | | `auto-info` | Whether to display infoboxes | `true` | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index ec86e4460..68aa7703c 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()); } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 0de1732c3..29aea74b7 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -127,6 +127,8 @@ pub struct Config { pub auto_pairs: AutoPairConfig, /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true. pub auto_completion: bool, + /// Automatic formatting on save. Defaults to true. + pub auto_format: bool, /// Time in milliseconds since last keypress before idle timers trigger. /// Used for autocompletion, set to 0 for instant. Defaults to 400ms. #[serde( @@ -374,6 +376,7 @@ impl Default for Config { middle_click_paste: true, auto_pairs: AutoPairConfig::default(), auto_completion: true, + auto_format: true, idle_timeout: Duration::from_millis(400), completion_trigger_len: 2, auto_info: true,