From 7f8cbb10d56d4175946c822d2e03ea262210447c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 3 Mar 2024 06:01:53 -0800 Subject: [PATCH] FormatTokensRewrite: add `createIfRequested()` --- .../org/scalafmt/rewrite/FormatTokensRewrite.scala | 5 +++++ .../scala/org/scalafmt/rewrite/RedundantBraces.scala | 10 ++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala index 12f14c730e..6880c37758 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/FormatTokensRewrite.scala @@ -222,6 +222,11 @@ object FormatTokensRewrite { private[rewrite] trait RuleFactory { def enabled(implicit style: ScalafmtConfig): Boolean def create(implicit ftoks: FormatTokens): Rule + final def createIfRequested(implicit + ftoks: FormatTokens, + style: ScalafmtConfig + ): Option[Rule] = + if (getFactories.contains(this) && enabled) Some(create) else None } private def getFactories(implicit style: ScalafmtConfig): Seq[RuleFactory] = 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 e0e98ab5df..fca76ed754 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 @@ -213,12 +213,6 @@ class RedundantBraces(implicit val ftoks: FormatTokens) ): RedundantBracesSettings = style.rewrite.redundantBraces - private def redundantParensFunc(implicit - style: ScalafmtConfig - ): Option[Rule] = - if (!style.rewrite.rules.contains(RedundantParens)) None - else Some(RedundantParens.create) - private def processInterpolation(implicit ft: FormatToken): Boolean = { def isIdentifierAtStart(value: String) = value.headOption.exists(x => Character.isLetterOrDigit(x) || x == '_') @@ -450,8 +444,8 @@ class RedundantBraces(implicit val ftoks: FormatTokens) case x: Token.LeftParen => ftoks.matchingOpt(x) match { case Some(y) if y ne stat.tokens.last => - redundantParensFunc.exists { parensRule => - parensRule.onToken(ftoks(x, -1), session, style).exists { + RedundantParens.createIfRequested.exists { + _.onToken(ftoks(x, -1), session, style).exists { _.how eq ReplacementType.Remove } }