diff --git a/CHANGELOG b/CHANGELOG index 5ac3e6329..13b030231 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,13 @@ # All notable changes to this project will be documented in this file. # This project adheres to [Semantic Versioning](http://semver.org/). +## [0.41.2] 2022-08-30 +### Fixed +- Fixed a problem in _AlignTrailingComments(final_lines). + Exclude the pseudo parantheses when calculating the line length before the comment. +- Fixed another problem when the number of the existing spaces before the + comment is larger than the padding spaces needed to align. + ## [0.40.0] UNRELEASED ### Added - Add a new Python parser to generate logical lines. diff --git a/yapf/yapflib/reformatter.py b/yapf/yapflib/reformatter.py index 14e0bde70..c0530c935 100644 --- a/yapf/yapflib/reformatter.py +++ b/yapf/yapflib/reformatter.py @@ -321,7 +321,7 @@ def _AlignTrailingComments(final_lines): if line_tok.is_comment: pc_line_lengths.append(len(line_content)) - else: + elif not line_tok.is_pseudo: line_content += '{}{}'.format(whitespace_prefix, line_tok.value) if pc_line_lengths: @@ -362,14 +362,14 @@ def _AlignTrailingComments(final_lines): pc_line_length_index += 1 line_content = [] - + padded_space = whitespace for comment_line_index, comment_line in enumerate( line_tok.value.split('\n')): - line_content.append('{}{}'.format(whitespace, + line_content.append('{}{}'.format(padded_space, comment_line.strip())) if comment_line_index == 0: - whitespace = ' ' * (aligned_col - 1) + padded_space = ' ' * (aligned_col - 1) line_content = '\n'.join(line_content) @@ -378,7 +378,12 @@ def _AlignTrailingComments(final_lines): existing_whitespace_prefix = \ line_tok.formatted_whitespace_prefix.lstrip('\n') - if line_content.startswith(existing_whitespace_prefix): + # In case that the existing spaces larger than + # spaces that needed to pad, set the whitespace_prefix to empty + if len(existing_whitespace_prefix)>len(whitespace): + line_tok.whitespace_prefix = '' + + elif line_content.startswith(existing_whitespace_prefix): line_content = line_content[len(existing_whitespace_prefix):] line_tok.value = line_content