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

FormatOps: use token index instead of hash #3966

Merged
merged 1 commit into from
May 10, 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 @@ -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
Original file line number Diff line number Diff line change
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
Loading