aboutsummaryrefslogtreecommitdiff
path: root/dottydoc/js/src/model/entities.scala
blob: 4ae76c58c6152a7ea02a4907679239da8b6bf689 (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
package dotty.tools
package dottydoc
package js
package model

import scala.scalajs.{ js => sjs }
import sjs.annotation.ScalaJSDefined

/** This file defines the interface for which to interact with the searchable
 *  index. To use the normal operations available on the traits on the JVM:
 *
 *  {{{
 *  import dotty.tools.dottydoc.js.model.ops._
 *  val x: Package = ...
 *  }}}
 *
 *  Please note that some of the actual fields have not been added to this
 *  interface, this is simply due to the fact that they're not necessary for
 *  search - YET. They could be added, for instance `comment` is missing.
 */
@ScalaJSDefined
trait Entity extends sjs.Object {
  val kind: String
  val name: String
  val path: sjs.Array[String]
  val comment: sjs.UndefOr[Comment]
}

@ScalaJSDefined
trait Comment extends sjs.Object {
  val body: String
  val short: String
}

@ScalaJSDefined
trait Members extends sjs.Object {
  val members: sjs.Array[Entity]
}

@ScalaJSDefined
trait Modifiers extends sjs.Object {
  val modifiers: sjs.Array[String]
}

@ScalaJSDefined
trait ReturnValue extends sjs.Object {
  val returnValue: Reference
}

@ScalaJSDefined
trait TypeParams extends sjs.Object {
  val typeParams: sjs.Array[String]
}

@ScalaJSDefined
trait SuperTypes extends sjs.Object {
  val superTypes: sjs.Array[MaterializableLink]
}

@ScalaJSDefined
trait Package extends Entity with Members

@ScalaJSDefined
trait Class extends Entity with Members with Modifiers

@ScalaJSDefined
trait CaseClass extends Class

@ScalaJSDefined
trait Object extends Class

@ScalaJSDefined
trait Trait extends Class

@ScalaJSDefined
trait ParamList extends sjs.Object {
  val list: sjs.Array[NamedReference]
  val isImplicit: Boolean
}

@ScalaJSDefined
trait Def extends Entity with Modifiers with ReturnValue {
  val typeParams: sjs.Array[String]
  val paramLists: sjs.Array[ParamList]
}

@ScalaJSDefined
trait Val extends Entity with Modifiers

@ScalaJSDefined
trait Var extends Entity with Modifiers

object ops {
  val EntitiesWithModifiers =
    "case class" :: "class" :: "object" :: "trait" :: "def" :: "val" :: Nil

  val EntitiesWithMembers =
    "package" :: "case class" :: "class" :: "object" :: "trait" :: Nil

  val EntitiesWithTypeParams =
    "case class" :: "class" :: "trait" :: "def" :: Nil

  val EntitiesWithSuperTypes =
    "case class" :: "class" :: "trait" :: "object" :: Nil

  implicit class PackageOps(val p: Package) {
    def children: sjs.Array[Entity with Members] =
      p.members.collect {
        case x if EntitiesWithMembers contains x.kind =>
          x.asInstanceOf[Entity with Members]
      }

    def withMembers(mbrs: sjs.Array[Entity]): Package = new Package {
      val kind = p.kind
      val name = p.name
      val path = p.path
      val members = mbrs
      val comment = p.comment
    }
  }

  implicit class EntityOps(val ent: Entity) {
    def typeParams: sjs.Array[String] =
      if (ent.kind == "def")
        ent.asInstanceOf[Def].typeParams
      else sjs.Array()

    def hasMembers: Boolean =
      EntitiesWithMembers contains ent.kind

    def hasModifiers: Boolean =
      EntitiesWithModifiers contains ent.kind

    def hasTypeParams: Boolean =
      EntitiesWithTypeParams contains ent.kind

    def hasSuperTypes: Boolean =
      EntitiesWithSuperTypes contains ent.kind

    def isPrivate: Boolean =
      hasModifiers &&
      ent.asInstanceOf[Modifiers].modifiers.contains("private")
  }
}