Fix unexpected whitespace removal outside line range#1102
Open
limwz01 wants to merge 2 commits intogoogle:mainfrom
Open
Fix unexpected whitespace removal outside line range#1102limwz01 wants to merge 2 commits intogoogle:mainfrom
limwz01 wants to merge 2 commits intogoogle:mainfrom
Conversation
* save the whitespace prefix in the pytree node even for those occurring with comments * disable the comment splicer's removal of trailing whitespace * add back the whitespace before emitting unformatted line * remove trailing whitespace in comments for enabled lines later when printing TODO: this still will remove trailing whitespace in line just before line range and will ignore trailing whitespace in last line of line range
Member
|
Could you give an example of what this is fixing? |
Author
|
The example is in the linked issue. |
This was referenced Dec 15, 2023
Contributor
|
Resolve conflicts |
Author
|
After a long while of using this with only the problems mentioned in my first post, I've found a further problem that occurs with the following: def main():
if 1:
if 1:
pass
# extra
elif 1:
pass
passApplying the modified yapf on anywhere except including the extra comment line, we will get extra trailing space on the line before the extra comment line. I've not figured out the bug yet. |
bwendling
reviewed
Jan 22, 2024
| Reformat(): the main function exported by this module. | ||
| """ | ||
|
|
||
| import json |
|
|
||
| from yapf_third_party._ylib2to3 import pytree | ||
| from yapf_third_party._ylib2to3.pgen2 import token | ||
| import sys |
| from yapf.yapflib import line_joiner | ||
| from yapf.yapflib import style | ||
| from yapf_third_party._ylib2to3 import pytree | ||
| from yapf_third_party._ylib2to3.pgen2 import token |
| _RetainRequiredVerticalSpacing(lline, prev_line, lines) | ||
| if not _LineHasContinuationMarkers(lline): | ||
| for token in lline.tokens: | ||
| # print( |
Member
There was a problem hiding this comment.
Please remove the debugging comments.
| comment_prefix, | ||
| comment_lineno, | ||
| comment_column, | ||
| "", |
Member
There was a problem hiding this comment.
Use single-quotes for consistency.
| def __init__(self): | ||
| # A list of all logical lines finished visiting so far. | ||
| self._logical_lines = [] | ||
| self.prefix = "" |
| if leaf.type in _WHITESPACE_TOKENS: | ||
| self._StartNewLine() | ||
| if leaf.type == grammar_token.NEWLINE: | ||
| self.prefix += "\n" |
| format_token.FormatToken(leaf, pytree_utils.NodeName(leaf))) | ||
| format_token.FormatToken(leaf, pytree_utils.NodeName(leaf), | ||
| self.prefix)) | ||
| self.prefix = "" |
| def _CreateCommentsFromPrefix(comment_prefix, | ||
| comment_lineno, | ||
| comment_column, | ||
| prefix, |
Member
There was a problem hiding this comment.
Add a default prefix='' so you only have to pass it in when it's not empty.
| if not (index < len(lines) and lines[index].lstrip().startswith("#")): | ||
| break | ||
|
|
||
| # print(repr(comment_block), repr(prefix), file=sys.stderr) |
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.
TODO: