From 879a0d8b125ca0bc4ae589615c3307b2f0c17c1e Mon Sep 17 00:00:00 2001 From: Julia Date: Fri, 23 Sep 2022 16:52:00 -0400 Subject: [PATCH] Backward compat format settings --- assets/settings/default.json | 2 +- crates/project/src/project.rs | 17 ++++++++++------- crates/settings/src/settings.rs | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/assets/settings/default.json b/assets/settings/default.json index 34b665b41d..b3f3e3e7d8 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -43,7 +43,7 @@ // "default_dock_anchor": "expanded" "default_dock_anchor": "right", // Whether or not to perform a buffer format before saving - "format_on_save": true, + "format_on_save": "off", // How to perform a buffer format. This setting can take two values: // // 1. Format code using the current language server: diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 8a3ffbc1c4..36d0b4835a 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -40,7 +40,7 @@ use postage::watch; use rand::prelude::*; use search::SearchQuery; use serde::Serialize; -use settings::Settings; +use settings::{FormatOnSave, Formatter, Settings}; use sha2::{Digest, Sha256}; use similar::{ChangeTag, TextDiff}; use std::{ @@ -3120,12 +3120,11 @@ impl Project { ) }); - if trigger == FormatTrigger::Save && !format_on_save { - continue; - } + let transaction = match (formatter, format_on_save) { + (_, FormatOnSave::Off) if trigger == FormatTrigger::Save => continue, - let transaction = match formatter { - settings::Formatter::LanguageServer => Self::format_via_lsp( + (Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off) + | (_, FormatOnSave::LanguageServer) => Self::format_via_lsp( &this, &buffer, &buffer_abs_path, @@ -3136,7 +3135,11 @@ impl Project { .await .context("failed to format via language server")?, - settings::Formatter::External { command, arguments } => { + ( + Formatter::External { command, arguments }, + FormatOnSave::On | FormatOnSave::Off, + ) + | (_, FormatOnSave::External { command, arguments }) => { Self::format_via_external_command( &buffer, &buffer_abs_path, diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index 8e4c49572a..e346ff60e6 100644 --- a/crates/settings/src/settings.rs +++ b/crates/settings/src/settings.rs @@ -58,7 +58,7 @@ pub struct EditorSettings { pub hard_tabs: Option, pub soft_wrap: Option, pub preferred_line_length: Option, - pub format_on_save: Option, + pub format_on_save: Option, pub formatter: Option, pub enable_language_server: Option, } @@ -70,6 +70,17 @@ pub enum SoftWrap { EditorWidth, PreferredLineLength, } +#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum FormatOnSave { + On, + Off, + LanguageServer, + External { + command: String, + arguments: Vec, + }, +} #[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] @@ -324,8 +335,8 @@ impl Settings { self.language_setting(language, |settings| settings.preferred_line_length) } - pub fn format_on_save(&self, language: Option<&str>) -> bool { - self.language_setting(language, |settings| settings.format_on_save) + pub fn format_on_save(&self, language: Option<&str>) -> FormatOnSave { + self.language_setting(language, |settings| settings.format_on_save.clone()) } pub fn formatter(&self, language: Option<&str>) -> Formatter { @@ -364,7 +375,7 @@ impl Settings { hard_tabs: Some(false), soft_wrap: Some(SoftWrap::None), preferred_line_length: Some(80), - format_on_save: Some(true), + format_on_save: Some(FormatOnSave::On), formatter: Some(Formatter::LanguageServer), enable_language_server: Some(true), },