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

fix for indent block comments issue #1005

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions yapf/yapflib/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def Reformat(llines, verify=False, lines=None):

if not lline.disable:
if lline.first.is_comment:
for character in lline.first.value:
if character == '#':
comment_before_tab = True
elif character == '\t':
break

if comment_before_tab:
non_comment_text_index = (len(lline.first.value[1:]) - len(lline.first.value[1:].lstrip())) + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80-column formatting please.

new_string = '# ' + lline.first.value[non_comment_text_index:]

lline.first.value = new_string
lline.first.value = lline.first.value.rstrip()
elif lline.last.is_comment:
lline.last.value = lline.last.value.rstrip()
Expand Down
58 changes: 58 additions & 0 deletions yapftests/reformatter_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3165,6 +3165,64 @@ def testWalrus(self):
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected, reformatter.Reformat(llines))

def testIndentBlockComments(self):
unformatted_code = textwrap.dedent("""\
class Foo(object):
def __init__(self):
# pass
pass
""")

expected = textwrap.dedent("""\
class Foo(object):

def __init__(self):
# pass
pass
""")

llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected, reformatter.Reformat(llines))

def testIndentBlockCommentsMultipleTabs(self):
unformatted_code = textwrap.dedent("""\
class Foo(object):
def __init__(self):
# pass
pass
""")

expected = textwrap.dedent("""\
class Foo(object):

def __init__(self):
# pass
pass
""")

llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected, reformatter.Reformat(llines))

def testIndentBlockCommentsRepeats(self):
unformatted_code = textwrap.dedent("""\
class Foo(object):
def __init__(self):
# pass
# pass
pass
""")

expected = textwrap.dedent("""\
class Foo(object):

def __init__(self):
# pass
# pass
pass
""")

llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected, reformatter.Reformat(llines))

if __name__ == '__main__':
unittest.main()