aboutsummaryrefslogtreecommitdiff
path: root/dottydoc/js/src/html/Member.scala
blob: 487b7d0c9979d2b4cdf233760c187648c6f3da47 (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
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}

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 {
          case (x, y: UnsetLink) => s"$x: ${y.query}"
        }.mkString ("(", ", ", ")")
      }.mkString("")
    case _ => ""
  }

  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: MaterializableLink) = rv match {
      case ml: MaterializedLink =>
        span(cls := "member-return-value no-left-margin", ": ", raw(ml.target))
      case un: UnsetLink =>
        span(cls := "member-return-value no-left-margin", ": " + shorten(un.query))
    }

    m match {
      case rv: ReturnValue => Some(link(rv.returnValue))
      case _ => None
    }
  }
}