Skip to content

Commit 6bb6b91

Browse files
committed
Fix printing package objects and nested packages
1 parent 6fb7970 commit 6bb6b91

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,31 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
4141
def doubleLineBreak(): String = "\n\n" + (" " * indent)
4242

4343
def printTree(tree: Tree): Buffer = tree match {
44-
case tree @ PackageClause(Term.Ident(name), stats) =>
44+
case PackageObject(body)=>
45+
printTree(body) // Print package object
46+
47+
case PackageClause(Term.Ident(name), (inner @ PackageClause(_, _)) :: Nil) if name != "<empty>" && PackageObject.unapply(inner).isEmpty =>
48+
// print inner package as `package outer.inner { ... }`
49+
printTree(inner)
50+
51+
case tree @ PackageClause(name, stats) =>
4552
val stats1 = stats.collect {
4653
case stat @ PackageClause(_, _) => stat
4754
case stat @ Definition() if !(stat.flags.isObject && stat.flags.isLazy) => stat
4855
case stat @ Import(_, _) => stat
4956
}
50-
51-
if (name == "<empty>") {
52-
printTrees(stats1, lineBreak())
53-
} else {
54-
this += "package " += name += " {"
55-
indented {
56-
this += lineBreak()
57+
name match {
58+
case Term.Ident("<empty>") =>
5759
printTrees(stats1, lineBreak())
58-
}
59-
this += lineBreak() += "}"
60+
case _ =>
61+
this += "package "
62+
printType(name.tpe)
63+
this += " {"
64+
indented {
65+
this += lineBreak()
66+
printTrees(stats1, lineBreak())
67+
}
68+
this += lineBreak() += "}"
6069
}
6170

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

77-
if (flags.isObject) this += "object " += name.stripSuffix("$")
86+
if (name == "package$") {
87+
this += "package object "
88+
printDefinitionName(cdef.owner)
89+
}
90+
else if (flags.isObject) this += "object " += name.stripSuffix("$")
7891
else if (flags.isTrait) this += "trait " += name
7992
else if (flags.isAbstract) this += "abstract class " += name
8093
else this += "class " += name
@@ -1134,5 +1147,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
11341147
}
11351148
}
11361149

1150+
object PackageObject {
1151+
def unapply(tree: Tree)(implicit ctx: Context): Option[Tree] = tree match {
1152+
case PackageClause(_, ValDef("package", _, _) :: body :: Nil) => Some(body)
1153+
case _ => None
1154+
}
1155+
}
11371156

11381157
}

tests/pos/t6225.decompiled

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** Decompiled from out/posTestFromTasty/pos/t6225/ko.class */
2+
object ko {
3+
import library.y.{Foo, foo}
4+
dotty.DottyPredef.implicitly[library.y.Foo](library.y.foo)
5+
}
6+
/** Decompiled from out/posTestFromTasty/pos/t6225/ko2.class */
7+
object ko2 {
8+
import library.y.{_}
9+
dotty.DottyPredef.implicitly[library.y.Foo](library.y.foo)
10+
}
11+
/** Decompiled from out/posTestFromTasty/pos/t6225/library/x/X.class */
12+
package library.x {
13+
class X() {
14+
class Foo()
15+
implicit val foo: X.this.Foo = new X.this.Foo()
16+
}
17+
}
18+
/** Decompiled from out/posTestFromTasty/pos/t6225/library/y/package.class */
19+
package library {
20+
package object y extends library.x.X()
21+
}

0 commit comments

Comments
 (0)