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: return index, vs rule applied #3866

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 @@ -140,18 +140,17 @@ class FormatTokensRewrite(
arr.foreach { implicit ft =>
ft.right match {
case _: T.LeftBrace | _: T.LeftParen | _: T.LeftBracket =>
val ldelimIdx = tokens.length
val formatOff = ft.meta.formatOff
formatOffStack.prepend(formatOff)
val ruleOpt =
val ldelimIdxOpt =
if (formatOff) None
else
session.claimedRule match {
case Some(c) => applyRule(c.rule)
case _ => applyRules
}
val ldelimIdx = ldelimIdxOpt.getOrElse(session.claim(null))
leftDelimIndex.prepend(ldelimIdx)
if (ruleOpt.isEmpty) tokens.append(null)

case _: T.RightBrace | _: T.RightParen | _: T.RightBracket =>
val formatOff = formatOffStack.remove(0)
Expand Down Expand Up @@ -199,15 +198,15 @@ class FormatTokensRewrite(
private def applyRules(implicit
ft: FormatToken,
session: Session
): Option[Rule] = {
): Option[Int] = {
implicit val style = styleMap.at(ft.right)
session.applyRules(rules)
}

private def applyRule(rule: Rule)(implicit
ft: FormatToken,
session: Session
): Option[Rule] = {
): Option[Int] = {
implicit val style = styleMap.at(ft.right)
session.applyRule(rule)
}
Expand Down Expand Up @@ -368,19 +367,19 @@ object FormatTokensRewrite {

private[FormatTokensRewrite] def applyRule(
attemptedRule: Rule
)(implicit ft: FormatToken, style: ScalafmtConfig): Option[Rule] =
)(implicit ft: FormatToken, style: ScalafmtConfig): Option[Int] =
if (attemptedRule.enabled) attemptedRule.onToken.map { repl =>
val idx = claim(repl)
repl.claim.foreach { claimed.getOrElseUpdate(_, idx) }
repl.rule
try idx
finally repl.claim.foreach { claimed.getOrElseUpdate(_, idx) }
}
else None

private[FormatTokensRewrite] def applyRules(
rules: Seq[Rule]
)(implicit ft: FormatToken, style: ScalafmtConfig): Option[Rule] = {
)(implicit ft: FormatToken, style: ScalafmtConfig): Option[Int] = {
@tailrec
def iter(remainingRules: Seq[Rule]): Option[Rule] = remainingRules match {
def iter(remainingRules: Seq[Rule]): Option[Int] = remainingRules match {
case r +: rs =>
applyRule(r) match {
case None => iter(rs)
Expand Down
Loading