When function prototype doesn't fit in one line, always put the return type in a new line

Summary: See updated test.

Reviewed By: strulovich

Differential Revision: D19862926

fbshipit-source-id: 583da2426366264e2855d5a68d694f4ea25d0a78
diff --git a/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
index 1875e27..6efdbcf 100644
--- a/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
+++ b/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
@@ -259,7 +259,6 @@
       if (emitParenthesis) {
         builder.token("(")
       }
-
       builder.block(ZERO) {
         if (parameters != null && parameters.isNotEmpty()) {
           builder.breakOp(Doc.FillMode.UNIFIED, "", expressionBreakIndent)
@@ -273,13 +272,17 @@
           }
           builder.token(")")
         }
-      }
-      if (type != null) {
-        builder.block(ZERO) {
-          builder.token(":")
-          builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
-          builder.block(expressionBreakIndent) {
-            type.accept(this)
+        if (type != null) {
+          builder.block(ZERO) {
+            builder.token(":")
+            if (parameters.isNullOrEmpty()) {
+              builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
+            } else {
+              builder.space()
+            }
+            builder.block(expressionBreakIndent) {
+              type.accept(this)
+            }
           }
         }
       }
diff --git a/core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
index cbc5999..e817e1d 100644
--- a/core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
+++ b/core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
@@ -253,9 +253,9 @@
       |}
       |""".trimMargin(), 20)
 
-    @Test
-    fun `once a binary expression is broken, split on every line`() = assertFormatted(
-        """
+  @Test
+  fun `once a binary expression is broken, split on every line`() = assertFormatted(
+      """
         |fun foo() {
         |  val sentence =
         |      "The" +
@@ -269,9 +269,9 @@
         |}
         |""".trimMargin(), 40)
 
-    @Test
-    fun `long binary expressions with ranges in the middle`() = assertFormatted(
-        """
+  @Test
+  fun `long binary expressions with ranges in the middle`() = assertFormatted(
+      """
         |fun foo() {
         |  val sentence =
         |      "The" +
@@ -583,10 +583,11 @@
 
   @Test
   fun `return type doesn't fit in one line`() = assertFormatted(
-  """
+      """
       |interface X {
-      |  fun f(arg1: Arg1Type, arg2: Arg2Type):
-      |      Map<String, Map<String, Double>>? {
+      |  fun f(
+      |      arg1: Arg1Type, arg2: Arg2Type
+      |  ): Map<String, Map<String, Double>>? {
       |    //
       |  }
       |
@@ -1065,7 +1066,7 @@
       """
       |val p: Pair<in T, out S>
       |""".trimMargin())
-  
+
   @Test
   fun `handle covariant and contravariant type parameters`() = assertFormatted(
       """