ruff/crates
Charlie Marsh a3d4f08f29
Add general support for parenthesized comments on expressions (#6485)
## Summary

This PR adds support for parenthesized comments. A parenthesized comment
is a comment that appears within a parenthesis, but not within the range
of the expression enclosed by the parenthesis. For example, the comment
here is a parenthesized comment:

```python
if (
    # comment
    True
):
    ...
```

The parentheses enclose the `True`, but the range of `True` doesn’t
include the `# comment`.

There are at least two problems associated with parenthesized comments:
(1) associating the comment with the correct (i.e., enclosed) node; and
(2) formatting the comment correctly, once it has been associated with
the enclosed node.

The solution proposed here for (1) is to search for parentheses between
preceding and following node, and use open and close parentheses to
break ties, rather than always assigning to the preceding node.

For (2), we handle these special parenthesized comments in `FormatExpr`.
The biggest risk with this approach is that we forget some codepath that
force-disables parenthesization (by passing in `Parentheses::Never`).
I've audited all usages of that enum and added additional handling +
test coverage for such cases.

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

## Test Plan

`cargo test` with new cases.

Before:

| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75472          |
| django       | 0.99804          |
| transformers | 0.99618          |
| typeshed     | 0.74233          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |

After:

| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75472          |
| django       | 0.99804          |
| transformers | 0.99618          |
| typeshed     | 0.74237          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |
2023-08-15 18:59:18 +00:00
..
flake8_to_ruff Bump version to 0.0.284 (#6453) 2023-08-09 13:32:33 -05:00
ruff Fix PT005 doc (#6596) 2023-08-15 12:48:44 +00:00
ruff_benchmark Remove some extraneous newlines in Cargo.toml (#6577) 2023-08-14 23:39:41 +00:00
ruff_cache Error on zero tab width (#6429) 2023-08-08 16:51:37 -04:00
ruff_cli Remove some extraneous newlines in Cargo.toml (#6577) 2023-08-14 23:39:41 +00:00
ruff_dev Use a faster diffing library for the formatter ecosystem checks (#6497) 2023-08-11 15:51:54 +02:00
ruff_diagnostics Skip partial duplicates when applying multi-edit fixes (#6144) 2023-07-29 12:11:57 +00:00
ruff_formatter Replace inline(always) with inline (#6590) 2023-08-15 08:58:11 +02:00
ruff_index Add unreachable code rule (#5384) 2023-07-04 14:27:23 +00:00
ruff_macros Remove parser dependency from ruff-python-ast (#6096) 2023-07-26 17:47:22 +02:00
ruff_python_ast Replace inline(always) with inline (#6590) 2023-08-15 08:58:11 +02:00
ruff_python_codegen Remove Stmt::TryStar (#6566) 2023-08-14 13:39:44 -04:00
ruff_python_formatter Add general support for parenthesized comments on expressions (#6485) 2023-08-15 18:59:18 +00:00
ruff_python_index Remove some extraneous newlines in Cargo.toml (#6577) 2023-08-14 23:39:41 +00:00
ruff_python_literal Remove allow(pedantic) from formatter (#6549) 2023-08-14 14:02:06 +02:00
ruff_python_parser Remove Stmt::TryStar (#6566) 2023-08-14 13:39:44 -04:00
ruff_python_resolver Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_semantic Remove Stmt::TryStar (#6566) 2023-08-14 13:39:44 -04:00
ruff_python_stdlib Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_trivia Add general support for parenthesized comments on expressions (#6485) 2023-08-15 18:59:18 +00:00
ruff_shrinking Use Jupyter mode while parsing Notebook files (#5552) 2023-08-05 00:32:07 +00:00
ruff_source_file Skip BOM when determining Locator's line starts (#6159) 2023-07-29 11:47:13 +00:00
ruff_text_size Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
ruff_wasm Set a default on PythonVersion (#6446) 2023-08-09 15:19:27 +00:00