Skip to content

Commit 5ab7689

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 a34130f commit 5ab7689

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21782178
given ConstantMethods: ConstantMethods with
21792179
extension (self: Constant)
21802180
def value: Any = self.value
2181-
def show: String = Extractors.showConstant(using QuotesImpl.this)(self)
2181+
def show(using printer: Printer[Constant]): String = printer.show(self)
21822182
end extension
21832183
end ConstantMethods
21842184

@@ -2714,6 +2714,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27142714
def show(tpe: TypeRepr): String =
27152715
Extractors.showType(using QuotesImpl.this)(tpe)
27162716

2717+
lazy val ConstantCode: Printer[Constant] = new Printer[Constant]:
2718+
def show(const: Constant): String = const.show
2719+
2720+
lazy val ConstantStructure: Printer[Constant] = new Printer[Constant]:
2721+
def show(const: Constant): String =
2722+
Extractors.showConstant(using QuotesImpl.this)(const)
2723+
27172724
end Printer
27182725
end reflect
27192726

library/src/scala/quoted/Quotes.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2889,7 +2889,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
28892889
def value: Any
28902890

28912891
/** Shows the constant as a String */
2892-
def show: String
2892+
def show(using Printer[Constant]): String
28932893

28942894
end extension
28952895
}
@@ -4041,6 +4041,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
40414041
/** Default pinter for `TypeRepr` used when calling `tpe.show` */
40424042
given TypeReprPrinter: Printer[TypeRepr] = Printer.TypeReprCode
40434043

4044+
/** Default pinter for `Constant` used when calling `const.show` */
4045+
given ConstantPrinter: Printer[Constant] = Printer.ConstantCode
4046+
40444047
/** Module object of `type Printer`.
40454048
* Contains custom printers such as `TreeCode`, `TreeAnsiCode`, `TreeCases`, `TypeReprCode`, ..., `SymbolFullName` and `FlagsCombination`.
40464049
*/
@@ -4080,6 +4083,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
40804083
*/
40814084
def TypeReprStructure: Printer[TypeRepr]
40824085

4086+
/** Prints the constant in source code. */
4087+
def ConstantCode: Printer[Constant]
4088+
4089+
/** Prints a pattern like representation of the `Constant`. */
4090+
def ConstantStructure: Printer[Constant]
40834091
}
40844092

40854093
}

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)