Skip to content

Commit 74bcfd2

Browse files
Merge pull request #4213 from dotty-staging/print-quotes-for-users
Pretty print quotes
2 parents 962b4bf + 36e2018 commit 74bcfd2

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
5858
val impl1 = impl.copy(parents = impl.parents.filterNot(_.symbol.maybeOwner == defn.ObjectClass))
5959
super.toTextTemplate(impl1, ofNew)
6060
}
61+
62+
override protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {
63+
if (tree.symbol eq defn.quoteMethod) "'"
64+
else if (tree.symbol eq defn.typeQuoteMethod) "'[" ~ toTextGlobal(tree.args, ", ") ~ "]"
65+
else super.typeApplyText(tree)
66+
}
6167
}

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
232232
protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
233233
("{" ~ toText(trees, "\n") ~ "}").close
234234

235-
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
235+
protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text =
236+
toTextLocal(tree.fun) ~ "[" ~ toTextGlobal(tree.args, ", ") ~ "]"
237+
238+
protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = {
236239
import untpd.{modsDeco => _, _}
237240

238241
def isLocalThis(tree: Tree) = tree.typeOpt match {
@@ -277,7 +280,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
277280
case _ => toTextGlobal(arg)
278281
}
279282

280-
def toTextCore(tree: Tree): Text = tree match {
283+
tree match {
281284
case id: Trees.BackquotedIdent[_] if !homogenizedView =>
282285
"`" ~ toText(id.name) ~ "`"
283286
case id: Trees.SearchFailureIdent[_] =>
@@ -311,8 +314,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
311314
}
312315
else
313316
toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"
314-
case TypeApply(fun, args) =>
315-
toTextLocal(fun) ~ "[" ~ toTextGlobal(args, ", ") ~ "]"
317+
case tree: TypeApply =>
318+
typeApplyText(tree)
316319
case Literal(c) =>
317320
tree.typeOpt match {
318321
case ConstantType(tc) => withPos(toText(tc), tree.pos)
@@ -514,6 +517,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
514517
case _ =>
515518
tree.fallbackToText(this)
516519
}
520+
}
521+
522+
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
523+
import untpd.{modsDeco => _, _}
517524

518525
var txt = toTextCore(tree)
519526

@@ -553,7 +560,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
553560
if (ctx.settings.YprintPosSyms.value && tree.isDef)
554561
txt = (txt ~
555562
s"@@(${tree.symbol.name}=" ~ tree.symbol.pos.toString ~ ")").close
556-
}
563+
}
557564
if (ctx.settings.YshowTreeIds.value)
558565
txt = (txt ~ "#" ~ tree.uniqueId.toString).close
559566
tree match {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<special-ops>.'[Int](3)
1+
'(3)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
2+
val a: quoted.Expr[Int] = '(4)
33
a
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
val t: quoted.Type[String] = <special-ops>.type_'[String]
2+
val t: quoted.Type[String] = '[String]
33
t: quoted.Type[String]
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
2+
val a: quoted.Expr[Int] = '(4)
33
a
44
}

0 commit comments

Comments
 (0)