mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Add "preserve" quote-style to mimic Black's skip-string-normalization (#8822)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
6bbabceead
commit
2414298289
10 changed files with 482 additions and 96 deletions
|
@ -158,12 +158,21 @@ impl Configuration {
|
|||
let format = self.format;
|
||||
let format_defaults = FormatterSettings::default();
|
||||
|
||||
let quote_style = format.quote_style.unwrap_or(format_defaults.quote_style);
|
||||
let format_preview = match format.preview.unwrap_or(global_preview) {
|
||||
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
|
||||
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
|
||||
};
|
||||
|
||||
if quote_style == QuoteStyle::Preserve && !format_preview.is_enabled() {
|
||||
return Err(anyhow!(
|
||||
"'quote-style = preserve' is a preview only feature. Run with '--preview' to enable it."
|
||||
));
|
||||
}
|
||||
|
||||
let formatter = FormatterSettings {
|
||||
exclude: FilePatternSet::try_from_iter(format.exclude.unwrap_or_default())?,
|
||||
preview: match format.preview.unwrap_or(global_preview) {
|
||||
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
|
||||
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
|
||||
},
|
||||
preview: format_preview,
|
||||
line_width: self
|
||||
.line_length
|
||||
.map_or(format_defaults.line_width, |length| {
|
||||
|
@ -176,7 +185,7 @@ impl Configuration {
|
|||
.map_or(format_defaults.indent_width, |tab_size| {
|
||||
ruff_formatter::IndentWidth::from(NonZeroU8::from(tab_size))
|
||||
}),
|
||||
quote_style: format.quote_style.unwrap_or(format_defaults.quote_style),
|
||||
quote_style,
|
||||
magic_trailing_comma: format
|
||||
.magic_trailing_comma
|
||||
.unwrap_or(format_defaults.magic_trailing_comma),
|
||||
|
|
|
@ -2819,13 +2819,18 @@ pub struct FormatOptions {
|
|||
)]
|
||||
pub indent_style: Option<IndentStyle>,
|
||||
|
||||
/// Whether to prefer single `'` or double `"` quotes for strings. Defaults to double quotes.
|
||||
/// Configures the preferred quote character for strings. Valid options are:
|
||||
///
|
||||
/// * `double` (default): Use double quotes `"`
|
||||
/// * `single`: Use single quotes `'`
|
||||
/// * `preserve` (preview only): Keeps the existing quote character. We don't recommend using this option except for projects
|
||||
/// that already use a mixture of single and double quotes and can't migrate to using double or single quotes.
|
||||
///
|
||||
/// In compliance with [PEP 8](https://peps.python.org/pep-0008/) and [PEP 257](https://peps.python.org/pep-0257/),
|
||||
/// Ruff prefers double quotes for multiline strings and docstrings, regardless of the
|
||||
/// configured quote style.
|
||||
///
|
||||
/// Ruff may also deviate from this option if using the configured quotes would require
|
||||
/// Ruff may also deviate from using the configured quotes if doing so requires
|
||||
/// escaping quote characters within the string. For example, given:
|
||||
///
|
||||
/// ```python
|
||||
|
@ -2834,11 +2839,11 @@ pub struct FormatOptions {
|
|||
/// ```
|
||||
///
|
||||
/// Ruff will change `a` to use single quotes when using `quote-style = "single"`. However,
|
||||
/// `b` will be unchanged, as converting to single quotes would require the inner `'` to be
|
||||
/// escaped, which leads to less readable code: `'It\'s monday morning'`.
|
||||
/// `b` remains unchanged, as converting to single quotes requires escaping the inner `'`,
|
||||
/// which leads to less readable code: `'It\'s monday morning'`. This does not apply when using `preserve`.
|
||||
#[option(
|
||||
default = r#"double"#,
|
||||
value_type = r#""double" | "single""#,
|
||||
value_type = r#""double" | "single" | "preserve""#,
|
||||
example = r#"
|
||||
# Prefer single quotes over double quotes.
|
||||
quote-style = "single"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue