Skip to content

Two fixes for the _AlignTrailingComments(final_lines) function #1021

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
update the two fixes
  • Loading branch information
lizawang committed Aug 18, 2022
commit e1e28c2f7640b60e20e20087aae309de2100f95b
18 changes: 13 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 @@ -361,15 +361,18 @@ def _AlignTrailingComments(final_lines):
aligned_col - pc_line_lengths[pc_line_length_index] - 1)
pc_line_length_index += 1

line_content = []
print('line_tok:', len(line_tok.formatted_whitespace_prefix.lstrip('\n')))
print('padded spaces:', len(whitespace))

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 +381,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