aboutsummaryrefslogtreecommitdiff
path: root/dottydoc/js/src/html/Member.scala
blob: 1786f745a5a1101882b7764d41f96fceec866076 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package dotty.tools.dottydoc
package js
package html

import scalatags.JsDom.all._
import scalatags.JsDom.TypedTag
import org.scalajs.dom
import org.scalajs.dom.html.{Anchor, Div, Span}

trait MemberLayout {
  import js.model._

  def member(m: Entity, parent: Entity) = {
    def toggleBetween(short: Div, and: Div): Unit =
      if (and.style.display == "none") {
        and.style.display = "block"
        short.style.display = "none"
      } else {
        and.style.display = "none"
        short.style.display = "block"
      }

    m.kind match {
      case "class" | "case class" | "object" | "trait" | "def" | "val" =>
        val entity = m.asInstanceOf[Entity with Modifiers]
        val shortComment = div(
          cls := "mdl-cell mdl-cell--12-col summary-comment",
          raw(m.comment.fold("")(_.short))
        ).render

        val fullComment = div(
          cls := "mdl-cell mdl-cell--12-col full-comment",
          style := "display: none;",
          raw(m.comment.fold("")(_.body))
        ).render


        val hasLongerFullComment = m.comment.fold(false) { c =>
          c.short.length + 5 < c.body.length
        }

        val divs = div(
          cls :=
            s"""
            mdl-cell mdl-cell--12-col member
            ${if (hasLongerFullComment) "member-fullcomment" else ""}
            """,
          onclick := { () => toggleBetween(shortComment, and = fullComment) },
          div(
            cls := "mdl-cell mdl-cell--12-col member-definition",
            span(
              cls := "member-modifiers-kind",
              entity.modifiers.mkString(" ") + " " + m.kind
            ),
            span(
              cls := "member-name",
              m.name
            ),
            spanWith("member-type-params no-left-margin", typeParams(m)),
            span(cls := "member-param-list no-left-margin", paramList(m)),
            returnValue(entity, parent)
          ),
          shortComment,
          fullComment
        )
        Seq(divs)
      case _ => Seq(h1("ERROR: " + m.name))
    }
  }

  def spanWith(clazz: String, contents: String) = contents match {
    case "" => None
    case _  => Some(span(cls := clazz, contents))
  }

  def paramList(m: Entity): Span = m.kind match {
    case "def" =>
      val d = m.asInstanceOf[Def]
      if (d.paramLists.nonEmpty)
        span(
          cls := "member-param-lists",
          d.paramLists.map { xs =>
            span(
              cls := "param-list",
              "(",
              xs.flatMap { tr =>
                Seq(
                  span(cls := "param-name", tr.title).render,
                  span(cls := "type-separator no-left-margin", ":").render,
                  span(referenceToLinks(tr.ref)).render,
                  span(cls := "type-separator no-left-margin", ",").render
                )
              }.toList.dropRight(1),
              ")"
            ).render
          }.toList
        ).render
      else span().render
    case _ => span().render
  }

  def referenceToLinks(ref: Reference): Span = {
    def linkToAnchor(link: MaterializableLink) = link.kind match {
      case "MaterializedLink" =>
        val (t, url) = (link.asInstanceOf[MaterializedLink].title, link.asInstanceOf[MaterializedLink].target)
        a(href := url, t).render
      case "NoLink" => span(link.title).render

      //FIXME: there should be no UnsetLinks - either MaterializedLink or NoLink
      case "UnsetLink" => span(link.title).render
    }

    ref.kind match {
      case "TypeReference" =>
        val tref = ref.asInstanceOf[TypeReference]
        if (tref.paramLinks.nonEmpty) span(
          linkToAnchor(tref.tpeLink),
          "[",
          tref
            .paramLinks
            .map(linkToAnchor)
            .flatMap(link => Seq(link, span(cls := "type-separator no-left-margin", ",").render))
            .toList.dropRight(1),
          "]"
        ).render
      else span(linkToAnchor(tref.tpeLink)).render

      case "OrTypeReference" =>
        val (left, right) = (ref.asInstanceOf[OrTypeReference].left, ref.asInstanceOf[OrTypeReference].right)
        span(
          referenceToLinks(left),
          span(cls := "type-separator", "|"),
          referenceToLinks(right)
        ).render

      case "AndTypeReference" =>
        val (left, right) = (ref.asInstanceOf[AndTypeReference].left, ref.asInstanceOf[AndTypeReference].right)
        span(
          referenceToLinks(left),
          span(cls := "type-separator", "&"),
          referenceToLinks(right)
        ).render
    }
  }

  def typeParams(m: Entity): String = m.kind match {
    case "def" =>
      val d = m.asInstanceOf[Def]
      if (d.typeParams.nonEmpty)
        d.typeParams.mkString("[", ", ", "]")
      else ""
    case _ => ""
  }

  def returnValue(m: Entity with Modifiers, parent: Entity) = {
    // shortens: "Option.this.A" => "A"
    def shorten(s: String): String = s.split('.').toList match {
      case x :: Nil => x
      case x :: xs if x == parent.name => xs.last
      case xs => s
    }

    def link(rv: Reference): Span = {
      def decodeTpeLink(link: MaterializableLink): Span = link.kind match {
        case "MaterializedLink" =>
          val ml = link.asInstanceOf[MaterializedLink]
          span(cls := "member-return-value", a(href := ml.target, ml.title)).render
        case "UnsetLink" =>
          val un = link.asInstanceOf[UnsetLink]
          span(cls := "member-return-value", shorten(un.query)).render
        case "NoLink" =>
          val no = link.asInstanceOf[NoLink]
          span(cls := "member-return-value", shorten(no.title)).render
      }

      rv.kind match {
        case "TypeReference" =>
          val trv = rv.asInstanceOf[TypeReference]
          val returnValue = decodeTpeLink(trv.tpeLink)

          if (trv.paramLinks.nonEmpty) span(
            returnValue,
            "[",
            trv.paramLinks
              .map(decodeTpeLink)
              .flatMap { sp =>
                Seq(sp, span(cls := "type-separator no-left-margin", ",").render)
              }
              .toList.dropRight(1),
            "]"
          ).render
          else returnValue

        case "OrTypeReference" =>
          val (left, right) = (rv.asInstanceOf[OrTypeReference].left, rv.asInstanceOf[OrTypeReference].right)
          span(
            cls := "member-return-value or-type",
            link(left),
            span(cls := "type-separator", "|"),
            link(right)
          ).render
        case "AndTypeReference" =>
          val (left, right) = (rv.asInstanceOf[AndTypeReference].left, rv.asInstanceOf[AndTypeReference].right)
          span(
            cls := "member-return-value and-type",
            link(left),
            span(cls := "type-separator", "&"),
            link(right)
          ).render
      }
    }

    m.kind match {
      case "def" =>
        val rv = m.asInstanceOf[ReturnValue]
        Some(span(cls := "no-left-margin", ": ", link(rv.returnValue)))
      case _ => None
    }
  }
}