From b6be853e25e8f0a77931e8720b21af67119eb7b0 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 4 Dec 2023 12:11:04 -0600 Subject: [PATCH] Update formatter with `FString*Element` nodes --- crates/ruff_python_formatter/generate.py | 12 ++++--- .../src/expression/string/mod.rs | 9 +++-- crates/ruff_python_formatter/src/generated.rs | 36 ------------------- 3 files changed, 14 insertions(+), 43 deletions(-) diff --git a/crates/ruff_python_formatter/generate.py b/crates/ruff_python_formatter/generate.py index 52c256dd17767..16fddd11fbd7e 100755 --- a/crates/ruff_python_formatter/generate.py +++ b/crates/ruff_python_formatter/generate.py @@ -30,10 +30,14 @@ def rustfmt(code: str) -> str: node_lines = ( nodes_file.split("pub enum AnyNode {")[1].split("}")[0].strip().splitlines() ) -nodes = [ - node_line.split("(")[1].split(")")[0].split("::")[-1].split("<")[0] - for node_line in node_lines -] +nodes = [] +for node_line in node_lines: + node = node_line.split("(")[1].split(")")[0].split("::")[-1].split("<")[0] + # These nodes aren't used in the formatter as the formatting of them is handled + # in one of the other nodes containing them. + if node in ("FStringLiteralElement", "FStringExpressionElement"): + continue + nodes.append(node) print(nodes) # %% diff --git a/crates/ruff_python_formatter/src/expression/string/mod.rs b/crates/ruff_python_formatter/src/expression/string/mod.rs index 5a7a6a1b4677b..f5f15f388852c 100644 --- a/crates/ruff_python_formatter/src/expression/string/mod.rs +++ b/crates/ruff_python_formatter/src/expression/string/mod.rs @@ -52,8 +52,11 @@ impl<'a> AnyString<'a> { .trim_start_matches(|c| c != '"' && c != '\''); let triple_quoted = unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r"'''"); - if f_string.value.elements().any(|value| match value { - Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => { + if f_string.value.elements().any(|element| match element { + ast::FStringElement::Expression(ast::FStringExpressionElement { + range, + .. + }) => { let string_content = locator.slice(*range); if triple_quoted { string_content.contains(r#"""""#) || string_content.contains("'''") @@ -61,7 +64,7 @@ impl<'a> AnyString<'a> { string_content.contains(['"', '\'']) } } - _ => false, + ast::FStringElement::Literal(_) => false, }) { Quoting::Preserve } else { diff --git a/crates/ruff_python_formatter/src/generated.rs b/crates/ruff_python_formatter/src/generated.rs index 30b5bb122ae9e..5b01cd16f842b 100644 --- a/crates/ruff_python_formatter/src/generated.rs +++ b/crates/ruff_python_formatter/src/generated.rs @@ -1534,42 +1534,6 @@ impl<'ast> IntoFormat> for ast::ExprCall { } } -impl FormatRule> - for crate::expression::expr_formatted_value::FormatExprFormattedValue -{ - #[inline] - fn fmt(&self, node: &ast::ExprFormattedValue, f: &mut PyFormatter) -> FormatResult<()> { - FormatNodeRule::::fmt(self, node, f) - } -} -impl<'ast> AsFormat> for ast::ExprFormattedValue { - type Format<'a> = FormatRefWithRule< - 'a, - ast::ExprFormattedValue, - crate::expression::expr_formatted_value::FormatExprFormattedValue, - PyFormatContext<'ast>, - >; - fn format(&self) -> Self::Format<'_> { - FormatRefWithRule::new( - self, - crate::expression::expr_formatted_value::FormatExprFormattedValue::default(), - ) - } -} -impl<'ast> IntoFormat> for ast::ExprFormattedValue { - type Format = FormatOwnedWithRule< - ast::ExprFormattedValue, - crate::expression::expr_formatted_value::FormatExprFormattedValue, - PyFormatContext<'ast>, - >; - fn into_format(self) -> Self::Format { - FormatOwnedWithRule::new( - self, - crate::expression::expr_formatted_value::FormatExprFormattedValue::default(), - ) - } -} - impl FormatRule> for crate::expression::expr_f_string::FormatExprFString {