Skip to content

Commit

Permalink
Fix printing package objects and nested packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Jun 22, 2018
1 parent 6fb7970 commit 6bb6b91
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
41 changes: 30 additions & 11 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def doubleLineBreak(): String = "\n\n" + (" " * indent)

def printTree(tree: Tree): Buffer = tree match {
case tree @ PackageClause(Term.Ident(name), stats) =>
case PackageObject(body)=>
printTree(body) // Print package object

case PackageClause(Term.Ident(name), (inner @ PackageClause(_, _)) :: Nil) if name != "<empty>" && PackageObject.unapply(inner).isEmpty =>
// print inner package as `package outer.inner { ... }`
printTree(inner)

case tree @ PackageClause(name, stats) =>
val stats1 = stats.collect {
case stat @ PackageClause(_, _) => stat
case stat @ Definition() if !(stat.flags.isObject && stat.flags.isLazy) => stat
case stat @ Import(_, _) => stat
}

if (name == "<empty>") {
printTrees(stats1, lineBreak())
} else {
this += "package " += name += " {"
indented {
this += lineBreak()
name match {
case Term.Ident("<empty>") =>
printTrees(stats1, lineBreak())
}
this += lineBreak() += "}"
case _ =>
this += "package "
printType(name.tpe)
this += " {"
indented {
this += lineBreak()
printTrees(stats1, lineBreak())
}
this += lineBreak() += "}"
}

case Import(expr, selectors) =>
Expand All @@ -74,7 +83,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
if (flags.isFinal && !flags.isObject) this += "final "
if (flags.isCase) this += "case "

if (flags.isObject) this += "object " += name.stripSuffix("$")
if (name == "package$") {
this += "package object "
printDefinitionName(cdef.owner)
}
else if (flags.isObject) this += "object " += name.stripSuffix("$")
else if (flags.isTrait) this += "trait " += name
else if (flags.isAbstract) this += "abstract class " += name
else this += "class " += name
Expand Down Expand Up @@ -1134,5 +1147,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}
}

object PackageObject {
def unapply(tree: Tree)(implicit ctx: Context): Option[Tree] = tree match {
case PackageClause(_, ValDef("package", _, _) :: body :: Nil) => Some(body)
case _ => None
}
}

}
21 changes: 21 additions & 0 deletions tests/pos/t6225.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** Decompiled from out/posTestFromTasty/pos/t6225/ko.class */
object ko {
import library.y.{Foo, foo}
dotty.DottyPredef.implicitly[library.y.Foo](library.y.foo)
}
/** Decompiled from out/posTestFromTasty/pos/t6225/ko2.class */
object ko2 {
import library.y.{_}
dotty.DottyPredef.implicitly[library.y.Foo](library.y.foo)
}
/** Decompiled from out/posTestFromTasty/pos/t6225/library/x/X.class */
package library.x {
class X() {
class Foo()
implicit val foo: X.this.Foo = new X.this.Foo()
}
}
/** Decompiled from out/posTestFromTasty/pos/t6225/library/y/package.class */
package library {
package object y extends library.x.X()
}

0 comments on commit 6bb6b91

Please sign in to comment.