From 4cf24dc5be9947051e61898d08a834c45b6e9c2f Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 8 Jun 2023 16:58:40 +0200 Subject: [PATCH] Fix binary expression formatting with leading comments --- .../ruff/expression/binary_expression.py | 11 +++++++++ .../src/expression/expr_bin_op.rs | 5 ++-- ...est__expression__binary_expression_py.snap | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary_expression.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary_expression.py index 90042fa065a667..c27141d22b2130 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary_expression.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary_expression.py @@ -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 diff --git a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs index 1016895c2a220f..0700bc888df1cc 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -90,9 +90,8 @@ impl FormatNodeRule 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()])?; diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_expression_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_expression_py.snap index 92a4f61a785e02..ab5311bf8a858b 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_expression_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_expression_py.snap @@ -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 ``` @@ -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 ```