Skip to content

Commit

Permalink
FormatOps: handle if with rewritten braces around
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 6, 2023
1 parent 4d79989 commit abb155f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2398,9 +2398,12 @@ class FormatOps(
): Option[OptionalBracesRegion] = {
ft.meta.leftOwner match {
case t: Term.If =>
val nr = nft.right
t.cond match {
case b: Term.Block
if !matchingOpt(nft.right).exists(_.end >= b.pos.end) =>
case b: Term.Block if (matchingOpt(nr) match {
case Some(t) => t.end < b.pos.end
case None => isMultiStatBlock(b)
}) =>
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits = Some {
Expand All @@ -2426,6 +2429,12 @@ class FormatOps(
(t.elsep match {
case _: Term.If => None
case x if isTreeMultiStatBlock(x) => Some(true)
case b @ Term.Block((_: Term.If) :: _)
if (matchingOpt(nft.right) match {
case Some(t) => t.end < b.pos.end
case None => true
}) =>
None
case _ if isThenPWithOptionalBraces(t) =>
Some(shouldBreakInOptionalBraces(nft))
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2173,7 +2173,14 @@ class Router(formatOps: FormatOps) {
Seq(Split(Newline, 0))
// Last else branch
case FormatToken(_: T.KwElse, _, _) if (leftOwner match {
case t: Term.If => !t.elsep.is[Term.If]
case t: Term.If =>
t.elsep match {
case _: Term.If => false
case b @ Term.Block((_: Term.If) :: _) =>
matchingOpt(nextNonComment(formatToken).right)
.exists(_.end >= b.pos.end)
case _ => true
}
case x => throw new UnexpectedTree[Term.If](x)
}) =>
val body = leftOwner.asInstanceOf[Term.If].elsep
Expand Down
16 changes: 5 additions & 11 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,8 @@ private def mtd: Res =
} then
foo
>>>
Idempotency violated
private def mtd: Res =
- if bar &&
+ if
+ bar &&
if bar &&
baz
then foo
<<< remove optional braces within else-if
Expand All @@ -904,13 +901,10 @@ private def mtd: Res =
else bazBody
}
>>>
Idempotency violated
if foo then fooBody
- else if bar then barBody
- else bazBody
+ else
+ if bar then barBody
+ else bazBody
private def mtd: Res =
if foo then fooBody
else if bar then barBody
else bazBody
<<< rewrite with given-with
rewrite.scala3.removeOptionalBraces = oldSyntaxToo
===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,7 @@ private def mtd: Res =
} then
foo
>>>
Idempotency violated
-private def mtd: Res = if bar && baz then foo
+private def mtd: Res =
+ if
+ bar && baz
+ then foo
private def mtd: Res = if bar && baz then foo
<<< remove optional braces within else-if
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,11 @@ private def mtd: Res =
else bazBody
}
>>>
Idempotency violated
private def mtd: Res =
if foo then fooBody
- else if bar then
- barBody
- else bazBody
+ else
+ if bar then
+ barBody
+ else bazBody
else if bar then
barBody
else bazBody
<<< rewrite with given-with
rewrite.scala3.removeOptionalBraces = oldSyntaxToo
===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,14 +929,9 @@ private def mtd: Res =
} then
foo
>>>
Idempotency violated
private def mtd: Res =
- if bar && baz then
+ if
+ bar && baz
+ then
baz
then foo
if bar && baz then
foo
<<< remove optional braces within else-if
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -948,18 +943,13 @@ private def mtd: Res =
else bazBody
}
>>>
Idempotency violated
private def mtd: Res =
if foo then
fooBody
- else if bar then
- barBody
else
- bazBody
+ if bar then
+ barBody
+ else
+ bazBody
else if bar then
barBody
else
bazBody
<<< rewrite with given-with
rewrite.scala3.removeOptionalBraces = oldSyntaxToo
===
Expand Down

0 comments on commit abb155f

Please sign in to comment.