Skip to content

Commit

Permalink
Router: fix enclosed case body w/ paren after NL
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 15, 2024
1 parent df90d9a commit bd565e9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1759,12 +1759,14 @@ class FormatOps(
})

// Redundant () delims around case statements
def isCaseBodyEnclosedAsBlock(ft: FormatToken, caseStat: CaseTree)(implicit
beforeMultiline: Newlines.SourceHints,
): Boolean = {
def getClosingIfCaseBodyEnclosedAsBlock(
postArrowFt: FormatToken,
caseStat: CaseTree,
)(implicit beforeMultiline: Newlines.SourceHints): Option[FormatToken] = {
val body = caseStat.body
(ft.noBreak || beforeMultiline.ignoreSourceSplit) &&
body.eq(ft.meta.rightOwner) && isBodyEnclosedAsBlock(body)
val ok = body.eq(postArrowFt.meta.rightOwner) &&
(beforeMultiline.ignoreSourceSplit || postArrowFt.noBreak)
if (ok) getClosingIfBodyEnclosedAsBlock(body) else None
}

// Redundant () delims around body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ class Router(formatOps: FormatOps) {
)
implicit val beforeMultiline = style.newlines.getBeforeMultiline
if (
isCaseBodyABlock(nft, owner) || isCaseBodyEnclosedAsBlock(nft, owner)
isCaseBodyABlock(nft, owner) ||
getClosingIfCaseBodyEnclosedAsBlock(nft, owner).isDefined
) Seq(baseSplit)
else if (nft.right.is[T.KwCase]) Seq(nlSplit(nft)(0))
else if (hasBreak() && !beforeMultiline.ignoreSourceSplit)
Expand Down Expand Up @@ -2185,29 +2186,30 @@ class Router(formatOps: FormatOps) {
if (bodyBlock || (arrowFt ne postArrowFt) && postArrowFt.hasBreak)
NoPolicy
else {
// postArrowFt points to non-comment after arrowFt
// possibly on next line without intervening comments
implicit val beforeMultiline = style.newlines.getBeforeMultiline
if (!isCaseBodyEnclosedAsBlock(postArrowFt, owner))
getClosingIfCaseBodyEnclosedAsBlock(postArrowFt, owner).fold {
Policy ? beforeMultiline.in(Newlines.fold, Newlines.keep) ||
defaultPolicy
else {
val postParenFt = nextNonCommentSameLineAfter(postArrowFt)
val lparen = postParenFt.left
val rparen = matching(lparen)
if (postParenFt.right.start >= rparen.start) defaultPolicy
else {
val indent = style.indent.main
val lindents = Seq(
Indent(indent, rparen, Before),
Indent(-indent, expire, After),
)
val lmod = NewlineT(noIndent = rhsIsCommentedOut(postParenFt))
val lsplit = Seq(Split(lmod, 0).withIndents(lindents))
val rsplit = Seq(Split(Newline, 0))
Policy.after(lparen, prefix = "CASE[(]") {
case d: Decision if d.formatToken eq postParenFt => lsplit
} ==> Policy.on(rparen, prefix = "CASE[)]") {
case d: Decision if d.formatToken.right eq rparen => rsplit
}
} { rparenFt =>
val lparentFt = next(postArrowFt)
val postParenFt = nextNonCommentSameLine(lparentFt)
val rparen = rparenFt.right
val indent = style.indent.main
val lindents = Seq(
Indent(indent, rparen, Before),
Indent(-indent, expire, After),
)
val lmod = NewlineT(noIndent = rhsIsCommentedOut(postParenFt))
val lsplit = Seq(Split(lmod, 0).withIndents(lindents))
val rsplit = Seq(Split(Newline, 0))
Policy.after(postParenFt.left, prefix = "CASE[(]") {
case Decision(`postParenFt`, _) => lsplit
// fires only if there's a comment between lparentFt and postParentFt
case Decision(`lparentFt`, _) => Seq(Split(Space, 0))
} ==> Policy.on(rparen, prefix = "CASE[)]") {
case Decision(`rparenFt`, _) => rsplit
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9206,7 +9206,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [43..49) @2:18: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, no break before lparen, break after lparen
object a {
foo match {
Expand Down
18 changes: 16 additions & 2 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -8578,7 +8578,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [43..49) @2:18: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, no break before lparen, break after lparen
object a {
foo match {
Expand Down Expand Up @@ -8610,7 +8617,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [49..55) @3:8: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, break before lparen, comment after arrow
object a {
foo match {
Expand Down
9 changes: 8 additions & 1 deletion scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9004,7 +9004,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [43..49) @2:18: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, no break before lparen, break after lparen
object a {
foo match {
Expand Down
18 changes: 16 additions & 2 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9302,7 +9302,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [43..49) @2:18: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, no break before lparen, break after lparen
object a {
foo match {
Expand Down Expand Up @@ -9334,7 +9341,14 @@ object a {
}
}
>>>
org.scalafmt.util.FormatException: java.util.NoSuchElementException: Missing matching token index Comment( c1a) [49..55) @3:8: `// c1a`
object a {
foo match {
case bar => ( // c1a
// c2a
qux
)
}
}
<<< #4133 case body enclosed, break before lparen, comment after arrow
object a {
foo match {
Expand Down

0 comments on commit bd565e9

Please sign in to comment.