Skip to content

Commit

Permalink
Fix binary expression formatting with leading comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 8, 2023
1 parent b9826c1 commit 4cf24dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@
# comment
content + b
)


if (
aaaaaaaaaaaaaaaaaa +
# has the child process finished?
bbbbbbbbbbbbbbb +
# the child process has finished, but the
# transport hasn't been notified yet?
ccccccccccc
):
pass
5 changes: 2 additions & 3 deletions crates/ruff_python_formatter/src/expression/expr_bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
]
)?;

// Format the operator on its own line if the operator has trailing comments and the right side has leading comments.
if !operator_comments.is_empty() && comments.has_leading_comments(right.as_ref().into())
{
// Format the operator on its own line if the right side has any leading comments.
if comments.has_leading_comments(right.as_ref().into()) {
write!(f, [hard_line_break()])?;
} else if needs_space {
write!(f, [space()])?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ not (aaaaaaaaaaaaaa + {a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
# comment
content + b
)
if (
aaaaaaaaaaaaaaaaaa +
# has the child process finished?
bbbbbbbbbbbbbbb +
# the child process has finished, but the
# transport hasn't been notified yet?
ccccccccccc
):
pass
```


Expand Down Expand Up @@ -105,6 +116,19 @@ NOT_YET_IMPLEMENTED_ExprUnaryOp
content
+ b
)
if (
aaaaaaaaaaaaaaaaaa
+
# has the child process finished?
bbbbbbbbbbbbbbb
+
# the child process has finished, but the
# transport hasn't been notified yet?
ccccccccccc
):
pass
```


0 comments on commit 4cf24dc

Please sign in to comment.