Skip to content

Commit afc6396

Browse files
Merge pull request #10755 from dotty-staging/add-printer-to-constant-show
Add Printer to reflect Constant.show
2 parents a6e24e4 + 6f4b958 commit afc6396

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
@@ -2113,7 +2113,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
21132113
given ConstantMethods: ConstantMethods with
21142114
extension (self: Constant)
21152115
def value: Any = self.value
2116-
def show: String = Extractors.showConstant(using QuotesImpl.this)(self)
2116+
def show(using printer: Printer[Constant]): String = printer.show(self)
21172117
end extension
21182118
end ConstantMethods
21192119

@@ -2795,6 +2795,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27952795
def show(tpe: TypeRepr): String =
27962796
Extractors.showType(using QuotesImpl.this)(tpe)
27972797

2798+
lazy val ConstantCode: Printer[Constant] = new Printer[Constant]:
2799+
def show(const: Constant): String =
2800+
const.show(using ctx.fresh.setSetting(ctx.settings.color, "never"))
2801+
2802+
lazy val ConstantStructure: Printer[Constant] = new Printer[Constant]:
2803+
def show(const: Constant): String =
2804+
Extractors.showConstant(using QuotesImpl.this)(const)
2805+
27982806
end Printer
27992807
end reflect
28002808

library/src/scala/quoted/Quotes.scala

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

27952795
/** Shows the constant as a String */
2796-
def show: String
2796+
def show(using Printer[Constant]): String
2797+
27972798
end extension
27982799
}
27992800

@@ -4148,6 +4149,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41484149
/** Default pinter for `TypeRepr` used when calling `tpe.show` */
41494150
given TypeReprPrinter: Printer[TypeRepr] = Printer.TypeReprCode
41504151

4152+
/** Default pinter for `Constant` used when calling `const.show` */
4153+
given ConstantPrinter: Printer[Constant] = Printer.ConstantCode
4154+
41514155
/** Module object of `type Printer`.
41524156
* Contains custom printers such as `TreeCode`, `TreeAnsiCode`, `TreeCases`, `TypeReprCode`, ..., `SymbolFullName` and `FlagsCombination`.
41534157
*/
@@ -4187,6 +4191,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41874191
*/
41884192
def TypeReprStructure: Printer[TypeRepr]
41894193

4194+
/** Prints the constant in source code. */
4195+
def ConstantCode: Printer[Constant]
4196+
4197+
/** Prints a pattern like representation of the `Constant`. */
4198+
def ConstantStructure: Printer[Constant]
41904199
}
41914200

41924201
}

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)