Skip to content

Commit

Permalink
Router: fix closing paren if spaces.inParentheses
Browse files Browse the repository at this point in the history
Currently, for space.inParentheses, one case is excluded for the opening
paren (if/while/etc.), but a completely different one (isDefnOrCallSite)
applies to the closing paren. Let's fix that.
  • Loading branch information
kitbellew committed Oct 16, 2020
1 parent 354df7c commit b768f88
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1918,12 +1918,16 @@ class Router(formatOps: FormatOps) {
Seq(
Split(NoSplit, 0)
)
case FormatToken(_, T.RightParen(), _) =>
val mod =
Space(style.spaces.inParentheses && isDefnOrCallSite(rightOwner))
Seq(
Split(mod, 0)
)
case FormatToken(_, close: T.RightParen, _) =>
def modNoNL = {
def allowSpace = rightOwner match {
case _: Term.If | _: Term.While | _: Term.For | _: Term.ForYield =>
isLastToken(close, rightOwner)
case _ => true
}
Space(style.spaces.inParentheses && allowSpace)
}
Seq(Split(modNoNL, 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 @@ -345,10 +345,17 @@ object TreeOps {
def isSuperfluousParenthesis(open: LeftParen, owner: Tree): Boolean =
!isTuple(owner) && owner.tokens.headOption.contains(open)

def isFirstOrLastToken(token: Token, owner: Tree): Boolean = {
owner.tokens.headOption.contains(token) ||
@inline
def isFirstOrLastToken(token: Token, owner: Tree): Boolean =
isFirstToken(token, owner) || isLastToken(token, owner)

@inline
def isFirstToken(token: Token, owner: Tree): Boolean =
owner.tokens.headOption.contains(token)

@inline
def isLastToken(token: Token, owner: Tree): Boolean =
owner.tokens.lastOption.contains(token)
}

def isCallSite(tree: Tree)(implicit style: ScalafmtConfig): Boolean =
tree match {
Expand Down
8 changes: 4 additions & 4 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3156,11 +3156,11 @@ a match {
>>>
a match {
case ( Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux") =>
( if (foo) ( foo) + bar
else foo + ( bar)) + ( baz)
Root / "baz" / _ / "qux" ) =>
( if (foo) ( foo ) + bar
else foo + ( bar ) ) + ( baz )
case ( Root / "foo" | Root / "bar" |
Root / "baz" / _ / "qux") => (
Root / "baz" / _ / "qux" ) => (
foo
)
}

0 comments on commit b768f88

Please sign in to comment.