Correctly handle newlines after/before comments#4895
Merged
MichaReiser merged 3 commits intomainfrom Jun 7, 2023
Merged
Conversation
Member
Author
|
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
5dde064 to
5645338
Compare
Contributor
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinuxWindows |
8e5706c to
b2ec055
Compare
b2ec055 to
f23ff61
Compare
This was referenced Jun 7, 2023
konstin
approved these changes
Jun 7, 2023
| // Skip the comment | ||
| let newline_offset = iter | ||
| .as_str() | ||
| .find(['\n', '\r']) |
Member
There was a problem hiding this comment.
Does this handle CR LF correctly?
Member
Author
There was a problem hiding this comment.
Yes. Because we are only interested in finding the offset before the newline. It then doesn't matter whether it is \r, \n or \r\n
Member
Author
|
@MichaReiser started a stack merge that includes this pull request via Graphite. |
Member
Author
|
@MichaReiser merged this pull request with Graphite. |
konstin
pushed a commit
that referenced
this pull request
Jun 13, 2023
<!-- 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 This issue fixes the removal of empty lines between a leading comment and the previous statement: ```python a = 20 # leading comment b = 10 ``` Ruff removed the empty line between `a` and `b` because: * The leading comments formatting does not preserve leading newlines (to avoid adding new lines at the top of a body) * The `JoinNodesBuilder` counted the lines before `b`, which is 1 -> Doesn't insert a new line This is fixed by changing the `JoinNodesBuilder` to count the lines instead *after* the last node. This correctly gives 1, and the `# leading comment` will insert the empty lines between any other leading comment or the node. ## Test Plan I added a new test for empty lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
This issue fixes the removal of empty lines between a leading comment and the previous statement:
Ruff removed the empty line between
aandbbecause:JoinNodesBuildercounted the lines beforeb, which is 1 -> Doesn't insert a new lineThis is fixed by changing the
JoinNodesBuilderto count the lines instead after the last node. This correctly gives 1, and the# leading commentwill insert the empty lines between any other leading comment or the node.Test Plan
I added a new test for empty lines.