Skip to content

Commit

Permalink
Stabilize 'f_string_implicit_concatenated_string_literal_quotes'
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Oct 24, 2024
1 parent 5201aea commit 45c4985
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 74 deletions.
9 changes: 4 additions & 5 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use crate::expression::parentheses::{
};
use crate::prelude::*;
use crate::preview::{
is_f_string_implicit_concatenated_string_literal_quotes_enabled,
is_hug_parens_with_braces_and_square_brackets_enabled,
is_f_string_formatting_enabled, is_hug_parens_with_braces_and_square_brackets_enabled,
};

mod binary_like;
Expand Down Expand Up @@ -755,7 +754,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
Expr::StringLiteral(ast::ExprStringLiteral { value, .. })
if value.is_implicit_concatenated() =>
{
if !is_f_string_implicit_concatenated_string_literal_quotes_enabled(self.context) {
if !is_f_string_formatting_enabled(self.context) {
self.update_max_precedence(OperatorPrecedence::String);
}

Expand All @@ -764,14 +763,14 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. })
if value.is_implicit_concatenated() =>
{
if !is_f_string_implicit_concatenated_string_literal_quotes_enabled(self.context) {
if !is_f_string_formatting_enabled(self.context) {
self.update_max_precedence(OperatorPrecedence::String);
}

return;
}
Expr::FString(ast::ExprFString { value, .. }) if value.is_implicit_concatenated() => {
if !is_f_string_implicit_concatenated_string_literal_quotes_enabled(self.context) {
if !is_f_string_formatting_enabled(self.context) {
self.update_max_precedence(OperatorPrecedence::String);
}

Expand Down
17 changes: 6 additions & 11 deletions crates/ruff_python_formatter/src/other/f_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use ruff_python_ast::{AnyStringFlags, FString, StringFlags};
use ruff_source_file::Locator;

use crate::prelude::*;
use crate::preview::{
is_f_string_formatting_enabled, is_f_string_implicit_concatenated_string_literal_quotes_enabled,
};
use crate::preview::is_f_string_formatting_enabled;
use crate::string::{Quoting, StringNormalizer, StringQuotes};

use super::f_string_element::FormatFStringElement;
Expand All @@ -31,14 +29,11 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let locator = f.context().locator();

// If the preview style is enabled, make the decision on what quotes to use locally for each
// f-string instead of globally for the entire f-string expression.
let quoting =
if is_f_string_implicit_concatenated_string_literal_quotes_enabled(f.context()) {
Quoting::CanChange
} else {
self.quoting
};
let quoting = if is_f_string_formatting_enabled(f.context()) {
Quoting::CanChange
} else {
self.quoting
};

let normalizer = StringNormalizer::from_context(f.context()).with_quoting(quoting);

Expand Down
8 changes: 1 addition & 7 deletions crates/ruff_python_formatter/src/other/f_string_part.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ruff_python_ast::FStringPart;

use crate::other::f_string::FormatFString;
use crate::other::string_literal::StringLiteralKind;
use crate::prelude::*;
use crate::string::Quoting;

Expand All @@ -26,12 +25,7 @@ impl Format<PyFormatContext<'_>> for FormatFStringPart<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
match self.part {
#[allow(deprecated)]
FStringPart::Literal(string_literal) => string_literal
.format()
.with_options(StringLiteralKind::InImplicitlyConcatenatedFString(
self.quoting,
))
.fmt(f),
FStringPart::Literal(string_literal) => string_literal.format().fmt(f),
FStringPart::FString(f_string) => FormatFString::new(f_string, self.quoting).fmt(f),
}
}
Expand Down
30 changes: 1 addition & 29 deletions crates/ruff_python_formatter/src/other/string_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use ruff_formatter::FormatRuleWithOptions;
use ruff_python_ast::StringLiteral;

use crate::prelude::*;
use crate::preview::is_f_string_implicit_concatenated_string_literal_quotes_enabled;
use crate::string::{docstring, Quoting, StringNormalizer};
use crate::string::{docstring, StringNormalizer};
use crate::QuoteStyle;

#[derive(Default)]
Expand All @@ -28,39 +27,13 @@ pub enum StringLiteralKind {
String,
/// A string literal used as a docstring.
Docstring,
/// A string literal that is implicitly concatenated with an f-string. This
/// makes the overall expression an f-string whose quoting detection comes
/// from the parent node (f-string expression).
#[deprecated]
#[allow(private_interfaces)]
InImplicitlyConcatenatedFString(Quoting),
}

impl StringLiteralKind {
/// Checks if this string literal is a docstring.
pub(crate) const fn is_docstring(self) -> bool {
matches!(self, StringLiteralKind::Docstring)
}

/// Returns the quoting to be used for this string literal.
fn quoting(self, context: &PyFormatContext) -> Quoting {
match self {
StringLiteralKind::String | StringLiteralKind::Docstring => Quoting::CanChange,
#[allow(deprecated)]
StringLiteralKind::InImplicitlyConcatenatedFString(quoting) => {
// Allow string literals to pick the "optimal" quote character
// even if any other fstring in the implicit concatenation uses an expression
// containing a quote character.
// TODO: Remove StringLiteralKind::InImplicitlyConcatenatedFString when promoting
// this style to stable and remove the layout from `AnyStringPart::String`.
if is_f_string_implicit_concatenated_string_literal_quotes_enabled(context) {
Quoting::CanChange
} else {
quoting
}
}
}
}
}

impl FormatNodeRule<StringLiteral> for FormatStringLiteral {
Expand All @@ -75,7 +48,6 @@ impl FormatNodeRule<StringLiteral> for FormatStringLiteral {
};

let normalized = StringNormalizer::from_context(f.context())
.with_quoting(self.layout.quoting(f.context()))
.with_preferred_quote_style(quote_style)
.normalize(item.into());

Expand Down
9 changes: 1 addition & 8 deletions crates/ruff_python_formatter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(

/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
/// WARNING: This preview style depends on `is_f_string_implicit_concatenated_string_literal_quotes_enabled`.
/// TODO: Remove `Quoting` when promoting this preview style and convert `FormatStringPart` etc. regular `FormatWithRule` implementations.
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}

/// See [#13539](https://github.com/astral-sh/ruff/pull/13539)
/// Remove `Quoting` when stabalizing this preview style.
pub(crate) fn is_f_string_implicit_concatenated_string_literal_quotes_enabled(
context: &PyFormatContext,
) -> bool {
context.is_preview()
}

/// Returns `true` if implicitly concatenated strings should be joined if they all fit on a single line.
/// See [#9457](https://github.com/astral-sh/ruff/issues/9457)
/// WARNING: This preview style depends on `is_empty_parameters_no_unnecessary_parentheses_around_return_value_enabled`
Expand Down
12 changes: 1 addition & 11 deletions crates/ruff_python_formatter/src/string/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::comments::{leading_comments, trailing_comments};
use crate::expression::parentheses::in_parentheses_only_soft_line_break_or_space;
use crate::other::f_string::{FStringContext, FStringLayout, FormatFString};
use crate::other::f_string_element::FormatFStringExpressionElement;
use crate::other::string_literal::StringLiteralKind;
use crate::prelude::*;
use crate::preview::{
is_f_string_formatting_enabled, is_join_implicit_concatenated_string_enabled,
Expand Down Expand Up @@ -80,16 +79,7 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedStringExpanded<'_

for part in self.string.parts() {
let format_part = format_with(|f: &mut PyFormatter| match part {
StringLikePart::String(part) => {
let kind = if self.string.is_fstring() {
#[allow(deprecated)]
StringLiteralKind::InImplicitlyConcatenatedFString(quoting)
} else {
StringLiteralKind::String
};

part.format().with_options(kind).fmt(f)
}
StringLikePart::String(part) => part.format().fmt(f),
StringLikePart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
StringLikePart::FString(part) => FormatFString::new(part, quoting).fmt(f),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ hello {
# Implicit concatenated f-string containing quotes
_ = (
'This string should change its quotes to double quotes'
"This string should change its quotes to double quotes"
f'This string uses double quotes in an expression {"it's a quote"}'
f'This f-string does not use any quotes.'
)
Expand Down Expand Up @@ -1482,10 +1482,9 @@ _ = (

# Implicit concatenated f-string containing quotes
_ = (
- 'This string should change its quotes to double quotes'
"This string should change its quotes to double quotes"
- f'This string uses double quotes in an expression {"it's a quote"}'
- f'This f-string does not use any quotes.'
+ "This string should change its quotes to double quotes"
+ f"This string uses double quotes in an expression {"it's a quote"}"
+ f"This f-string does not use any quotes."
)
Expand Down

0 comments on commit 45c4985

Please sign in to comment.