From dc3275fe7f1a6b3f611613d08e69c084dcf5a3e5 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 10 Aug 2023 20:39:53 +0800 Subject: [PATCH] Improve Ruff Formatter Interoperability (#6472) --- crates/ruff_formatter/src/lib.rs | 8 ++++- crates/ruff_python_formatter/src/options.rs | 39 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/ruff_formatter/src/lib.rs b/crates/ruff_formatter/src/lib.rs index 9034e271d9..7ca7c5c7df 100644 --- a/crates/ruff_formatter/src/lib.rs +++ b/crates/ruff_formatter/src/lib.rs @@ -92,7 +92,13 @@ impl FromStr for IndentStyle { "tab" | "Tabs" => Ok(Self::Tab), "space" | "Spaces" => Ok(Self::Space(IndentStyle::DEFAULT_SPACES)), // TODO: replace this error with a diagnostic - _ => Err("Value not supported for IndentStyle"), + v => { + let v = v.strip_prefix("Spaces, size: ").unwrap_or(v); + + u8::from_str(v) + .map(Self::Space) + .map_err(|_| "Value not supported for IndentStyle") + } } } } diff --git a/crates/ruff_python_formatter/src/options.rs b/crates/ruff_python_formatter/src/options.rs index e5aef3dc35..ef2d84ca79 100644 --- a/crates/ruff_python_formatter/src/options.rs +++ b/crates/ruff_python_formatter/src/options.rs @@ -2,6 +2,7 @@ use ruff_formatter::printer::{LineEnding, PrinterOptions}; use ruff_formatter::{FormatOptions, IndentStyle, LineWidth}; use ruff_python_ast::PySourceType; use std::path::Path; +use std::str::FromStr; #[derive(Clone, Debug)] #[cfg_attr( @@ -16,6 +17,7 @@ pub struct PyFormatOptions { /// Specifies the indent style: /// * Either a tab /// * or a specific amount of spaces + #[cfg_attr(feature = "serde", serde(default = "default_indent_style"))] indent_style: IndentStyle, /// The preferred line width at which the formatter should wrap lines. @@ -33,12 +35,16 @@ fn default_line_width() -> LineWidth { LineWidth::try_from(88).unwrap() } +fn default_indent_style() -> IndentStyle { + IndentStyle::Space(4) +} + impl Default for PyFormatOptions { fn default() -> Self { Self { source_type: PySourceType::default(), - indent_style: IndentStyle::Space(4), - line_width: LineWidth::try_from(88).unwrap(), + indent_style: default_indent_style(), + line_width: default_line_width(), quote_style: QuoteStyle::default(), magic_trailing_comma: MagicTrailingComma::default(), } @@ -66,7 +72,8 @@ impl PyFormatOptions { self.quote_style } - pub fn with_quote_style(&mut self, style: QuoteStyle) -> &mut Self { + #[must_use] + pub fn with_quote_style(mut self, style: QuoteStyle) -> Self { self.quote_style = style; self } @@ -150,6 +157,19 @@ impl TryFrom for QuoteStyle { } } +impl FromStr for QuoteStyle { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "\"" | "double" | "Double" => Ok(Self::Double), + "'" | "single" | "Single" => Ok(Self::Single), + // TODO: replace this error with a diagnostic + _ => Err("Value not supported for QuoteStyle"), + } + } +} + #[derive(Copy, Clone, Debug, Default)] #[cfg_attr( feature = "serde", @@ -167,3 +187,16 @@ impl MagicTrailingComma { matches!(self, Self::Respect) } } + +impl FromStr for MagicTrailingComma { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + match s { + "respect" | "Respect" => Ok(Self::Respect), + "ignore" | "Ignore" => Ok(Self::Ignore), + // TODO: replace this error with a diagnostic + _ => Err("Value not supported for MagicTrailingComma"), + } + } +}