Skip to content

Pretty print quotes #4213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -51,4 +51,10 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
}
}
}

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)
}
}
17 changes: 12 additions & 5 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ 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 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 => _, _}

def isLocalThis(tree: Tree) = tree.typeOpt match {
Expand Down Expand Up @@ -274,7 +277,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[_] =>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -511,6 +514,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)

Expand Down Expand Up @@ -550,7 +557,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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-1.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<special-ops>.'[Int](3)
'(3)
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-2.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
val a: quoted.Expr[Int] = '(4)
a
}
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-4.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
val t: quoted.Type[String] = <special-ops>.type_'[String]
val t: quoted.Type[String] = '[String]
t: quoted.Type[String]
}
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-5.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
val a: quoted.Expr[Int] = '(4)
a
}