From 5326ca187341e6d3ff9a32fb7dab348fa7f25a4c Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 16 Apr 2024 09:11:09 +0200 Subject: [PATCH] parens_for_long_if_clauses_in_case_block preview style --- .../src/expression/expr_named.rs | 1 + .../src/other/match_case.rs | 19 ++++++++- crates/ruff_python_formatter/src/preview.rs | 5 +++ ...ove_redundant_parens_in_case_guard.py.snap | 38 +++--------------- .../snapshots/format@statement__match.py.snap | 39 ++++++++++++++++++- 5 files changed, 67 insertions(+), 35 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_named.rs b/crates/ruff_python_formatter/src/expression/expr_named.rs index 5f8ec7f291..dcda9af468 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named.rs @@ -74,6 +74,7 @@ impl NeedsParentheses for ExprNamed { || parent.is_stmt_delete() || parent.is_stmt_for() || parent.is_stmt_function_def() + || parent.is_match_case() { OptionalParentheses::Always } else { diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 785ab73f50..5d2f6570a7 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -4,8 +4,12 @@ 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::preview::parens_for_long_if_clauses_in_case_block; use crate::statement::clause::{clause_body, clause_header, ClauseHeader}; #[derive(Default)] @@ -58,7 +62,18 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!(f, [space(), token("if"), space(), guard.format()])?; + write!(f, [space(), token("if"), space(),])?; + + if parens_for_long_if_clauses_in_case_block(f.context()) { + maybe_parenthesize_expression( + guard, + item, + Parenthesize::IfBreaksOrIfRequired, + ) + .fmt(f)?; + } else { + guard.format().fmt(f)?; + } } Ok(()) diff --git a/crates/ruff_python_formatter/src/preview.rs b/crates/ruff_python_formatter/src/preview.rs index a403e4a801..109a908eac 100644 --- a/crates/ruff_python_formatter/src/preview.rs +++ b/crates/ruff_python_formatter/src/preview.rs @@ -22,3 +22,8 @@ pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool pub(crate) fn is_with_single_item_pre_39_enabled(context: &PyFormatContext) -> bool { context.is_preview() } + +/// Returns `true` if the [`parens_for_long_if_clauses_in_case_block`](https://github.com/psf/black/pull/4269) preview style is enabled. +pub(crate) fn parens_for_long_if_clauses_in_case_block(context: &PyFormatContext) -> bool { + context.is_preview() +} diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__remove_redundant_parens_in_case_guard.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__remove_redundant_parens_in_case_guard.py.snap index 6f0c6257e3..d848db4ce8 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__remove_redundant_parens_in_case_guard.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__remove_redundant_parens_in_case_guard.py.snap @@ -69,20 +69,7 @@ match 1: ```diff --- Black +++ Ruff -@@ -1,10 +1,10 @@ - match 1: -- case _ if True: -+ case _ if (True): - pass - - - match 1: -- case _ if True: -+ case _ if (True): - pass - - -@@ -25,27 +25,33 @@ +@@ -25,12 +25,16 @@ match 1: @@ -101,18 +88,7 @@ match 1: pass - match 1: -- case _ if True: # this is a comment -+ case _ if (True): # this is a comment - pass - - - match 1: -- case _ if True: # comment over the line limit unless parens are removed x -+ case _ if ( -+ True -+ ): # comment over the line limit unless parens are removed x - pass +@@ -45,7 +49,7 @@ match 1: @@ -129,12 +105,12 @@ match 1: ```python match 1: - case _ if (True): + case _ if True: pass match 1: - case _ if (True): + case _ if True: pass @@ -169,14 +145,12 @@ match 1: match 1: - case _ if (True): # this is a comment + case _ if True: # this is a comment pass match 1: - case _ if ( - True - ): # comment over the line limit unless parens are removed x + case _ if True: # comment over the line limit unless parens are removed x pass 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 cd64f26de6..4eb1195783 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 @@ -1235,4 +1235,41 @@ match x: ``` - +## Preview changes +```diff +--- Stable ++++ Preview +@@ -82,7 +82,9 @@ + + + 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 ( +@@ -90,7 +92,7 @@ + ): # comment + pass + +- case "named expressions aren't special" if foo := 1: ++ case "named expressions aren't special" if (foo := 1): + pass + + case "named expressions aren't that special" if (foo := 1): +@@ -101,9 +103,9 @@ + ): # 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 + + +```