feat(commands): make it possible to disable format-on-save via the 'auto-format' option (#2321)

pull/2372/head
unrelentingtech 3 years ago committed by GitHub
parent f9baced216
commit 20162a426b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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` | | `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"]` | | `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-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` | | `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` | | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
| `auto-info` | Whether to display infoboxes | `true` | | `auto-info` | Whether to display infoboxes | `true` |

@ -195,6 +195,7 @@ fn write_impl(
path: Option<&Cow<str>>, path: Option<&Cow<str>>,
force: bool, force: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let auto_format = cx.editor.config().auto_format;
let jobs = &mut cx.jobs; let jobs = &mut cx.jobs;
let doc = doc_mut!(cx.editor); let doc = doc_mut!(cx.editor);
@ -205,7 +206,8 @@ fn write_impl(
if doc.path().is_none() { if doc.path().is_none() {
bail!("cannot write a buffer without a filename"); bail!("cannot write a buffer without a filename");
} }
let fmt = doc.auto_format().map(|fmt| { let fmt = if auto_format {
doc.auto_format().map(|fmt| {
let shared = fmt.shared(); let shared = fmt.shared();
let callback = make_format_callback( let callback = make_format_callback(
doc.id(), doc.id(),
@ -215,7 +217,10 @@ fn write_impl(
); );
jobs.callback(callback); jobs.callback(callback);
shared shared
}); })
} else {
None
};
let future = doc.format_and_save(fmt, force); let future = doc.format_and_save(fmt, force);
cx.jobs.add(Job::new(future).wait_before_exiting()); cx.jobs.add(Job::new(future).wait_before_exiting());
@ -454,6 +459,7 @@ fn write_all_impl(
force: bool, force: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut errors = String::new(); let mut errors = String::new();
let auto_format = cx.editor.config().auto_format;
let jobs = &mut cx.jobs; let jobs = &mut cx.jobs;
// save all documents // save all documents
for doc in &mut cx.editor.documents.values_mut() { for doc in &mut cx.editor.documents.values_mut() {
@ -466,7 +472,8 @@ fn write_all_impl(
continue; continue;
} }
let fmt = doc.auto_format().map(|fmt| { let fmt = if auto_format {
doc.auto_format().map(|fmt| {
let shared = fmt.shared(); let shared = fmt.shared();
let callback = make_format_callback( let callback = make_format_callback(
doc.id(), doc.id(),
@ -476,7 +483,10 @@ fn write_all_impl(
); );
jobs.callback(callback); jobs.callback(callback);
shared shared
}); })
} else {
None
};
let future = doc.format_and_save(fmt, force); let future = doc.format_and_save(fmt, force);
jobs.add(Job::new(future).wait_before_exiting()); jobs.add(Job::new(future).wait_before_exiting());
} }

@ -127,6 +127,8 @@ pub struct Config {
pub auto_pairs: AutoPairConfig, pub auto_pairs: AutoPairConfig,
/// Automatic auto-completion, automatically pop up without user trigger. Defaults to true. /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true.
pub auto_completion: bool, 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. /// Time in milliseconds since last keypress before idle timers trigger.
/// Used for autocompletion, set to 0 for instant. Defaults to 400ms. /// Used for autocompletion, set to 0 for instant. Defaults to 400ms.
#[serde( #[serde(
@ -374,6 +376,7 @@ impl Default for Config {
middle_click_paste: true, middle_click_paste: true,
auto_pairs: AutoPairConfig::default(), auto_pairs: AutoPairConfig::default(),
auto_completion: true, auto_completion: true,
auto_format: true,
idle_timeout: Duration::from_millis(400), idle_timeout: Duration::from_millis(400),
completion_trigger_len: 2, completion_trigger_len: 2,
auto_info: true, auto_info: true,

Loading…
Cancel
Save