Skip to content

Commit

Permalink
Don't enforce a split before a comment after a container opening.
Browse files Browse the repository at this point in the history
  • Loading branch information
bwendling committed Oct 17, 2017
1 parent bd9d955 commit d404b70
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- When calculating the split penalty for a "trailer", process the child nodes
afterwards because their penalties may change. For example if a list
comprehension is an argument.
- Don't enforce a split before a comment after the opening of a container if it
doesn't it on the current line. We try hard not to move such comments around.

## [0.19.0] 2017-10-14
### Added
Expand Down
20 changes: 10 additions & 10 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,15 @@ def SurroundedByParens(token):
return True

if (previous.OpensScope() and not current.OpensScope() and
not current.is_comment and
format_token.Subtype.SUBSCRIPT_BRACKET not in previous.subtypes):
if not current.is_comment:
if pprevious and not pprevious.is_keyword and not pprevious.is_name:
# We want to split if there's a comment in the container.
token = current
while token != previous.matching_bracket:
if token.is_comment:
return True
token = token.next_token

if pprevious and not pprevious.is_keyword and not pprevious.is_name:
# We want to split if there's a comment in the container.
token = current
while token != previous.matching_bracket:
if token.is_comment:
return True
token = token.next_token
if previous.value == '(':
pptoken = previous.previous_token
if not pptoken or not pptoken.is_name:
Expand All @@ -431,7 +430,7 @@ def SurroundedByParens(token):
return current.next_token != previous.matching_bracket
else:
# Split after the opening of a container if it doesn't fit on the
# current line or if it has a comment.
# current line.
if not self._FitsOnLine(previous, previous.matching_bracket):
return True

Expand Down Expand Up @@ -756,6 +755,7 @@ def ImplicitStringConcatenation(tok):
return length + self.stack[-2].indent <= self.column_limit

def _ArgumentListHasDictionaryEntry(self, token):
"""Check if the function argument list has a dictionary as an arg."""
if _IsArgumentToFunction(token):
while token:
if token.value == '{':
Expand Down
49 changes: 40 additions & 9 deletions yapftests/reformatter_buganizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ class BuganizerFixes(yapf_test_helper.YAPFTest):
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())

def testB66011084(self):
unformatted_code = """\
X = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": # Comment 1.
([] if True else [ # Comment 2.
"bbbbbbbbbbbbbbbbbbb", # Comment 3.
"cccccccccccccccccccccccc", # Comment 4.
"ddddddddddddddddddddddddd", # Comment 5.
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", # Comment 6.
"fffffffffffffffffffffffffffffff", # Comment 7.
"ggggggggggggggggggggggggggg", # Comment 8.
"hhhhhhhhhhhhhhhhhh", # Comment 9.
]),
}
"""
expected_formatted_code = """\
X = {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": # Comment 1.
([] if True else [ # Comment 2.
"bbbbbbbbbbbbbbbbbbb", # Comment 3.
"cccccccccccccccccccccccc", # Comment 4.
"ddddddddddddddddddddddddd", # Comment 5.
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", # Comment 6.
"fffffffffffffffffffffffffffffff", # Comment 7.
"ggggggggggggggggggggggggggg", # Comment 8.
"hhhhhhhhhhhhhhhhhh", # Comment 9.
]),
}
"""
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testB67455376(self):
unformatted_code = """\
sponge_ids.extend(invocation.id() for invocation in self._client.GetInvocationsByLabels(labels))
Expand Down Expand Up @@ -1292,15 +1324,14 @@ def testB19194420(self):
self.assertCodeEqual(code, reformatter.Reformat(uwlines))

def testB19073499(self):
code = textwrap.dedent("""\
instance = (
aaaaaaa.bbbbbbb().ccccccccccccccccc().ddddddddddd({
'aa': 'context!'
}).eeeeeeeeeeeeeeeeeee(
{ # Inline comment about why fnord has the value 6.
'fnord': 6
}))
""")
code = """\
instance = (
aaaaaaa.bbbbbbb().ccccccccccccccccc().ddddddddddd({
'aa': 'context!'
}).eeeeeeeeeeeeeeeeeee({ # Inline comment about why fnord has the value 6.
'fnord': 6
}))
"""
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))

Expand Down

0 comments on commit d404b70

Please sign in to comment.