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 97a1aea19..cc518bb78 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 @@ -2028,12 +2028,16 @@ class Router(formatOps: FormatOps) { if (style.align.openParenCtrlSite) getOpenParenAlignIndents(close) else Seq(Indent(indentLen, close, ExpiresOn.Before)) val penalizeNewlines = penalizeNewlineByNesting(open, close) + def baseNoSplit(commentNL: Modification = null)(implicit + fileLine: FileLine, + ) = Split.opt(getNoSplitAfterOpening(ft, commentNL = commentNL), 0) + if (style.danglingParentheses.ctrlSite) { val noSplit = - if (style.align.openParenCtrlSite) Split(NoSplit, 0) + if (style.align.openParenCtrlSite) baseNoSplit() .withIndents(indents).withPolicy(penalizeNewlines) .andPolicy(decideNewlinesOnlyBeforeCloseOnBreak(close)) - else Split(NoSplit, 0).withSingleLine(close) + else baseNoSplit().withSingleLine(close) Seq( noSplit, Split(Newline, 1).withIndent(indentLen, close, Before) @@ -2041,8 +2045,9 @@ class Router(formatOps: FormatOps) { .andPolicy(decideNewlinesOnlyBeforeClose(close)), ) } else Seq( - Split(NoSplit, 0).withIndents(indents).withPolicy(penalizeNewlines), + baseNoSplit(Newline).withIndents(indents).withPolicy(penalizeNewlines), ) + case FormatToken(_: T.KwIf, right, _) if leftOwner.is[Term.If] => val owner = leftOwner.asInstanceOf[Term.If] val expire = getLastToken(owner) @@ -2544,33 +2549,11 @@ class Router(formatOps: FormatOps) { case FormatToken(T.Dot(), T.Ident(_) | T.KwThis() | T.KwSuper(), _) => Seq(Split(NoSplit, 0)) case FormatToken(_, T.RightBracket(), _) => Seq(Split(NoSplit, 0)) - - /* - def modNoNL = { - def allowSpace = rightOwner match { - case _: Term.EnumeratorsBlock => false - case _: Term.If | _: Term.While | _: Term.For | _: Term.ForYield => - isTokenLastOrAfter(close, rightOwner) - case _ => true - } - getNoSplitBeforeClosing(ft, Newline, spaceOk = allowSpace) - */ - case FormatToken(_, close: T.RightParen, _) => - def modNoNL(allowSpace: Boolean) = - getNoSplitBeforeClosing(ft, Newline, spaceOk = allowSpace) - val mod = rightOwner match { - case _: Term.EnumeratorsBlock => modNoNL(false) - case _: Term.If | _: Term.While | _: Term.For | _: Term.ForYield => - modNoNL(isTokenLastOrAfter(close, rightOwner)) - case _: Pat.Alternative => - if (style.newlines.keepBreak(newlines)) Newline - else modNoNL(allowSpace = true) - case t => - val nlOnly = style.newlines.keepBreak(newlines) && - style.binPack.siteFor(t).exists(_._1 ne BinPack.Site.Never) - if (nlOnly) Newline else modNoNL(allowSpace = true) - } - Seq(Split(mod, 0)) + case FormatToken(_, _: T.RightParen, _) => + val ok = !style.newlines.keepBreak(newlines) || + style.binPack.siteFor(rightOwner) + .fold(!rightOwner.is[Pat.Alternative])(_._1 eq BinPack.Site.Never) + Seq(Split(if (ok) getNoSplitBeforeClosing(ft, Newline) else Newline, 0)) case FormatToken(left, _: T.KwCatch | _: T.KwFinally, _) if style.newlines.alwaysBeforeElseAfterCurlyIf || 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 41aaf3b4b..1f6345b89 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat @@ -4049,7 +4049,7 @@ a match { a match { case ( Root / "foo" | Root / "bar" | Root / "baz" / _ / "qux" ) => - ( if (foo) ( foo ) + bar + ( if ( foo ) ( foo ) + bar else foo + ( bar ) ) + ( baz ) case ( Root / "foo" | Root / "bar" | Root / "baz" / _ / "qux" ) => ( @@ -4068,13 +4068,13 @@ spaces.inParentheses = true === for ((a, b) <- c.d) (((a, b))) >>> -for (( a, b ) <- c.d) ((( a, b ))) +for ( ( a, b ) <- c.d ) ((( a, b ))) <<< #2219 with spaces, for-do with braces spaces.inParentheses = true === for ((a, b) <- c.d) {{ (((a, b))) }} >>> -for (( a, b ) <- c.d) { +for ( ( a, b ) <- c.d ) { { ((( a, b ))) } } <<< #2219 with spaces, for-yield @@ -4082,20 +4082,21 @@ spaces.inParentheses = true === for ((a, b) <- c.d) yield (((a, b))) >>> -for (( a, b ) <- c.d) yield ((( a, b ))) +for ( ( a, b ) <- c.d ) + yield ((( a, b ))) <<< #2219 with spaces, while spaces.inParentheses = true === while ((((a,b) < (1,1)))) (((a, b))) >>> -while ((( ( a, b ) < ( 1, 1 ) ))) +while ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2219 with spaces, if spaces.inParentheses = true === if ((((a,b) < (1,1)))) (((a, b))) >>> -if ((( ( a, b ) < ( 1, 1 ) ))) +if ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2250 danglingParentheses.callSite = true 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 ac06d2fa5..3f42c01f1 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -3659,7 +3659,7 @@ a match { a match { case ( Root / "foo" | Root / "bar" | Root / "baz" / _ / "qux" ) => - ( if (foo) ( foo ) + bar + ( if ( foo ) ( foo ) + bar else foo + ( bar ) ) + ( baz ) case ( Root / "foo" | Root / "bar" | Root / "baz" / _ / "qux" ) => ( @@ -3678,13 +3678,13 @@ spaces.inParentheses = true === for ((a, b) <- c.d) (((a, b))) >>> -for (( a, b ) <- c.d) ((( a, b ))) +for ( ( a, b ) <- c.d ) ((( a, b ))) <<< #2219 with spaces, for-do with braces spaces.inParentheses = true === for ((a, b) <- c.d) {{ (((a, b))) }} >>> -for (( a, b ) <- c.d) { +for ( ( a, b ) <- c.d ) { { ((( a, b ))) } } <<< #2219 with spaces, for-yield @@ -3692,20 +3692,21 @@ spaces.inParentheses = true === for ((a, b) <- c.d) yield (((a, b))) >>> -for (( a, b ) <- c.d) yield ((( a, b ))) +for ( ( a, b ) <- c.d ) + yield ((( a, b ))) <<< #2219 with spaces, while spaces.inParentheses = true === while ((((a,b) < (1,1)))) (((a, b))) >>> -while ((( ( a, b ) < ( 1, 1 ) ))) +while ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2219 with spaces, if spaces.inParentheses = true === if ((((a,b) < (1,1)))) (((a, b))) >>> -if ((( ( a, b ) < ( 1, 1 ) ))) +if ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2250 danglingParentheses.callSite = true 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 5d3ab510f..8db7e686c 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat @@ -3792,7 +3792,7 @@ a match { | Root / "bar" | Root / "baz" / _ / "qux" ) => - ( if (foo) ( foo ) + bar + ( if ( foo ) ( foo ) + bar else foo + ( bar ) ) + ( baz ) case ( Root / "foo" | Root / "bar" @@ -3812,13 +3812,13 @@ spaces.inParentheses = true === for ((a, b) <- c.d) (((a, b))) >>> -for (( a, b ) <- c.d) ((( a, b ))) +for ( ( a, b ) <- c.d ) ((( a, b ))) <<< #2219 with spaces, for-do with braces spaces.inParentheses = true === for ((a, b) <- c.d) {{ (((a, b))) }} >>> -for (( a, b ) <- c.d) { +for ( ( a, b ) <- c.d ) { { ((( a, b ))) } } <<< #2219 with spaces, for-yield @@ -3826,20 +3826,21 @@ spaces.inParentheses = true === for ((a, b) <- c.d) yield (((a, b))) >>> -for (( a, b ) <- c.d) yield ((( a, b ))) +for ( ( a, b ) <- c.d ) + yield ((( a, b ))) <<< #2219 with spaces, while spaces.inParentheses = true === while ((((a,b) < (1,1)))) (((a, b))) >>> -while ((( ( a, b ) < ( 1, 1 ) ))) +while ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2219 with spaces, if spaces.inParentheses = true === if ((((a,b) < (1,1)))) (((a, b))) >>> -if ((( ( a, b ) < ( 1, 1 ) ))) +if ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2250 danglingParentheses.callSite = true 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 a8ede0219..38d373cc8 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat @@ -3948,7 +3948,7 @@ a match { Root / "foo" | Root / "bar" | Root / "baz" / _ / "qux" ) => - ( if (foo) + ( if ( foo ) ( foo ) + bar else foo + ( bar ) ) + ( baz ) @@ -3971,14 +3971,14 @@ spaces.inParentheses = true === for ((a, b) <- c.d) (((a, b))) >>> -for (( a, b ) <- c.d) +for ( ( a, b ) <- c.d ) ((( a, b ))) <<< #2219 with spaces, for-do with braces spaces.inParentheses = true === for ((a, b) <- c.d) {{ (((a, b))) }} >>> -for (( a, b ) <- c.d) { +for ( ( a, b ) <- c.d ) { { ((( a, b ))) } @@ -3988,21 +3988,21 @@ spaces.inParentheses = true === for ((a, b) <- c.d) yield (((a, b))) >>> -for (( a, b ) <- c.d) +for ( ( a, b ) <- c.d ) yield ((( a, b ))) <<< #2219 with spaces, while spaces.inParentheses = true === while ((((a,b) < (1,1)))) (((a, b))) >>> -while ((( ( a, b ) < ( 1, 1 ) ))) +while ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2219 with spaces, if spaces.inParentheses = true === if ((((a,b) < (1,1)))) (((a, b))) >>> -if ((( ( a, b ) < ( 1, 1 ) ))) +if ( (( ( a, b ) < ( 1, 1 ) )) ) ((( a, b ))) <<< #2250 danglingParentheses.callSite = true diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index ccc15b9a7..9aa5cfd66 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -138,7 +138,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions { val explored = Debug.explored.get() logger.debug(s"Total explored: $explored") if (!onlyUnit && !onlyManual) - assertEquals(explored, 1493367, "total explored") + assertEquals(explored, 1493404, "total explored") val results = debugResults.result() // TODO(olafur) don't block printing out test results. // I don't want to deal with scalaz's Tasks :'(