on master help sign in
feat(commands): make it possible to disable format-on-save via the 'auto-format' option (#2321)
commit20162a426b991f1a07f347f8180480871d15a27a authorunrelentingtech <greg@unrelenting.technology> dateMon 2 May 2022, 17:31 (4 years ago) parentf9baced
3 files changed+36−22
book/src/configuration.md+1 helix-term/src/commands/typed.rs+32−22 helix-view/src/editor.rs+3
book/src/configuration.md+1
@@ -39,6 +39,7 @@ hidden = false
3939| `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` |
4040| `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers`, note that `diagnostics` also includes other features like breakpoints | `["diagnostics", "line-numbers"]` |
4141| `auto-completion` | Enable automatic pop up of auto-completion. | `true` |
42+| `auto-format` | Enable automatic formatting on save. | `true` |
4243| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
4344| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
4445| `auto-info` | Whether to display infoboxes | `true` |
helix-term/src/commands/typed.rs+32−22
@@ -195,6 +195,7 @@ fn write_impl(
195195 path: Option<&Cow<str>>,
196196 force: bool,
197197) -> anyhow::Result<()> {
198+ let auto_format = cx.editor.config().auto_format;
198199 let jobs = &mut cx.jobs;
199200 let doc = doc_mut!(cx.editor);
200201
@@ -205,17 +206,21 @@ fn write_impl(
205206 if doc.path().is_none() {
206207 bail!("cannot write a buffer without a filename");
207208 }
208− let fmt = doc.auto_format().map(|fmt| {
209− let shared = fmt.shared();
210− let callback = make_format_callback(
211− doc.id(),
212− doc.version(),
213− Modified::SetUnmodified,
214− shared.clone(),
215− );
216− jobs.callback(callback);
217− shared
218− });
209+ let fmt = if auto_format {
210+ doc.auto_format().map(|fmt| {
211+ let shared = fmt.shared();
212+ let callback = make_format_callback(
213+ doc.id(),
214+ doc.version(),
215+ Modified::SetUnmodified,
216+ shared.clone(),
217+ );
218+ jobs.callback(callback);
219+ shared
220+ })
221+ } else {
222+ None
223+ };
219224 let future = doc.format_and_save(fmt, force);
220225 cx.jobs.add(Job::new(future).wait_before_exiting());
221226
@@ -454,6 +459,7 @@ fn write_all_impl(
454459 force: bool,
455460) -> anyhow::Result<()> {
456461 let mut errors = String::new();
462+ let auto_format = cx.editor.config().auto_format;
457463 let jobs = &mut cx.jobs;
458464 // save all documents
459465 for doc in &mut cx.editor.documents.values_mut() {
@@ -466,17 +472,21 @@ fn write_all_impl(
466472 continue;
467473 }
468474
469− let fmt = doc.auto_format().map(|fmt| {
470− let shared = fmt.shared();
471− let callback = make_format_callback(
472− doc.id(),
473− doc.version(),
474− Modified::SetUnmodified,
475− shared.clone(),
476− );
477− jobs.callback(callback);
478− shared
479− });
475+ let fmt = if auto_format {
476+ doc.auto_format().map(|fmt| {
477+ let shared = fmt.shared();
478+ let callback = make_format_callback(
479+ doc.id(),
480+ doc.version(),
481+ Modified::SetUnmodified,
482+ shared.clone(),
483+ );
484+ jobs.callback(callback);
485+ shared
486+ })
487+ } else {
488+ None
489+ };
480490 let future = doc.format_and_save(fmt, force);
481491 jobs.add(Job::new(future).wait_before_exiting());
482492 }
helix-view/src/editor.rs+3
@@ -127,6 +127,8 @@ pub struct Config {
127127 pub auto_pairs: AutoPairConfig,
128128 /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
129129 pub auto_completion: bool,
130+ /// Automatic formatting on save. Defaults to true.
131+ pub auto_format: bool,
130132 /// Time in milliseconds since last keypress before idle timers trigger.
131133 /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
132134 #[serde(
@@ -374,6 +376,7 @@ impl Default for Config {
374376 middle_click_paste: true,
375377 auto_pairs: AutoPairConfig::default(),
376378 auto_completion: true,
379+ auto_format: true,
377380 idle_timeout: Duration::from_millis(400),
378381 completion_trigger_len: 2,
379382 auto_info: true,