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: onRight proxy in Replacement #3865

Merged
merged 1 commit into from
Mar 27, 2024
Merged
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 @@ -160,21 +160,13 @@ class FormatTokensRewrite(
formatOffStack.update(0, true)
val left = tokens(ldelimIdx)
if (left ne null) {
val rule = left.rule
val replacement =
if (ft.meta.formatOff) None
else if (session.claimedRule.exists(_.rule ne rule)) None
else {
implicit val style = styleMap.at(ft.right)
if (rule.enabled) rule.onRight(left, formatOff) else None
}
replacement match {
case None =>
tokens(ldelimIdx) = null
session.claim(null)
case Some((ltRepl, rtRepl)) =>
tokens(ldelimIdx) = ltRepl
session.claim(rtRepl)
val ko = ft.meta.formatOff ||
session.claimedRule.exists(_.rule ne left.rule)
if (ko) {
tokens(ldelimIdx) = null
} else {
implicit val style = styleMap.at(ft.right)
left.onRightAndClaim(formatOff, ldelimIdx)
}
}

Expand Down Expand Up @@ -419,6 +411,27 @@ object FormatTokensRewrite {
) {
@inline def isRemove: Boolean = how eq ReplacementType.Remove
@inline def idx: Int = ft.meta.idx

def onRight(hasFormatOff: Boolean)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig
): Option[(Replacement, Replacement)] =
if (rule.enabled) rule.onRight(this, hasFormatOff) else None

def onRightAndClaim(hasFormatOff: Boolean, leftIdx: Int)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig
): Unit =
onRight(hasFormatOff) match {
case None =>
session.claim(null)
session.tokens(leftIdx) = null
case Some((ltRepl, rtRepl)) =>
session.claim(rtRepl)
session.tokens(leftIdx) = ltRepl
}
}

private[rewrite] sealed trait ReplacementType
Expand Down
Loading