Skip to content

Commit

Permalink
FormatTokensRewrite: reuse index claimed earlier
Browse files Browse the repository at this point in the history
We just need to check that this index is not occupied by another format
token, which could happen if a rule staked claim for this format token
previously.
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent cfebea9 commit d749bac
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,8 @@ object FormatTokensRewrite {
private[rewrite] def claimedRule(ftIdx: Int): Option[Replacement] =
claimed.get(ftIdx).map(tokens.apply).filter(_ ne null)

@inline
private[rewrite] def claim(ftIdx: Int, repl: Replacement): Int = {
val idx = tokens.length
claimed.update(ftIdx, idx)
tokens.append(
justClaim(ftIdx) {
if (repl eq null) null
else
(repl.how match {
Expand All @@ -337,8 +334,27 @@ object FormatTokensRewrite {
}
case _ => None
}).getOrElse(repl)
)
idx
}
}

private def justClaim(ftIdx: Int)(repl: Replacement): Int = {
val idx = tokens.length
val claimedIdx = claimed.getOrElseUpdate(ftIdx, idx)
val preClaimed = claimedIdx < idx
if (
preClaimed && {
val oldrepl = tokens(claimedIdx)
oldrepl != null && oldrepl.idx == ftIdx
}
) {
tokens(claimedIdx) = repl
claimedIdx
} else {
if (preClaimed)
claimed.update(ftIdx, idx)
tokens.append(repl)
idx
}
}

private[FormatTokensRewrite] def applyRule(
Expand Down

0 comments on commit d749bac

Please sign in to comment.