Skip to content

Commit

Permalink
Update formatter with FString*Element nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Dec 6, 2023
1 parent a36eebd commit b6be853
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
12 changes: 8 additions & 4 deletions crates/ruff_python_formatter/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

# %%
Expand Down
9 changes: 6 additions & 3 deletions crates/ruff_python_formatter/src/expression/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ 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("'''")
} else {
string_content.contains(['"', '\''])
}
}
_ => false,
ast::FStringElement::Literal(_) => false,
}) {
Quoting::Preserve
} else {
Expand Down
36 changes: 0 additions & 36 deletions crates/ruff_python_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,42 +1534,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprCall {
}
}

impl FormatRule<ast::ExprFormattedValue, PyFormatContext<'_>>
for crate::expression::expr_formatted_value::FormatExprFormattedValue
{
#[inline]
fn fmt(&self, node: &ast::ExprFormattedValue, f: &mut PyFormatter) -> FormatResult<()> {
FormatNodeRule::<ast::ExprFormattedValue>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> 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<PyFormatContext<'ast>> 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<ast::ExprFString, PyFormatContext<'_>>
for crate::expression::expr_f_string::FormatExprFString
{
Expand Down

0 comments on commit b6be853

Please sign in to comment.