Skip to content

Commit 35d6b7c

Browse files
committedOct 22, 2016
Don't count newlines as part of cmt prefix indent
Closes google#327
1 parent 1f340d6 commit 35d6b7c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
 

‎CHANGELOG

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# All notable changes to this project will be documented in this file.
33
# This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.13.2] 2016-10-22
6+
### Fixed
7+
- REGRESSION: A comment may have a prefix with newlines in it. When calculating
8+
the prefix indent, we cannot take the newlines into account. Otherwise, the
9+
comment will be misplaced causing the code to fail.
10+
511
## [0.13.1] 2016-10-17
612
### Fixed
713
- Correct emitting a diff that was accidentally removed.

‎yapf/yapflib/comment_splicer.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def _VisitNodeRec(node):
6161
# child in the next iteration...
6262
child_prefix = child.prefix.lstrip('\n')
6363
prefix_indent = child_prefix[:child_prefix.find('#')]
64+
if '\n' in prefix_indent:
65+
prefix_indent = prefix_indent[prefix_indent.find('\n') + 1:]
6466
child.prefix = ''
6567

6668
if child.type == token.NEWLINE:

‎yapftests/reformatter_test.py

+17
Original file line numberDiff line numberDiff line change
@@ -4056,6 +4056,23 @@ def _pack_results_for_constraint_or(cls, combination, constraints):
40564056
reformatted_code = reformatter.Reformat(uwlines)
40574057
self.assertCodeEqual(code, reformatted_code)
40584058

4059+
def testCommentWithNewlinesInPrefix(self):
4060+
code = textwrap.dedent("""\
4061+
def foo():
4062+
if 0:
4063+
return False
4064+
4065+
#a deadly comment
4066+
elif 1:
4067+
return True
4068+
4069+
4070+
print(foo())
4071+
""")
4072+
uwlines = _ParseAndUnwrap(code)
4073+
reformatted_code = reformatter.Reformat(uwlines)
4074+
self.assertCodeEqual(code, reformatted_code)
4075+
40594076

40604077
def _ParseAndUnwrap(code, dumptree=False):
40614078
"""Produces unwrapped lines from the given code.

0 commit comments

Comments
 (0)
Please sign in to comment.