Preserve prefixes outside --line-ranges - #5175
Conversation
84afab8 to
682d265
Compare
682d265 to
392adbf
Compare
|
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" )
|
|
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. |
|
@cyphercodes are you still updating this? |
|
Updated — merged current Verification:
|
|
@cyphercodes Please resolve my two review comments above. |
|
Updated — addressed both line-ranges prefix cases from the review:
Verification:
|
| """) | ||
| assert expected == formatted | ||
|
|
||
| def test_line_ranges_preserves_unselected_prefix_trailing_whitespace(self) -> None: |
There was a problem hiding this comment.
Is there a reason these aren't their own test case files? They should be if possible.
| # True when this Leaf is converted from unchanged code for --line-ranges. | ||
| line_ranges_converted: bool = False | ||
| line_ranges_first_lineno: int = 0 | ||
| line_ranges_selected: Optional[set[int]] = None |
There was a problem hiding this comment.
Wouldn't line_ranges_first_lineno != 0 or line_ranges_selected is not None always be equal to line_ranges_converted? Could we replace line_ranges_converted with one of those?
There was a problem hiding this comment.
Also, when I first read through, I assumed that min(line_ranges_selected) would be the same as line_ranges_first_lineno, but I don't think that's always the case after reading through the code. It may be worth adding comments describing the use of both and the differences.
| selected: set[int] = set() | ||
| for start, end in lines: | ||
| selected.update(range(start, end + 1)) |
There was a problem hiding this comment.
Could we use comparison operators (<=/>=) instead of creating a set and checking inclusion?
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