diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py index 18f6570f0b4e9e..ef542f52d06140 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/binary.py @@ -334,3 +334,27 @@ + header.isstdcnt # Standard/wall indicators + header.isutcnt # UT/local indicators ) + + +if ( + (1 + 2) # test + or (3 + 4) # other + or (4 + 5) # more +): + pass + + +if ( + (1 and 2) # test + + (3 and 4) # other + + (4 and 5) # more +): + pass + + +if ( + (1 + 2) # test + < (3 + 4) # other + > (4 + 5) # more +): + pass diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 8c77c1114b8f94..eed2090fb5b7ce 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -205,8 +205,9 @@ fn handle_enclosed_comment<'a>( locator, ) } - AnyNodeRef::ExprBoolOp(_) => handle_trailing_bool_expression_comment(comment, locator), - AnyNodeRef::ExprCompare(_) => handle_trailing_bool_expression_comment(comment, locator), + AnyNodeRef::ExprBoolOp(_) | AnyNodeRef::ExprCompare(_) => { + handle_trailing_binary_like_comment(comment, locator) + } AnyNodeRef::Keyword(keyword) => handle_keyword_comment(comment, keyword, locator), AnyNodeRef::PatternKeyword(pattern_keyword) => { handle_pattern_keyword_comment(comment, pattern_keyword, locator) @@ -848,7 +849,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>( /// and 3 == 3 /// ) /// ``` -fn handle_trailing_bool_expression_comment<'a>( +fn handle_trailing_binary_like_comment<'a>( comment: DecoratedComment<'a>, locator: &Locator, ) -> CommentPlacement<'a> { diff --git a/crates/ruff_python_formatter/src/expression/binary_like.rs b/crates/ruff_python_formatter/src/expression/binary_like.rs index 2be7f933cf3617..82d88cc8d8e16d 100644 --- a/crates/ruff_python_formatter/src/expression/binary_like.rs +++ b/crates/ruff_python_formatter/src/expression/binary_like.rs @@ -24,9 +24,9 @@ use crate::prelude::*; #[derive(Copy, Clone, Debug)] pub(super) enum BinaryLike<'a> { - BinaryExpression(&'a ExprBinOp), - CompareExpression(&'a ExprCompare), - BoolExpression(&'a ExprBoolOp), + Binary(&'a ExprBinOp), + Compare(&'a ExprCompare), + Bool(&'a ExprBoolOp), } impl<'a> BinaryLike<'a> { @@ -102,7 +102,7 @@ impl<'a> BinaryLike<'a> { if let Some((left, rest)) = bool_expression.values.split_first() { rec( Operand::Left { - expression: &left, + expression: left, leading_comments, }, comments, @@ -245,15 +245,15 @@ impl<'a> BinaryLike<'a> { let mut parts = SmallVec::new(); match self { - BinaryLike::BinaryExpression(binary) => { + BinaryLike::Binary(binary) => { // Leading and trailing comments are handled by the binary's ``FormatNodeRule` implementation. recurse_binary(binary, &[], &[], comments, source, &mut parts); } - BinaryLike::CompareExpression(compare) => { + BinaryLike::Compare(compare) => { // Leading and trailing comments are handled by the compare's ``FormatNodeRule` implementation. recurse_compare(compare, &[], &[], comments, source, &mut parts); } - BinaryLike::BoolExpression(bool) => { + BinaryLike::Bool(bool) => { recurse_bool(bool, &[], &[], comments, source, &mut parts); } } @@ -262,7 +262,7 @@ impl<'a> BinaryLike<'a> { } const fn is_bool_op(self) -> bool { - matches!(self, BinaryLike::BoolExpression(_)) + matches!(self, BinaryLike::Bool(_)) } } 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 3a87b6d69191f0..8601f6b1df555e 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -14,7 +14,7 @@ pub struct FormatExprBinOp; impl FormatNodeRule for FormatExprBinOp { #[inline] fn fmt_fields(&self, item: &ExprBinOp, f: &mut PyFormatter) -> FormatResult<()> { - BinaryLike::BinaryExpression(item).fmt(f) + BinaryLike::Binary(item).fmt(f) } fn fmt_dangling_comments( diff --git a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs index 3ddab34198183a..27e511dd96b98a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs @@ -12,7 +12,7 @@ pub struct FormatExprBoolOp; impl FormatNodeRule for FormatExprBoolOp { #[inline] fn fmt_fields(&self, item: &ExprBoolOp, f: &mut PyFormatter) -> FormatResult<()> { - BinaryLike::BoolExpression(item).fmt(f) + BinaryLike::Bool(item).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 9ad639a049d51e..8ca05d864a9223 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -15,7 +15,7 @@ pub struct FormatExprCompare; impl FormatNodeRule for FormatExprCompare { #[inline] fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> { - BinaryLike::CompareExpression(item).fmt(f) + BinaryLike::Compare(item).fmt(f) } fn fmt_dangling_comments( diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index fcd3538306df2b..1e2a5c3d94f1bf 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -69,16 +69,6 @@ impl FormatNodeRule for FormatExprSubscript { parenthesized("[", &format_slice, "]") .with_dangling_comments(dangling_comments) .fmt(f) - - // write!( - // f, - // [group(&format_args![ - // token("["), - // trailing_comments(dangling_comments), - // soft_block_indent(&format_slice), - // token("]") - // ])] - // ) } fn fmt_dangling_comments( diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 85f3ddc2c42b46..4d96a9374eda82 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -216,18 +216,12 @@ if True: #[test] fn quick_test() { let src = r#" -return ( - # pypiserver (https://pypi.org/project/pypiserver) - status == 409 - # PyPI / TestPyPI / GCP Artifact Registry - or (status == 400 and any("already exist" in x for x in [reason, text])) - # Nexus Repository OSS (https://www.sonatype.com/nexus-repository-oss) - or (status == 400 and any("updating asset" in x for x in [reason, text])) - # Artifactory (https://jfrog.com/artifactory/) - or (status == 403 and "overwrite artifact" in text) - # Gitlab Enterprise Edition (https://about.gitlab.com) - or (status == 400 and "already been taken" in text) -) +(header.timecnt * 5 # Transition times and types + + header.typecnt * 6 # Local time type records + + header.charcnt # Time zone designations + + header.leapcnt * 8 # Leap second records + + header.isstdcnt # Standard/wall indicators + + header.isutcnt) # UT/local indicators "#; // Tokenize once let mut tokens = Vec::new(); diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap index 5ec38f2dbce2a6..9a4825e084a5ef 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__binary.py.snap @@ -340,6 +340,30 @@ skip_bytes = ( + header.isstdcnt # Standard/wall indicators + header.isutcnt # UT/local indicators ) + + +if ( + (1 + 2) # test + or (3 + 4) # other + or (4 + 5) # more +): + pass + + +if ( + (1 and 2) # test + + (3 and 4) # other + + (4 and 5) # more +): + pass + + +if ( + (1 + 2) # test + < (3 + 4) # other + > (4 + 5) # more +): + pass ``` ## Output @@ -737,6 +761,30 @@ skip_bytes = ( + header.isstdcnt # Standard/wall indicators + header.isutcnt # UT/local indicators ) + + +if ( + (1 + 2) # test + or (3 + 4) # other + or (4 + 5) # more +): + pass + + +if ( + (1 and 2) # test + + (3 and 4) # other + + (4 and 5) # more +): + pass + + +if ( + (1 + 2) # test + < (3 + 4) # other + > (4 + 5) # more +): + pass ```