Repository: google/yapf
Pull request #396 (2017-04-22 10:47:06)

Changes to coalesce brackets not to line split before closing bracket

gwelymernans: Please update the `CHANGELOG` file and look into the travis-ci failures.
joachimmetz: will do so later this week; I got preempted by other tasks

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 95aa19a..f54f297 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -13,6 +13,7 @@
 - Split before a function call in a list if the full list isn't able to fit on
   a single line.
 - Trying not to split around the '=' of a named assign.
+- Changed coalesce brackets not to line split before closing bracket.
 
 ## [0.16.1] 2017-03-22
 ### Changed
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index c258cfd..6ce24fc 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -499,7 +499,12 @@ class FormatDecisionState(object):
          previous.previous_token.OpensScope())):
       self.stack[-1].closing_scope_indent = max(
           0, self.stack[-1].indent - style.Get('CONTINUATION_INDENT_WIDTH'))
-      self.stack[-1].split_before_closing_bracket = True
+
+      split_before_closing_bracket = True
+      if style.Get('COALESCE_BRACKETS'):
+        split_before_closing_bracket = False
+
+      self.stack[-1].split_before_closing_bracket = split_before_closing_bracket
 
     # Calculate the split penalty.
     penalty = current.split_penalty


Test patch:
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index 5b326dc..b1acde9 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -2228,6 +2228,37 @@ xxxxxxxxxxx, yyyyyyyyyyyy, vvvvvvvvv)
     finally:
       style.SetGlobalStyle(style.CreateChromiumStyle())
 
+  def testCoalesceBracketsOnDict(self):
+    """Tests coalesce_brackets on a dictionary."""
+    try:
+      style.SetGlobalStyle(
+          style.CreateStyleFromConfig(
+              '{based_on_style: chromium, coalesce_brackets: True}'))
+      unformatted_code = textwrap.dedent("""\
+          date_time_values = {
+              u'year': year,
+              u'month': month,
+              u'day_of_month': day_of_month,
+              u'hours': hours,
+              u'minutes': minutes,
+              u'seconds': seconds
+          }
+          """)
+      expected_formatted_code = textwrap.dedent("""\
+          date_time_values = {
+              u'year': year,
+              u'month': month,
+              u'day_of_month': day_of_month,
+              u'hours': hours,
+              u'minutes': minutes,
+              u'seconds': seconds}
+          """)
+      uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+      self.assertCodeEqual(expected_formatted_code,
+                           reformatter.Reformat(uwlines))
+    finally:
+      style.SetGlobalStyle(style.CreateChromiumStyle())
+
 
 if __name__ == '__main__':
   unittest.main()
diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py
index fdd4fb9..c9caa03 100644
--- a/yapftests/yapf_test.py
+++ b/yapftests/yapf_test.py
@@ -1017,8 +1017,7 @@ class CommandLineTest(unittest.TestCase):
     expected_formatted_code = textwrap.dedent("""\
        some_long_function_name_foo({
            'first_argument_of_the_thing': id,
-           'second_argument_of_the_thing': "some thing"
-       })
+           'second_argument_of_the_thing': "some thing"})
        """)
     with utils.NamedTempFile(dirname=self.test_tmpdir, mode='w') as (f, name):
       f.write(