From bfcde4859d13c3cae1484f215f4b272a34962111 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 12 Sep 2020 12:37:24 -0700 Subject: [PATCH] Router: beforeCurlyLambdaParams for paren lambdas --- .../scala/org/scalafmt/internal/Router.scala | 13 ++++---- .../src/test/resources/unit/Lambda.stat | 33 ++++++++++--------- 2 files changed, 25 insertions(+), 21 deletions(-) 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 570aa6506e..a9822a325c 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 @@ -592,15 +592,15 @@ class Router(formatOps: FormatOps) { // Term.Apply and friends case FormatToken(T.LeftParen(), _, _) - if style.optIn.configStyleArguments && - !style.newlines.alwaysBeforeCurlyLambdaParams && - getLambdaAtSingleArgCallSite(formatToken).isDefined => { + if getLambdaAtSingleArgCallSite(formatToken).isDefined => val lambda = getLambdaAtSingleArgCallSite(formatToken).get val close = matching(formatToken.left) val newlinePolicy = if (!style.danglingParentheses.callSite) None else Some(decideNewlinesOnlyBeforeClose(close)) - val noSplitMod = getNoSplit(formatToken, true) + val noSplitMod = + if (style.newlines.alwaysBeforeCurlyLambdaParams) null + else getNoSplit(formatToken, true) def multilineSpaceSplit(implicit line: sourcecode.Line): Split = { val lambdaLeft: Option[Token] = @@ -631,15 +631,16 @@ class Router(formatOps: FormatOps) { ) else { val newlinePenalty = 3 + nestedApplies(leftOwner) + val noMultiline = style.newlines.beforeCurlyLambdaParams eq + Newlines.BeforeCurlyLambdaParams.multiline Seq( Split(noSplitMod, 0).withSingleLine(close), - multilineSpaceSplit, + if (noMultiline) Split.ignored else multilineSpaceSplit, Split(Newline, newlinePenalty) .withPolicyOpt(newlinePolicy) .withIndent(style.continuationIndent.callSite, close, Before) ) } - } case FormatToken(T.LeftParen(), T.RightParen(), _) => val noNL = style.newlines.sourceIgnored || formatToken.noBreak diff --git a/scalafmt-tests/src/test/resources/unit/Lambda.stat b/scalafmt-tests/src/test/resources/unit/Lambda.stat index 56a37d856d..04ae6bdf08 100644 --- a/scalafmt-tests/src/test/resources/unit/Lambda.stat +++ b/scalafmt-tests/src/test/resources/unit/Lambda.stat @@ -213,15 +213,16 @@ object a { x.map { x => s"${x._1} -> ${x._2}" } - x.map(x => s"${x._1} -> ${x._2}") + x.map( + x => s"${x._1} -> ${x._2}") x.map { case (c, i) => s"$c -> $i" } x.zipWithIndex.map { x => s"${x._1} -> ${x._2}" } - x.zipWithIndex.map(x => - s"${x._1} -> ${x._2}") + x.zipWithIndex.map( + x => s"${x._1} -> ${x._2}") x.zipWithIndex.map { case (c, i) => s"$c -> $i" } @@ -233,11 +234,12 @@ object a { case (c, i) if c != i => s"$c -> $i" } - x.flatMap(result => - for { - _ <- a - _ <- b - } yield ()) + x.flatMap( + result => + for { + _ <- a + _ <- b + } yield ()) } <<< #2099 beforeCurlyLambdaParams = multilineWithCaseOnly newlines.beforeCurlyLambdaParams = multilineWithCaseOnly @@ -310,8 +312,8 @@ object a { x.zipWithIndex.map { x => s"${x._1} -> ${x._2}" } - x.zipWithIndex.map(x => - s"${x._1} -> ${x._2}") + x.zipWithIndex.map( + x => s"${x._1} -> ${x._2}") x.zipWithIndex.map { case (c, i) => s"$c -> $i" } @@ -323,9 +325,10 @@ object a { case (c, i) if c != i => s"$c -> $i" } - x.flatMap(result => - for { - _ <- a - _ <- b - } yield ()) + x.flatMap( + result => + for { + _ <- a + _ <- b + } yield ()) }