Skip to content

Commit

Permalink
FormatTokensRewrite: ScalafmtConfig in Replacement
Browse files Browse the repository at this point in the history
We will need it to check configuration of the left token when processing
the right token via a different rule.
  • Loading branch information
kitbellew committed Mar 16, 2024
1 parent cc8af48 commit ff0d93f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -199,17 +206,18 @@ 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 =>
val right = replaceTokenBy("}", ft.meta.rightOwner.parent) { rt =>
// shifted right
new Token.RightBrace(rt.input, rt.dialect, rt.start + 1)
}
(removeToken(lft), right)
(removeToken(lft, style), right)
case _ => (left, removeToken)
}

Expand Down

0 comments on commit ff0d93f

Please sign in to comment.