diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 177cfa794a..f1b96af8ea 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -1368,16 +1368,18 @@ class Router(formatOps: FormatOps) { ) } bopSplits.getOrElse { - val penalizeNewlines = - PenalizeAllNewlines(expire, Constants.BracketPenalty) + def penalizeNewlines(extra: Int)(implicit fileLine: FileLine) = + PenalizeAllNewlines(expire, Constants.BracketPenalty + extra) val indent = style.indent.getDefnSite(leftOwner) + val nlPenalty = 2 + treeDepth(returnType) Seq( Split(style.newlines.keepBreak(hasBreak()), 0)(sameLineSplit) - .withPolicy(penalizeNewlines), + .withPolicy(penalizeNewlines(0)), // Spark style guide allows this: // https://github.com/databricks/scala-style-guide#indent - Split(Newline, Constants.SparkColonNewline) - .withIndent(indent, expire, After).withPolicy(penalizeNewlines), + Split(Newline, Constants.SparkColonNewline + nlPenalty) + .withIndent(indent, expire, After) + .withPolicy(penalizeNewlines(nlPenalty)), ) } case FormatToken(_: T.Colon, _, ColonDeclTpeLeft(returnType)) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat index 94c75e8d5d..a422ded05a 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat @@ -9856,17 +9856,17 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): Flow[MultipartFormData.Part[ -- Source_ByteString -- ], ByteString, NotUsed] = { -+ ) -+ : Flow[MultipartFormData.Part[ -+ Source_ByteString -+ ], ByteString, NotUsed] = { - // +object Multipart { + def format( + // + ): Flow[ + MultipartFormData.Part[Source_ByteString], + ByteString, + NotUsed + ] = { + // + } +} <<< #4133 overflowing complex result type 2 preset = default maxColumn = 80 @@ -9880,14 +9880,16 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): GraphStage[FlowShape[MultipartFormData.Part[ -- Source[ByteString, _] -- ], Source[ByteString, Any]]] = -+ ) -+ : GraphStage[FlowShape[MultipartFormData.Part[ -+ Source[ByteString, _] -+ ], Source[ByteString, Any]]] = - new GraphStage[FlowShape[MultipartFormData.Part[ +object Multipart { + private def streamed( + // + ): GraphStage[FlowShape[ + MultipartFormData.Part[Source[ByteString, _]], + Source[ByteString, Any] + ]] = + new GraphStage[FlowShape[MultipartFormData.Part[ + Source[ByteString, _] + ], Source[ByteString, Any]]] { + // + } +} diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index af9ca3b805..70eb8b197a 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -9252,17 +9252,17 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): Flow[MultipartFormData.Part[ -- Source_ByteString -- ], ByteString, NotUsed] = { -+ ) -+ : Flow[MultipartFormData.Part[ -+ Source_ByteString -+ ], ByteString, NotUsed] = { - // +object Multipart { + def format( + // + ): Flow[ + MultipartFormData.Part[Source_ByteString], + ByteString, + NotUsed + ] = { + // + } +} <<< #4133 overflowing complex result type 2 preset = default maxColumn = 80 @@ -9276,14 +9276,16 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): GraphStage[FlowShape[MultipartFormData.Part[ -- Source[ByteString, _] -- ], Source[ByteString, Any]]] = -+ ) -+ : GraphStage[FlowShape[MultipartFormData.Part[ -+ Source[ByteString, _] -+ ], Source[ByteString, Any]]] = - new GraphStage[FlowShape[MultipartFormData.Part[ +object Multipart { + private def streamed( + // + ): GraphStage[FlowShape[ + MultipartFormData.Part[Source[ByteString, _]], + Source[ByteString, Any] + ]] = + new GraphStage[FlowShape[MultipartFormData.Part[ + Source[ByteString, _] + ], Source[ByteString, Any]]] { + // + } +} diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat index eb467df457..77e2f2f3c4 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat @@ -9653,17 +9653,17 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): Flow[MultipartFormData.Part[ -- Source_ByteString -- ], ByteString, NotUsed] = { -+ ) -+ : Flow[MultipartFormData.Part[ -+ Source_ByteString -+ ], ByteString, NotUsed] = { - // +object Multipart { + def format( + // + ): Flow[ + MultipartFormData.Part[Source_ByteString], + ByteString, + NotUsed + ] = { + // + } +} <<< #4133 overflowing complex result type 2 preset = default maxColumn = 80 @@ -9677,14 +9677,16 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): GraphStage[FlowShape[MultipartFormData.Part[ -- Source[ByteString, _] -- ], Source[ByteString, Any]]] = -+ ) -+ : GraphStage[FlowShape[MultipartFormData.Part[ -+ Source[ByteString, _] -+ ], Source[ByteString, Any]]] = - new GraphStage[FlowShape[MultipartFormData.Part[ +object Multipart { + private def streamed( + // + ): GraphStage[FlowShape[ + MultipartFormData.Part[Source[ByteString, _]], + Source[ByteString, Any] + ]] = + new GraphStage[FlowShape[MultipartFormData.Part[ + Source[ByteString, _] + ], Source[ByteString, Any]]] { + // + } +} diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat index faf9230e32..63a809b573 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat @@ -9949,17 +9949,17 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): Flow[MultipartFormData.Part[ -- Source_ByteString -- ], ByteString, NotUsed] = { -+ ) -+ : Flow[MultipartFormData.Part[ -+ Source_ByteString -+ ], ByteString, NotUsed] = { - // +object Multipart { + def format( + // + ): Flow[ + MultipartFormData.Part[Source_ByteString], + ByteString, + NotUsed + ] = { + // + } +} <<< #4133 overflowing complex result type 2 preset = default maxColumn = 80 @@ -9973,14 +9973,16 @@ object Multipart { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - // -- ): GraphStage[FlowShape[MultipartFormData.Part[ -- Source[ByteString, _] -- ], Source[ByteString, Any]]] = -+ ) -+ : GraphStage[FlowShape[MultipartFormData.Part[ -+ Source[ByteString, _] -+ ], Source[ByteString, Any]]] = - new GraphStage[FlowShape[MultipartFormData.Part[ +object Multipart { + private def streamed( + // + ): GraphStage[FlowShape[ + MultipartFormData.Part[Source[ByteString, _]], + Source[ByteString, Any] + ]] = + new GraphStage[FlowShape[MultipartFormData.Part[ + Source[ByteString, _] + ], Source[ByteString, Any]]] { + // + } +}