Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatTokensRewrite: define Replacement.isRemove #3851

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading