Skip to content

Commit

Permalink
Merge pull request #10172 from tgodzik/strip-type-prefix
Browse files Browse the repository at this point in the history
Add mode to strip type prefix for SourceCodePrinter
  • Loading branch information
nicolasstucki authored Nov 27, 2020
2 parents 9c04baa + f51b6cd commit 9ce7791
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 52 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 @@ -92,9 +92,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 @@ -1588,10 +1590,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 @@ -2152,9 +2157,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 @@ -218,6 +218,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 @@ -2363,6 +2366,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 @@ -3293,6 +3299,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

0 comments on commit 9ce7791

Please sign in to comment.