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

FormatOps: fix Defn as optional braces block #3658

Merged
merged 2 commits into from
Oct 4, 2023
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 @@ -2174,7 +2174,7 @@ class FormatOps(
def rightBrace = blockLast(t.body)
})
case t: Term.If if !nft.right.is[T.KwThen] && {
isTreeMultiStatBlock(t.thenp) || !ifWithoutElse(t) &&
!isTreeSingleExpr(t.thenp) || !ifWithoutElse(t) &&
(isElsePWithOptionalBraces(t) ||
existsBlockIfWithoutElse(t.thenp, false))
} =>
Expand All @@ -2187,15 +2187,15 @@ class FormatOps(
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits =
if (!isTreeMultiStatBlock(b)) None
if (isTreeSingleExpr(b)) None
else Some(getSplits(ft, b, true))
def rightBrace = blockLast(b)
})
case t @ Term.While(_, b) if !nft.right.is[T.KwDo] =>
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits =
if (!isTreeMultiStatBlock(b)) None
if (isTreeSingleExpr(b)) None
else Some(getSplits(ft, b, true))
def rightBrace = blockLast(b)
})
Expand Down Expand Up @@ -2308,7 +2308,7 @@ class FormatOps(
style: ScalafmtConfig
): Option[OptionalBracesRegion] = {
def trySplits(expr: Term, finallyp: Option[Term], usesOB: => Boolean) =
if (isTreeMultiStatBlock(expr)) Some(getSplits(ft, expr, true))
if (!isTreeSingleExpr(expr)) Some(getSplits(ft, expr, true))
else if (finallyp.exists(isTreeUsingOptionalBraces) || usesOB)
Some(getSplits(ft, expr, shouldBreakInOptionalBraces(nft)))
else None
Expand Down Expand Up @@ -2357,7 +2357,7 @@ class FormatOps(
ft.meta.leftOwner match {
case t: Term.Try =>
t.finallyp.map { x =>
val usesOB = isTreeMultiStatBlock(x) ||
val usesOB = !isTreeSingleExpr(x) ||
isCatchUsingOptionalBraces(t) ||
isTreeUsingOptionalBraces(t.expr)
new OptionalBracesRegion {
Expand All @@ -2369,7 +2369,7 @@ class FormatOps(
}
case t: Term.TryWithHandler =>
t.finallyp.map { x =>
val usesOB = isTreeMultiStatBlock(x) ||
val usesOB = !isTreeSingleExpr(x) ||
isTreeUsingOptionalBraces(t.expr)
new OptionalBracesRegion {
def owner = Some(t)
Expand Down Expand Up @@ -2460,7 +2460,7 @@ class FormatOps(
case t: Term.If =>
(t.elsep match {
case _: Term.If => None
case x if isTreeMultiStatBlock(x) => Some(true)
case x if !isTreeSingleExpr(x) => Some(true)
case b @ Term.Block(List(_: Term.If))
if (matchingOpt(nft.right) match {
case Some(t) => t.end < b.pos.end
Expand Down Expand Up @@ -2544,7 +2544,7 @@ class FormatOps(
case _: T.KwThen => true
case _: T.LeftBrace => false
case _ =>
isTreeMultiStatBlock(thenp) && (!before.right.is[T.LeftBrace] ||
!isTreeSingleExpr(thenp) && (!before.right.is[T.LeftBrace] ||
matchingOpt(before.right).exists(rb => rb.end < thenp.pos.end))
}
}
Expand All @@ -2559,7 +2559,7 @@ class FormatOps(
case Term.Block(List(t: Term.If)) =>
isThenPWithOptionalBraces(t) ||
!ifWithoutElse(t) && isElsePWithOptionalBraces(t)
case t => isTreeMultiStatBlock(t)
case t => !isTreeSingleExpr(t)
})
}

Expand All @@ -2573,7 +2573,7 @@ class FormatOps(
}

private def isTreeUsingOptionalBraces(tree: Tree): Boolean =
isTreeMultiStatBlock(tree) && !tokenBefore(tree).left.is[T.LeftBrace]
!isTreeSingleExpr(tree) && !tokenBefore(tree).left.is[T.LeftBrace]

private def isBlockStart(tree: Term.Block, ft: FormatToken): Boolean =
tokens.tokenJustBeforeOpt(tree.stats).contains(ft)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ object TreeOps {
case _ => false
}

@tailrec
def isTreeSingleExpr(tree: Tree): Boolean = tree match {
case t: Term.Block =>
t.stats match {
case stat :: Nil => isTreeSingleExpr(stat)
case _ => false
}
case _: Defn => false
case _ => true
}

/* An end marker is really more like a closing brace for formatting purposes
* (but not when rewriting) so we should ignore it when considering whether a
* block contains only a single statement. NB: in FormatWriter, when choosing
Expand Down
27 changes: 27 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5180,3 +5180,30 @@ object a:
object a:
List(1, 2, 3) map: x =>
x + 1
<<< #3653 try
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
>>>
object a:
try
val x = 3 / 0
catch case e: Throwable =>
<<< #3653 if-!then
object a:
if (true)
val x = 3 / 0
>>>
object a:
if (true)
val x = 3 / 0
<<< #3653 for-!do
object a:
for (a <- b)
val x = 3 / 0
>>>
object a:
for (a <- b)
val x = 3 / 0
27 changes: 27 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4947,3 +4947,30 @@ object a:
object a:
List(1, 2, 3) map: x =>
x + 1
<<< #3653 try
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
>>>
object a:
try
val x = 3 / 0
catch case e: Throwable =>
<<< #3653 if-!then
object a:
if (true)
val x = 3 / 0
>>>
object a:
if (true)
val x = 3 / 0
<<< #3653 for-!do
object a:
for (a <- b)
val x = 3 / 0
>>>
object a:
for (a <- b)
val x = 3 / 0
28 changes: 28 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5215,3 +5215,31 @@ object a:
object a:
List(1, 2, 3) map: x =>
x + 1
<<< #3653 try
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
>>>
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
<<< #3653 if-!then
object a:
if (true)
val x = 3 / 0
>>>
object a:
if (true)
val x = 3 / 0
<<< #3653 for-!do
object a:
for (a <- b)
val x = 3 / 0
>>>
object a:
for (a <- b)
val x = 3 / 0
Original file line number Diff line number Diff line change
Expand Up @@ -5346,3 +5346,31 @@ object a:
object a:
List(1, 2, 3) map: x =>
x + 1
<<< #3653 try
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
>>>
object a:
try
val x = 3 / 0
catch
case e: Throwable =>
<<< #3653 if-!then
object a:
if (true)
val x = 3 / 0
>>>
object a:
if (true)
val x = 3 / 0
<<< #3653 for-!do
object a:
for (a <- b)
val x = 3 / 0
>>>
object a:
for (a <- b)
val x = 3 / 0
Loading