Skip to content

Commit 6f4b958

Browse files
committed
Add Printer to reflect Constant.show
We can either print the structure with the pattern or the code like representation of the value.
1 parent 4d0a7d8 commit 6f4b958

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
20922092
given ConstantMethods: ConstantMethods with
20932093
extension (self: Constant)
20942094
def value: Any = self.value
2095-
def show: String = Extractors.showConstant(using QuotesImpl.this)(self)
2095+
def show(using printer: Printer[Constant]): String = printer.show(self)
20962096
end extension
20972097
end ConstantMethods
20982098

@@ -2774,6 +2774,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27742774
def show(tpe: TypeRepr): String =
27752775
Extractors.showType(using QuotesImpl.this)(tpe)
27762776

2777+
lazy val ConstantCode: Printer[Constant] = new Printer[Constant]:
2778+
def show(const: Constant): String =
2779+
const.show(using ctx.fresh.setSetting(ctx.settings.color, "never"))
2780+
2781+
lazy val ConstantStructure: Printer[Constant] = new Printer[Constant]:
2782+
def show(const: Constant): String =
2783+
Extractors.showConstant(using QuotesImpl.this)(const)
2784+
27772785
end Printer
27782786
end reflect
27792787

library/src/scala/quoted/Quotes.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,7 +2758,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
27582758
def value: Any
27592759

27602760
/** Shows the constant as a String */
2761-
def show: String
2761+
def show(using Printer[Constant]): String
2762+
27622763
end extension
27632764
}
27642765

@@ -4113,6 +4114,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41134114
/** Default pinter for `TypeRepr` used when calling `tpe.show` */
41144115
given TypeReprPrinter: Printer[TypeRepr] = Printer.TypeReprCode
41154116

4117+
/** Default pinter for `Constant` used when calling `const.show` */
4118+
given ConstantPrinter: Printer[Constant] = Printer.ConstantCode
4119+
41164120
/** Module object of `type Printer`.
41174121
* Contains custom printers such as `TreeCode`, `TreeAnsiCode`, `TreeCases`, `TypeReprCode`, ..., `SymbolFullName` and `FlagsCombination`.
41184122
*/
@@ -4152,6 +4156,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41524156
*/
41534157
def TypeReprStructure: Printer[TypeRepr]
41544158

4159+
/** Prints the constant in source code. */
4160+
def ConstantCode: Printer[Constant]
4161+
4162+
/** Prints a pattern like representation of the `Constant`. */
4163+
def ConstantStructure: Printer[Constant]
41554164
}
41564165

41574166
}

scala3doc/src/dotty/dokka/tasty/BasicSupport.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ trait BasicSupport:
1818
val params = annotTerm match
1919
case Apply(target, appliedWith) => {
2020
appliedWith.flatMap {
21-
case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.value match {
22-
case s: String => "\"" + s"$s" + "\""
23-
case other => other.toString()
24-
}))
25-
case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.value match
26-
case s: String => "\"" + s"$s" + "\""
27-
case other => other.toString()
28-
))
21+
case Literal(constant) => Some(Annotation.PrimitiveParameter(None, constant.show))
22+
case NamedArg(name, Literal(constant)) => Some(Annotation.PrimitiveParameter(Some(name), constant.show))
2923
case x @ Select(qual, name) => None
3024
case other => Some(Annotation.UnresolvedParameter(None, other.show))
3125
}
3226
}
3327

3428
Annotation(dri, params)
3529

36-
3730
extension (sym: Symbol)
3831
def documentation = sym.docstring match
3932
case Some(docstring) =>

scala3doc/src/dotty/dokka/tasty/TypesSupport.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ trait TypesSupport:
8484
case AndType(left, right) => inner(left) ++ texts(" & ") ++ inner(right)
8585
case ByNameType(tpe) => text("=> ") :: inner(tpe)
8686
case ConstantType(constant) =>
87-
texts(constant.value match
88-
case c: Char => s"'$c'"
89-
case other => other.toString
90-
)
87+
texts(constant.show)
9188
case ThisType(tpe) => inner(tpe)
9289
case AnnotatedType(AppliedType(_, Seq(tpe)), annotation) if isRepeated(annotation) =>
9390
inner(tpe) :+ text("*")

0 commit comments

Comments
 (0)