Skip to content

Commit

Permalink
FormatTokensRewrite: define Replacement.isRemove
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent ec965a5 commit 3a32215
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private class ConvertToNewScala3Syntax(implicit val ftoks: FormatTokens)
def nextRight = ftoks.nextNonComment(ftoks.next(ft)).right
ft.right match {

case x: Token.RightParen if left.how eq ReplacementType.Remove =>
case x: Token.RightParen if left.isRemove =>
ft.meta.rightOwner match {
case _: Term.If =>
if (!nextRight.is[Token.KwThen])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ object FormatTokensRewrite {
case rt: ReplacementType.RemoveAndResurrect =>
getClaimed(rt.idx).flatMap { case (oldidx, orepl) =>
val ok = orepl != null && (orepl.rule eq repl.rule) &&
(orepl.how eq ReplacementType.Remove)
orepl.isRemove
if (ok) {
tokens(oldidx) =
repl.copy(ft = repl.ft.withIdx(orepl.ft.meta.idx))
Expand Down Expand Up @@ -373,6 +373,15 @@ object FormatTokensRewrite {
val ruleOpt = rules.find(tag.runtimeClass.isInstance)
ruleOpt.map(_.asInstanceOf[A]).filter(_.enabled)
}

private[rewrite] def isRemovedOnLeftOpt(x: FormatToken): Option[Boolean] = {
val ftIdx = x.meta.idx - 1
claimedRule(ftIdx).filter(_.ft.meta.idx == ftIdx).map(_.isRemove)
}

private[rewrite] def isRemovedOnLeft(x: FormatToken, ok: Boolean): Boolean =
isRemovedOnLeftOpt(x).contains(ok)

}

private[rewrite] case class Replacement(
Expand All @@ -382,7 +391,9 @@ object FormatTokensRewrite {
style: ScalafmtConfig,
// list of FormatToken indices, with the claimed token on the **right**
claim: Iterable[Int] = Nil
)
) {
@inline def isRemove: Boolean = how eq ReplacementType.Remove
}

private[rewrite] sealed trait ReplacementType
private[rewrite] object ReplacementType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
ftoks.matchingOpt(x) match {
case Some(y) if y ne stat.tokens.last =>
session.rule[RedundantParens].exists {
_.onToken(ftoks(x, -1), session, style)
.exists(_.how eq ReplacementType.Remove)
_.onToken(ftoks(x, -1), session, style).exists(_.isRemove)
}
case _ => true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class RedundantParens(implicit val ftoks: FormatTokens)
style: ScalafmtConfig
): Option[(Replacement, Replacement)] =
ft.right match {
case _: Token.RightParen if (left.how eq ReplacementType.Remove) && {
case _: Token.RightParen if left.isRemove && {
val maybeCommaFt = ftoks.prevNonComment(ft)
!maybeCommaFt.left.is[Token.Comma] ||
session.claimedRule(maybeCommaFt.meta.idx - 1).isDefined
session.isRemovedOnLeft(maybeCommaFt, true)
} /* check for trailing comma */ =>
Some((left, removeToken))
case _ => None
Expand Down

0 comments on commit 3a32215

Please sign in to comment.