Skip to content

Commit f51b6cd

Browse files
committed
Add an option to not print typePrefix for tasty's SourceCodePrinter and add an additional showShortTypes method
1 parent 07ee78d commit f51b6cd

File tree

4 files changed

+78
-52
lines changed

4 files changed

+78
-52
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
9494
def showExtractors: String =
9595
Extractors.showTree(using QuotesImpl.this)(self)
9696
def show: String =
97-
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
97+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
98+
def showShort: String =
99+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
98100
def showAnsiColored: String =
99-
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
101+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
100102
def isExpr: Boolean =
101103
self match
102104
case TermTypeTest(self) =>
@@ -1590,10 +1592,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
15901592
Extractors.showType(using QuotesImpl.this)(self)
15911593

15921594
def show: String =
1593-
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
1595+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
1596+
1597+
def showShort: String =
1598+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
15941599

15951600
def showAnsiColored: String =
1596-
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
1601+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
15971602

15981603
def seal: scala.quoted.Type[_] = self.asType
15991604

@@ -2154,9 +2159,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21542159
def showExtractors: String =
21552160
Extractors.showConstant(using QuotesImpl.this)(self)
21562161
def show: String =
2157-
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
2162+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
2163+
def showShort: String =
2164+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
21582165
def showAnsiColored: String =
2159-
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
2166+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
21602167
end extension
21612168
end ConstantMethodsImpl
21622169

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import scala.annotation.switch
66
/** Printer for fully elaborated representation of the source code */
77
object SourceCode {
88

9-
def showTree(using Quotes)(tree: quotes.reflect.Tree)(syntaxHighlight: SyntaxHighlight): String =
10-
new SourceCodePrinter[quotes.type](syntaxHighlight).printTree(tree).result()
9+
def showTree(using Quotes)(tree: quotes.reflect.Tree)(syntaxHighlight: SyntaxHighlight, fullNames: Boolean): String =
10+
new SourceCodePrinter[quotes.type](syntaxHighlight, fullNames).printTree(tree).result()
1111

12-
def showType(using Quotes)(tpe: quotes.reflect.TypeRepr)(syntaxHighlight: SyntaxHighlight): String =
13-
new SourceCodePrinter[quotes.type](syntaxHighlight).printType(tpe)(using None).result()
12+
def showType(using Quotes)(tpe: quotes.reflect.TypeRepr)(syntaxHighlight: SyntaxHighlight, fullNames: Boolean): String =
13+
new SourceCodePrinter[quotes.type](syntaxHighlight, fullNames).printType(tpe)(using None).result()
1414

15-
def showConstant(using Quotes)(const: quotes.reflect.Constant)(syntaxHighlight: SyntaxHighlight): String =
16-
new SourceCodePrinter[quotes.type](syntaxHighlight).printConstant(const).result()
15+
def showConstant(using Quotes)(const: quotes.reflect.Constant)(syntaxHighlight: SyntaxHighlight, fullNames: Boolean): String =
16+
new SourceCodePrinter[quotes.type](syntaxHighlight, fullNames).printConstant(const).result()
1717

1818
def showSymbol(using Quotes)(symbol: quotes.reflect.Symbol)(syntaxHighlight: SyntaxHighlight): String =
1919
symbol.fullName
@@ -58,7 +58,7 @@ object SourceCode {
5858
flagList.result().mkString("/*", " ", "*/")
5959
}
6060

61-
private class SourceCodePrinter[Q <: Quotes & Singleton](syntaxHighlight: SyntaxHighlight)(using val quotes: Q) {
61+
private class SourceCodePrinter[Q <: Quotes & Singleton](syntaxHighlight: SyntaxHighlight, fullNames: Boolean)(using val quotes: Q) {
6262
import syntaxHighlight._
6363
import quotes.reflect._
6464

@@ -1073,41 +1073,45 @@ object SourceCode {
10731073

10741074
case tpe: TypeRef =>
10751075
val sym = tpe.typeSymbol
1076-
tpe.qualifier match {
1077-
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1078-
case NoPrefix() =>
1079-
if (sym.owner.flags.is(Flags.Package)) {
1080-
// TODO should these be in the prefix? These are at least `scala`, `java` and `scala.collection`.
1081-
val packagePath = sym.owner.fullName.stripPrefix("<root>").stripPrefix("<empty>").stripPrefix(".")
1082-
if (packagePath != "")
1083-
this += packagePath += "."
1084-
}
1085-
case prefix: TermRef if prefix.termSymbol.isClassDef =>
1086-
printType(prefix)
1087-
this += "#"
1088-
case prefix: TypeRef if prefix.typeSymbol.isClassDef =>
1089-
printType(prefix)
1090-
this += "#"
1091-
case ThisType(TermRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
1092-
case ThisType(TypeRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
1093-
case prefix: TypeRepr =>
1094-
printType(prefix)
1095-
this += "."
1096-
}
1076+
if fullNames then
1077+
tpe.qualifier match {
1078+
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1079+
case NoPrefix() =>
1080+
if (sym.owner.flags.is(Flags.Package)) {
1081+
// TODO should these be in the prefix? These are at least `scala`, `java` and `scala.collection`.
1082+
val packagePath = sym.owner.fullName.stripPrefix("<root>").stripPrefix("<empty>").stripPrefix(".")
1083+
if (packagePath != "")
1084+
this += packagePath += "."
1085+
}
1086+
case prefix: TermRef if prefix.termSymbol.isClassDef =>
1087+
printType(prefix)
1088+
this += "#"
1089+
case prefix: TypeRef if prefix.typeSymbol.isClassDef =>
1090+
printType(prefix)
1091+
this += "#"
1092+
case ThisType(TermRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
1093+
case ThisType(TypeRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
1094+
case prefix: TypeRepr =>
1095+
printType(prefix)
1096+
this += "."
1097+
}
10971098
this += highlightTypeDef(sym.name.stripSuffix("$"))
10981099

10991100
case TermRef(prefix, name) =>
1100-
prefix match {
1101-
case NoPrefix() =>
1102-
this += highlightTypeDef(name)
1103-
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1104-
this += highlightTypeDef(name)
1105-
case _ =>
1106-
printType(prefix)
1107-
if (name != "package")
1108-
this += "." += highlightTypeDef(name)
1109-
this
1110-
}
1101+
if fullNames then
1102+
prefix match {
1103+
case NoPrefix() =>
1104+
this += highlightTypeDef(name)
1105+
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1106+
this += highlightTypeDef(name)
1107+
case _ =>
1108+
printType(prefix)
1109+
if (name != "package")
1110+
this += "." += highlightTypeDef(name)
1111+
this
1112+
}
1113+
else
1114+
this += highlightTypeDef(name)
11111115

11121116
case tpe @ Refinement(_, _, _) =>
11131117
printRefinement(tpe)
@@ -1157,12 +1161,14 @@ object SourceCode {
11571161
printFullClassName(tp)
11581162
this += highlightTypeDef(".this")
11591163
case TypeRef(prefix, name) if name.endsWith("$") =>
1160-
prefix match {
1161-
case NoPrefix() =>
1162-
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1163-
case _ =>
1164-
printType(prefix)
1165-
this += "."
1164+
if (fullNames){
1165+
prefix match {
1166+
case NoPrefix() =>
1167+
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
1168+
case _ =>
1169+
printType(prefix)
1170+
this += "."
1171+
}
11661172
}
11671173
this += highlightTypeDef(name.stripSuffix("$"))
11681174
case _ =>
@@ -1378,7 +1384,7 @@ object SourceCode {
13781384

13791385
private def printFullClassName(tp: TypeRepr): Unit = {
13801386
def printClassPrefix(prefix: TypeRepr): Unit = prefix match {
1381-
case TypeRef(prefix2, name) =>
1387+
case TypeRef(prefix2, name) if fullNames =>
13821388
printClassPrefix(prefix2)
13831389
this += name += "."
13841390
case _ =>

library/src/scala/quoted/Quotes.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
210210
/** Shows the tree as fully typed source code */
211211
def show: String
212212

213+
/** Shows the tree as without package prefix*/
214+
def showShort: String
215+
213216
/** Shows the tree as fully typed source code colored with ANSI */
214217
def showAnsiColored: String
215218

@@ -1865,6 +1868,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
18651868
/** Shows the tree as fully typed source code */
18661869
def show: String
18671870

1871+
/** Shows the tree as without package prefix*/
1872+
def showShort: String
1873+
18681874
/** Shows the tree as fully typed source code colored with ANSI */
18691875
def showAnsiColored: String
18701876

@@ -2608,6 +2614,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
26082614
/** Shows the tree as extractors */
26092615
def showExtractors: String
26102616

2617+
/** Shows the tree as without package prefix*/
2618+
def showShort: String
2619+
26112620
/** Shows the tree as fully typed source code */
26122621
def show: String
26132622

library/src/scala/quoted/Type.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ object Type:
1515
def show[T](using Type[T])(using Quotes): String =
1616
quotes.reflect.TypeTree.of[T].show
1717

18+
/** Show a source code like representation of this type without syntax highlight */
19+
def showShort[T](using Type[T])(using Quotes): String =
20+
quotes.reflect.TypeTree.of[T].showShort
21+
1822
/** Shows the tree as fully typed source code colored with ANSI */
1923
def showAnsiColored[T](using Type[T])(using Quotes): String =
2024
quotes.reflect.TypeTree.of[T].showAnsiColored

0 commit comments

Comments
 (0)