Skip to content
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

Two fixes for the _AlignTrailingComments(final_lines) function #1021

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lizawang
Copy link

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:

    func( 1 )                                             # Line 1, the maximum length includes pseudo
    func( 2 )                                             # Line 2, the maximum length includes pseudo
    d = { key1: value1, key2: value2, key3: value3} # Line 3, but the pseudo parentheses are not shown
    func( 3 )                                             # Line 4, the maximum length includes pseudo
    func( 4 )                                             # line 5, the maximum length includes pseudo
    

    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

    else:
      line_content += '{}{}'.format(whitespace_prefix, line_tok.value)
    

    into

    elif not line_tok.is_pseudo:
      line_content += '{}{}'.format(whitespace_prefix, line_tok.value)
    
  • 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.

    func( 1 )               # Line 1
    func( 2 )               # Line 2
    d = {                    # line 3 comment 1 where the existing spaces before is 1 space larger than padded spaces
        key1: value1,       # line 3 comment2
        key2: value2,       # line 3 comment3
        key3: value3
    }                       # line 3 comment4
    
    

    The solution is to set the whitespace_prefix of the comment token into empty string when the number of
    existing spaces are larger:

    
    if len(existing_whitespace_prefix)>len(whitespace):
                      line_tok.whitespace_prefix = ''
    
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant