@@ -5,7 +5,7 @@ package tasty
55import java .util .regex .Pattern
66
77import scala .util .{Try , Success , Failure }
8- import scala .tasty .inspector .{ TastyInspector , Inspector , Tasty }
8+ import scala .tasty .inspector .DocTastyInspector
99import scala .quoted ._
1010
1111import dotty .tools .dotc
@@ -24,12 +24,24 @@ import ScaladocSupport._
2424 *
2525 * Delegates most of the work to [[TastyParser ]] [[dotty.tools.scaladoc.tasty.TastyParser ]].
2626 */
27- case class ScaladocTastyInspector ()(using ctx : DocContext ) extends Inspector :
27+ case class ScaladocTastyInspector ()(using ctx : DocContext ) extends DocTastyInspector :
2828
2929 private val topLevels = Seq .newBuilder[(String , Member )]
3030 private var rootDoc : Option [Comment ] = None
3131
32- def inspect (using Quotes )(tastys : List [scala.tasty.inspector.Tasty [quotes.type ]]): Unit =
32+ def processCompilationUnit (using Quotes )(root : reflect.Tree ): Unit = ()
33+
34+ override def postProcess (using Quotes ): Unit =
35+ // hack into the compiler to get a list of all top-level trees
36+ // in principle, to do this, one would collect trees in processCompilationUnit
37+ // however, path-dependent types disallow doing so w/o using casts
38+ inline def hackForeachTree (thunk : reflect.Tree => Unit ): Unit =
39+ given dctx : dotc.core.Contexts .Context = quotes.asInstanceOf [scala.quoted.runtime.impl.QuotesImpl ].ctx
40+ dctx.run.nn.units.foreach { compilationUnit =>
41+ // mirrors code from TastyInspector
42+ thunk(compilationUnit.tpdTree.asInstanceOf [reflect.Tree ])
43+ }
44+
3345 val symbolsToSkip : Set [reflect.Symbol ] =
3446 ctx.args.identifiersToSkip.flatMap { ref =>
3547 val qrSymbol = reflect.Symbol
@@ -104,8 +116,7 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
104116 rootDoc = Some (parseCommentString(using parser.qctx, summon[DocContext ])(content, topLevelPck, None ))
105117 }
106118
107- for tasty <- tastys do {
108- val root = tasty.ast
119+ hackForeachTree { root =>
109120 if ! isSkipped(root.symbol) then
110121 val treeRoot = root.asInstanceOf [parser.qctx.reflect.Tree ]
111122 processRootDocIfNeeded(treeRoot)
@@ -130,7 +141,23 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
130141 topLevels += " scala" -> Member (scalaPckg.fullName, " " , scalaPckg.dri, Kind .Package )
131142 topLevels += mergeAnyRefAliasAndObject(parser)
132143
144+ def result (): (List [Member ], Option [Comment ]) =
145+ topLevels.clear()
146+ rootDoc = None
147+ val filePaths = ctx.args.tastyFiles.map(_.getAbsolutePath).toList
148+ val classpath = ctx.args.classpath.split(java.io.File .pathSeparator).toList
133149
150+ if filePaths.nonEmpty then inspectFilesInContext(classpath, filePaths)
151+
152+ val all = topLevels.result()
153+ all.groupBy(_._1).map { case (pckName, members) =>
154+ val (pcks, rest) = members.map(_._2).partition(_.kind == Kind .Package )
155+ val basePck = pcks.reduce( (p1, p2) =>
156+ val withNewMembers = p1.withNewMembers(p2.members)
157+ if withNewMembers.docs.isEmpty then withNewMembers.withDocs(p2.docs) else withNewMembers
158+ )
159+ basePck.withMembers((basePck.members ++ rest).sortBy(_.name))
160+ }.toList -> rootDoc
134161
135162 def mergeAnyRefAliasAndObject (parser : TastyParser ) =
136163 import parser .qctx .reflect ._
@@ -144,36 +171,6 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
144171 kind = Kind .Class (Nil , Nil ),
145172 members = objectMembers
146173 )
147-
148- object ScaladocTastyInspector :
149-
150- def loadDocs ()(using ctx : DocContext ): (List [Member ], Option [Comment ]) =
151- val filePaths = ctx.args.tastyFiles.map(_.getAbsolutePath).toList
152- val classpath = ctx.args.classpath.split(java.io.File .pathSeparator).toList
153-
154- val inspector = new ScaladocTastyInspector
155-
156- val (tastyPaths, nonTastyPaths) = filePaths.partition(_.endsWith(" .tasty" ))
157- val (jarPaths, invalidPaths) = nonTastyPaths.partition(_.endsWith(" .jar" ))
158-
159- for invalidPath <- invalidPaths do
160- report.error(" File extension is not `tasty` or `jar`: " + invalidPath)
161-
162- if tastyPaths.nonEmpty then
163- TastyInspector .inspectAllTastyFiles(tastyPaths, jarPaths, classpath)(inspector)
164-
165- val all = inspector.topLevels.result()
166- all.groupBy(_._1).map { case (pckName, members) =>
167- val (pcks, rest) = members.map(_._2).partition(_.kind == Kind .Package )
168- val basePck = pcks.reduce( (p1, p2) =>
169- val withNewMembers = p1.withNewMembers(p2.members)
170- if withNewMembers.docs.isEmpty then withNewMembers.withDocs(p2.docs) else withNewMembers
171- )
172- basePck.withMembers((basePck.members ++ rest).sortBy(_.name))
173- }.toList -> inspector.rootDoc
174-
175- end ScaladocTastyInspector
176-
177174/** Parses a single Tasty compilation unit. */
178175case class TastyParser (
179176 qctx : Quotes ,
0 commit comments