Repository: google/yapf
Pull request #397 (2017-04-22 10:59:53)

Changes to split before the first argument in function declarations.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/11193918/badge)](https://coveralls.io/builds/11193918)

Coverage remained the same at 95.792% when pulling **c6c9219f8093e83d6e648a087baa412fb07b2fd1 on joachimmetz:split** into **251c5f11ee8c38d1c536c945d188a03fbc572886 on google:master**.

gwelymernans: Thanks. Please update the `CHANGELOG` file. :-)
joachimmetz: will do so later this week; I got preempted by other tasks

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index f54f297..81673b8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -13,6 +13,8 @@
 - 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 split before the first argument behavior to ignore compound
+  statements like if and while, but not function declarations.
 - Changed coalesce brackets not to line split before closing bracket.
 
 ## [0.16.1] 2017-03-22
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 6ce24fc..3c17dc4 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -161,8 +161,10 @@ 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.
+    # Prevent splitting before the first argument in compound statements
+    # with the exception of function declarations.
     if (style.Get('SPLIT_BEFORE_FIRST_ARGUMENT') and
+        self.line.first.value != 'def' and
         self.line.first.value in _COMPOUND_STMTS):
       return False
 


Test patch:
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index b1acde9..472f635 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -2204,6 +2204,28 @@ xxxxxxxxxxx, yyyyyyyyyyyy, vvvvvvvvv)
     finally:
       style.SetGlobalStyle(style.CreateChromiumStyle())
 
+  def testSplittingBeforeFirstArgumentOnFunctionDefinition(self):
+    """Tests split_before_first_argument on a function definition."""
+    try:
+      style.SetGlobalStyle(
+          style.CreateStyleFromConfig(
+              '{based_on_style: chromium, split_before_first_argument: True}'))
+      unformatted_code = textwrap.dedent("""\
+          def _GetNumberOfSecondsFromElements(year, month, day, hours,
+                                              minutes, seconds, microseconds):
+            return
+          """)
+      expected_formatted_code = textwrap.dedent("""\
+          def _GetNumberOfSecondsFromElements(
+              year, month, day, hours, minutes, seconds, microseconds):
+            return
+          """)
+      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: