Skip to content

Commit f4ed639

Browse files
committed
Update formatter with FString*Element nodes
1 parent 7b7d095 commit f4ed639

File tree

5 files changed

+108
-39
lines changed

5 files changed

+108
-39
lines changed

crates/ruff_python_formatter/src/expression/string/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,19 @@ impl<'a> AnyString<'a> {
5252
.trim_start_matches(|c| c != '"' && c != '\'');
5353
let triple_quoted =
5454
unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r"'''");
55-
if f_string.value.elements().any(|value| match value {
56-
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => {
55+
if f_string.value.elements().any(|element| match element {
56+
ast::FStringElement::Expression(ast::FStringExpressionElement {
57+
range,
58+
..
59+
}) => {
5760
let string_content = locator.slice(*range);
5861
if triple_quoted {
5962
string_content.contains(r#"""""#) || string_content.contains("'''")
6063
} else {
6164
string_content.contains(['"', '\''])
6265
}
6366
}
64-
_ => false,
67+
ast::FStringElement::Literal(_) => false,
6568
}) {
6669
Quoting::Preserve
6770
} else {

crates/ruff_python_formatter/src/generated.rs

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,42 +1534,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprCall {
15341534
}
15351535
}
15361536

1537-
impl FormatRule<ast::ExprFormattedValue, PyFormatContext<'_>>
1538-
for crate::expression::expr_formatted_value::FormatExprFormattedValue
1539-
{
1540-
#[inline]
1541-
fn fmt(&self, node: &ast::ExprFormattedValue, f: &mut PyFormatter) -> FormatResult<()> {
1542-
FormatNodeRule::<ast::ExprFormattedValue>::fmt(self, node, f)
1543-
}
1544-
}
1545-
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExprFormattedValue {
1546-
type Format<'a> = FormatRefWithRule<
1547-
'a,
1548-
ast::ExprFormattedValue,
1549-
crate::expression::expr_formatted_value::FormatExprFormattedValue,
1550-
PyFormatContext<'ast>,
1551-
>;
1552-
fn format(&self) -> Self::Format<'_> {
1553-
FormatRefWithRule::new(
1554-
self,
1555-
crate::expression::expr_formatted_value::FormatExprFormattedValue::default(),
1556-
)
1557-
}
1558-
}
1559-
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprFormattedValue {
1560-
type Format = FormatOwnedWithRule<
1561-
ast::ExprFormattedValue,
1562-
crate::expression::expr_formatted_value::FormatExprFormattedValue,
1563-
PyFormatContext<'ast>,
1564-
>;
1565-
fn into_format(self) -> Self::Format {
1566-
FormatOwnedWithRule::new(
1567-
self,
1568-
crate::expression::expr_formatted_value::FormatExprFormattedValue::default(),
1569-
)
1570-
}
1571-
}
1572-
15731537
impl FormatRule<ast::ExprFString, PyFormatContext<'_>>
15741538
for crate::expression::expr_f_string::FormatExprFString
15751539
{
@@ -2148,6 +2112,78 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExceptHandlerExceptHandler
21482112
}
21492113
}
21502114

2115+
impl FormatRule<ast::FStringExpressionElement, PyFormatContext<'_>>
2116+
for crate::other::f_string_expression_element::FormatFStringExpressionElement
2117+
{
2118+
#[inline]
2119+
fn fmt(&self, node: &ast::FStringExpressionElement, f: &mut PyFormatter) -> FormatResult<()> {
2120+
FormatNodeRule::<ast::FStringExpressionElement>::fmt(self, node, f)
2121+
}
2122+
}
2123+
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::FStringExpressionElement {
2124+
type Format<'a> = FormatRefWithRule<
2125+
'a,
2126+
ast::FStringExpressionElement,
2127+
crate::other::f_string_expression_element::FormatFStringExpressionElement,
2128+
PyFormatContext<'ast>,
2129+
>;
2130+
fn format(&self) -> Self::Format<'_> {
2131+
FormatRefWithRule::new(
2132+
self,
2133+
crate::other::f_string_expression_element::FormatFStringExpressionElement::default(),
2134+
)
2135+
}
2136+
}
2137+
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::FStringExpressionElement {
2138+
type Format = FormatOwnedWithRule<
2139+
ast::FStringExpressionElement,
2140+
crate::other::f_string_expression_element::FormatFStringExpressionElement,
2141+
PyFormatContext<'ast>,
2142+
>;
2143+
fn into_format(self) -> Self::Format {
2144+
FormatOwnedWithRule::new(
2145+
self,
2146+
crate::other::f_string_expression_element::FormatFStringExpressionElement::default(),
2147+
)
2148+
}
2149+
}
2150+
2151+
impl FormatRule<ast::FStringLiteralElement, PyFormatContext<'_>>
2152+
for crate::other::f_string_literal_element::FormatFStringLiteralElement
2153+
{
2154+
#[inline]
2155+
fn fmt(&self, node: &ast::FStringLiteralElement, f: &mut PyFormatter) -> FormatResult<()> {
2156+
FormatNodeRule::<ast::FStringLiteralElement>::fmt(self, node, f)
2157+
}
2158+
}
2159+
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::FStringLiteralElement {
2160+
type Format<'a> = FormatRefWithRule<
2161+
'a,
2162+
ast::FStringLiteralElement,
2163+
crate::other::f_string_literal_element::FormatFStringLiteralElement,
2164+
PyFormatContext<'ast>,
2165+
>;
2166+
fn format(&self) -> Self::Format<'_> {
2167+
FormatRefWithRule::new(
2168+
self,
2169+
crate::other::f_string_literal_element::FormatFStringLiteralElement::default(),
2170+
)
2171+
}
2172+
}
2173+
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::FStringLiteralElement {
2174+
type Format = FormatOwnedWithRule<
2175+
ast::FStringLiteralElement,
2176+
crate::other::f_string_literal_element::FormatFStringLiteralElement,
2177+
PyFormatContext<'ast>,
2178+
>;
2179+
fn into_format(self) -> Self::Format {
2180+
FormatOwnedWithRule::new(
2181+
self,
2182+
crate::other::f_string_literal_element::FormatFStringLiteralElement::default(),
2183+
)
2184+
}
2185+
}
2186+
21512187
impl FormatRule<ast::PatternMatchValue, PyFormatContext<'_>>
21522188
for crate::pattern::pattern_match_value::FormatPatternMatchValue
21532189
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use ruff_python_ast::FStringExpressionElement;
2+
3+
use crate::prelude::*;
4+
5+
#[derive(Default)]
6+
pub struct FormatFStringExpressionElement;
7+
8+
impl FormatNodeRule<FStringExpressionElement> for FormatFStringExpressionElement {
9+
fn fmt_fields(
10+
&self,
11+
_item: &FStringExpressionElement,
12+
_f: &mut PyFormatter,
13+
) -> FormatResult<()> {
14+
unreachable!("Handled inside of `FormatExprFString");
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use ruff_python_ast::FStringLiteralElement;
2+
3+
use crate::prelude::*;
4+
5+
#[derive(Default)]
6+
pub struct FormatFStringLiteralElement;
7+
8+
impl FormatNodeRule<FStringLiteralElement> for FormatFStringLiteralElement {
9+
fn fmt_fields(&self, _item: &FStringLiteralElement, _f: &mut PyFormatter) -> FormatResult<()> {
10+
unreachable!("Handled inside of `FormatExprFString");
11+
}
12+
}

crates/ruff_python_formatter/src/other/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub(crate) mod decorator;
77
pub(crate) mod elif_else_clause;
88
pub(crate) mod except_handler_except_handler;
99
pub(crate) mod f_string;
10+
pub(crate) mod f_string_expression_element;
11+
pub(crate) mod f_string_literal_element;
1012
pub(crate) mod identifier;
1113
pub(crate) mod keyword;
1214
pub(crate) mod match_case;

0 commit comments

Comments
 (0)