Skip to content

Commit

Permalink
{Format,Tree}Ops: use token index instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 6, 2024
1 parent a8fb8e5 commit 8784617
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.scalafmt.config.FormatEvent.Explored
import org.scalafmt.config.FormatEvent.VisitToken
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.LoggerOps
import org.scalafmt.util.TokenOps
import org.scalafmt.util.TreeOps

import scala.meta.Term
Expand All @@ -25,7 +24,6 @@ private class BestFirstSearch private (
)(implicit val formatOps: FormatOps) {
import BestFirstSearch._
import LoggerOps._
import TokenOps._
import TreeOps._
import formatOps._

Expand Down Expand Up @@ -132,15 +130,13 @@ private class BestFirstSearch private (
tokens(deepestYet.depth),
)

if (curr.split != null && curr.split.isNL) {
val tokenHash = hash(leftTok)
if (curr.split != null && curr.split.isNL)
if (
emptyQueueSpots.contains(tokenHash) ||
emptyQueueSpots.contains(curr.depth) ||
dequeueOnNewStatements && curr.allAltAreNL &&
!(depth == 0 && noOptZone) &&
(leftTok.is[Token.KwElse] || statementStarts.contains(tokenHash))
(leftTok.is[Token.KwElse] || statementStarts.contains(curr.depth))
) addGeneration()
}

val blockClose =
if (start == curr && 0 != maxCost || !noOptZone) None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,8 @@ class FormatOps(
case _ => false
})

@inline
final def startsStatement(token: T): Option[Tree] = statementStarts
.get(hash(token))
val StartsStatementRight =
new ExtractFromMeta[Tree](meta => startsStatement(tokens(meta.idx).right))
new ExtractFromMeta[Tree](meta => statementStarts.get(meta.idx + 1))

def parensTuple(token: T): TokenRanges = matchingOpt(token)
.fold(TokenRanges.empty)(other => TokenRanges(TokenRange(token, other)))
Expand Down Expand Up @@ -879,7 +876,7 @@ class FormatOps(
opensConfigStyleImplicitParamList(ft)
def opensConfigStyle = !style.newlines.sourceIgnored && // classic
(ft.hasBreak || opensImplicit) && beforeCloseFt.hasBreak
if (allowForce && forceConfigStyle(hash(ft.left))) ConfigStyle.Forced
if (allowForce && forceConfigStyle(ft.meta.idx)) ConfigStyle.Forced
else if (opensConfigStyle) ConfigStyle.Source
else ConfigStyle.None
}
Expand Down Expand Up @@ -1028,30 +1025,34 @@ class FormatOps(
} else Seq(Split(Space, 0), Split(Newline, 1))
}

def getForceConfigStyle: (Set[TokenHash], Set[TokenHash]) = {
def getForceConfigStyle: (Set[Int], Set[Int]) = {
val maxDistance = initStyle.runner.optimizer.forceConfigStyleMinSpan
if (maxDistance < 0) (Set.empty, Set.empty)
else {
val clearQueues = Set.newBuilder[TokenHash]
val forces = Set.newBuilder[TokenHash]
val clearQueues = Set.newBuilder[Int]
val forces = Set.newBuilder[Int]
val minArgs = initStyle.runner.optimizer.forceConfigStyleMinArgCount
def process(args: Seq[Tree], open: T, close: T): Unit =
def process(
clause: Member.SyntaxValuesClause,
ftOpen: FormatToken,
): Unit = matchingOpt(ftOpen.left).foreach { close =>
val values = clause.values
if (
args.lengthCompare(minArgs) >= 0 &&
distance(open, close) > maxDistance
values.lengthCompare(minArgs) >= 0 &&
distance(ftOpen.left, close) > maxDistance
) {
forces += hash(open)
args.foreach(arg => clearQueues += hash(getHeadToken(arg)))
forces += ftOpen.meta.idx
values.foreach(x => clearQueues += tokens.getHead(x).meta.idx)
}
}
tokens.foreach {
case FormatToken(left: T.LeftParen, _, meta) => meta.leftOwner match {
case ft @ FormatToken(_: T.LeftParen, _, m) => m.leftOwner match {
case t: Term.ArgClause if !t.parent.exists(_.is[Term.ApplyInfix]) =>
matchingOpt(left).foreach(process(t.values, left, _))
process(t, ft)
case _ =>
}
case FormatToken(left: T.LeftBracket, _, meta) => meta.leftOwner match {
case t: Type.ArgClause => matchingOpt(left)
.foreach(process(t.values, left, _))
case ft @ FormatToken(_: T.LeftBracket, _, m) => m.leftOwner match {
case t: Type.ArgClause => process(t, ft)
case _ =>
}
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class Router(formatOps: FormatOps) {
) if dialect.allowSignificantIndentation => splits

// { ... } Blocks
case tok @ FormatToken(open @ T.LeftBrace(), right, _) =>
case tok @ FormatToken(open @ T.LeftBrace(), right, m) =>
val close = matching(open)
val closeFT = tokens(close)
val newlineBeforeClosingCurly = decideNewlinesOnlyBeforeClose(close)
Expand All @@ -246,7 +246,7 @@ class Router(formatOps: FormatOps) {

// lambdaNLOnly: None for single line only
val (lambdaExpire, lambdaArrow, lambdaIndent, lambdaNLOnly) =
startsStatement(right) match {
statementStarts.get(m.idx + 1) match {
case Some(owner: Term.FunctionTerm) if (leftOwner match {
case t: Template => t.parent
.exists(_.is[Term.NewAnonymous]) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ object TreeOps {

def getStatementStarts(tree: Tree, soft: SoftKeywordClasses)(implicit
ftoks: FormatTokens,
): Map[TokenHash, Tree] = {
val ret = Map.newBuilder[TokenHash, Tree]
): Map[Int, Tree] = {
val ret = Map.newBuilder[Int, Tree]
ret.sizeHint(tree.tokens.length)

def addFT(ft: FormatToken, tree: Tree): Unit = ret += hash(ft.left) -> tree
def addFT(ft: FormatToken, tree: Tree): Unit = ret += ft.meta.idx -> tree
def addTok(token: Token, tree: Tree) = addFT(ftoks.after(token), tree)
def addTree(t: Tree, o: Tree) = ftoks.getHeadOpt(t).foreach(addFT(_, o))
def addOne(t: Tree) = addTree(t, t)
Expand Down

0 comments on commit 8784617

Please sign in to comment.