-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly handle newlines after/before comments #4895
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
5dde064
to
5645338
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
8e5706c
to
b2ec055
Compare
b2ec055
to
f23ff61
Compare
// Skip the comment | ||
let newline_offset = iter | ||
.as_str() | ||
.find(['\n', '\r']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this handle CR LF correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@MichaReiser started a stack merge that includes this pull request via Graphite. |
@MichaReiser merged this pull request with Graphite. |
<!-- 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.
Summary
This issue fixes the removal of empty lines between a leading comment and the previous statement:
Ruff removed the empty line between
a
andb
because:JoinNodesBuilder
counted the lines beforeb
, which is 1 -> Doesn't insert a new lineThis 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.