Skip to content

Commit

Permalink
Add an option to not print typePrefix for tasty's SourceCodePrinter a…
Browse files Browse the repository at this point in the history
…nd add an additional `showShortTypes` method
  • Loading branch information
tgodzik committed Nov 25, 2020
1 parent 07ee78d commit 894fe06
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
19 changes: 13 additions & 6 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def showExtractors: String =
Extractors.showTree(using QuotesImpl.this)(self)
def show: String =
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
def showShortTypes: String =
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
def showAnsiColored: String =
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
SourceCode.showTree(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
def isExpr: Boolean =
self match
case TermTypeTest(self) =>
Expand Down Expand Up @@ -1590,10 +1592,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
Extractors.showType(using QuotesImpl.this)(self)

def show: String =
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)

def showShortTypes: String =
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)

def showAnsiColored: String =
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
SourceCode.showType(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)

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

Expand Down Expand Up @@ -2154,9 +2159,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def showExtractors: String =
Extractors.showConstant(using QuotesImpl.this)(self)
def show: String =
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain)
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = true)
def showShortTypes: String =
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.plain, fullNames = false)
def showAnsiColored: String =
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI)
SourceCode.showConstant(using QuotesImpl.this)(self)(SyntaxHighlight.ANSI, fullNames = true)
end extension
end ConstantMethodsImpl

Expand Down
97 changes: 52 additions & 45 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import scala.annotation.switch
/** Printer for fully elaborated representation of the source code */
object SourceCode {

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

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

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

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

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

Expand Down Expand Up @@ -1073,40 +1073,45 @@ object SourceCode {

case tpe: TypeRef =>
val sym = tpe.typeSymbol
tpe.qualifier match {
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
case NoPrefix() =>
if (sym.owner.flags.is(Flags.Package)) {
// TODO should these be in the prefix? These are at least `scala`, `java` and `scala.collection`.
val packagePath = sym.owner.fullName.stripPrefix("<root>").stripPrefix("<empty>").stripPrefix(".")
if (packagePath != "")
this += packagePath += "."
}
case prefix: TermRef if prefix.termSymbol.isClassDef =>
printType(prefix)
this += "#"
case prefix: TypeRef if prefix.typeSymbol.isClassDef =>
printType(prefix)
this += "#"
case ThisType(TermRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
case ThisType(TypeRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
case prefix: TypeRepr =>
printType(prefix)
this += "."
}
if (fullNames)
tpe.qualifier match {
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
case NoPrefix() =>
if (sym.owner.flags.is(Flags.Package)) {
// TODO should these be in the prefix? These are at least `scala`, `java` and `scala.collection`.
val packagePath = sym.owner.fullName.stripPrefix("<root>").stripPrefix("<empty>").stripPrefix(".")
if (packagePath != "")
this += packagePath += "."
}
case prefix: TermRef if prefix.termSymbol.isClassDef =>
printType(prefix)
this += "#"
case prefix: TypeRef if prefix.typeSymbol.isClassDef =>
printType(prefix)
this += "#"
case ThisType(TermRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
case ThisType(TypeRef(cdef, _)) if elideThis.nonEmpty && cdef == elideThis.get =>
case prefix: TypeRepr =>
printType(prefix)
this += "."
}
this += highlightTypeDef(sym.name.stripSuffix("$"))

case TermRef(prefix, name) =>
prefix match {
case NoPrefix() =>
this += highlightTypeDef(name)
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
this += highlightTypeDef(name)
case _ =>
printType(prefix)
if (name != "package")
this += "." += highlightTypeDef(name)
this
if (fullNames){
prefix match {
case NoPrefix() =>
this += highlightTypeDef(name)
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
this += highlightTypeDef(name)
case _ =>
printType(prefix)
if (name != "package")
this += "." += highlightTypeDef(name)
this
}
} else {
this += highlightTypeDef(name)
}

case tpe @ Refinement(_, _, _) =>
Expand Down Expand Up @@ -1157,12 +1162,14 @@ object SourceCode {
printFullClassName(tp)
this += highlightTypeDef(".this")
case TypeRef(prefix, name) if name.endsWith("$") =>
prefix match {
case NoPrefix() =>
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
case _ =>
printType(prefix)
this += "."
if (fullNames){
prefix match {
case NoPrefix() =>
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
case _ =>
printType(prefix)
this += "."
}
}
this += highlightTypeDef(name.stripSuffix("$"))
case _ =>
Expand Down Expand Up @@ -1378,7 +1385,7 @@ object SourceCode {

private def printFullClassName(tp: TypeRepr): Unit = {
def printClassPrefix(prefix: TypeRepr): Unit = prefix match {
case TypeRef(prefix2, name) =>
case TypeRef(prefix2, name) if fullNames =>
printClassPrefix(prefix2)
this += name += "."
case _ =>
Expand Down

0 comments on commit 894fe06

Please sign in to comment.