diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index fb716ec56a..0fb0e1f4ff 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -389,10 +389,14 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) { case Decision(t @ FormatToken(_: T.Comma, right, _), splits) if owner == t.meta.leftOwner && // TODO(olafur) what the right { decides to be single line? - !right.is[T.LeftBrace] && // If comment is bound to comma, see unit/Comment. (!right.is[T.Comment] || t.newlinesBetween != 0) => - splits.filter(_.modification.isNewline) + if (!right.is[T.LeftBrace]) + splits.filter(_.modification.isNewline) + else if (!style.activeForEdition_2020_03) + splits + else + SplitTag.OneArgPerLine.activateOnly(splits) } } 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 e73526298e..1b05769a03 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 @@ -893,6 +893,26 @@ class Router(formatOps: FormatOps) { else isDefnSite(leftOwner) && !style.binPack.unsafeDefnSite } => val close = matching(open) + val oneArgPerLineSplits = + if (!style.activeForEdition_2020_03) Seq.empty + else + (rightOwner match { + case _: Term.PartialFunction | Term.Block( + List(_: Term.Function | _: Term.PartialFunction) + ) => + Seq(Split(Newline, 0)) + case _ => + val breakAfter = + rhsOptimalToken(next(nextNonCommentSameLine(formatToken))) + val multiLine = + newlinesOnlyBeforeClosePolicy(close) + .orElse(decideNewlinesOnlyAfterToken(breakAfter)) + Seq( + Split(Newline, 0, policy = SingleLineBlock(close)) + .withOptimalToken(close, killOnFail = true), + Split(Space, 1, policy = multiLine) + ) + }).map(_.onlyFor(SplitTag.OneArgPerLine)) def oneLineBody = open.pos.endLine == close.pos.startLine Seq( Split(Space, 0), @@ -900,7 +920,7 @@ class Router(formatOps: FormatOps) { .onlyIf(newlines != 0 && oneLineBody) .withOptimalToken(close, killOnFail = true) .withPolicy(SingleLineBlock(close)) - ) + ) ++ oneArgPerLineSplits case FormatToken(_, _: T.LeftBrace, _) => Seq(Split(Space, 0)) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/SplitTag.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/SplitTag.scala index ad22261c31..b35c32e3d1 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/SplitTag.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/SplitTag.scala @@ -22,4 +22,6 @@ object SplitTag { case object Active extends Base case object Ignored extends Base + case object OneArgPerLine extends Custom + } diff --git a/scalafmt-tests/src/test/resources/default/Apply.stat b/scalafmt-tests/src/test/resources/default/Apply.stat index 571dc182bc..7be167d10e 100644 --- a/scalafmt-tests/src/test/resources/default/Apply.stat +++ b/scalafmt-tests/src/test/resources/default/Apply.stat @@ -207,7 +207,8 @@ writeRead( // Use list because Array has a shitty toString { (b: List[Byte], os) => os.writePosVarInt(b.size); os.writeBytes(b.toArray) - }, { is => + }, + { is => val bytes = new Array[Byte](is.readPosVarInt) is.readFully(bytes) bytes.toList diff --git a/scalafmt-tests/src/test/resources/newlines/beforeConfigSingleArgParenLambdaParamsF_danglingT.stat b/scalafmt-tests/src/test/resources/newlines/beforeConfigSingleArgParenLambdaParamsF_danglingT.stat index f00d595278..c5c80d6015 100644 --- a/scalafmt-tests/src/test/resources/newlines/beforeConfigSingleArgParenLambdaParamsF_danglingT.stat +++ b/scalafmt-tests/src/test/resources/newlines/beforeConfigSingleArgParenLambdaParamsF_danglingT.stat @@ -719,7 +719,8 @@ object A { x.map( _.y( abcd, - { case ((abcdefghij, aswbasdfaw), asfda) => aswdf }, { + { case ((abcdefghij, aswbasdfaw), asfda) => aswdf }, + { case (abcdefghij, sadfasdass) => ( asdfa.sadvfs(abcdefghij).get, @@ -748,7 +749,8 @@ object A { x.map( _.y( abcd, - { { case ((abcdefghij, aswbasdfaw), asfda) => aswdf } }, { + { { case ((abcdefghij, aswbasdfaw), asfda) => aswdf } }, + { { case (abcdefghij, sadfasdass) => ( @@ -779,12 +781,12 @@ object A { x.map( _.y( abcd, - { case_abcdefghij_aswbasdfaw_asfda => aswdf }, { - case_abcdefghij_sadfasdass => - ( - asdfa.sadvfs(abcdefghij).get, - asdfasdfasfdasda.asdfas(asdfasdaas).get - ) + { case_abcdefghij_aswbasdfaw_asfda => aswdf }, + { case_abcdefghij_sadfasdass => + ( + asdfa.sadvfs(abcdefghij).get, + asdfasdfasfdasda.asdfas(asdfasdaas).get + ) }, foo )