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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 10 additions & 5 deletions yapf/yapflib/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down