Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "preserve" quote-style to mimic Black's skip-string-normalization #8822

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,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 @@ -385,7 +385,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 @@ -664,7 +666,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 @@ -716,8 +718,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,
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
}

impl QuoteStyle {
pub const fn as_char(self) -> char {
match self {
QuoteStyle::Single => '\'',
QuoteStyle::Double => '"',
QuoteStyle::Preserve => '"', // not used
sciyoshi marked this conversation as resolved.
Show resolved Hide resolved
}
}

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.