Two fixes for the _AlignTrailingComments(final_lines) function #1021
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.
Hello all yapf founders!
I want to suggest two fixes for the _AlignTrailingComments(final_lines) function.
Two fixes
When INDENT_DICTIONARY_VALUE is set True (default setting for yapf and google style), there will be pseudo parentheses added around dictionary values in the logical line, and they will be added when calculating the maximum line length before inline comment, but in output lines, the pseudo parentheses are excluded, so the following will happen:
So when appending to the line content that is before the inline comment, do not include the pseudo
parentheses around the dictionary values by changing the condition
into
Fix the case when the existing space before the inline comment is larger than the padding spaces needed to align.
Some edge cases (often happens to comments inside an object(e.g. a dictionary) logical line as following) when _AnalyzeSolutionSpace(state) is False for the logical line, the function _RetainHorizontalSpacing(lline) is called where spaces_required_before as a list is replaced with the actual spaces before the comment token. And if the number of the actual spaces before (aka the existing spaces before) the comment token is larger than the spaces needed to pad for alignment, the comment token will keep being added with the padding spaces and keep moving further towards right each time you run.
The solution is to set the whitespace_prefix of the comment token into empty string when the number of
existing spaces are larger: