From 8d8c03fc7cf0d4b58d6a59dcb9a7a7ac1c635f83 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 11 Jan 2019 16:12:47 +0100 Subject: [PATCH 1/3] Print quotes with ' rather than Expr.apply Also add coloring for clearer visualization of the code --- .../src/dotty/tools/dotc/printing/RefinedPrinter.scala | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 72ad6850a1ce..08c3b6fbf990 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -318,7 +318,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { if (name.isTypeName) typeText(txt) else txt case tree @ Select(qual, name) => - if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name)) + if (tree.symbol == defn.QuotedExpr_~ || tree.symbol == defn.QuotedType_~) keywordStr("~(") ~ toTextLocal(qual) ~ keywordStr(")") + else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name)) else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR) case tree: This => optDotPrefix(tree) ~ keywordStr("this") ~ idText(tree) @@ -329,6 +330,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { changePrec (GlobalPrec) { keywordStr("throw ") ~ toText(args.head) } + else if (fun.symbol == defn.QuotedExpr_apply) + keywordStr("'{") ~ toTextGlobal(args, ", ") ~ keywordStr("}") + else if (fun.symbol == defn.QuotedType_apply) + keywordStr("'[") ~ toTextGlobal(args, ", ") ~ keywordStr("]") else toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")" case tree: TypeApply => From d70513369c1ceac10454c388b89f483b47aafc54 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 12 Jan 2019 22:20:42 +0100 Subject: [PATCH 2/3] Color untyped quotes --- compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 08c3b6fbf990..1595760a4340 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -563,7 +563,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { keywordStr("try ") ~ toText(expr) ~ " " ~ keywordStr("catch") ~ " {" ~ toText(handler) ~ "}" ~ optText(finalizer)(keywordStr(" finally ") ~ _) } case Quote(tree) => - if (tree.isType) "'[" ~ toTextGlobal(tree) ~ "]" else "'(" ~ toTextGlobal(tree) ~ ")" + if (tree.isType) keywordStr("'[") ~ toTextGlobal(tree) ~ keywordStr("]") else keywordStr("'{") ~ toTextGlobal(tree) ~ keywordStr("}") case Thicket(trees) => "Thicket {" ~~ toTextGlobal(trees, "\n") ~~ "}" case _ => From bb50f8b02c0ff6034336756c094775bb381a7ca9 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 14 Jan 2019 09:13:08 +0100 Subject: [PATCH 3/3] Check that the trees are typed before accessing the symbol --- compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 1595760a4340..849c0c5d7826 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -318,7 +318,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { if (name.isTypeName) typeText(txt) else txt case tree @ Select(qual, name) => - if (tree.symbol == defn.QuotedExpr_~ || tree.symbol == defn.QuotedType_~) keywordStr("~(") ~ toTextLocal(qual) ~ keywordStr(")") + if (tree.hasType && tree.symbol == defn.QuotedExpr_~ || tree.symbol == defn.QuotedType_~) keywordStr("~(") ~ toTextLocal(qual) ~ keywordStr(")") else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name)) else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR) case tree: This => @@ -330,9 +330,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { changePrec (GlobalPrec) { keywordStr("throw ") ~ toText(args.head) } - else if (fun.symbol == defn.QuotedExpr_apply) + else if (fun.hasType && fun.symbol == defn.QuotedExpr_apply) keywordStr("'{") ~ toTextGlobal(args, ", ") ~ keywordStr("}") - else if (fun.symbol == defn.QuotedType_apply) + else if (fun.hasType && fun.symbol == defn.QuotedType_apply) keywordStr("'[") ~ toTextGlobal(args, ", ") ~ keywordStr("]") else toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"