From 9505aff3aa44bb3a37c47c244ac6ab9de83fd2a4 Mon Sep 17 00:00:00 2001 From: VladimirNik Date: Wed, 12 Feb 2014 05:59:57 +0400 Subject: printers flag processing improvements: printRootPkg flag fixed, comments to flags metadata printing added --- src/reflect/scala/reflect/internal/Printers.scala | 35 ++++++++++++++-------- .../scala/reflect/internal/PrintersTest.scala | 9 ++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/reflect/scala/reflect/internal/Printers.scala b/src/reflect/scala/reflect/internal/Printers.scala index cb4abd8bd8..45be394231 100644 --- a/src/reflect/scala/reflect/internal/Printers.scala +++ b/src/reflect/scala/reflect/internal/Printers.scala @@ -73,11 +73,14 @@ trait Printers extends api.Printers { self: SymbolTable => def indent() = indentMargin += indentStep def undent() = indentMargin -= indentStep - def printPosition(tree: Tree) = if (printPositions) print(tree.pos.show) + def printPosition(tree: Tree) = + if (printPositions) comment(print(tree.pos.show)) protected def printTypesInfo(tree: Tree) = if (printTypes && tree.isTerm && tree.canHaveAttrs) - print("{", if (tree.tpe eq null) "" else tree.tpe.toString, "}") + comment{ + print("{", if (tree.tpe eq null) "" else tree.tpe.toString, "}") + } def println() = { out.println() @@ -128,12 +131,17 @@ trait Printers extends api.Printers { self: SymbolTable => print(symName(p, p.name)); printOpt(": ", TypeTree() setType p.tpe) } - protected def parenthesize(condition: Boolean = true)(body: => Unit) = { - if (condition) print("(") + protected def parenthesize(condition: Boolean = true, open: String = "(", close: String = ")")(body: => Unit) = { + if (condition) print(open) body - if (condition) print(")") + if (condition) print(close) } + protected val commentsRequired = false + + protected def comment(body: => Unit) = + parenthesize(commentsRequired, "/*", "*/")(body) + protected def printImplicitInParamsList(vds: List[ValDef]) = if (vds.nonEmpty) printFlags(vds.head.mods.flags & IMPLICIT, "") @@ -279,7 +287,11 @@ trait Printers extends api.Printers { self: SymbolTable => print("("); printValueParams print(" => ", body, ")") - if (printIds && tree.symbol != null) print("#" + tree.symbol.id) + if (printIds && tree.symbol != null) + comment{ + print("#" + tree.symbol.id) + } + if (printOwners && tree.symbol != null) print("@" + tree.symbol.owner.id) } @@ -547,6 +559,8 @@ trait Printers extends api.Printers { self: SymbolTable => qualIsIntLit && name.isOperatorName } + override protected val commentsRequired = true + protected def needsParentheses(parent: Tree)(insideIf: Boolean = true, insideMatch: Boolean = true, insideTry: Boolean = true, insideAnnotated: Boolean = true, insideBlock: Boolean = true, insideLabelDef: Boolean = true) = { parent match { @@ -1040,11 +1054,7 @@ trait Printers extends api.Printers { self: SymbolTable => case _ if syntheticToRemove(tree) => case tt: TypeTree => - if (printPositions) { - if (tt.original != null) - print("") - else print("") - } else if (!emptyTree(tt)) print(tt.original) + if (!emptyTree(tt)) print(tt.original) // print only fun when targs are TypeTrees with empty original case TypeApply(fun, targs) => @@ -1076,7 +1086,7 @@ trait Printers extends api.Printers { self: SymbolTable => }) && (tr match { // check that Select contains package case Select(q, _) => checkRootPackage(q) case _: Ident | _: This => val sym = tr.symbol - tr.hasExistingSymbol && sym.isPackage && !sym.isRootPackage + tr.hasExistingSymbol && sym.isPackage && sym.name != nme.ROOTPKG case _ => false }) @@ -1093,6 +1103,7 @@ trait Printers extends api.Printers { self: SymbolTable => } case This(qual) => + // todo: add symbol checking for common printer if (tree.symbol.isPackage) print(tree.symbol.fullName) else super.processTreePrinting(tree) diff --git a/test/junit/scala/reflect/internal/PrintersTest.scala b/test/junit/scala/reflect/internal/PrintersTest.scala index 534cd5b95f..0e93e30dcc 100644 --- a/test/junit/scala/reflect/internal/PrintersTest.scala +++ b/test/junit/scala/reflect/internal/PrintersTest.scala @@ -28,7 +28,7 @@ object PrinterHelper { private def normalizeEOL(resultCode: String) = resultCode.lines mkString s"$LF" - def assertResultCode(code: String)(parsedCode: String = "", typedCode: String = "", wrap: Boolean = false) = { + def assertResultCode(code: String)(parsedCode: String = "", typedCode: String = "", wrap: Boolean = false, printRoot: Boolean = false) = { def toolboxTree(tree: => Tree) = try{ tree } catch { @@ -55,7 +55,7 @@ object PrinterHelper { assertEquals("using toolbox parser" + LF, wrapCode(parsedCode), normalizeEOL(showCode(parsedTree))) if (!typedCode.isEmpty()) { val typedTree = toolboxTree(toolbox.typecheck(parsedTree)) - assertEquals("using toolbox typechecker" + LF, wrapCode(typedCode), normalizeEOL(showCode(typedTree))) + assertEquals("using toolbox typechecker" + LF, wrapCode(typedCode), normalizeEOL(showCode(typedTree, printRootPkg = printRoot))) } } @@ -328,6 +328,11 @@ trait BasePrintTests { code = "List(1, 2, 3) map (_ - 1)")( parsedCode = "List(1, 2, 3).map(((x$1) => x$1.-(1))) ", typedCode = "scala.collection.immutable.List.apply(1, 2, 3).map(((x$1) => x$1.-(1)))(scala.collection.immutable.List.canBuildFrom)") + + @Test def testFunc4 = assertResultCode( + code = "val x: String => Int = ((str: String) => 1)")( + parsedCode = "val x: _root_.scala.Function1[String, Int] = ((str: String) => 1)", + typedCode = " val x: _root_.scala.Function1[_root_.scala.Predef.String, _root_.scala.Int] = ((str: _root_.scala.Predef.String) => 1)", printRoot = true) @Test def testImport1 = assertPrintedCode("import scala.collection.mutable") -- cgit v1.2.3