Skip to content

Commit

Permalink
Router: apply space.inParens consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 11, 2024
1 parent dc2d928 commit 559f6c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 54 deletions.
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

0 comments on commit 559f6c6

Please sign in to comment.