on master help sign in
feat(commands): make it possible to disable format-on-save via the 'auto-format' option (#2321)
20162a4unrelentingtech
book/src/configuration.md+1
@@ -39,6 +39,7 @@ hidden = false
39| `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` |
39| `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` |
40| `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers`, note that `diagnostics` also includes other features like breakpoints | `["diagnostics", "line-numbers"]` |
40| `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers`, note that `diagnostics` also includes other features like breakpoints | `["diagnostics", "line-numbers"]` |
41| `auto-completion` | Enable automatic pop up of auto-completion. | `true` |
41| `auto-completion` | Enable automatic pop up of auto-completion. | `true` |
42+| `auto-format` | Enable automatic formatting on save. | `true` |
42| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
43| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
43| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
44| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
44| `auto-info` | Whether to display infoboxes | `true` |
45| `auto-info` | Whether to display infoboxes | `true` |
helix-term/src/commands/typed.rs+32−22
@@ -195,6 +195,7 @@ fn write_impl(
195 path: Option<&Cow<str>>,
195 path: Option<&Cow<str>>,
196 force: bool,
196 force: bool,
197) -> anyhow::Result<()> {
197) -> anyhow::Result<()> {
198+ let auto_format = cx.editor.config().auto_format;
198 let jobs = &mut cx.jobs;
199 let jobs = &mut cx.jobs;
199 let doc = doc_mut!(cx.editor);
200 let doc = doc_mut!(cx.editor);
200
201
@@ -205,17 +206,21 @@ fn write_impl(
205 if doc.path().is_none() {
206 if doc.path().is_none() {
206 bail!("cannot write a buffer without a filename");
207 bail!("cannot write a buffer without a filename");
207 }
208 }
209+ let fmt = if auto_format {
208− let fmt = doc.auto_format().map(|fmt| {
210+ doc.auto_format().map(|fmt| {
209− let shared = fmt.shared();
211+ let shared = fmt.shared();
210− let callback = make_format_callback(
212+ let callback = make_format_callback(
211− doc.id(),
213+ doc.id(),
212− doc.version(),
214+ doc.version(),
213− Modified::SetUnmodified,
215+ Modified::SetUnmodified,
214− shared.clone(),
216+ shared.clone(),
215− );
217+ );
216− jobs.callback(callback);
218+ jobs.callback(callback);
217− shared
219+ shared
218− });
220+ })
221+ } else {
222+ None
223+ };
219 let future = doc.format_and_save(fmt, force);
224 let future = doc.format_and_save(fmt, force);
220 cx.jobs.add(Job::new(future).wait_before_exiting());
225 cx.jobs.add(Job::new(future).wait_before_exiting());
221
226
@@ -454,6 +459,7 @@ fn write_all_impl(
454 force: bool,
459 force: bool,
455) -> anyhow::Result<()> {
460) -> anyhow::Result<()> {
456 let mut errors = String::new();
461 let mut errors = String::new();
462+ let auto_format = cx.editor.config().auto_format;
457 let jobs = &mut cx.jobs;
463 let jobs = &mut cx.jobs;
458 // save all documents
464 // save all documents
459 for doc in &mut cx.editor.documents.values_mut() {
465 for doc in &mut cx.editor.documents.values_mut() {
@@ -466,17 +472,21 @@ fn write_all_impl(
466 continue;
472 continue;
467 }
473 }
468
474
475+ let fmt = if auto_format {
469− let fmt = doc.auto_format().map(|fmt| {
476+ doc.auto_format().map(|fmt| {
470− let shared = fmt.shared();
477+ let shared = fmt.shared();
471− let callback = make_format_callback(
478+ let callback = make_format_callback(
472− doc.id(),
479+ doc.id(),
473− doc.version(),
480+ doc.version(),
474− Modified::SetUnmodified,
481+ Modified::SetUnmodified,
475− shared.clone(),
482+ shared.clone(),
476− );
483+ );
477− jobs.callback(callback);
484+ jobs.callback(callback);
478− shared
485+ shared
479− });
486+ })
487+ } else {
488+ None
489+ };
480 let future = doc.format_and_save(fmt, force);
490 let future = doc.format_and_save(fmt, force);
481 jobs.add(Job::new(future).wait_before_exiting());
491 jobs.add(Job::new(future).wait_before_exiting());
482 }
492 }
helix-view/src/editor.rs+3
@@ -127,6 +127,8 @@ pub struct Config {
127 pub auto_pairs: AutoPairConfig,
127 pub auto_pairs: AutoPairConfig,
128 /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
128 /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
129 pub auto_completion: bool,
129 pub auto_completion: bool,
130+ /// Automatic formatting on save. Defaults to true.
131+ pub auto_format: bool,
130 /// Time in milliseconds since last keypress before idle timers trigger.
132 /// Time in milliseconds since last keypress before idle timers trigger.
131 /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
133 /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
132 #[serde(
134 #[serde(
@@ -374,6 +376,7 @@ impl Default for Config {
374 middle_click_paste: true,
376 middle_click_paste: true,
375 auto_pairs: AutoPairConfig::default(),
377 auto_pairs: AutoPairConfig::default(),
376 auto_completion: true,
378 auto_completion: true,
379+ auto_format: true,
377 idle_timeout: Duration::from_millis(400),
380 idle_timeout: Duration::from_millis(400),
378 completion_trigger_len: 2,
381 completion_trigger_len: 2,
379 auto_info: true,
382 auto_info: true,