Skip to content

Commit

Permalink
FormatOps: a "top-level" block is one with braces
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 14, 2024
1 parent a13f01a commit 905dca1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,18 @@ class FormatOps(
case t => t
}
def isOldTopLevel(child: Tree) = child.parent.exists {
case _: Term.Block | _: Term.If | _: Term.While | _: Source => true
case _: Term.If | _: Term.While | _: Source => true
case t: Term.Block => !isSingleStatBlock(t) || t.parent.forall { p =>
t.tokens.head match { // check brace was not rewritten
case head: Token.LeftBrace =>
(tokens.before(head).left eq head) ||
(p match {
case p: Tree.WithCond => p.cond eq t
case _ => false
})
case _ => true
}
}
case fun: Term.FunctionTerm => isBlockFunction(fun)
case t: Case => t.pat.eq(child) || t.body.eq(child)
case SingleArgInBraces(_, arg, _) => child eq arg
Expand Down
15 changes: 7 additions & 8 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1917,11 +1917,10 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
t.className != "scala/Console$" &&
- // console accesses DynamicVariable, let's discard it too
- !t.className.startsWith("scala/util/DynamicVariable")
+ // console accesses DynamicVariable, let's discard it too
+ !t.className.startsWith("scala/util/DynamicVariable")
}
object a {
val standardFilter: MethodCallTrace => Boolean = t =>
// ignore all calls to Console trigger by printing
t.className != "scala/Console$" &&
// console accesses DynamicVariable, let's discard it too
!t.className.startsWith("scala/util/DynamicVariable")
}

0 comments on commit 905dca1

Please sign in to comment.