From 384f5a0dba5c2102327015c67781462ba475e43a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Thu, 19 Jan 2017 16:38:48 +0100 Subject: Link companions in doc AST --- doc-tool/resources/_layouts/doc.html | 3 +- .../src/dotty/tools/dottydoc/DocCompiler.scala | 1 + .../dotty/tools/dottydoc/core/DocASTPhase.scala | 2 +- .../tools/dottydoc/core/LinkCompanionsPhase.scala | 46 ++++++++++++++++++++++ .../tools/dottydoc/core/MiniPhaseTransform.scala | 12 ++++-- .../src/dotty/tools/dottydoc/model/entities.scala | 18 +++++++-- .../src/dotty/tools/dottydoc/model/internal.scala | 12 ++++-- doc-tool/src/dotty/tools/dottydoc/model/java.scala | 16 ++++++-- 8 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 doc-tool/src/dotty/tools/dottydoc/core/LinkCompanionsPhase.scala (limited to 'doc-tool') diff --git a/doc-tool/resources/_layouts/doc.html b/doc-tool/resources/_layouts/doc.html index c9dc8da39..b6c9a7053 100644 --- a/doc-tool/resources/_layouts/doc.html +++ b/doc-tool/resources/_layouts/doc.html @@ -16,7 +16,8 @@ layout: main {{ pkg.name }} {% for member in pkg.children %} - {% if member.kind != "package" %} + {% if member.kind == "object" and member.hasCompanion %} + {% elsif member.kind != "package" %}
  • {{ member.kind }} {{ member.name }}
  • diff --git a/doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala b/doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala index 55b079adf..081883597 100644 --- a/doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala +++ b/doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala @@ -30,6 +30,7 @@ class DocCompiler extends Compiler { new LinkParamListTypes, new LinkImplicitlyAddedTypes, new LinkSuperTypes, + new LinkCompanions, new AlternateConstructors, new SortMembers)) ) diff --git a/doc-tool/src/dotty/tools/dottydoc/core/DocASTPhase.scala b/doc-tool/src/dotty/tools/dottydoc/core/DocASTPhase.scala index f8ffdc0aa..411818614 100644 --- a/doc-tool/src/dotty/tools/dottydoc/core/DocASTPhase.scala +++ b/doc-tool/src/dotty/tools/dottydoc/core/DocASTPhase.scala @@ -111,7 +111,7 @@ class DocASTPhase extends Phase { /** class / case class */ case c @ TypeDef(n, rhs) if c.symbol.isClass => //TODO: should not `collectMember` from `rhs` - instead: get from symbol, will get inherited members as well - (c.symbol, annotations(c.symbol), n.show, collectMembers(rhs), flags(c), path(c.symbol), typeParams(c.symbol), constructors(c.symbol), superTypes(c), None) match { + (c.symbol, annotations(c.symbol), n.show, collectMembers(rhs), flags(c), path(c.symbol), typeParams(c.symbol), constructors(c.symbol), superTypes(c), None, Nil) match { case x if c.symbol.is(Flags.CaseClass) => CaseClassImpl.tupled(x) case x => ClassImpl.tupled(x) } diff --git a/doc-tool/src/dotty/tools/dottydoc/core/LinkCompanionsPhase.scala b/doc-tool/src/dotty/tools/dottydoc/core/LinkCompanionsPhase.scala new file mode 100644 index 000000000..82bd90da0 --- /dev/null +++ b/doc-tool/src/dotty/tools/dottydoc/core/LinkCompanionsPhase.scala @@ -0,0 +1,46 @@ +package dotty.tools +package dottydoc +package core + +import dotc.core.Contexts.Context +import dotc.ast.tpd + +import transform.DocMiniPhase +import model.internal._ +import model._ +import model.factories._ +import dotty.tools.dotc.core.Symbols.Symbol +import util.syntax._ + +class LinkCompanions extends DocMiniPhase { + private def linkCompanions(ent: Entity)(implicit ctx: Context): ent.type = { + ent.children.groupBy(_.name).foreach { + case (_, List(x1: Companion, x2: Companion)) => { + x1.companionPath = x2.path + x2.companionPath = x1.path + } + case _ => () + } + ent + } + + override def transformPackage(implicit ctx: Context) = { case ent: PackageImpl => + linkCompanions(ent) + } + + override def transformClass(implicit ctx: Context) = { case ent: ClassImpl => + linkCompanions(ent) + } + + override def transformCaseClass(implicit ctx: Context) = { case ent: CaseClassImpl => + linkCompanions(ent) + } + + override def transformObject(implicit ctx: Context) = { case ent: ObjectImpl => + linkCompanions(ent) + } + + override def transformTrait(implicit ctx: Context) = { case ent: TraitImpl => + linkCompanions(ent) + } +} diff --git a/doc-tool/src/dotty/tools/dottydoc/core/MiniPhaseTransform.scala b/doc-tool/src/dotty/tools/dottydoc/core/MiniPhaseTransform.scala index a35da579e..1cfcec09a 100644 --- a/doc-tool/src/dotty/tools/dottydoc/core/MiniPhaseTransform.scala +++ b/doc-tool/src/dotty/tools/dottydoc/core/MiniPhaseTransform.scala @@ -121,7 +121,8 @@ object transform { cls.typeParams, cls.constructors, cls.superTypes, - cls.comment + cls.comment, + cls.companionPath ) } case cc: CaseClass => transformEntity(cc, _.caseClassTransformation) { cc => @@ -135,7 +136,8 @@ object transform { cc.typeParams, cc.constructors, cc.superTypes, - cc.comment + cc.comment, + cc.companionPath ) } case trt: Trait => transformEntity(trt, _.traitTransformation) { trt => @@ -149,7 +151,8 @@ object transform { trt.typeParams, trt.traitParams, trt.superTypes, - trt.comment + trt.comment, + trt.companionPath ) } case obj: Object => transformEntity(obj, _.objectTransformation) { obj => @@ -161,7 +164,8 @@ object transform { obj.modifiers, obj.path, obj.superTypes, - obj.comment + obj.comment, + obj.companionPath ) } case df: Def => transformEntity(df, _.defTransformation) { df => diff --git a/doc-tool/src/dotty/tools/dottydoc/model/entities.scala b/doc-tool/src/dotty/tools/dottydoc/model/entities.scala index 613788964..7027a72eb 100644 --- a/doc-tool/src/dotty/tools/dottydoc/model/entities.scala +++ b/doc-tool/src/dotty/tools/dottydoc/model/entities.scala @@ -80,6 +80,14 @@ trait Constructors { def constructors: List[List[ParamList]] } +trait Companion extends Entity { + def hasCompanion: Boolean = companionPath ne Nil + + def companionPath: List[String] + + def companionPath_=(xs: List[String]): Unit +} + trait ImplicitlyAddedEntity extends Entity { def implicitlyAddedFrom: Option[Reference] } @@ -94,20 +102,20 @@ trait TypeAlias extends Entity with Modifiers { def isAbstract: Boolean = !alias.isDefined } -trait Class extends Entity with Modifiers with TypeParams with Constructors with SuperTypes with Members { +trait Class extends Entity with Modifiers with TypeParams with Constructors with SuperTypes with Members with Companion { val kind = "class" } -trait CaseClass extends Entity with Modifiers with TypeParams with Constructors with SuperTypes with Members { +trait CaseClass extends Entity with Modifiers with TypeParams with Constructors with SuperTypes with Members with Companion { override val kind = "case class" } -trait Trait extends Entity with Modifiers with TypeParams with SuperTypes with Members { +trait Trait extends Entity with Modifiers with TypeParams with SuperTypes with Members with Companion { def traitParams: List[ParamList] override val kind = "trait" } -trait Object extends Entity with Modifiers with SuperTypes with Members { +trait Object extends Entity with Modifiers with SuperTypes with Members with Companion { override val kind = "object" } @@ -137,6 +145,8 @@ sealed trait NonEntity extends Package with TypeAlias with Class with CaseClass val typeParams = Nil val traitParams = Nil val alias = None + val companionPath = Nil + def companionPath_=(xs: List[String]) = () } final case object NonEntity extends NonEntity diff --git a/doc-tool/src/dotty/tools/dottydoc/model/internal.scala b/doc-tool/src/dotty/tools/dottydoc/model/internal.scala index ac789f29f..fe21a0a0a 100644 --- a/doc-tool/src/dotty/tools/dottydoc/model/internal.scala +++ b/doc-tool/src/dotty/tools/dottydoc/model/internal.scala @@ -41,7 +41,8 @@ object internal { typeParams: List[String] = Nil, constructors: List[List[ParamList]] = Nil, superTypes: List[MaterializableLink] = Nil, - var comment: Option[Comment] = None + var comment: Option[Comment] = None, + var companionPath: List[String] = Nil ) extends Class with Impl final case class CaseClassImpl( @@ -54,7 +55,8 @@ object internal { typeParams: List[String] = Nil, constructors: List[List[ParamList]] = Nil, superTypes: List[MaterializableLink] = Nil, - var comment: Option[Comment] = None + var comment: Option[Comment] = None, + var companionPath: List[String] = Nil ) extends CaseClass with Impl final case class TraitImpl( @@ -67,7 +69,8 @@ object internal { typeParams: List[String] = Nil, traitParams: List[ParamList] = Nil, superTypes: List[MaterializableLink] = Nil, - var comment: Option[Comment] = None + var comment: Option[Comment] = None, + var companionPath: List[String] = Nil ) extends Trait with Impl final case class ObjectImpl( @@ -78,7 +81,8 @@ object internal { modifiers: List[String], path: List[String], superTypes: List[MaterializableLink] = Nil, - var comment: Option[Comment] = None + var comment: Option[Comment] = None, + var companionPath: List[String] = Nil ) extends Object with Impl final case class DefImpl( diff --git a/doc-tool/src/dotty/tools/dottydoc/model/java.scala b/doc-tool/src/dotty/tools/dottydoc/model/java.scala index a9ae0dbee..6b4e8b06d 100644 --- a/doc-tool/src/dotty/tools/dottydoc/model/java.scala +++ b/doc-tool/src/dotty/tools/dottydoc/model/java.scala @@ -71,7 +71,9 @@ object java { "comment" -> ent.comment.map(_.asJava).asJava, "isPrivate" -> ent.isPrivate, "isProtected" -> ent.isProtected, - "hasVisibleMembers" -> ent.hasVisibleMembers + "hasVisibleMembers" -> ent.hasVisibleMembers, + "hasCompanion" -> ent.hasCompanion, + "companionPath" -> ent.companionPath.asJava ) ++ extras).asJava } @@ -88,7 +90,9 @@ object java { "comment" -> ent.comment.map(_.asJava).asJava, "isPrivate" -> ent.isPrivate, "isProtected" -> ent.isProtected, - "hasVisibleMembers" -> ent.hasVisibleMembers + "hasVisibleMembers" -> ent.hasVisibleMembers, + "hasCompanion" -> ent.hasCompanion, + "companionPath" -> ent.companionPath.asJava ) ++ extras).asJava } @@ -105,7 +109,9 @@ object java { "comment" -> ent.comment.map(_.asJava).asJava, "isPrivate" -> ent.isPrivate, "isProtected" -> ent.isProtected, - "hasVisibleMembers" -> ent.hasVisibleMembers + "hasVisibleMembers" -> ent.hasVisibleMembers, + "hasCompanion" -> ent.hasCompanion, + "companionPath" -> ent.companionPath.asJava ) ++ extras).asJava } @@ -121,7 +127,9 @@ object java { "comment" -> ent.comment.map(_.asJava).asJava, "isPrivate" -> ent.isPrivate, "isProtected" -> ent.isProtected, - "hasVisibleMembers" -> ent.hasVisibleMembers + "hasVisibleMembers" -> ent.hasVisibleMembers, + "hasCompanion" -> ent.hasCompanion, + "companionPath" -> ent.companionPath.asJava ) ++ extras).asJava } -- cgit v1.2.3