Skip to content

Commit

Permalink
Debug: move routes and state logging to LoggerOps
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 10, 2024
1 parent df012a8 commit b5af71a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.util

import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.FormatTokens
import org.scalafmt.internal.Split
import org.scalafmt.internal.State

Expand All @@ -13,6 +14,7 @@ import scala.meta.tokens.Token.Interpolation
import scala.meta.tokens.Tokens

import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.DynamicVariable

import sourcecode.Text
Expand Down Expand Up @@ -136,4 +138,39 @@ object LoggerOps {
): Unit = {}
}

def logDebugRoutes(
routes: IndexedSeq[Seq[Split]],
ftoks: FormatTokens,
): Unit = if (null ne routes) {
var tokidx = 0
val toks = ftoks.arr
while (tokidx < toks.length) {
logger.debug(s"FT: ${log2(toks(tokidx))}")
routes(tokidx).foreach(s => logger.debug(s"> S: ${log(s)}"))
tokidx += 1
}
}

def logDebugStateStack(finalState: State, ftoks: FormatTokens): Unit =
if ((null ne finalState) && (finalState ne State.start)) {
val toks = ftoks.arr
val stack = new mutable.ListBuffer[String]
val posWidth = s"%${1 + math.log10(toks.last.left.end).toInt}d"
@tailrec
def iter(state: State): Unit = if (state.prev ne State.start) {
val prev = state.prev
val idx = prev.depth
val tok = toks(idx).left
val clean = "%-15s".format(cleanup(tok).slice(0, 15))
stack.prepend(
s"[$idx] ${posWidth.format(tok.end)}: $clean" +
s" ${state.split} ${prev.indentation} ${prev.column} [${state.cost}]",
)
iter(prev)
}
iter(finalState)
stack.foreach(logger.debug)
logger.debug(s"Total cost: ${finalState.cost}")
}

}
33 changes: 2 additions & 31 deletions scalafmt-tests/shared/src/test/scala/org/scalafmt/Debug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import org.scalafmt.config.FormatEvent._
import org.scalafmt.internal.FormatOps
import org.scalafmt.internal.FormatWriter
import org.scalafmt.internal.Split
import org.scalafmt.internal.State
import org.scalafmt.util.LoggerOps

import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

import scala.annotation.tailrec
import scala.collection.mutable

/** (ugly) Utility to collect data about formatter.
Expand Down Expand Up @@ -93,35 +91,8 @@ object Debug {
logger.debug(sb.toString())
}

if (null ne routes) {
var tokidx = 0
while (tokidx < toks.length) {
logger.debug(s"FT: ${log2(toks(tokidx))}")
routes(tokidx).foreach(s => logger.debug(s"> S: ${log(s)}"))
tokidx += 1
}
}

val stack = new mutable.ListBuffer[String]
val posWidth = s"%${1 + math.log10(toks.last.left.end).toInt}d"
@tailrec
def iter(state: State): Unit = if (state.prev ne State.start) {
val prev = state.prev
val idx = prev.depth
val tok = toks(idx).left
val clean = "%-15s".format(cleanup(tok).slice(0, 15))
stack.prepend(
s"[$idx] ${posWidth.format(tok.end)}: $clean" +
s" ${state.split} ${prev.indentation} ${prev.column} [${state.cost}]",
)
iter(prev)
}
val finalState = completedEvent.finalState
if (null != finalState) {
if (finalState ne State.start) iter(finalState)
stack.foreach(logger.debug)
logger.debug(s"Total cost: ${finalState.cost}")
}
LoggerOps.logDebugRoutes(routes, formatOps.tokens)
LoggerOps.logDebugStateStack(completedEvent.finalState, formatOps.tokens)
}
}

Expand Down

0 comments on commit b5af71a

Please sign in to comment.