aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/src/dotty/tools/dottydoc/core/AlternateConstructorsPhase.scala
blob: 53c96fc87487ca99976d5abd5330d5e6eafc45c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dotty.tools
package dottydoc
package core

import dotc.core.Contexts.Context

import transform.DocMiniPhase
import model._
import model.internal._

/** This DocMiniPhase adds the alternate constructors, currently defined as
 *  methods with the name `<init>`, to the Entity#constructors list
 */
class AlternateConstructors extends DocMiniPhase {
  def partitionMembers(ent: Entity with Constructors with Members): (List[List[ParamList]], List[Entity]) = {
    val (constructors, members) = ent.members.partition(x => x.name == "<init>")

    val paramLists: List[List[ParamList]] = constructors.collect {
      case df: Def => df.paramLists
    }

    (ent.constructors ++ paramLists, members)
  }

  override def transformClass(implicit ctx: Context) = { case cls: ClassImpl =>
    val (constructors, members) = partitionMembers(cls)
    cls.copy(members = members, constructors = constructors)
  }

  override def transformCaseClass(implicit ctx: Context) = { case cc: CaseClassImpl =>
    val (constructors, members) = partitionMembers(cc)
    cc.copy(members = members, constructors = constructors)
  }
}