Fix comment formatting for yielded tuples (#6603)

## Summary
Closes https://github.com/astral-sh/ruff/issues/6384, although I think
the issue was fixed already on main, for the most part.

The linked issue is around formatting expressions like:

```python
def test():
    (
        yield 
        #comment 1
        * # comment 2
        # comment 3
        test # comment 4
    )

```

On main, prior to this PR, we now format like:

```python
def test():
    (
        yield (
            # comment 1
            # comment 2
            # comment 3
            *test
        )  # comment 4
    )
```

Which strikes me as reasonable. (We can't test this, since it's a syntax
error after for our parser, despite being a syntax error in both cases
from CPython's perspective.)

Meanwhile, Black does:

```python
def test():
    (
        yield
        # comment 1
        *  # comment 2
        # comment 3
        test  # comment 4
    )
```

So our formatting differs in that we move comments between the star and
the expression above the star.

As of this PR, we also support formatting this input, which is valid:

```python
def test():
    (
        yield 
        #comment 1
        * # comment 2
        # comment 3
        test, # comment 4
        1
    )
```

Like:

```python
def test():
    (
        yield (
            # comment 1
            (
                # comment 2
                # comment 3
                *test,  # comment 4
                1,
            )
        )
    )
```

There were two fixes here: (1) marking starred comments as dangling and
formatting them properly; and (2) supporting parenthesized comments for
tuples that don't contain their own parentheses, as is often the case
for yielded tuples (previously, we hit a debug assert).

Note that this diff

## Test Plan
cargo test
This commit is contained in:
Charlie Marsh 2023-08-16 09:41:07 -04:00 committed by GitHub
parent 7ee2ae8395
commit 12f3c4c931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 21 deletions

View file

@ -29,6 +29,14 @@ call(
[What, i, this, s, very, long, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
] # trailing value comment
)
call(
x,
* # Trailing star comment
( # Leading value comment
y
)
)
```
## Output
@ -52,8 +60,7 @@ call(
call(
# Leading starred comment
# Leading value comment
*(
*( # Leading value comment
[
What,
i,
@ -83,6 +90,14 @@ call(
]
], # trailing value comment
)
call(
x,
# Trailing star comment
*( # Leading value comment
y
),
)
```

View file

@ -63,6 +63,14 @@ def foo():
yield from (yield l)
(
yield
#comment 1
* # comment 2
# comment 3
test, # comment 4
1
)
```
## Output
@ -115,6 +123,18 @@ def foo():
pass
yield from (yield l)
(
yield (
# comment 1
(
# comment 2
# comment 3
*test, # comment 4
1,
)
)
)
```