diff --git a/crates/rome_service/src/configuration/formatter.rs b/crates/rome_service/src/configuration/formatter.rs index 7dc4e66788a..149ff918df3 100644 --- a/crates/rome_service/src/configuration/formatter.rs +++ b/crates/rome_service/src/configuration/formatter.rs @@ -9,24 +9,29 @@ use serde::{Deserialize, Serialize}; #[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct FormatterConfiguration { // if `false`, it disables the feature. `true` by default - pub enabled: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub enabled: Option, /// Stores whether formatting should be allowed to proceed if a given file /// has syntax errors - pub format_with_errors: bool, + #[serde(skip_serializing_if = "Option::is_none")] + pub format_with_errors: Option, /// The indent style. - pub indent_style: PlainIndentStyle, + #[serde(skip_serializing_if = "Option::is_none")] + pub indent_style: Option, /// The size of the indentation, 2 by default - pub indent_size: u8, + #[serde(skip_serializing_if = "Option::is_none")] + pub indent_size: Option, /// What's the max width of a line. Defaults to 80. #[serde( deserialize_with = "deserialize_line_width", serialize_with = "serialize_line_width" )] - pub line_width: LineWidth, + #[serde(skip_serializing_if = "Option::is_none")] + pub line_width: Option, /// A list of Unix shell style patterns. The formatter will ignore files/folders that will /// match these patterns. @@ -52,11 +57,11 @@ impl FormatterConfiguration { impl Default for FormatterConfiguration { fn default() -> Self { Self { - enabled: true, - format_with_errors: false, - indent_size: 2, - indent_style: PlainIndentStyle::default(), - line_width: LineWidth::default(), + enabled: Some(true), + format_with_errors: Some(false), + indent_size: Some(2), + indent_style: Some(PlainIndentStyle::default()), + line_width: Some(LineWidth::default()), ignore: None, } } @@ -67,9 +72,10 @@ impl TryFrom for FormatSettings { fn try_from(conf: FormatterConfiguration) -> Result { let indent_style = match conf.indent_style { - PlainIndentStyle::Tab => IndentStyle::Tab, - PlainIndentStyle::Space => IndentStyle::Space(conf.indent_size), - }; + Some(PlainIndentStyle::Tab) => IndentStyle::Tab, + Some(PlainIndentStyle::Space) => IndentStyle::Space(conf.indent_size), + None => IndentStyle::Tab + }; let mut matcher = Matcher::new(MatchOptions { case_sensitive: true, require_literal_leading_dot: false, @@ -97,15 +103,16 @@ impl TryFrom for FormatSettings { } } -fn deserialize_line_width<'de, D>(deserializer: D) -> Result +fn deserialize_line_width<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::de::Deserializer<'de>, { let value: u16 = Deserialize::deserialize(deserializer)?; - LineWidth::try_from(value).map_err(serde::de::Error::custom) + let line_width = LineWidth::try_from(value).map_err(serde::de::Error::custom)?; + Ok(Some(line_width)) } -pub fn serialize_line_width(line_width: &LineWidth, s: S) -> Result +pub fn serialize_line_width(line_width: &Option, s: S) -> Result where S: serde::ser::Serializer, { diff --git a/crates/rome_service/src/configuration/parse/json/formatter.rs b/crates/rome_service/src/configuration/parse/json/formatter.rs index fe2dd9008fc..aa52900fd4d 100644 --- a/crates/rome_service/src/configuration/parse/json/formatter.rs +++ b/crates/rome_service/src/configuration/parse/json/formatter.rs @@ -27,26 +27,26 @@ impl VisitNode for FormatterConfiguration { let name_text = name.text(); match name_text { "formatWithErrors" => { - self.format_with_errors = self.map_to_boolean(&value, name_text, diagnostics)?; + self.format_with_errors = self.map_to_boolean(&value, name_text, diagnostics); } "enabled" => { - self.enabled = self.map_to_boolean(&value, name_text, diagnostics)?; + self.enabled = self.map_to_boolean(&value, name_text, diagnostics); } "ignore" => { self.ignore = self.map_to_index_set_string(&value, name_text, diagnostics); } "indentStyle" => { let mut indent_style = PlainIndentStyle::default(); - self.map_to_known_string(&value, name_text, &mut indent_style, diagnostics)?; - self.indent_style = indent_style; + self.map_to_known_string(&value, name_text, &mut indent_style, diagnostics); + self.indent_style = Some(indent_style); } "indentSize" => { - self.indent_size = self.map_to_u8(&value, name_text, u8::MAX, diagnostics)?; + self.indent_size = self.map_to_u8(&value, name_text, u8::MAX, diagnostics); } "lineWidth" => { - let line_width = self.map_to_u16(&value, name_text, LineWidth::MAX, diagnostics)?; + let line_width = self.map_to_u16(&value, name_text, LineWidth::MAX, diagnostics); - self.line_width = match LineWidth::try_from(line_width) { + self.line_width = Some(match LineWidth::try_from() { Ok(result) => result, Err(err) => { diagnostics.push( @@ -58,7 +58,7 @@ impl VisitNode for FormatterConfiguration { ); LineWidth::default() } - }; + }); } _ => {} }