aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/src/dotty/tools/dottydoc/model/entities.scala
blob: 7027a72ebe348572495acf934be7c1de536ef3d0 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dotty.tools.dottydoc
package model

import comment._
import references._
import dotty.tools.dotc.core.Symbols.{ Symbol, NoSymbol }

trait Entity { entity =>
  def symbol: Symbol

  def name: String

  /** Path from root, i.e. `scala.Option$` */
  def path: List[String]

  def comment: Option[Comment]

  def kind: String

  def parent: Entity

  def annotations: List[String]

  def children: List[Entity with Members] = entity match {
    case e: Entity with Members =>
      e.members.collect { case e: Entity with Members if e.kind != "package" => e }
    case _ => Nil
  }

  /** All parents from package level i.e. Package to Object to Member etc */
  def parents: List[Entity] = parent match {
    case NonEntity => Nil
    case e => e :: e.parents
  }

  /** Applies `f` to entity if != `NonEntity` */
  def fold[A](nonEntity: A)(f: Entity => A) = this match {
    case NonEntity => nonEntity
    case x => f(x)
  }
}

trait SuperTypes {
  def superTypes: List[MaterializableLink]
}

trait Members {
  def members: List[Entity]

  def hasVisibleMembers: Boolean = members.exists {
    case e: Entity with Modifiers => !(e.isPrivate || e.isProtected)
    case e => true
  }
}

trait Modifiers {
  def modifiers: List[String]

  def isPrivate: Boolean =
    modifiers.contains("private")

  def isProtected: Boolean =
    modifiers.contains("protected")
}

trait TypeParams {
  def typeParams: List[String]
}

trait ReturnValue {
  def returnValue: Reference
}

trait ParamList {
  def list: List[NamedReference]
  def isImplicit: Boolean
}

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]
}

trait Package extends Entity with Members with SuperTypes {
  val kind = "package"
}

trait TypeAlias extends Entity with Modifiers {
  val kind = "type"
  def alias: Option[Reference]
  def isAbstract: Boolean = !alias.isDefined
}

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 with Companion {
  override val kind = "case class"
}

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 with Companion {
  override val kind = "object"
}

trait Def extends Entity with Modifiers with TypeParams with ReturnValue with ImplicitlyAddedEntity {
  val kind = "def"
  def paramLists: List[ParamList]
}

trait Val extends Entity with Modifiers with ReturnValue with ImplicitlyAddedEntity

sealed trait NonEntity extends Package with TypeAlias with Class with CaseClass with Trait with Object with Def with Val {
  override val kind = ""
  val annotations = Nil
  val name = ""
  val symbol = NoSymbol
  val comment = None
  val path = Nil
  val parent = NonEntity
  val constructors = Nil
  val paramLists = Nil
  val implicitlyAddedFrom = None
  val members = Nil
  val modifiers = Nil
  val reference = EmptyReference
  val returnValue = EmptyReference
  val superTypes = Nil
  val typeParams = Nil
  val traitParams = Nil
  val alias = None
  val companionPath = Nil
  def companionPath_=(xs: List[String]) = ()
}

final case object NonEntity extends NonEntity
final case object RootEntity extends NonEntity {
  override val name = "root"
}