From 88ca3c1228920a8820af8957f4680f45b4d1d5e2 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 29 Mar 2018 15:47:42 +0200 Subject: [PATCH 1/2] Pretty print quotes --- .../tools/dotc/printing/DecompilerPrinter.scala | 14 +++++++++++++- .../dotty/tools/dotc/printing/RefinedPrinter.scala | 10 +++++++--- tests/run-with-compiler/quote-nested-1.check | 2 +- tests/run-with-compiler/quote-nested-2.check | 2 +- tests/run-with-compiler/quote-nested-4.check | 2 +- tests/run-with-compiler/quote-nested-5.check | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala index 392d719cfefd..3633ab4b497b 100644 --- a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala @@ -1,6 +1,6 @@ package dotty.tools.dotc.printing -import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef} +import dotty.tools.dotc.ast.Trees._ import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef} import dotty.tools.dotc.ast.{Trees, untpd} import dotty.tools.dotc.printing.Texts._ @@ -51,4 +51,16 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) { } } } + + override protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = { + import untpd.{modsDeco => _, _} + tree match { + case TypeApply(fun, args) => + if (tree.symbol eq defn.quoteMethod) "'" + else if (tree.symbol eq defn.typeQuoteMethod) "'[" ~ toTextGlobal(args, ", ") ~ "]" + else super.toTextCore(tree) + case _ => + super.toTextCore(tree) + } + } } diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 48bc992f8a47..a04fcf037baf 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -229,7 +229,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text = ("{" ~ toText(trees, "\n") ~ "}").close - override def toText[T >: Untyped](tree: Tree[T]): Text = controlled { + protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = { import untpd.{modsDeco => _, _} def isLocalThis(tree: Tree) = tree.typeOpt match { @@ -274,7 +274,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { case _ => toTextGlobal(arg) } - def toTextCore(tree: Tree): Text = tree match { + tree match { case id: Trees.BackquotedIdent[_] if !homogenizedView => "`" ~ toText(id.name) ~ "`" case id: Trees.SearchFailureIdent[_] => @@ -511,6 +511,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { case _ => tree.fallbackToText(this) } + } + + override def toText[T >: Untyped](tree: Tree[T]): Text = controlled { + import untpd.{modsDeco => _, _} var txt = toTextCore(tree) @@ -550,7 +554,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { if (ctx.settings.YprintPosSyms.value && tree.isDef) txt = (txt ~ s"@@(${tree.symbol.name}=" ~ tree.symbol.pos.toString ~ ")").close - } + } if (ctx.settings.YshowTreeIds.value) txt = (txt ~ "#" ~ tree.uniqueId.toString).close tree match { diff --git a/tests/run-with-compiler/quote-nested-1.check b/tests/run-with-compiler/quote-nested-1.check index 2378a7f62ea6..1ab350bb1762 100644 --- a/tests/run-with-compiler/quote-nested-1.check +++ b/tests/run-with-compiler/quote-nested-1.check @@ -1 +1 @@ -.'[Int](3) +'(3) diff --git a/tests/run-with-compiler/quote-nested-2.check b/tests/run-with-compiler/quote-nested-2.check index aea924902147..90df317c6cb6 100644 --- a/tests/run-with-compiler/quote-nested-2.check +++ b/tests/run-with-compiler/quote-nested-2.check @@ -1,4 +1,4 @@ { - val a: quoted.Expr[Int] = .'[Int](4) + val a: quoted.Expr[Int] = '(4) a } diff --git a/tests/run-with-compiler/quote-nested-4.check b/tests/run-with-compiler/quote-nested-4.check index ff8256f4afe7..5d37730e8890 100644 --- a/tests/run-with-compiler/quote-nested-4.check +++ b/tests/run-with-compiler/quote-nested-4.check @@ -1,4 +1,4 @@ { - val t: quoted.Type[String] = .type_'[String] + val t: quoted.Type[String] = '[String] t: quoted.Type[String] } diff --git a/tests/run-with-compiler/quote-nested-5.check b/tests/run-with-compiler/quote-nested-5.check index aea924902147..90df317c6cb6 100644 --- a/tests/run-with-compiler/quote-nested-5.check +++ b/tests/run-with-compiler/quote-nested-5.check @@ -1,4 +1,4 @@ { - val a: quoted.Expr[Int] = .'[Int](4) + val a: quoted.Expr[Int] = '(4) a } From 36e2018a4a347c38fd888c7eb7cc2414ea63beae Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 9 Apr 2018 10:51:33 +0200 Subject: [PATCH 2/2] Define and override RefinedPrinter.typeApplyText --- .../tools/dotc/printing/DecompilerPrinter.scala | 14 ++++---------- .../dotty/tools/dotc/printing/RefinedPrinter.scala | 7 +++++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala index 3633ab4b497b..ee4ff251ff8b 100644 --- a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala @@ -52,15 +52,9 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) { } } - override protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = { - import untpd.{modsDeco => _, _} - tree match { - case TypeApply(fun, args) => - if (tree.symbol eq defn.quoteMethod) "'" - else if (tree.symbol eq defn.typeQuoteMethod) "'[" ~ toTextGlobal(args, ", ") ~ "]" - else super.toTextCore(tree) - case _ => - super.toTextCore(tree) - } + override protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = { + if (tree.symbol eq defn.quoteMethod) "'" + else if (tree.symbol eq defn.typeQuoteMethod) "'[" ~ toTextGlobal(tree.args, ", ") ~ "]" + else super.typeApplyText(tree) } } diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index a04fcf037baf..447c033a4d18 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -229,6 +229,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text = ("{" ~ toText(trees, "\n") ~ "}").close + protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = + toTextLocal(tree.fun) ~ "[" ~ toTextGlobal(tree.args, ", ") ~ "]" + protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = { import untpd.{modsDeco => _, _} @@ -308,8 +311,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { } else toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")" - case TypeApply(fun, args) => - toTextLocal(fun) ~ "[" ~ toTextGlobal(args, ", ") ~ "]" + case tree: TypeApply => + typeApplyText(tree) case Literal(c) => tree.typeOpt match { case ConstantType(tc) => withPos(toText(tc), tree.pos)