Repository: psf/black

Issue:
`if` clauses in `match`-`case` blocks aren't reformatted to fit the line width limit
<!--
Please make sure that the bug is not already fixed either in newer versions or the
current development version. To confirm this, you have three options:

1. Update Black's version if a newer release exists: `pip install -U black`
2. Use the online formatter at <https://black.vercel.app/?version=main>, which will use
   the latest main branch.
3. Or run _Black_ on your machine:
   - create a new virtualenv (make sure it's the same Python version);
   - clone this repository;
   - run `pip install -e .[d]`;
   - run `pip install -r test_requirements.txt`
   - make sure it's sane by running `python -m pytest`; and
   - run `black` like you did last time.
-->

**Describe the bug**

<!-- A clear and concise description of what the bug is. -->

`if` clauses in `match`-`case` blocks in are not reformatted when the line length is above the maximum length set.

**To Reproduce**

<!--
Minimal steps to reproduce the behavior with source code and Black's configuration.
-->

Take this code, where the second line is 99 characters (11 characters above the horizontal limit):

```python
match "test":
    case "test" if "any long condition" != "another long condition" and "this is a long condition":
        print("Test")

```

And try formatting it:

```sh
$ black file.py
```

Black leaves the file unchanged:

```
All done! ✨ 🍰 ✨
1 file left unchanged.
```


**Expected behavior**

Black should reformat the file as:

```python
match "test":
    case "test" if (
        "any long condition" != "another long condition" and "this is a long condition"
    ):
        print("Test")
```

<!-- A clear and concise description of what you expected to happen. -->

**Environment**

<!-- Please complete the following information: -->

- Black's version: 23.7.0
- OS and Python version: Ubuntu Linux 22.04 (running on the Windows Subsystem for Linux) / Python 3.10.4 <!-- e.g. [Linux/Python 3.7.4rc1] -->

**Additional context**

This appears to happen regardless of the line length.  Also, interestingly, if parentheses are added around the boolean expression manually, Black _does_ format the file correctly.

<!-- Add any other context about the problem here. -->



Discussion:
We now format this as
```python
match "test":
    case (
        "test"
    ) if "any long condition" != "another long condition" and "this is a long condition":
        print("Test")
```

Which still isn't great.

Resolving patch:
diff --git a/src/black/linegen.py b/src/black/linegen.py
--- a/src/black/linegen.py
+++ b/src/black/linegen.py
@@ -1310,6 +1310,16 @@ def normalize_invisible_parens(  # noqa: C901
                 child, parens_after={"case"}, mode=mode, features=features
             )
 
+        # Add parentheses around if guards in case blocks
+        if (
+            isinstance(child, Node)
+            and child.type == syms.guard
+            and Preview.parens_for_long_if_clauses_in_case_block in mode
+        ):
+            normalize_invisible_parens(
+                child, parens_after={"if"}, mode=mode, features=features
+            )
+
         # Add parentheses around long tuple unpacking in assignments.
         if (
             index == 0
diff --git a/src/black/mode.py b/src/black/mode.py
--- a/src/black/mode.py
+++ b/src/black/mode.py
@@ -180,6 +180,7 @@ class Preview(Enum):
     is_simple_lookup_for_doublestar_expression = auto()
     docstring_check_for_newline = auto()
     remove_redundant_guard_parens = auto()
+    parens_for_long_if_clauses_in_case_block = auto()
 
 
 UNSTABLE_FEATURES: Set[Preview] = {


Test patch:
diff --git a/tests/data/cases/pattern_matching_complex.py b/tests/data/cases/pattern_matching_complex.py
--- a/tests/data/cases/pattern_matching_complex.py
+++ b/tests/data/cases/pattern_matching_complex.py
@@ -83,7 +83,7 @@
 match x:
     case [0]:
         y = 0
-    case [1, 0] if (x := x[:0]):
+    case [1, 0] if x := x[:0]:
         y = 1
     case [1, 0]:
         y = 2
diff --git a/tests/data/cases/pattern_matching_with_if_stmt.py b/tests/data/cases/pattern_matching_with_if_stmt.py
new file mode 100644
--- /dev/null
+++ b/tests/data/cases/pattern_matching_with_if_stmt.py
@@ -0,0 +1,72 @@
+# flags: --preview --minimum-version=3.10
+match match:
+    case "test" if case != "not very loooooooooooooog condition":  # comment
+        pass
+
+match smth:
+    case "test" if "any long condition" != "another long condition" and "this is a long condition":
+        pass
+    case test if "any long condition" != "another long condition" and "this is a looooong condition":
+        pass
+    case test if "any long condition" != "another long condition" and "this is a looooong condition":  # some additional comments
+        pass
+    case test if (True): # some comment
+        pass
+    case test if (False
+        ): # some comment
+        pass
+    case test if (True  # some comment
+        ):
+        pass  # some comment
+    case cases if (True  # some comment
+                   ): # some other comment
+        pass  # some comment
+    case match if (True  # some comment
+                   ):
+        pass  # some comment
+
+# case black_test_patma_052 (originally in the pattern_matching_complex test case)
+match x:
+    case [1, 0] if x := x[:0]:
+        y = 1
+    case [1, 0] if (x := x[:0]):
+        y = 1
+
+# output
+
+match match:
+    case "test" if case != "not very loooooooooooooog condition":  # comment
+        pass
+
+match smth:
+    case "test" if (
+        "any long condition" != "another long condition" and "this is a long condition"
+    ):
+        pass
+    case test if (
+        "any long condition" != "another long condition"
+        and "this is a looooong condition"
+    ):
+        pass
+    case test if (
+        "any long condition" != "another long condition"
+        and "this is a looooong condition"
+    ):  # some additional comments
+        pass
+    case test if True:  # some comment
+        pass
+    case test if False:  # some comment
+        pass
+    case test if True:  # some comment
+        pass  # some comment
+    case cases if True:  # some comment  # some other comment
+        pass  # some comment
+    case match if True:  # some comment
+        pass  # some comment
+
+# case black_test_patma_052 (originally in the pattern_matching_complex test case)
+match x:
+    case [1, 0] if x := x[:0]:
+        y = 1
+    case [1, 0] if x := x[:0]:
+        y = 1