Skip to content

Commit

Permalink
FormatOps: use token index instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 10, 2024
1 parent fc22552 commit bd7860d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Expand Up @@ -84,14 +84,13 @@ class FormatOps(
*
*/
val (argumentStarts, optionalNewlines) = {
val arguments = mutable.Map.empty[TokenHash, Tree]
val optional = Set.newBuilder[TokenHash]
def getHeadHash(tree: Tree): Option[TokenHash] = tokens.getHeadOpt(tree)
.map(x => hash(x.left))
def add(tree: Tree): Unit = getHeadHash(tree).foreach { x =>
if (!arguments.contains(x)) arguments += x -> tree
}
def addOptional(tree: Tree): Unit = getHeadHash(tree).foreach(optional += _)
val arguments = mutable.Map.empty[Int, Tree]
val optional = Set.newBuilder[Int]
def getHeadIndex(tree: Tree): Option[Int] = tokens.getHeadOpt(tree)
.map(_.meta.idx - 1)
def add(tree: Tree): Unit = getHeadIndex(tree)
.foreach(arguments.getOrElseUpdate(_, tree))
def addOptional(tree: Tree): Unit = getHeadIndex(tree).foreach(optional += _)

val queue = new mutable.ListBuffer[Seq[Tree]]
queue += topSourceTree :: Nil
Expand Down
Expand Up @@ -1108,7 +1108,7 @@ class Router(formatOps: FormatOps) {
val onlyConfigStyle = getMustDangleForTrailingCommas(beforeClose) ||
ConfigStyle.None != mustUseConfigStyle(ft, beforeClose)

val argsHeadOpt = argumentStarts.get(hash(right))
val argsHeadOpt = argumentStarts.get(ft.meta.idx)
val isSingleArg = isSeqSingle(getArgs(leftOwner))
val oneline = style.binPack.defnSiteFor(isBracket) eq
BinPack.Site.Oneline
Expand Down Expand Up @@ -1438,8 +1438,8 @@ class Router(formatOps: FormatOps) {
rightIsCloseDelimToAddTrailingComma(lc, nextNonComment(nextFt))
) Seq(Split(Space, 0), Split(Newline, 1))
else Seq(Split(Newline, 0))
case FormatToken(_: T.Comma, right, _) if leftOwner.isNot[Template] =>
val splitsOpt = argumentStarts.get(hash(right)).flatMap { nextArg =>
case FormatToken(_: T.Comma, right, _) if !leftOwner.is[Template] =>
val splitsOpt = argumentStarts.get(ft.meta.idx).flatMap { nextArg =>
val callSite = isArgClauseSite(leftOwner)
val binPack =
if (callSite) style.binPack.callSite
Expand Down Expand Up @@ -2260,7 +2260,7 @@ class Router(formatOps: FormatOps) {
)
Seq(spaceSplit, Split(Newline, if (spaceSplit.isActive) 1 else 0))

case FormatToken(_, r, _) if optionalNewlines(hash(r)) =>
case FormatToken(_, r, _) if optionalNewlines(ft.meta.idx) =>
@tailrec
def noAnnoLeftFor(tree: Tree): Boolean = tree.parent match {
case Some(_: Mod.Annot) => false
Expand Down

0 comments on commit bd7860d

Please sign in to comment.