ruff/crates/ruff_python_formatter/src
Charlie Marsh 1044d66c1c
Add support for PatternMatchMapping formatting (#6836)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

Adds support for `PatternMatchMapping` -- i.e., cases like:

```python
match foo:
    case {"a": 1, "b": 2, **rest}:
        pass
```

Unfortunately, this node has _three_ kinds of dangling comments:

```python
{  # "open parenthesis comment"
   key: pattern,
   **  # end-of-line "double star comment"
   # own-line "double star comment"
   rest  # end-of-line "after rest comment"
   # own-line "after rest comment"
}
```

Some of the complexity comes from the fact that in `**rest`, `rest` is
an _identifier_, not a node, so we have to handle comments _after_ it as
dangling on the enclosing node, rather than trailing on `**rest`. (We
could change the AST to use `PatternMatchAs` there, which would be more
permissive than the grammar but not totally crazy -- `PatternMatchAs` is
used elsewhere to mean "a single identifier".)

Closes https://github.com/astral-sh/ruff/issues/6644.

## Test Plan

`cargo test`
2023-08-25 04:33:34 +00:00
..
comments Add support for PatternMatchMapping formatting (#6836) 2023-08-25 04:33:34 +00:00
expression Respect own-line leading comments before parenthesized nodes (#6820) 2023-08-25 00:18:05 -04:00
module Add empty lines before nested functions and classes (#6206) 2023-08-01 15:30:59 +00:00
other Handle pattern parentheses in FormatPattern (#6800) 2023-08-25 03:45:49 +00:00
pattern Add support for PatternMatchMapping formatting (#6836) 2023-08-25 04:33:34 +00:00
snapshots Remove exception-handler lexing from unused-bound-exception fix (#5851) 2023-07-18 18:27:46 +00:00
statement Allow up to two empty lines after top-level imports (#6777) 2023-08-22 12:27:40 -04:00
type_param Rename Comments methods (#6649) 2023-08-18 06:37:01 +00:00
builders.rs Improve with statement comment handling and expression breaking (#6621) 2023-08-18 03:30:38 +00:00
cli.rs Formatter: Add SourceType to context to enable special formatting for stub files (#6331) 2023-08-04 11:52:26 +00:00
context.rs Implement DerefMut for WithNodeLevel (#6443) 2023-08-11 10:41:48 +00:00
generated.rs Remove Stmt::TryStar (#6566) 2023-08-14 13:39:44 -04:00
lib.rs Maybe parenthesize long constants and names (#6816) 2023-08-24 09:47:57 +00:00
main.rs Formatter: Add SourceType to context to enable special formatting for stub files (#6331) 2023-08-04 11:52:26 +00:00
options.rs Improve with statement comment handling and expression breaking (#6621) 2023-08-18 03:30:38 +00:00
prelude.rs Accept any Into<AnyNodeRef> as Comments arguments (#5205) 2023-06-20 16:49:21 +00:00
verbatim.rs Rename Comments methods (#6649) 2023-08-18 06:37:01 +00:00