aboutsummaryrefslogtreecommitdiff
path: root/dottydoc/js/src/html/Member.scala
blob: 4ab214fdb7fe09c98e700c26ec94604e83df8347 (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
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 dotty.tools.dottydoc.model._
  import dotty.tools.dottydoc.model.comment._

  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 match {
      case m: 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",
              m.modifiers.mkString(" ") + " " + m.kind
            ),
            span(
              cls := "member-name",
              m.name
            ),
            spanWith("member-type-params no-left-margin", typeParams(m)),
            spanWith("member-param-list no-left-margin", paramList(m)),
            returnValue(m, parent)
          ),
          shortComment,
          fullComment
        )
        Seq(divs)
      case x => Seq(h1("ERROR: " + x.name))
    }
  }

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

  def paramList(m: Entity): String = m match {
    case d: Def if d.paramLists.nonEmpty =>
      d.paramLists.map { xs =>
        xs.map { tr =>
          // FIXME: should not cast like this - it won't be guaranteed to be a
          // TypeReference after Or/Types for paramlists have been implemented
          tr.title + ": " + decodeLink(tr.ref.asInstanceOf[TypeReference].tpeLink)
        }.mkString ("(", ", ", ")")
      }.mkString("")
    case _ => ""
  }

  def decodeLink: MaterializableLink => String = {
    case MaterializedLink(t, _) => t
    case NoLink(t, _) => t
    //FIXME: there should be no UnsetLinks - either MaterializedLink or NoLink
    case UnsetLink(_, q) => q
  }

  def typeParams(m: Entity): String = m match {
    case d: Def if d.typeParams.nonEmpty => d.typeParams.mkString("[", ", ", "]")
    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: MaterializableLink => Span  = {
        case ml: MaterializedLink =>
          println(s"received tpeLink: $ml")
          span(cls := "member-return-value", a(href := ml.target, ml.title)).render
        case un: UnsetLink =>
          span(cls := "member-return-value", shorten(un.query)).render
        case no: NoLink =>
          span(cls := "member-return-value", shorten(no.title)).render
      }

      rv match {
        case rv: TypeReference =>
          val returnValue = decodeTpeLink(rv.tpeLink)

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

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

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