Repository: google/yapf
Pull request #395 (2017-04-21 05:57:10)

Changes to prevent yapf splitting before the first argument in compound statements.


Patch:
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 5622905..1e27bb1 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -161,6 +161,11 @@ class FormatDecisionState(object):
       # Split before the closing bracket if we can.
       return current.node_split_penalty != split_penalty.UNBREAKABLE
 
+    # Prevent yapf splitting before the first argument in compound statements.
+    if (style.Get('SPLIT_BEFORE_FIRST_ARGUMENT') and
+        self.line.first.value in _COMPOUND_STMTS):
+      return False
+
     ###########################################################################
     # List Splitting
     if (style.Get('DEDENT_CLOSING_BRACKETS') or


Test patch:
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index 6ad5ed7..5b326dc 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -2184,6 +2184,50 @@ xxxxxxxxxxx, yyyyyyyyyyyy, vvvvvvvvv)
     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
     self.assertEqual(expected_code, reformatter.Reformat(uwlines))
 
+  def testSplittingBeforeFirstArgumentOnFunctionCall(self):
+    """Tests split_before_first_argument on a function call."""
+    try:
+      style.SetGlobalStyle(
+          style.CreateStyleFromConfig(
+              '{based_on_style: chromium, split_before_first_argument: True}'))
+      unformatted_code = textwrap.dedent("""\
+          a_very_long_function_name("long string with formatting {0:s}".format(
+              "mystring"))
+          """)
+      expected_formatted_code = textwrap.dedent("""\
+          a_very_long_function_name(
+              "long string with formatting {0:s}".format("mystring"))
+          """)
+      uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+      self.assertCodeEqual(expected_formatted_code,
+                           reformatter.Reformat(uwlines))
+    finally:
+      style.SetGlobalStyle(style.CreateChromiumStyle())
+
+  def testSplittingBeforeFirstArgumentOnCompoundStatement(self):
+    """Tests split_before_first_argument on a compound statement."""
+    try:
+      style.SetGlobalStyle(
+          style.CreateStyleFromConfig(
+              '{based_on_style: chromium, split_before_first_argument: True}'))
+      unformatted_code = textwrap.dedent("""\
+          if (long_argument_name_1 == 1 or
+              long_argument_name_2 == 2 or
+              long_argument_name_3 == 3 or
+              long_argument_name_4 == 4):
+            pass
+          """)
+      expected_formatted_code = textwrap.dedent("""\
+          if (long_argument_name_1 == 1 or long_argument_name_2 == 2 or
+              long_argument_name_3 == 3 or long_argument_name_4 == 4):
+            pass
+          """)
+      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()