Preserve prefixes outside --line-ranges#5175
Open
cyphercodes wants to merge 1 commit into
Open
Conversation
84afab8 to
682d265
Compare
682d265 to
392adbf
Compare
Collaborator
|
Thanks - this fixes issues where whitespace at the beginning of lines before comments is stripped when using As an example: # format whitespace
print( "format me" )
# don't format whitespace
print("don't format me" )
Output with # format whitespace
print("format me")
# don't format whitespace
print("don't format me" )
[empty line at EOF]Expected output: # format whitespace
print( "format me" )
# don't format whitespace
print("don't format me" )
|
Collaborator
|
I also found an edge case where this fix doesn't seem to be applied. Example with print( "format me" )
# format whitespace
print("don't format me" )The spaces before the comment are retained, but they should be removed here. They are removed when running Black on the whole file. |
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.
Description
Preserves comments and blank lines that are outside the requested
--line-rangesinstead of normalizing them while unchanged code is converted to standalone-comment leaves for range formatting.This keeps the issue repro constrained to the requested lines: comments and separation before unchanged statements are preserved when the selected range ends immediately before them.
Fixes #4430.
Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?Verification
python -m black --check src/black/comments.py src/black/linegen.py src/black/lines.py src/black/ranges.py tests/test_ranges.pypython -m pytest tests/test_ranges.py tests/test_format.py -qgit diff --check