Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mode to strip type prefix for SourceCodePrinter #10172

Merged
merged 1 commit into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 showShort: 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 showShort: 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 showShort: 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
98 changes: 52 additions & 46 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,41 +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 then
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 then
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(_, _, _) =>
printRefinement(tpe)
Expand Down Expand Up @@ -1157,12 +1161,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 +1384,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
9 changes: 9 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Shows the tree as fully typed source code */
def show: String

/** Shows the tree as without package prefix*/
def showShort: String

/** Shows the tree as fully typed source code colored with ANSI */
def showAnsiColored: String

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

/** Shows the tree as without package prefix*/
def showShort: String

/** Shows the tree as fully typed source code colored with ANSI */
def showAnsiColored: String

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

/** Shows the tree as without package prefix*/
def showShort: String

/** Shows the tree as fully typed source code */
def show: String

Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ object Type:
def show[T](using Type[T])(using Quotes): String =
quotes.reflect.TypeTree.of[T].show

/** Show a source code like representation of this type without syntax highlight */
def showShort[T](using Type[T])(using Quotes): String =
quotes.reflect.TypeTree.of[T].showShort

/** Shows the tree as fully typed source code colored with ANSI */
def showAnsiColored[T](using Type[T])(using Quotes): String =
quotes.reflect.TypeTree.of[T].showAnsiColored
Expand Down