ruff/crates/ruff_python_formatter
Micha Reiser d6daa61563
Handle trailing end-of-line comments in-between-bodies (#4812)
<!--
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

And more custom logic around comments in bodies... uff. 

Let's say we have the following code

```python
if x == y:
    pass # trailing comment of pass
else: # trailing comment of `else`
    print("I have no comments")
```

Right now, the formatter attaches the `# trailing comment of `else` as a trailing comment of `pass` because it doesn't "see" that there's an `else` keyword in between (because the else body is just a Vec and not a node). 

This PR adds custom logic that attaches the trailing comments after the `else` as dangling comments to the `if` statement. The if statement must then split the dangling comments by `comments.text_position()`:
* All comments up to the first end-of-line comment are leading comments of the `else` keyword.
* All end-of-line comments coming after are `trailing` comments for the `else` keyword.


## Test Plan

I added new unit tests.
2023-06-03 15:29:22 +02:00
..
resources/test/fixtures/black Move typos to pre-commit config (#4148) 2023-04-29 12:13:35 -04:00
src Handle trailing end-of-line comments in-between-bodies (#4812) 2023-06-03 15:29:22 +02:00
Cargo.toml Move universal newline handling into its own crate (#4729) 2023-05-31 12:00:47 -04:00
Docs.md Generate FormatRule definitions (#4724) 2023-06-01 08:38:53 +02:00
generate.py Use dummy verbatim formatter for all nodes (#4755) 2023-06-01 08:25:26 +00:00
orphan_rules_in_the_formatter.svg Generate FormatRule definitions (#4724) 2023-06-01 08:38:53 +02:00