@@ -6,14 +6,14 @@ import scala.annotation.switch
66/** Printer for fully elaborated representation of the source code */
77object 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 _ =>
0 commit comments