diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index cd5a664298..93e49d6de5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -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 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 1d8b2ed529..0279021556 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -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 @@ -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 @@ -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