From 81a86be75dd45f6e3eae4bb5fdd016058738c497 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Sat, 25 Jun 2016 11:24:32 +0200 Subject: Materialize parameter list references --- .../src/dotty/tools/dottydoc/core/DocPhase.scala | 15 +++++--- .../src/dotty/tools/dottydoc/model/parsers.scala | 45 ++++++++++++++++------ .../jvm/src/dotty/tools/dottydoc/util/mutate.scala | 7 +++- .../dotty/tools/dottydoc/model/factories.scala | 17 +++++--- 4 files changed, 61 insertions(+), 23 deletions(-) (limited to 'dottydoc') diff --git a/dottydoc/jvm/src/dotty/tools/dottydoc/core/DocPhase.scala b/dottydoc/jvm/src/dotty/tools/dottydoc/core/DocPhase.scala index 71d33950e..522b224d1 100644 --- a/dottydoc/jvm/src/dotty/tools/dottydoc/core/DocPhase.scala +++ b/dottydoc/jvm/src/dotty/tools/dottydoc/core/DocPhase.scala @@ -11,7 +11,7 @@ import dotc.core.Phases.Phase import dotc.core.Symbols.Symbol class DocPhase extends Phase { - import model.parsers.{ WikiParser, ReturnTypeParser } + import model.parsers.{ WikiParser, ReturnTypeLinker, ParamListLinker } import model._ import model.factories._ import model.internal._ @@ -24,7 +24,10 @@ class DocPhase extends Phase { def phaseName = "docphase" private[this] val commentParser = new WikiParser - private[this] val returnLinker = new ReturnTypeParser + private[this] val linkers = + new ReturnTypeLinker :: + new ParamListLinker :: + Nil /** Saves the commentParser function for later evaluation, for when the AST has been filled */ def track(symbol: Symbol, ctx: Context)(op: => Entity) = { @@ -59,11 +62,11 @@ class DocPhase extends Phase { def addedFromSymbol(sym: Symbol): List[Entity] = { val defs = sym.info.membersBasedOnFlags(Flags.Method, Flags.Synthetic | Flags.Private).map { meth => track(meth.symbol, ctx) { - DefImpl(meth.symbol.name.decode.toString, Nil, path(meth.symbol), returnType(meth.info), typeParams(meth.symbol), Nil/*paramLists(???)*/) + DefImpl(meth.symbol.name.decode.toString, Nil, path(meth.symbol), returnType(meth.info), typeParams(meth.symbol), paramLists(meth.info)) } }.toList - val vals = sym.info.fields.map { value => + val vals = sym.info.fields.filterNot(_.symbol.is(Flags.Private | Flags.Synthetic)).map { value => track(value.symbol, ctx) { ValImpl(value.symbol.name.decode.toString, Nil, path(value.symbol), returnType(value.info)) } @@ -104,7 +107,7 @@ class DocPhase extends Phase { /** def */ case d: DefDef => - DefImpl(d.name.decode.toString, flags(d), path(d.symbol), returnType(d.tpt.tpe), typeParams(d.symbol), paramLists(d)) + DefImpl(d.name.decode.toString, flags(d), path(d.symbol), returnType(d.tpt.tpe), typeParams(d.symbol), paramLists(d.symbol.info)) /** val */ case v: ValDef if !v.symbol.is(Flags.ModuleVal) => @@ -164,7 +167,7 @@ class DocPhase extends Phase { // (3) Set returntypes to correct entities - returnLinker.link(packages) + for (linker <- linkers) linker.link(packages) // (3) Create documentation template from docstrings, with internal links println("Generating documentation, this might take a while...") diff --git a/dottydoc/jvm/src/dotty/tools/dottydoc/model/parsers.scala b/dottydoc/jvm/src/dotty/tools/dottydoc/model/parsers.scala index d73f8fe9f..2c163b1e6 100644 --- a/dottydoc/jvm/src/dotty/tools/dottydoc/model/parsers.scala +++ b/dottydoc/jvm/src/dotty/tools/dottydoc/model/parsers.scala @@ -84,16 +84,9 @@ object parsers { def clear(): Unit = commentCache = Map.empty } - class ReturnTypeParser extends MemberLookup { - def link(packs: Map[String, Package]): Unit = - for (pack <- packs.values) mutateEntities(pack) { - case ent: ReturnValue => - setReturnValue(ent, returnValue(ent, ent, packs)) - case _ => () - } - - private def returnValue(ent: Entity, rv: ReturnValue, packs: Map[String, Package]): Reference = - rv.returnValue match { + sealed trait TypeLinker extends MemberLookup { + protected def linkReference(ent: Entity, rv: Reference, packs: Map[String, Package]): Reference = + rv match { case rv @ TypeReference(_, UnsetLink(t, query), tps) => val inlineToHtml = InlineToHtml(ent) val title = inlineToHtml(t) @@ -113,7 +106,37 @@ object parsers { } rv.copy(tpeLink = target, paramLinks = tpTargets) - case _ => rv.returnValue + case rv @ OrTypeReference(left, right) => + rv.copy(left = linkReference(ent, left, packs), right = linkReference(ent, right, packs)) + case rv @ AndTypeReference(left, right) => + rv.copy(left = linkReference(ent, left, packs), right = linkReference(ent, right, packs)) + case rv @ NamedReference(_, ref) => rv.copy(ref = linkReference(ent, ref, packs)) + case _ => rv + } + + def link(packs: Map[String, Package]): Unit + } + + class ReturnTypeLinker extends TypeLinker { + def link(packs: Map[String, Package]): Unit = + for (pack <- packs.values) mutateEntities(pack) { + case ent: ReturnValue => + setReturnValue(ent, linkReference(ent, ent.returnValue, packs)) + case _ => () + } + } + + class ParamListLinker extends TypeLinker { + def link(packs: Map[String, Package]): Unit = + for (pack <- packs.values) mutateEntities(pack) { + case ent: Def => + val newParamLists = for { + list <- ent.paramLists + newList = list.map(linkReference(ent, _, packs)) + } yield newList.asInstanceOf[List[NamedReference]] + + setParamLists(ent, newParamLists) + case _ => () } } } diff --git a/dottydoc/jvm/src/dotty/tools/dottydoc/util/mutate.scala b/dottydoc/jvm/src/dotty/tools/dottydoc/util/mutate.scala index e6f1e6245..4ce94459d 100644 --- a/dottydoc/jvm/src/dotty/tools/dottydoc/util/mutate.scala +++ b/dottydoc/jvm/src/dotty/tools/dottydoc/util/mutate.scala @@ -4,7 +4,7 @@ package internal object setters { import model._ - import comment.{ Comment, MaterializableLink, Reference } + import comment.{ Comment, MaterializableLink, Reference, NamedReference } import internal._ def setComment(ent: Entity, to: Option[Comment]) = ent match { @@ -24,6 +24,11 @@ object setters { case _ => () } + def setParamLists(ent: Entity, refs: List[List[NamedReference]]) = ent match { + case x: DefImpl => x.paramLists = refs + case _ => () + } + def setParent(ent: Entity, to: Entity): Unit = ent match { case e: ClassImpl => e.parent = to diff --git a/dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/factories.scala b/dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/factories.scala index eb0f54480..c2c7f9940 100644 --- a/dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/factories.scala +++ b/dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/factories.scala @@ -107,11 +107,18 @@ object factories { case _ => Nil } - def paramLists(t: DefDef)(implicit ctx: Context): List[List[NamedReference]] = { - def getParams(xs: List[ValDef]): List[NamedReference] = - xs.map(vd => NamedReference(vd.name.decode.toString, returnType(vd.tpt.tpe))) - - t.vparamss.map(getParams) + def paramLists(tpe: Type)(implicit ctx: Context): List[List[NamedReference]] = tpe match { + case pt: PolyType => + paramLists(pt.resultType) + + case mt: MethodType => + mt.paramNames.zip(mt.paramTypes).map { case (name, tpe) => + NamedReference(name.decode.toString, returnType(tpe)) + } :: paramLists(mt.resultType) + + case annot: AnnotatedType => paramLists(annot.tpe) + case (_: PolyParam | _: RefinedType | _: TypeRef | _: ThisType | + _: ExprType | _: OrType | _: AndType) => Nil // return types should not be in the paramlist } def superTypes(t: Tree)(implicit ctx: Context): List[MaterializableLink] = t.symbol.denot match { -- cgit v1.2.3