diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index f03b22a10fbf..8c3962597032 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -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) => @@ -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 @@ -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 diff --git a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala index cb0f86b85db5..6c997e132253 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala @@ -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 @@ -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._ @@ -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("").stripPrefix("").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("").stripPrefix("").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) @@ -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 _ => @@ -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 _ => diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index a443caf47051..27cbd9e04d3e 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -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 @@ -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 @@ -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 diff --git a/library/src/scala/quoted/Type.scala b/library/src/scala/quoted/Type.scala index 799314a5a64d..1f1a10a15702 100644 --- a/library/src/scala/quoted/Type.scala +++ b/library/src/scala/quoted/Type.scala @@ -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