From 3ae961ce56a90defd179905f74cc8fb0376e6b64 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 14 Aug 2024 08:11:01 +0300 Subject: [PATCH] FormatTokens: move `getBraces` from TreeOps --- .../main/scala/org/scalafmt/internal/FormatTokens.scala | 8 ++++++++ .../main/scala/org/scalafmt/rewrite/RedundantBraces.scala | 2 +- .../shared/src/main/scala/org/scalafmt/util/TreeOps.scala | 8 ++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala index 7b34c78d85..7b9957095e 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatTokens.scala @@ -121,6 +121,14 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken] def isEnclosedInMatching(tree: Tree): Boolean = isEnclosedInMatching(tree.tokens, tree) + @inline + def getBracesIfEnclosed(tree: Tree): Option[(FormatToken, FormatToken)] = + getDelimsIfEnclosed(tree).filter(_._1.left.is[Token.LeftBrace]) + + @inline + def isEnclosedInBraces(tree: Tree): Boolean = getDelimsIfEnclosed(tree) + .exists(_._1.left.is[Token.LeftBrace]) + @inline private def areMatchingParens(close: Token)(open: => Token): Boolean = close .is[Token.RightParen] && areMatching(close)(open) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 181338f586..6f5e83f61b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -412,7 +412,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens) // Leave this alone for now. // In future there should be an option to surround such expressions with parens instead of braces if (isSeqMulti(t.values)) okToRemoveBlockWithinApply(b) - else (t.pos.start != b.pos.start) && SingleArgInBraces.inBraces(t) + else (t.pos.start != b.pos.start) && ftoks.isEnclosedInBraces(t) case d: Defn.Def => def disqualifiedByUnit = !settings.includeUnitMethods && diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index a25103ba48..6b37517f1e 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -64,13 +64,9 @@ object TreeOps { case _ => None } @inline - def inBraces(tree: Tree)(implicit ftoks: FormatTokens): Boolean = - getBraces(tree).isDefined - @inline - def getBraces(tree: Tree)(implicit + private def getBraces(tree: Tree)(implicit ftoks: FormatTokens, - ): Option[(FormatToken, FormatToken)] = ftoks.getDelimsIfEnclosed(tree) - .filter(_._1.left.is[LeftBrace]) + ): Option[(FormatToken, FormatToken)] = ftoks.getBracesIfEnclosed(tree) def orBlock(tree: Tree)(implicit ftoks: FormatTokens,