From 8793f8def8ddc322654f95fc6f2caf5e4d869aaa Mon Sep 17 00:00:00 2001 From: Samuel Cormier-Iijima Date: Wed, 22 Nov 2023 15:23:51 -0500 Subject: [PATCH] [formatter] Add "preserve" quote-style to mimic Black's skip-string-normalization Fixes #7525 --- crates/ruff_python_formatter/src/expression/string.rs | 10 ++++++---- crates/ruff_python_formatter/src/options.rs | 4 ++++ ruff.schema.json | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/string.rs b/crates/ruff_python_formatter/src/expression/string.rs index 0fef24af91cf48..dd0deed2de3fa3 100644 --- a/crates/ruff_python_formatter/src/expression/string.rs +++ b/crates/ruff_python_formatter/src/expression/string.rs @@ -443,7 +443,9 @@ impl StringPart { let quotes = match quoting { Quoting::Preserve => self.quotes, Quoting::CanChange => { - if self.prefix.is_raw_string() { + if preferred_style == QuoteStyle::Preserve { + self.quotes + } else if self.prefix.is_raw_string() { choose_quotes_raw(raw_content, self.quotes, preferred_style) } else { choose_quotes(raw_content, self.quotes, preferred_style) @@ -722,7 +724,7 @@ fn choose_quotes(input: &str, quotes: StringQuotes, preferred_style: QuoteStyle) QuoteStyle::Single } } - QuoteStyle::Double => { + QuoteStyle::Double | QuoteStyle::Preserve => { if double_quotes > single_quotes { QuoteStyle::Single } else { @@ -774,8 +776,8 @@ impl Format> for StringQuotes { let quotes = match (self.style, self.triple) { (QuoteStyle::Single, false) => "'", (QuoteStyle::Single, true) => "'''", - (QuoteStyle::Double, false) => "\"", - (QuoteStyle::Double, true) => "\"\"\"", + (QuoteStyle::Double | QuoteStyle::Preserve, false) => "\"", + (QuoteStyle::Double | QuoteStyle::Preserve, true) => "\"\"\"", }; token(quotes).fmt(f) diff --git a/crates/ruff_python_formatter/src/options.rs b/crates/ruff_python_formatter/src/options.rs index b39c7b6ff79c64..f0ddfc8909d7d9 100644 --- a/crates/ruff_python_formatter/src/options.rs +++ b/crates/ruff_python_formatter/src/options.rs @@ -190,6 +190,7 @@ pub enum QuoteStyle { Single, #[default] Double, + Preserve, } impl QuoteStyle { @@ -197,6 +198,7 @@ impl QuoteStyle { match self { QuoteStyle::Single => '\'', QuoteStyle::Double => '"', + QuoteStyle::Preserve => '"', // not used } } @@ -205,6 +207,7 @@ impl QuoteStyle { match self { QuoteStyle::Single => QuoteStyle::Double, QuoteStyle::Double => QuoteStyle::Single, + QuoteStyle::Preserve => QuoteStyle::Preserve, } } } @@ -228,6 +231,7 @@ impl FromStr for QuoteStyle { match s { "\"" | "double" | "Double" => Ok(Self::Double), "'" | "single" | "Single" => Ok(Self::Single), + "preserve" | "Preserve" => Ok(Self::Preserve), // TODO: replace this error with a diagnostic _ => Err("Value not supported for QuoteStyle"), } diff --git a/ruff.schema.json b/ruff.schema.json index c4acb767cd5859..325384665220a7 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2418,7 +2418,8 @@ "type": "string", "enum": [ "single", - "double" + "double", + "preserve" ] }, "RelativeImportsOrder": {