Don't require splitting before comments
The SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES knob is meant for values,
not other things like comments. Don't require splits before those as it
may conflict with other formatting decisions.
Closes #802
diff --git a/CHANGELOG b/CHANGELOG
index fe6e020..acf2153 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -5,6 +5,9 @@
## [0.29.1] UNRELEASED
### Fixed
- Honor a disable directive at the end of a multiline comment.
+- Don't require splitting before comments in a list when
+ `SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES` is set. The knob is meant for
+ values, not comments, which may be associated with the current line.
## [0.29.0] 2019-11-28
### Added
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 17ade12..ee4e808 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -187,10 +187,15 @@
opening = _GetOpeningBracket(current)
# Can't find opening bracket, behave the same way as
- # SPLIT_ALL_COMMA_SEPARATED_VALUES
+ # SPLIT_ALL_COMMA_SEPARATED_VALUES.
if not opening:
return True
+ if current.is_comment:
+ # Don't require splitting before a comment, since it may be related to
+ # the current line.
+ return False
+
# Allow the fallthrough code to handle the closing bracket.
if current != opening.matching_bracket:
# If the container doesn't fit in the current line, must split
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index 7e5cf28..891b4e8 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -202,6 +202,28 @@
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+ # Don't require splitting before comments.
+ unformatted_code = textwrap.dedent("""\
+ KO = {
+ 'ABC': Abc, # abc
+ 'DEF': Def, # def
+ 'LOL': Lol, # wtf
+ 'GHI': Ghi,
+ 'JKL': Jkl,
+ }
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ KO = {
+ 'ABC': Abc, # abc
+ 'DEF': Def, # def
+ 'LOL': Lol, # wtf
+ 'GHI': Ghi,
+ 'JKL': Jkl,
+ }
+ """)
+ uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+
def testSimpleFunctionsWithTrailingComments(self):
unformatted_code = textwrap.dedent("""\
def g(): # Trailing comment