Skip to content

Commit 9ce7791

Browse files
Merge pull request #10172 from tgodzik/strip-type-prefix
Add mode to strip type prefix for SourceCodePrinter
2 parents 9c04baa + f51b6cd commit 9ce7791

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
@@ -92,9 +92,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
9292
def showExtractors: String =
9393
Extractors.showTree(using QuotesImpl.this)(self)
9494
def show: String =
95-
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
95+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
96+
def showShort: String =
97+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
9698
def showAnsiColored: String =
97-
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
99+
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
98100
def isExpr: Boolean =
99101
self match
100102
case TermTypeTest(self) =>
@@ -1588,10 +1590,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
15881590
Extractors.showType(using QuotesImpl.this)(self)
15891591

15901592
def show: String =
1591-
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
1593+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
1594+
1595+
def showShort: String =
1596+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
15921597

15931598
def showAnsiColored: String =
1594-
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
1599+
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
15951600

15961601
def seal: scala.quoted.Type[_] = self.asType
15971602

@@ -2152,9 +2157,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21522157
def showExtractors: String =
21532158
Extractors.showConstant(using QuotesImpl.this)(self)
21542159
def show: String =
2155-
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
2160+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
2161+
def showShort: String =
2162+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
21562163
def showAnsiColored: String =
2157-
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
2164+
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
21582165
end extension
21592166
end ConstantMethodsImpl
21602167

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
@@ -218,6 +218,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
218218
/** Shows the tree as fully typed source code */
219219
def show: String
220220

221+
/** Shows the tree as without package prefix*/
222+
def showShort: String
223+
221224
/** Shows the tree as fully typed source code colored with ANSI */
222225
def showAnsiColored: String
223226

@@ -2363,6 +2366,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
23632366
/** Shows the tree as fully typed source code */
23642367
def show: String
23652368

2369+
/** Shows the tree as without package prefix*/
2370+
def showShort: String
2371+
23662372
/** Shows the tree as fully typed source code colored with ANSI */
23672373
def showAnsiColored: String
23682374

@@ -3293,6 +3299,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
32933299
/** Shows the tree as extractors */
32943300
def showExtractors: String
32953301

3302+
/** Shows the tree as without package prefix*/
3303+
def showShort: String
3304+
32963305
/** Shows the tree as fully typed source code */
32973306
def show: String
32983307

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)