Skip to content

Commit

Permalink
FormatToken: alias, use everywhere as {FT}
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 12, 2024
1 parent eca059b commit ae2c3b8
Show file tree
Hide file tree
Showing 31 changed files with 1,065 additions and 1,186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object Error {
) extends Error(
s"Search state exploded on '$tok', line $line: $why [see $cfgUrl#search-state-exploded]",
) {
def this(deepestState: State, ft: FormatToken, why: String)(implicit
def this(deepestState: State, ft: FT, why: String)(implicit
formatWriter: FormatWriter,
) = this(
deepestState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sealed abstract class FormatEvent
object FormatEvent {
case class CreateFormatOps(formatOps: FormatOps) extends FormatEvent
case class Routes(routes: IndexedSeq[Seq[Split]]) extends FormatEvent
case class VisitToken(formatToken: FormatToken) extends FormatEvent
case class VisitToken(formatToken: FT) extends FormatEvent
case class Explored(n: Int, depth: Int, queueSize: Int) extends FormatEvent
case class Enqueue(split: Split) extends FormatEvent
case class CompleteFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ case class Newlines(
@inline
def keepBreak(hasBreak: => Boolean): Boolean = keep && hasBreak
@inline
def keepBreak(newlines: Int): Boolean =
keepBreak(!FormatToken.noBreak(newlines))
def keepBreak(newlines: Int): Boolean = keepBreak(!FT.noBreak(newlines))
@inline
def keepBreak(implicit ft: FormatToken): Boolean = keepBreak(ft.hasBreak)
def keepBreak(implicit ft: FT): Boolean = keepBreak(ft.hasBreak)

val breakAfterInfix: AfterInfix = afterInfix.getOrElse {
source match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit

var stats = new StateStats(tokens, initStyle.runner)

private def getBlockCloseToRecurse(ft: FormatToken, stop: T)(implicit
private def getBlockCloseToRecurse(ft: FT, stop: T)(implicit
style: ScalafmtConfig,
): Option[T] = getEndOfBlock(ft, parensToo = true).collect {
case close if close.left != stop && {
Expand Down Expand Up @@ -171,14 +171,14 @@ private class BestFirstSearch private (range: Set[Range])(implicit

private def killOnFail(
isKillOnFail: Boolean,
)(end: => FormatToken)(implicit nextState: State): State = {
)(end: => FT)(implicit nextState: State): State = {
val kill = isKillOnFail || nextState.hasSlbUntil(end)
if (kill) null else nextState
}

private def killOnFail(
opt: OptimalToken,
end: => FormatToken = null,
end: => FT = null,
nextNextState: State = null,
)(implicit nextState: State): State = killOnFail(opt.killOnFail) {
if (
Expand Down Expand Up @@ -222,8 +222,8 @@ private class BestFirstSearch private (range: Set[Range])(implicit
}
}

private def getActiveSplits(ft: FormatToken, state: State, maxCost: Int)(
implicit Q: StateQueue,
private def getActiveSplits(ft: FT, state: State, maxCost: Int)(implicit
Q: StateQueue,
): Seq[Split] = {
stats.trackState(state)
val useProvided = ft.meta.formatOff || !ft.inside(range)
Expand Down Expand Up @@ -316,21 +316,21 @@ object BestFirstSearch {

private def getNoOptZones(tokens: FormatTokens)(implicit styleMap: StyleMap) = {
val result = mutable.Map.empty[T, Boolean]
var expire: FormatToken = null
var expire: FT = null
@inline
def addRange(t: T): Unit = expire = tokens.matching(t)
@inline
def addBlock(t: T): Unit = result.getOrElseUpdate(t, false)
tokens.foreach {
case ft if expire ne null =>
if (ft eq expire) expire = null else result.update(ft.left, true)
case FormatToken(t: T.LeftParen, _, m) if (m.leftOwner match {
case FT(t: T.LeftParen, _, m) if (m.leftOwner match {
case lo: Term.ArgClause => !lo.parent.is[Term.ApplyInfix] &&
!styleMap.at(t).newlines.keep
case _: Term.Apply => true // legacy: when enclosed in parens
case _ => false
}) => addRange(t)
case FormatToken(t: T.LeftBrace, _, m) => m.leftOwner match {
case FT(t: T.LeftBrace, _, m) => m.leftOwner match {
// Type compounds can be inside defn.defs
case lo: meta.Stat.Block if lo.parent.is[Type.Refine] => addRange(t)
case _: Type.Refine => addRange(t)
Expand Down Expand Up @@ -413,17 +413,15 @@ object BestFirstSearch {
updateBestImpl(state)
}

def checkExplored(
ft: FormatToken,
)(implicit formatWriter: FormatWriter): Unit = explode(
ft,
runner.getMaxStateVisits,
)(explored > _, x => s"exceeded `runner.maxStateVisits`=$x")

def explode[A](ft: FormatToken, value: A)(
cond: A => Boolean,
msg: A => String,
)(implicit formatWriter: FormatWriter): Unit = if (cond(value)) {
def checkExplored(ft: FT)(implicit formatWriter: FormatWriter): Unit =
explode(ft, runner.getMaxStateVisits)(
explored > _,
x => s"exceeded `runner.maxStateVisits`=$x",
)

def explode[A](ft: FT, value: A)(cond: A => Boolean, msg: A => String)(
implicit formatWriter: FormatWriter,
): Unit = if (cond(value)) {
complete(deepestYet)
throw new Error.SearchStateExploded(deepestYet, ft, msg(value))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package org.scalafmt.internal
*
* Used by [[Policy]] to enforce non-local formatting.
*/
case class Decision(formatToken: FormatToken, splits: Seq[Split]) {
case class Decision(formatToken: FT, splits: Seq[Split]) {

@inline
def noNewlines: Seq[Split] = Decision.noNewlineSplits(splits)
Expand Down
Loading

0 comments on commit ae2c3b8

Please sign in to comment.