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 7, 2023
1 parent b43161a commit bb93f55
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ class FormatOps(
}
term.elsep match {
case t: Term.If => getElseChain(t, newRes)
case b @ Term.Block(List(t: Term.If))
if !tokens.areMatching(ftElsep.left)(getLastToken(b)) =>
getElseChain(t, newRes)
case _ => newRes
}
case _ => res
Expand Down Expand Up @@ -2401,14 +2404,17 @@ 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 {
val dangle = style.danglingParentheses.ctrlSite
val forceNL = !nft.right.is[T.LeftParen]
val forceNL = !nr.is[T.LeftParen]
getSplits(ft, b, forceNL, dangle)
}
def rightBrace = blockLast(b)
Expand All @@ -2429,6 +2435,12 @@ class FormatOps(
(t.elsep match {
case _: Term.If => None
case x if isTreeMultiStatBlock(x) => Some(true)
case b @ Term.Block(List(_: 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(List(_: 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
21 changes: 6 additions & 15 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,14 +901,10 @@ 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
if foo then fooBody
else if bar then barBody
else bazBody
<<< remove optional braces within else-if 2
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -921,12 +914,10 @@ private def mtd: Res =
if bar then barBody 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 @@ -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
17 changes: 5 additions & 12 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
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
<<< remove optional braces within else-if 2
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -921,12 +916,10 @@ private def mtd: Res =
if bar then barBody 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
<<< remove optional braces within else-if 2
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down

0 comments on commit bb93f55

Please sign in to comment.