From 2220563bece0c10c42d5d6edddccb0fe0419bd3e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 4 Jan 2024 19:38:12 -0500 Subject: [PATCH 1/3] Parenthesize breaking named expressions in match guards --- .../test/fixtures/ruff/statement/match.py | 5 +++++ .../src/other/match_case.rs | 19 +++++++++++++++++-- .../snapshots/format@statement__match.py.snap | 14 +++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py index f24f9416cc0ef..af1e25c96f785 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py @@ -109,6 +109,11 @@ def foo(): ): # another comment pass + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": + pass + match pattern_comments: case ( diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 785ab73f502b1..55a8dfc6a636c 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -4,7 +4,10 @@ use ruff_python_ast::MatchCase; use crate::builders::parenthesize_if_expands; use crate::comments::SourceComment; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses}; +use crate::expression::maybe_parenthesize_expression; +use crate::expression::parentheses::{ + NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, +}; use crate::prelude::*; use crate::statement::clause::{clause_body, clause_header, ClauseHeader}; @@ -58,7 +61,19 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!(f, [space(), token("if"), space(), guard.format()])?; + write!( + f, + [ + space(), + token("if"), + space(), + maybe_parenthesize_expression( + guard, + item, + Parenthesize::Optional + ) + ] + )?; } Ok(()) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap index dad7a3c526f8b..76b9438b8d4aa 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap @@ -115,6 +115,11 @@ match long_lines: ): # another comment pass + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": + pass + match pattern_comments: case ( @@ -677,7 +682,9 @@ match newlines: match long_lines: - case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment + case "this is a long line for if condition" if ( + aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2 + ): # comment pass case "this is a long line for if condition with parentheses" if ( @@ -696,6 +703,11 @@ match long_lines: ): # another comment pass + case {"long_long_long_key": str(long_long_long_key)} if ( + value := "long long long long long long long long long long long value" + ): + pass + match pattern_comments: case ( From 60d72a12aaca0391d8b1526a5003a83b02d1bf52 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 4 Jan 2024 21:12:29 -0500 Subject: [PATCH 2/3] Use in_parentheses_only_soft_line_break_or_space --- .../src/expression/expr_named_expr.rs | 9 +++++++-- .../src/other/match_case.rs | 19 ++----------------- .../snapshots/format@statement__match.py.snap | 10 ++++------ 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 3fe831c3a0b9a..6b52a7031d2d6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -3,7 +3,9 @@ use ruff_python_ast::AnyNodeRef; use ruff_python_ast::ExprNamedExpr; use crate::comments::{dangling_comments, SourceComment}; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::expression::parentheses::{ + in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses, +}; use crate::prelude::*; #[derive(Default)] @@ -24,7 +26,10 @@ impl FormatNodeRule for FormatExprNamedExpr { write!( f, [ - group(&format_args!(target.format(), soft_line_break_or_space())), + group(&format_args!( + target.format(), + in_parentheses_only_soft_line_break_or_space() + )), token(":=") ] )?; diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 55a8dfc6a636c..785ab73f502b1 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -4,10 +4,7 @@ use ruff_python_ast::MatchCase; use crate::builders::parenthesize_if_expands; use crate::comments::SourceComment; -use crate::expression::maybe_parenthesize_expression; -use crate::expression::parentheses::{ - NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize, -}; +use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses}; use crate::prelude::*; use crate::statement::clause::{clause_body, clause_header, ClauseHeader}; @@ -61,19 +58,7 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!( - f, - [ - space(), - token("if"), - space(), - maybe_parenthesize_expression( - guard, - item, - Parenthesize::Optional - ) - ] - )?; + write!(f, [space(), token("if"), space(), guard.format()])?; } Ok(()) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap index 76b9438b8d4aa..cd64f26de63e0 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__match.py.snap @@ -682,9 +682,7 @@ match newlines: match long_lines: - case "this is a long line for if condition" if ( - aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2 - ): # comment + case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment pass case "this is a long line for if condition with parentheses" if ( @@ -703,9 +701,9 @@ match long_lines: ): # another comment pass - case {"long_long_long_key": str(long_long_long_key)} if ( - value := "long long long long long long long long long long long value" - ): + case { + "long_long_long_key": str(long_long_long_key) + } if value := "long long long long long long long long long long long value": pass From 0e361e10c9ce37e32c2cb18a8fa3ddc2d3de280a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 8 Jan 2024 09:41:09 -0500 Subject: [PATCH 3/3] Update crates/ruff_python_formatter/src/expression/expr_named_expr.rs Co-authored-by: Micha Reiser --- .../ruff_python_formatter/src/expression/expr_named_expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 6b52a7031d2d6..fff39ec4f29a3 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -26,10 +26,10 @@ impl FormatNodeRule for FormatExprNamedExpr { write!( f, [ - group(&format_args!( + group(&format_args![ target.format(), in_parentheses_only_soft_line_break_or_space() - )), + ]), token(":=") ] )?;