From ff0d93fcdb66cf3e9870da123fd95547369fabad Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 13 Mar 2024 13:51:04 -0700 Subject: [PATCH] FormatTokensRewrite: ScalafmtConfig in Replacement We will need it to check configuration of the left token when processing the right token via a different rule. --- .../rewrite/FormatTokensRewrite.scala | 27 +++++++++++-------- .../scalafmt/rewrite/RedundantBraces.scala | 26 +++++++++++------- 2 files changed, 33 insertions(+), 20 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 0cf3e8fd6e..a54f443ef1 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 @@ -223,29 +223,33 @@ object FormatTokensRewrite { style: ScalafmtConfig ): Option[(Replacement, Replacement)] - protected final def removeToken(implicit ft: FormatToken): Replacement = - new Replacement(this, ft, ReplacementType.Remove) + protected final def removeToken(implicit + ft: FormatToken, + style: ScalafmtConfig + ): Replacement = + Replacement(this, ft, ReplacementType.Remove, style) protected final def replaceToken( text: String, owner: Option[Tree] = None, claim: Iterable[Int] = Nil - )(tok: T)(implicit ft: FormatToken): Replacement = { + )(tok: T)(implicit ft: FormatToken, style: ScalafmtConfig): Replacement = { val mOld = ft.meta.right val mNew = mOld.copy(text = text, owner = owner.getOrElse(mOld.owner)) val ftNew = ft.copy(right = tok, meta = ft.meta.copy(right = mNew)) - new Replacement(this, ftNew, ReplacementType.Replace, claim) + Replacement(this, ftNew, ReplacementType.Replace, style, claim) } protected final def replaceTokenBy( text: String, owner: Option[Tree] = None, claim: Iterable[Int] = Nil - )(f: T => T)(implicit ft: FormatToken): Replacement = + )(f: T => T)(implicit ft: FormatToken, style: ScalafmtConfig): Replacement = replaceToken(text, owner, claim)(f(ft.right)) protected final def replaceTokenIdent(text: String, t: T)(implicit - ft: FormatToken + ft: FormatToken, + style: ScalafmtConfig ): Replacement = replaceToken(text)( new T.Ident(t.input, t.dialect, t.start, t.start + text.length, text) ) @@ -326,12 +330,13 @@ object FormatTokensRewrite { rules.find(tag.runtimeClass.isInstance).map(_.asInstanceOf[A]) } - private[rewrite] class Replacement( - val rule: Rule, - val ft: FormatToken, - val how: ReplacementType, + private[rewrite] case class Replacement( + rule: Rule, + ft: FormatToken, + how: ReplacementType, + style: ScalafmtConfig, // list of FormatToken indices, with the claimed token on the **right** - val claim: Iterable[Int] = Nil + claim: Iterable[Int] = Nil ) private[rewrite] sealed trait ReplacementType 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 3c17c22809..1894009cd7 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 @@ -94,12 +94,18 @@ class RedundantBraces(implicit val ftoks: FormatTokens) } // we might not keep it but will hint to onRight - private def replaceWithLeftParen(implicit ft: FormatToken): Replacement = + private def replaceWithLeftParen(implicit + ft: FormatToken, + style: ScalafmtConfig + ): Replacement = replaceTokenBy("(") { x => new Token.LeftParen(x.input, x.dialect, x.start) } - private def replaceWithEquals(implicit ft: FormatToken): Replacement = + private def replaceWithEquals(implicit + ft: FormatToken, + style: ScalafmtConfig + ): Replacement = replaceTokenBy("=") { x => new Token.Equals(x.input, x.dialect, x.start) } @@ -140,9 +146,10 @@ class RedundantBraces(implicit val ftoks: FormatTokens) lpFunction.orElse(lpPartialFunction).orNull } - private def onRightParen( - left: Replacement - )(implicit ft: FormatToken): (Replacement, Replacement) = + private def onRightParen(left: Replacement)(implicit + ft: FormatToken, + style: ScalafmtConfig + ): (Replacement, Replacement) = (left, removeToken) private def onLeftBrace(implicit @@ -199,9 +206,10 @@ class RedundantBraces(implicit val ftoks: FormatTokens) } } - private def onRightBrace( - left: Replacement - )(implicit ft: FormatToken): (Replacement, Replacement) = + private def onRightBrace(left: Replacement)(implicit + ft: FormatToken, + style: ScalafmtConfig + ): (Replacement, Replacement) = left.ft match { case lft @ FormatToken(_, _: Token.LeftParen, _) if left.how eq ReplacementType.Replace => @@ -209,7 +217,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens) // shifted right new Token.RightBrace(rt.input, rt.dialect, rt.start + 1) } - (removeToken(lft), right) + (removeToken(lft, style), right) case _ => (left, removeToken) }