From 6f4b95865fa366f65c435f55e1359f4342edc3bb Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 11 Dec 2020 14:01:39 +0100 Subject: [PATCH] Add Printer to reflect Constant.show We can either print the structure with the pattern or the code like representation of the value. --- .../src/scala/quoted/runtime/impl/QuotesImpl.scala | 10 +++++++++- library/src/scala/quoted/Quotes.scala | 11 ++++++++++- scala3doc/src/dotty/dokka/tasty/BasicSupport.scala | 11 ++--------- scala3doc/src/dotty/dokka/tasty/TypesSupport.scala | 5 +---- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 66622a253a93..2e63b7ee00f6 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -2092,7 +2092,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler given ConstantMethods: ConstantMethods with extension (self: Constant) def value: Any = self.value - def show: String = Extractors.showConstant(using QuotesImpl.this)(self) + def show(using printer: Printer[Constant]): String = printer.show(self) end extension end ConstantMethods @@ -2774,6 +2774,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def show(tpe: TypeRepr): String = Extractors.showType(using QuotesImpl.this)(tpe) + lazy val ConstantCode: Printer[Constant] = new Printer[Constant]: + def show(const: Constant): String = + const.show(using ctx.fresh.setSetting(ctx.settings.color, "never")) + + lazy val ConstantStructure: Printer[Constant] = new Printer[Constant]: + def show(const: Constant): String = + Extractors.showConstant(using QuotesImpl.this)(const) + end Printer end reflect diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index defdfe940bfb..e8dfcac20bba 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -2758,7 +2758,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => def value: Any /** Shows the constant as a String */ - def show: String + def show(using Printer[Constant]): String + end extension } @@ -4113,6 +4114,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** Default pinter for `TypeRepr` used when calling `tpe.show` */ given TypeReprPrinter: Printer[TypeRepr] = Printer.TypeReprCode + /** Default pinter for `Constant` used when calling `const.show` */ + given ConstantPrinter: Printer[Constant] = Printer.ConstantCode + /** Module object of `type Printer`. * Contains custom printers such as `TreeCode`, `TreeAnsiCode`, `TreeCases`, `TypeReprCode`, ..., `SymbolFullName` and `FlagsCombination`. */ @@ -4152,6 +4156,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => */ def TypeReprStructure: Printer[TypeRepr] + /** Prints the constant in source code. */ + def ConstantCode: Printer[Constant] + + /** Prints a pattern like representation of the `Constant`. */ + def ConstantStructure: Printer[Constant] } } diff --git a/scala3doc/src/dotty/dokka/tasty/BasicSupport.scala b/scala3doc/src/dotty/dokka/tasty/BasicSupport.scala index 91c8f8529765..1bc00cfd5d6a 100644 --- a/scala3doc/src/dotty/dokka/tasty/BasicSupport.scala +++ b/scala3doc/src/dotty/dokka/tasty/BasicSupport.scala @@ -18,14 +18,8 @@ trait BasicSupport: val params = annotTerm match case Apply(target, appliedWith) => { appliedWith.flatMap { - case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.value match { - case s: String => "\"" + s"$s" + "\"" - case other => other.toString() - })) - case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.value match - case s: String => "\"" + s"$s" + "\"" - case other => other.toString() - )) + case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.show)) + case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.show)) case x @ Select(qual, name) => None case other => Some(Annotation.UnresolvedParameter(None, other.show)) } @@ -33,7 +27,6 @@ trait BasicSupport: Annotation(dri, params) - extension (sym: Symbol) def documentation = sym.docstring match case Some(docstring) => diff --git a/scala3doc/src/dotty/dokka/tasty/TypesSupport.scala b/scala3doc/src/dotty/dokka/tasty/TypesSupport.scala index 72adc698fa18..32ab6c4083c7 100644 --- a/scala3doc/src/dotty/dokka/tasty/TypesSupport.scala +++ b/scala3doc/src/dotty/dokka/tasty/TypesSupport.scala @@ -84,10 +84,7 @@ trait TypesSupport: case AndType(left, right) => inner(left) ++ texts(" & ") ++ inner(right) case ByNameType(tpe) => text("=> ") :: inner(tpe) case ConstantType(constant) => - texts(constant.value match - case c: Char => s"'$c'" - case other => other.toString - ) + texts(constant.show) case ThisType(tpe) => inner(tpe) case AnnotatedType(AppliedType(_, Seq(tpe)), annotation) if isRepeated(annotation) => inner(tpe) :+ text("*")