Skip to content

Commit

Permalink
Remove OldTastyInspector from scaladoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed May 31, 2023
1 parent 28915c4 commit 931e9f2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ case class Module(rootPackage: Member, members: Map[DRI, Member])

object ScalaModuleProvider:
def mkModule()(using ctx: DocContext): Module =
val (result, rootDoc) = ScaladocTastyInspector().result()
val (result, rootDoc) = ScaladocTastyInspector.loadDocs()
val (rootPck, rest) = result.partition(_.name == "API")
val (emptyPackages, nonemptyPackages) = (rest ++ rootPck.flatMap(_.members))
.filter(p => p.members.nonEmpty || p.docs.nonEmpty).sortBy(_.name)
Expand Down
65 changes: 34 additions & 31 deletions scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package tasty
import java.util.regex.Pattern

import scala.util.{Try, Success, Failure}
import scala.tasty.inspector.DocTastyInspector
import scala.tasty.inspector.{TastyInspector, Inspector, Tasty}
import scala.quoted._

import dotty.tools.dotc
Expand All @@ -24,24 +24,12 @@ import ScaladocSupport._
*
* Delegates most of the work to [[TastyParser]] [[dotty.tools.scaladoc.tasty.TastyParser]].
*/
case class ScaladocTastyInspector()(using ctx: DocContext) extends DocTastyInspector:
case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:

private val topLevels = Seq.newBuilder[(String, Member)]
private var rootDoc: Option[Comment] = None

def processCompilationUnit(using Quotes)(root: reflect.Tree): Unit = ()

override def postProcess(using Quotes): Unit =
// hack into the compiler to get a list of all top-level trees
// in principle, to do this, one would collect trees in processCompilationUnit
// however, path-dependent types disallow doing so w/o using casts
inline def hackForeachTree(thunk: reflect.Tree => Unit): Unit =
given dctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
dctx.run.nn.units.foreach { compilationUnit =>
// mirrors code from TastyInspector
thunk(compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
}

def inspect(using Quotes)(tastys: List[scala.tasty.inspector.Tasty[quotes.type]]): Unit =
val symbolsToSkip: Set[reflect.Symbol] =
ctx.args.identifiersToSkip.flatMap { ref =>
val qrSymbol = reflect.Symbol
Expand Down Expand Up @@ -116,7 +104,8 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends DocTastyInspe
rootDoc = Some(parseCommentString(using parser.qctx, summon[DocContext])(content, topLevelPck, None))
}

hackForeachTree { root =>
for tasty <- tastys do {
val root = tasty.ast
if !isSkipped(root.symbol) then
val treeRoot = root.asInstanceOf[parser.qctx.reflect.Tree]
processRootDocIfNeeded(treeRoot)
Expand All @@ -138,33 +127,47 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends DocTastyInspe
topLevels += "scala" -> Member(scalaPckg.fullName, "", scalaPckg.dri, Kind.Package)
topLevels += mergeAnyRefAliasAndObject(parser)

def result(): (List[Member], Option[Comment]) =
topLevels.clear()
rootDoc = None


def mergeAnyRefAliasAndObject(parser: TastyParser) =
import parser.qctx.reflect._
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
"scala" -> aM.copy(
kind = Kind.Class(Nil, Nil),
members = objectMembers
)

object ScaladocTastyInspector:

def loadDocs()(using ctx: DocContext): (List[Member], Option[Comment]) =
val filePaths = ctx.args.tastyFiles.map(_.getAbsolutePath).toList
val classpath = ctx.args.classpath.split(java.io.File.pathSeparator).toList

if filePaths.nonEmpty then inspectFilesInContext(classpath, filePaths)
val inspector = new ScaladocTastyInspector

val (tastyPaths, nonTastyPaths) = filePaths.partition(_.endsWith(".tasty"))
val (jarPaths, invalidPaths) = nonTastyPaths.partition(_.endsWith(".jar"))

for invalidPath <- invalidPaths do
report.error("File extension is not `tasty` or `jar`: " + invalidPath)

if tastyPaths.nonEmpty then
TastyInspector.inspectAllTastyFiles(tastyPaths, jarPaths, classpath)(inspector)

val all = topLevels.result()
val all = inspector.topLevels.result()
all.groupBy(_._1).map { case (pckName, members) =>
val (pcks, rest) = members.map(_._2).partition(_.kind == Kind.Package)
val basePck = pcks.reduce( (p1, p2) =>
val withNewMembers = p1.withNewMembers(p2.members)
if withNewMembers.docs.isEmpty then withNewMembers.withDocs(p2.docs) else withNewMembers
)
basePck.withMembers((basePck.members ++ rest).sortBy(_.name))
}.toList -> rootDoc
}.toList -> inspector.rootDoc

end ScaladocTastyInspector

def mergeAnyRefAliasAndObject(parser: TastyParser) =
import parser.qctx.reflect._
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
"scala" -> aM.copy(
kind = Kind.Class(Nil, Nil),
members = objectMembers
)
/** Parses a single Tasty compilation unit. */
case class TastyParser(
qctx: Quotes,
Expand Down
9 changes: 0 additions & 9 deletions scaladoc/src/scala/tasty/inspector/DocTastyInspector.scala

This file was deleted.

136 changes: 0 additions & 136 deletions scaladoc/src/scala/tasty/inspector/OldTastyInspector.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dotty.tools.scaladoc
package tasty.comments

import scala.quoted.Quotes
import scala.quoted.*

import org.junit.{Test, Rule}
import org.junit.Assert.{assertSame, assertTrue}
Expand Down Expand Up @@ -198,14 +198,11 @@ class MemberLookupTests {

@Test
def test(): Unit = {
import scala.tasty.inspector.OldTastyInspector
class Inspector extends OldTastyInspector:
var alreadyRan: Boolean = false
import scala.tasty.inspector.*
class MyInspector extends Inspector:

override def processCompilationUnit(using ctx: quoted.Quotes)(root: ctx.reflect.Tree): Unit =
if !alreadyRan then
this.test()
alreadyRan = true
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
this.test()

def test()(using q: Quotes): Unit = {
import dotty.tools.scaladoc.tasty.comments.MemberLookup
Expand All @@ -215,6 +212,6 @@ class MemberLookupTests {
cases.testAll()
}

Inspector().inspectTastyFiles(TestUtils.listOurClasses())
TastyInspector.inspectTastyFiles(TestUtils.listOurClasses())(new MyInspector)
}
}

0 comments on commit 931e9f2

Please sign in to comment.