Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: apply space.inParens consistently #4431

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2028,21 +2028,26 @@ 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)
.withPolicy(penalizeNewlines)
.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)
Expand Down Expand Up @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) => (
Expand All @@ -4068,34 +4068,35 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) => (
Expand All @@ -3678,34 +3678,35 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -3812,34 +3812,35 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3948,7 +3948,7 @@ a match {
Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux"
) =>
( if (foo)
( if ( foo )
( foo ) + bar
else
foo + ( bar ) ) + ( baz )
Expand All @@ -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 )))
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :'(
Expand Down
Loading