summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/doc/ModelToXML.scala
blob: 7e3e36cfa0fb9b0fd4539ac2bf47406df240358e (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* NSC -- new Scala compiler
 * Copyright 2007-2008 LAMP/EPFL
 * @author  Sean McDirmid
 */
// $Id$

package scala.tools.nsc.doc

import scala.xml._

/** This class has functionality to format source code models as XML blocks.
 *
 *  @author  Sean McDirmid, Stephane Micheloud
 */
trait ModelToXML extends ModelExtractor {
  import global._;
  import DocUtil._;
  // decode entity into XML.
  type Frame;

  protected def urlFor(sym : Symbol)(implicit frame : Frame) : String;
  protected def anchor(sym : Symbol)(implicit frame : Frame) : NodeSeq;
  def aref(href : String, label : String)(implicit frame : Frame) : NodeSeq;
  def link(entity : Symbol)(implicit frame : Frame) : NodeSeq = {
    val url = urlFor(entity);
    // nothing to do but be verbose.
    if (url == null) Text({
      entity.owner.fullNameString('.') + '.' + entity.nameString;
    }) else aref(url, entity.nameString);
  }
  def link(tpe    : Type)(implicit frame : Frame) : NodeSeq = {
    if (!tpe.typeArgs.isEmpty) {
      if (definitions.isFunctionType(tpe)) {
        val (args,r) = tpe.typeArgs.splitAt(tpe.typeArgs.length - 1);
        args.mkXML("(", ", ", ")")(link) ++ Text(" => ") ++ link(r.head);
      } else if (tpe.symbol == definitions.RepeatedParamClass) {
        assert(tpe.typeArgs.length == 1);
        link(tpe.typeArgs(0)) ++ Text("*");
      } else if (tpe.symbol == definitions.ByNameParamClass) {
        assert(tpe.typeArgs.length == 1);
        Text("=> ") ++ link(tpe.typeArgs(0));
      } else if (tpe.symbol.name.toString.startsWith("Tuple") &&
                 tpe.symbol.owner.name == nme.scala_.toTypeName) {
        tpe.typeArgs.mkXML("(", ", ", ")")(link);
      } else link(decode(tpe.symbol)) ++ tpe.typeArgs.surround("[", "]")(link);
    } else tpe match {
      case PolyType(tparams,result) =>
        link(result) ++ tparams.surround("[", "]")(link);
      case RefinedType(parents,_) =>
        val parents1 =
          if ((parents.length > 1) &&
              (parents.head.symbol eq definitions.ObjectClass)) parents.tail;
          else parents;
       parents1.mkXML(Text(""), <code> with </code>, Text(""))(link);
     case _ => {
        link(decode(tpe.symbol));
      }
    }
  }
  private def printIf[T](what : Option[T], before : String, after : String)(f : T => NodeSeq) : NodeSeq = {
    if (what.isEmpty) return Text("");
    var seq : NodeSeq = Text(before);
    seq = seq ++ f(what.get);
    seq = seq ++ Text(after);
    seq;
  }
  def bodyFor(entity : Entity)(implicit frame : Frame) : NodeSeq = {
    var seq = {entity.typeParams.surround("[", "]")(e => {
      Text(e.variance) ++ <em>{e.name}</em> ++
        {printIf(e.hi, " <: ", "")(link)} ++
        {printIf(e.lo, " >: ", "")(link)}
    })} ++ printIf(entity.hi, " <: ", "")(link) ++
           printIf(entity.lo, " >: ", "")(link);
    {entity.params.foreach(xs => {
      seq = seq ++ xs.mkXML("(", ", ", ")")(arg => {
        var seq : NodeSeq = {
          val str = arg.flagsString.trim;
          if (str.length == 0) NodeSeq.Empty;
          else <code>{Text(str)} </code>;
        }
        seq = seq ++ <em>{arg.name}</em>;
        seq = seq ++ Text(" : ") ++ link(arg.resultType.get);
        seq;
      });
      seq;
    })};
    seq ++ {printIf(entity.resultType, " : ", "")(tpe => link(tpe))}
  }
  def extendsFor(entity : Entity)(implicit frame : Frame) : NodeSeq = {
    if (entity.parents.isEmpty) NodeSeq.Empty;
    else <code> extends </code>++
      entity.parents.mkXML(Text(""), <code> with </code>, Text(""))(link);
  }
  def parse(str: String): NodeSeq = {
    new SpecialNode {
      def label = "#PCDATA"
      def toString(sb: StringBuilder): StringBuilder = {
        sb.append(str.trim)
        sb
      }
    }
  }
  def longHeader(entity : Entity)(implicit from : Frame) : NodeSeq = Group({
    anchor(entity.sym) ++ <dl>
      <dt>
        {attrsFor(entity)}
        <code>{Text(entity.flagsString)}</code>
        <code>{Text(entity.kind)}</code>
        <em>{entity.sym.nameString}</em>{bodyFor(entity)}
      </dt>
      <dd>{extendsFor(entity)}</dd>
    </dl>;
  } ++ {
    val cmnt = entity.decodeComment;
    if (cmnt.isEmpty) NodeSeq.Empty;
    else longComment(cmnt.get);
  } ++ (entity match {
      case entity : ClassOrObject => {classBody(entity)};
      case _ => NodeSeq.Empty;
  }) ++ {
    val overridden = entity.overridden;
    if (!overridden.isEmpty) {
      var seq : NodeSeq = Text("Overrides ");
      seq = seq ++ overridden.mkXML("",", ", "")(sym => link(decode(sym.owner)) ++ Text(".") ++ link(sym));
      seq;
    } else NodeSeq.Empty;
  } ++ <hr/>);

  def longComment(cmnt : Comment) : NodeSeq = {
    val attrs = <dl>{
      var seq : NodeSeq = NodeSeq.Empty;
      cmnt.decodeAttributes.foreach{
      case (tag,xs) =>
        seq = seq ++ <dt style="margin:10px 0 0 20px;">
        {decodeTag(tag)}</dt> ++ {xs.flatMap{
        case (option,body) => <dd>{
          if (option == null) NodeSeq.Empty;
          else decodeOption(tag, option);
        }{parse(body)}</dd>
        }}
      };
      seq;
    }</dl>;
    <span><dl><dd>{parse(cmnt.body)}</dd></dl>{attrs}</span>
  }

  def classBody(entity : ClassOrObject)(implicit from : Frame) : NodeSeq = <span>
  {categories.mkXML("","\n","")(c => shortList(entity, c)) : NodeSeq}
  {categories.mkXML("","\n","")(c =>  longList(entity, c)) : NodeSeq}
  </span>;

  def longList(entity : ClassOrObject,category : Category)(implicit from : Frame) : NodeSeq = {
    val xs = entity.members(category);
    if (!xs.elements.hasNext) return NodeSeq.Empty;
    Group(
        <table cellpadding="3" class="member-detail" summary="">
          <tr><td class="title">{Text(category.label)} Details</td></tr>
        </table>
        <div>{xs.mkXML("","\n","")(m => longHeader(m))}</div>);
  }

  def shortList(entity: ClassOrObject, category: Category)(implicit from: Frame) : NodeSeq = {
    val xs = entity.members(category)
    var seq : NodeSeq = NodeSeq.Empty
    if (xs.elements.hasNext) {
      // alphabetic
      val set = new scala.collection.jcl.TreeSet[entity.Member]()(mA => new Ordered[entity.Member] {
        def compare(mB : entity.Member) : Int = {
          if (mA eq mB) return 0;
          val diff = mA.name compare mB.name;
          if (diff != 0) return diff;
          val diff0 = mA.hashCode - mB.hashCode;
          assert(diff0 != 0);
          return diff0
        }
      });
      set addAll xs;
      seq = seq ++   <table cellpadding="3" class="member" summary="">
      <tr><td colspan="2" class="title">{Text(category.label + " Summary")}</td></tr>
      {set.mkXML("","\n","")(mmbr => shortHeader(mmbr))}
      </table>
    }
    // list inherited members...if any.
    for ((tpe,members) <- entity.inherited) {
      val members0 = members.filter(m => category.f(m.sym));
      if (!members0.isEmpty) seq = seq ++ <table cellpadding="3" class="inherited" summary="">
        <tr><td colspan="2" class="title">
          {Text(category.plural + " inherited from ") ++ link(tpe)}
        </td></tr>
        <tr><td colspan="2" class="signature">
          {members0.mkXML((""), (", "), (""))(m => {
            link(decode(m.sym)) ++
              (if (m.sym.hasFlag(symtab.Flags.ABSTRACT) || m.sym.hasFlag(symtab.Flags.DEFERRED)) {
                Text(" (abstract)");
              } else NodeSeq.Empty);
           })}
        </td></tr>
      </table>
    }
    seq;
  }

  protected def decodeOption(tag: String, string: String): NodeSeq = <code>{Text(string + " - ")}</code>;
  protected def decodeTag(tag: String): String =
    "" + Character.toUpperCase(tag.charAt(0)) + tag.substring(1);

  def shortHeader(entity : Entity)(implicit from : Frame) : NodeSeq = {
    <tr>
      <td valign="top" class="modifiers">
        <code>{Text(entity.flagsString)} {Text(entity.kind)}</code>
      </td>
      <td class="signature">
        <em>{link(decode(entity.sym))}</em>{bodyFor(entity) ++ extendsFor(entity)}
        {
          val cmnt = entity.decodeComment;
          if (cmnt.isEmpty) NodeSeq.Empty;
          else <br>{parse(cmnt.get.body)}</br>;
        }
        </td>
    </tr>
  }
  def attrsFor(entity : Entity)(implicit from : Frame) : NodeSeq = {
    def attrFor(attr: AnnotationInfo[Constant]): Node = {
      val buf = new StringBuilder
      val AnnotationInfo(tpe, args, nvPairs) = attr
      val name = link(decode(tpe.symbol))
      if (!args.isEmpty)
        buf.append(args.map(.escapedStringValue).mkString("(", ",", ")"))
      if (!nvPairs.isEmpty)
        for (((name, value), index) <- nvPairs.zipWithIndex) {
          if (index > 0)
            buf.append(", ")
          buf.append(name).append(" = ").append(value)
        }
      Group(name ++ Text(buf.toString))
    }
    if (entity.sym.hasFlag(symtab.Flags.CASE)) NodeSeq.Empty;
    else {
      val sep = Text("@")
      for (attr <- entity.attributes)
        yield Group({(sep ++ attrFor(attr) ++ <br/>)})
    }
  }
}