Skip to content

Commit fb9b3e2

Browse files
committedOct 23, 2016
Look for last newline to calculate prefix
1 parent aeef5e0 commit fb9b3e2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed
 

‎yapf/yapflib/comment_splicer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _VisitNodeRec(node):
6262
child_prefix = child.prefix.lstrip('\n')
6363
prefix_indent = child_prefix[:child_prefix.find('#')]
6464
if '\n' in prefix_indent:
65-
prefix_indent = prefix_indent[prefix_indent.find('\n') + 1:]
65+
prefix_indent = prefix_indent[prefix_indent.rfind('\n') + 1:]
6666
child.prefix = ''
6767

6868
if child.type == token.NEWLINE:

‎yapftests/reformatter_facebook_test.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,33 @@ def _pack_results_for_constraint_or(cls, combination, constraints):
375375
self.assertCodeEqual(code, reformatted_code)
376376

377377
def testCommentWithNewlinesInPrefix(self):
378-
code = textwrap.dedent("""\
378+
unformatted_code = textwrap.dedent("""\
379379
def foo():
380380
if 0:
381381
return False
382382
383+
383384
#a deadly comment
384385
elif 1:
385386
return True
386387
387388
388389
print(foo())
389390
""")
390-
uwlines = reformatter_test.ParseAndUnwrap(code)
391-
reformatted_code = reformatter.Reformat(uwlines)
392-
self.assertCodeEqual(code, reformatted_code)
391+
expected_formatted_code = textwrap.dedent("""\
392+
def foo():
393+
if 0:
394+
return False
395+
396+
#a deadly comment
397+
elif 1:
398+
return True
399+
400+
401+
print(foo())
402+
""")
403+
uwlines = reformatter_test.ParseAndUnwrap(unformatted_code)
404+
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
393405

394406

395407
if __name__ == '__main__':

0 commit comments

Comments
 (0)
Please sign in to comment.