Skip to content

Commit

Permalink
FormatOps: ignore end marker for ctrl body purpose
Browse files Browse the repository at this point in the history
This makes formatting with or without an end marker consistent.
  • Loading branch information
kitbellew committed Dec 31, 2021
1 parent 856a5a9 commit e9ac433
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ class FormatOps(
@tailrec
def getBlockStat(t: Tree): Tree = t match {
case b: Term.Block =>
getBlockSingleStat(b) match {
getSingleStatExceptEndMarker(b.stats) match {
case Some(s) if !isEnclosedInMatching(b) => getBlockStat(s)
case _ => t
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class Router(formatOps: FormatOps) {
else if (
condIsDefined ||
beforeMultiline.eq(Newlines.classic) ||
isTreeMultiStatBlock(body)
getSingleStatExceptEndMarker(body).isEmpty
) withSlbSplit
else CtrlBodySplits.foldedNonEmptyNonComment(body, nlSplit(ft))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,22 @@ object TreeOps {
case _ => false
}

/* 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
* to insert or remove end markers, we avoid such borderline cases.
*/
def getSingleStatExceptEndMarker(s: Seq[Stat]): Option[Stat] =
s.headOption.filter { _ =>
val len2 = s.lengthCompare(2)
len2 < 0 || len2 == 0 && s(1).is[Term.EndMarker]
}

def getSingleStatExceptEndMarker(t: Tree): Option[Tree] = t match {
case Term.Block(s) => getSingleStatExceptEndMarker(s)
case _ => Some(t)
}

def getTreeSingleStat(t: Tree): Option[Tree] =
t match {
case b: Term.Block => getBlockSingleStat(b)
Expand Down
21 changes: 7 additions & 14 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ object a:
>>>
object a:
def foo = this match
case A =>
that match
case A => that match
case b => bb
case c => cc
end match
Expand Down Expand Up @@ -382,8 +381,7 @@ object a:
def foo =
try foo
catch
case A =>
that match
case A => that match
case b => bb
case c => cc
end match
Expand Down Expand Up @@ -880,8 +878,7 @@ enum IndentWidth {
>>>
enum IndentWidth:
def foo = this match
case a =>
that match
case a => that match
case b => bb
case c => cc
end match
Expand All @@ -890,8 +887,7 @@ enum IndentWidth:
case _ => dd
def foo =
this match
case a =>
that match
case a => that match
case b => bb
case c => cc
end match
Expand Down Expand Up @@ -929,8 +925,7 @@ enum IndentWidth:
def foo =
try foo
catch
case a =>
that match
case a => that match
case b => bb
case c => cc
end match
Expand Down Expand Up @@ -972,8 +967,7 @@ enum IndentWidth {
def foo = {
try foo
catch {
case a =>
that match {
case a => that match {
// format: off
case b => bb
// format: on
Expand Down Expand Up @@ -1019,8 +1013,7 @@ enum IndentWidth:
def foo =
try foo
catch
case a =>
that match
case a => that match
// scalafmt: { indent { significant = 1 } }
case b => bb
case c => cc
Expand Down

0 comments on commit e9ac433

Please sign in to comment.