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 Nov 22, 2023
1 parent 5b726f7 commit b8130e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions crates/ruff_python_formatter/src/expression/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -774,8 +776,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 @@ -190,13 +190,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 @@ -205,6 +207,7 @@ impl QuoteStyle {
match self {
QuoteStyle::Single => QuoteStyle::Double,
QuoteStyle::Double => QuoteStyle::Single,
QuoteStyle::Preserve => QuoteStyle::Preserve,
}
}
}
Expand All @@ -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"),
}
Expand Down
14 changes: 6 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ python-source = "python"
strip = true
exclude = [
"crates/ruff_linter/resources/test/fixtures/**/*",
"crates/ruff_linter/src/rules/*/snapshots/**/*"
]
include = [
"rust-toolchain.toml"
"crates/ruff_linter/src/rules/*/snapshots/**/*",
]
include = ["rust-toolchain.toml"]

[tool.ruff]
extend-exclude = [
"crates/ruff_linter/resources/",
"crates/ruff_python_formatter/resources/"
"crates/ruff_python_formatter/resources/",
]

[tool.black]
Expand All @@ -71,8 +69,8 @@ force-exclude = '''
'''

[tool.rooster]
major_labels = [] # Ruff never uses the major version number
minor_labels = ["breaking"] # Bump the minor version on breaking changes
major_labels = [] # Ruff never uses the major version number
minor_labels = ["breaking"] # Bump the minor version on breaking changes

changelog_ignore_labels = ["internal"]

Expand All @@ -91,7 +89,7 @@ changelog_sections.__unknown__ = "Other changes"
changelog_contributors = false

version_files = [
"README.md",
"README.md",
"docs/integrations.md",
"crates/flake8_to_ruff/Cargo.toml",
"crates/ruff_cli/Cargo.toml",
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 b8130e1

Please sign in to comment.