Skip to content

Commit

Permalink
LoggerOps: expand logging
Browse files Browse the repository at this point in the history
Extend information for FormatToken, Tree and State.
  • Loading branch information
kitbellew committed Oct 14, 2024
1 parent 832ea1c commit 22c8830
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.scalafmt.internal

import org.scalafmt.util.LoggerOps
import org.scalafmt.util.TokenOps._

import scala.meta.Tree
Expand Down Expand Up @@ -28,7 +29,13 @@ case class FormatToken(left: Token, right: Token, meta: FormatToken.Meta) {
case 1 => "LF"
case _ => "LFLF"
}
s"[$idx] ${meta.left.text}${meta.right.text}: ${left.structure} [$ws] ${right.structure}"
val lt = meta.left.text
val rt = meta.right.text
val ls = left.structure
val rs = right.structure
val lo = LoggerOps.treeInfo(leftOwner)
val ro = LoggerOps.treeInfo(rightOwner)
s"[$idx] $lt $rt >>> $ls | $rs >>> $lo | $ro <<<[$ws]>>>"
}

def inside(range: Set[Range]): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import org.scalafmt.internal.State

import org.scalameta.FileLine
import scala.meta.Tree
import scala.meta.inputs.InputRange
import scala.meta.prettyprinters.Structure
import scala.meta.tokens.Token
import scala.meta.tokens.Token.Interpolation
import scala.meta.tokens.Tokens

import scala.annotation.tailrec
import scala.util.DynamicVariable

import sourcecode.Text
Expand All @@ -27,15 +29,25 @@ object LoggerOps {
def name2style[T](styles: Text[T]*): Map[String, T] = styles
.map(x => x.source -> x.value).toMap

def log(s: State, indent: String = ""): String = {
def log(s: State, indent: String = "", nlIndices: Boolean = true): String = {
val delim = s"\n$indent "
val policies = s.policy.policies match {
case Nil => ""
case p :: Nil => s";${delim}P($p)"
case pp => pp.map(_.toString).mkString(s";${delim}P[", s",$delim ", s"]")
}
@tailrec
def getNlIndices(x: State, res: List[String]): List[String] =
if (x.depth == 0) res
else getNlIndices(
x.prev,
if (x.split.isNL) s"${x.cost}@${x.depth}" :: res else res,
)
val nls =
if (nlIndices) getNlIndices(s, Nil).mkString(s"${delim}nl=[", ",", "]")
else ""
s"d=${s.depth} w=${s.cost}[${s.appliedPenalty}] i=${s.indentation} col=${s
.column} #nl=${s.lineId}$policies;${delim}s=${log(s.split)}"
.column} #nl=${s.lineId}$policies;${delim}s=${log(s.split)}$nls"
}
def log(split: Split): String = s"$split"

Expand Down Expand Up @@ -63,11 +75,26 @@ object LoggerOps {
def logTok(token: Token): String = f"[${token.structure}%-40s"
def logTok(token: Option[Token]): String = token.fold("")(log)

def log(range: InputRange): String = s"[${range.start}..${range.end})"

def position(t: Tree): String = log(t.pos)

def treeInfo(t: Tree): String = {
val typeName = t.getClass.getName.stripPrefix("scala.meta.")
val parts = typeName.split('$')
val name = parts.length match {
case 0 => typeName
case 1 => parts(0)
case _ => s"${parts(0)}.${parts(1)}"
}
s"$name ${position(t)}"
}

def log(t: Tree): String = log(t, false)
def log(t: Tree, tokensOnly: Boolean): String = {
val tokens = s"TOKENS: ${t.tokens.map(x => reveal(x.syntax)).mkString(",")}"
if (tokensOnly) tokens
else s"""|TYPE: ${t.getClass.getName.stripPrefix("scala.meta.")}
else s"""|TYPE: ${treeInfo(t)}
|SOURCE: $t
|STRUCTURE: ${t.show[Structure]}
|$tokens
Expand Down

0 comments on commit 22c8830

Please sign in to comment.