Skip to content

Commit

Permalink
[formatter] Add "preserve" quote-style to mimic Black's skip-string-n…
Browse files Browse the repository at this point in the history
…ormalization

Fixes #7525
  • Loading branch information
sciyoshi committed Dec 3, 2023
1 parent 1dda669 commit 46f5ebd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ impl<'ast, 'buf, 'fmt, 'src> DocstringLinePrinter<'ast, 'buf, 'fmt, 'src> {
// `docstring_code_examples.py` for when this check is relevant.
let wrapped = match self.quote_style {
QuoteStyle::Single => std::format!("'''{}'''", printed.as_code()),
QuoteStyle::Double => std::format!(r#""""{}""""#, printed.as_code()),
QuoteStyle::Double | QuoteStyle::Preserve => {
std::format!(r#""""{}""""#, printed.as_code())
}
};
let result = ruff_python_parser::parse(
&wrapped,
Expand Down
10 changes: 6 additions & 4 deletions crates/ruff_python_formatter/src/expression/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,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)
Expand Down Expand Up @@ -665,7 +667,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 {
Expand Down Expand Up @@ -717,8 +719,8 @@ impl Format<PyFormatContext<'_>> 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)
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ pub enum QuoteStyle {
Single,
#[default]
Double,
Preserve,
}

impl QuoteStyle {
pub const fn as_char(self) -> char {
match self {
QuoteStyle::Single => '\'',
QuoteStyle::Double => '"',
QuoteStyle::Preserve => '"', // not used
}
}

Expand All @@ -222,6 +224,7 @@ impl QuoteStyle {
match self {
QuoteStyle::Single => QuoteStyle::Double,
QuoteStyle::Double => QuoteStyle::Single,
QuoteStyle::Preserve => QuoteStyle::Preserve,
}
}
}
Expand All @@ -245,6 +248,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"),
}
Expand Down
3 changes: 2 additions & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46f5ebd

Please sign in to comment.