Skip to content

Commit 141d804

Browse files
Merge pull request #10157 from dotty-staging/remove-context-from-tasty-printer
Remove Context from TastyPrinter class
2 parents 7eac235 + 13441ce commit 141d804

File tree

7 files changed

+42
-34
lines changed

7 files changed

+42
-34
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dotty.tools.dotc
2+
package core
3+
package tasty
4+
5+
class TastyAnsiiPrinter(bytes: Array[Byte]) extends TastyPrinter(bytes) {
6+
override protected def nameStr(str: String): String = Console.MAGENTA + str + Console.RESET
7+
override protected def treeStr(str: String): String = Console.YELLOW + str + Console.RESET
8+
override protected def lengthStr(str: String): String = Console.CYAN + str + Console.RESET
9+
}

compiler/src/dotty/tools/dotc/core/tasty/TastyHTMLPrinter.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@ package dotty.tools.dotc
22
package core
33
package tasty
44

5-
import dotty.tools.tasty.TastyBuffer.NameRef
6-
7-
import Contexts._, Decorators._
8-
import Names.Name
9-
import TastyUnpickler._
10-
import util.Spans.offsetToInt
11-
import printing.Highlighting._
12-
13-
class TastyHTMLPrinter(bytes: Array[Byte])(using Context) extends TastyPrinter(bytes) {
14-
override protected def nameColor(str: String): String = s"<span class='name'>$str</span>"
15-
override protected def treeColor(str: String): String = s"<span class='tree'>$str</span>"
16-
override protected def lengthColor(str: String): String = s"<span class='length'>$str</span>"
5+
class TastyHTMLPrinter(bytes: Array[Byte]) extends TastyPrinter(bytes) {
6+
override protected def nameStr(str: String): String = s"<span class='name'>$str</span>"
7+
override protected def treeStr(str: String): String = s"<span class='tree'>$str</span>"
8+
override protected def lengthStr(str: String): String = s"<span class='length'>$str</span>"
179
}

compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@ import TastyUnpickler._
1111
import util.Spans.offsetToInt
1212
import printing.Highlighting._
1313

14-
class TastyPrinter(bytes: Array[Byte])(using Context) {
14+
object TastyPrinter:
15+
def show(bytes: Array[Byte])(using Context): String =
16+
val printer =
17+
if ctx.settings.color.value == "never" then new TastyPrinter(bytes)
18+
else new TastyAnsiiPrinter(bytes)
19+
printer.showContents()
20+
21+
class TastyPrinter(bytes: Array[Byte]) {
1522

1623
private val sb: StringBuilder = new StringBuilder
1724

18-
val unpickler: TastyUnpickler = new TastyUnpickler(bytes)
25+
private val unpickler: TastyUnpickler = new TastyUnpickler(bytes)
1926
import unpickler.{nameAtRef, unpickle}
2027

21-
def nameToString(name: Name): String = name.debugString
28+
private def nameToString(name: Name): String = name.debugString
2229

23-
def nameRefToString(ref: NameRef): String = nameToString(nameAtRef(ref))
30+
private def nameRefToString(ref: NameRef): String = nameToString(nameAtRef(ref))
2431

25-
def printNames(): Unit =
32+
private def printNames(): Unit =
2633
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
27-
val index = nameColor("%4d".format(idx))
34+
val index = nameStr("%4d".format(idx))
2835
sb.append(index).append(": ").append(nameToString(name)).append("\n")
2936
}
3037

31-
def printContents(): String = {
38+
def showContents(): String = {
3239
sb.append("Names:\n")
3340
printNames()
3441
sb.append("\n")
@@ -59,13 +66,13 @@ class TastyPrinter(bytes: Array[Byte])(using Context) {
5966
import reader._
6067
var indent = 0
6168
def newLine() = {
62-
val length = treeColor("%5d".format(index(currentAddr) - index(startAddr)))
69+
val length = treeStr("%5d".format(index(currentAddr) - index(startAddr)))
6370
sb.append(s"\n $length:" + " " * indent)
6471
}
65-
def printNat() = sb.append(treeColor(" " + readNat()))
72+
def printNat() = sb.append(treeStr(" " + readNat()))
6673
def printName() = {
6774
val idx = readNat()
68-
sb.append(nameColor(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]"))
75+
sb.append(nameStr(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]"))
6976
}
7077
def printTree(): Unit = {
7178
newLine()
@@ -74,7 +81,7 @@ class TastyPrinter(bytes: Array[Byte])(using Context) {
7481
indent += 2
7582
if (tag >= firstLengthTreeTag) {
7683
val len = readNat()
77-
sb.append(s"(${lengthColor(len.toString)})")
84+
sb.append(s"(${lengthStr(len.toString)})")
7885
val end = currentAddr + len
7986
def printTrees() = until(end)(printTree())
8087
tag match {
@@ -116,7 +123,7 @@ class TastyPrinter(bytes: Array[Byte])(using Context) {
116123
}
117124
indent -= 2
118125
}
119-
sb.append(i"start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr\n")
126+
sb.append(s"start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr\n")
120127
sb.append(s"${endAddr.index - startAddr.index} bytes of AST, base = $currentAddr\n")
121128
while (!isAtEnd) {
122129
printTree()
@@ -136,7 +143,7 @@ class TastyPrinter(bytes: Array[Byte])(using Context) {
136143
sb.append(s" position bytes:\n")
137144
val sorted = spans.toSeq.sortBy(_._1.index)
138145
for ((addr, pos) <- sorted) {
139-
sb.append(treeColor("%10d".format(addr.index)))
146+
sb.append(treeStr("%10d".format(addr.index)))
140147
sb.append(s": ${offsetToInt(pos.start)} .. ${pos.end}\n")
141148
}
142149
sb.result
@@ -153,14 +160,14 @@ class TastyPrinter(bytes: Array[Byte])(using Context) {
153160
sb.append(s" comment bytes:\n")
154161
val sorted = comments.toSeq.sortBy(_._1.index)
155162
for ((addr, cmt) <- sorted) {
156-
sb.append(treeColor("%10d".format(addr.index)))
163+
sb.append(treeStr("%10d".format(addr.index)))
157164
sb.append(s": ${cmt.raw} (expanded = ${cmt.isExpanded})\n")
158165
}
159166
sb.result
160167
}
161168
}
162169

163-
protected def nameColor(str: String): String = Magenta(str).show
164-
protected def treeColor(str: String): String = Yellow(str).show
165-
protected def lengthColor(str: String): String = Cyan(str).show
170+
protected def nameStr(str: String): String = str
171+
protected def treeStr(str: String): String = str
172+
protected def lengthStr(str: String): String = str
166173
}

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DecompilationPrinter extends Phase {
4040
private def printToOutput(out: PrintStream)(using Context): Unit = {
4141
val unit = ctx.compilationUnit
4242
if (ctx.settings.printTasty.value)
43-
println(new TastyPrinter(unit.pickled.head._2()).printContents())
43+
println(TastyPrinter.show(unit.pickled.head._2()))
4444
else {
4545
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
4646
out.println(s"/** Decompiled from $unitFile */")

compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
3535
val unit = ctx.run.units.head
3636

3737
val decompiled = QuoteContextImpl.showTree(unit.tpdTree)
38-
val tree = new TastyHTMLPrinter(unit.pickled.head._2()).printContents()
38+
val tree = new TastyHTMLPrinter(unit.pickled.head._2()).showContents()
3939

4040
reporter.removeBufferedMessages.foreach(message => System.err.println(message))
4141
(tree, decompiled)

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ object PickledQuotes {
173173
positionWarnings.foreach(report.warning(_))
174174

175175
val pickled = pickler.assembleParts()
176-
quotePickling.println(s"**** pickled quote\n${new TastyPrinter(pickled).printContents()}")
176+
quotePickling.println(s"**** pickled quote\n${TastyPrinter.show(pickled)}")
177177
pickled
178178
}
179179

180180
/** Unpickle TASTY bytes into it's tree */
181181
private def unpickle(pickledQuote: PickledQuote, isType: Boolean)(using Context): Tree = {
182182
val bytes = pickledQuote.bytes()
183-
quotePickling.println(s"**** unpickling quote from TASTY\n${new TastyPrinter(bytes).printContents()}")
183+
quotePickling.println(s"**** unpickling quote from TASTY\n${TastyPrinter.show(bytes)}")
184184

185185
val mode = if (isType) UnpickleMode.TypeTree else UnpickleMode.Term
186186
val unpickler = new DottyUnpickler(bytes, mode)

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Pickler extends Phase {
9090
if pickling ne noPrinter then
9191
pickling.synchronized {
9292
println(i"**** pickled info of $cls")
93-
println(new TastyPrinter(pickled).printContents())
93+
println(TastyPrinter.show(pickled))
9494
}
9595
pickled
9696
}(using ExecutionContext.global)

0 commit comments

Comments
 (0)