|
Scala example source code file (Template.scala)
The Scala Template.scala source code/* NSC -- new Scala compiler * Copyright 2007-2011 LAMP/EPFL * @author David Bernard, Manohar Jonnalagedda */ package scala.tools.nsc package doc package html package page import model._ import scala.xml.{ NodeSeq, Text } class Template(tpl: DocTemplateEntity) extends HtmlPage { val path = templateToPath(tpl) val title = tpl.qualifiedName val headers = <xml:group> <link href={ relativeLinkTo{List("template.css", "lib")} } media="screen" type="text/css" rel="stylesheet"/> <script type="text/javascript" src={ relativeLinkTo{List("jquery.js", "lib")} }> <script type="text/javascript" src={ relativeLinkTo{List("jquery-ui.js", "lib")} }> <script type="text/javascript" src={ relativeLinkTo{List("template.js", "lib")} }> <script type="text/javascript" src={ relativeLinkTo{List("tools.tooltip.js", "lib")} }> </xml:group> val valueMembers = tpl.methods ++ tpl.values ++ tpl.templates.filter(x => x.isObject || x.isPackage) sorted val (absValueMembers, nonAbsValueMembers) = valueMembers partition (_.isAbstract) val (deprValueMembers, concValueMembers) = nonAbsValueMembers partition (_.deprecation.isDefined) val typeMembers = tpl.abstractTypes ++ tpl.aliasTypes ++ tpl.templates.filter(x => x.isTrait || x.isClass) sorted val constructors = (tpl match { case cls: Class => (cls.constructors: List[MemberEntity]).sorted case _ => Nil }) /* for body, there is a special case for AnyRef, otherwise AnyRef appears like a package/object * this problem should be fixed, this implementation is just a patch */ val body = { val templateName = if (tpl.isRootPackage) "root package" else tpl.name val displayName = tpl.companion match { case Some(companion) if (companion.visibility.isPublic && companion.inSource != None) => <a href={relativeLinkTo(companion)} title="Go to companion">{ templateName } case _ => templateName } val owner = { if (tpl.isRootPackage || tpl.inTemplate.isRootPackage) NodeSeq.Empty else <p id="owner">{ templatesToHtml(tpl.inTemplate.toRoot.reverse.tail, xml.Text(".")) } } <body class={ if (tpl.isTrait || tpl.isClass || tpl.qualifiedName == "scala.AnyRef") "type" else "value" }> <div id="definition"> { tpl.companion match { case Some(companion) if (companion.visibility.isPublic && companion.inSource != None) => <a href={relativeLinkTo(companion)} title="Go to companion"> case _ => <img src={ relativeLinkTo(List(docEntityKindToBigImage(tpl), "lib")) }/> }} { owner } <h1>{ displayName } </div> { signature(tpl, true) } { memberToCommentHtml(tpl, true) } <div id="template"> <div id="mbrsel"> <div id='textfilter'> {mig.arguments.view.map(_.value).drop(2).mkString(" ")} } } } val mainComment: Seq[scala.xml.Node] = mbr.comment match { case Some(comment) => val example = if(!comment.example.isEmpty && !isReduced) <div class="block">Example{ if (comment.example.length > 1) "s" else ""}: <ol>{ val exampleXml: List[scala.xml.NodeSeq] = for(example <- comment.example ) yield <li class="cmt">{ bodyToHtml(example) } exampleXml.reduceLeft(_ ++ Text(", ") ++ _) }</ol> </div> else NodeSeq.Empty val version: Seq[scala.xml.Node] = if(!comment.version.isEmpty && !isReduced) { <dt>Version <dd>{ for(body <- comment.version.toList) yield {bodyToHtml(body)} } } else NodeSeq.Empty val sinceVersion: Seq[scala.xml.Node] = if(!comment.since.isEmpty && !isReduced) { <dt>Since <dd>{ for(body <- comment.since.toList) yield {bodyToHtml(body)} } } else NodeSeq.Empty val seeAlso: Seq[scala.xml.Node] = if(!comment.see.isEmpty && !isReduced) { <dt>See also <dd>{ val seeXml:List[scala.xml.NodeSeq]=(for(see <- comment.see ) yield ) seeXml.reduceLeft(_ ++ Text(", ") ++ _) }</dd> } else NodeSeq.Empty example ++ version ++ sinceVersion ++ seeAlso case None => NodeSeq.Empty } // end attributes block vals --- val attributesInfo = attributes ++ definitionClasses ++ selfType ++ annotations ++ deprecation ++ migration ++ sourceLink ++ mainComment val attributesBlock = if (attributesInfo.isEmpty) NodeSeq.Empty else <dl class="attributes block"> { attributesInfo } val linearization = mbr match { case dtpl: DocTemplateEntity if isSelf && !isReduced && dtpl.linearizationTemplates.nonEmpty => <div class="toggleContainer block"> <span class="toggle">Linear Supertypes <div class="superTypes hiddenContent">{ typesToHtml(dtpl.linearizationTypes, hasLinks = true, sep = xml.Text(", ")) }</div> </div> case _ => NodeSeq.Empty } val subclasses = mbr match { case dtpl: DocTemplateEntity if isSelf && !isReduced && dtpl.subClasses.nonEmpty => <div class="toggleContainer block"> <span class="toggle">Known Subclasses <div class="subClasses hiddenContent">{ templatesToHtml(dtpl.subClasses.sortBy(_.name), xml.Text(", ")) }</div> </div> case _ => NodeSeq.Empty } memberComment ++ paramComments ++ attributesBlock ++ linearization ++ subclasses } def kindToString(mbr: MemberEntity): String = { mbr match { case tpl: DocTemplateEntity => docEntityKindToString(tpl) case ctor: Constructor => "new" case tme: MemberEntity => ( if (tme.isImplicit) "implicit " else "" ) + ( if (tme.isDef) "def" else if (tme.isVal) "val" else if (tme.isLazyVal) "lazy val" else if (tme.isVar) "var" else "type") } } def boundsToHtml(hi: Option[TypeEntity], lo: Option[TypeEntity], hasLinks: Boolean): NodeSeq = { def bound0(bnd: Option[TypeEntity], pre: String): NodeSeq = bnd match { case None => NodeSeq.Empty case Some(tpe) => xml.Text(pre) ++ typeToHtml(tpe, hasLinks) } bound0(lo, " >: ") ++ bound0(hi, " <: ") } def visibility(mbr: MemberEntity): Option[comment.Paragraph] = { import comment._ import comment.{ Text => CText } mbr.visibility match { case PrivateInInstance() => Some(Paragraph(CText("private[this]"))) case PrivateInTemplate(owner) if (owner == mbr.inTemplate) => Some(Paragraph(CText("private"))) case PrivateInTemplate(owner) => Some(Paragraph(Chain(List(CText("private["), EntityLink(owner), CText("]"))))) case ProtectedInInstance() => Some(Paragraph(CText("protected[this]"))) case ProtectedInTemplate(owner) if (owner == mbr.inTemplate) => Some(Paragraph(CText("protected"))) case ProtectedInTemplate(owner) => Some(Paragraph(Chain(List(CText("protected["), EntityLink(owner), CText("]"))))) case Public() => None } } /** name, tparams, params, result */ def signature(mbr: MemberEntity, isSelf: Boolean, isReduced: Boolean = false): NodeSeq = { def inside(hasLinks: Boolean, nameLink: String = ""): NodeSeq = <xml:group> <span class="kind">{ kindToString(mbr) } <span class="symbol"> { val nameHtml = <span class={"name" + (if (mbr.deprecation.isDefined) " deprecated" else "") }>{ if (mbr.isConstructor) tpl.name else mbr.name } if (!nameLink.isEmpty) <a href={nameLink}>{nameHtml} else nameHtml } { def tparamsToHtml(mbr: Entity): NodeSeq = mbr match { case hk: HigherKinded => val tpss = hk.typeParams if (tpss.isEmpty) NodeSeq.Empty else { def tparam0(tp: TypeParam): NodeSeq = <span name={ tp.name }>{ tp.variance + tp.name }{ tparamsToHtml(tp) }{ boundsToHtml(tp.hi, tp.lo, hasLinks)} def tparams0(tpss: List[TypeParam]): NodeSeq = (tpss: @unchecked) match { case tp :: Nil => tparam0(tp) case tp :: tps => tparam0(tp) ++ Text(", ") ++ tparams0(tps) } <span class="tparams">[{ tparams0(tpss) }] } case _ => NodeSeq.Empty } tparamsToHtml(mbr) } { if (isReduced) NodeSeq.Empty else { def paramsToHtml(vlsss: List[List[ValueParam]]): NodeSeq = { def param0(vl: ValueParam): NodeSeq = // notice the }{ in the next lines, they are necessary to avoid a undesired withspace in output <span name={ vl.name }>{ Text(vl.name) }{ Text(": ") ++ typeToHtml(vl.resultType, hasLinks) }{ vl.defaultValue match { case Some(v) => Text(" = ") ++ treeToHtml(v) case None => NodeSeq.Empty } }</span> def params0(vlss: List[ValueParam]): NodeSeq = vlss match { case Nil => NodeSeq.Empty case vl :: Nil => param0(vl) case vl :: vls => param0(vl) ++ Text(", ") ++ params0(vls) } def implicitCheck(vlss: List[ValueParam]): NodeSeq = vlss match { case vl :: vls => if(vl.isImplicit) { <span class="implicit">implicit } else Text("") case _ => Text("") } vlsss map { vlss => <span class="params">({implicitCheck(vlss) ++ params0(vlss) }) } } mbr match { case cls: Class => paramsToHtml(cls.valueParams) case ctr: Constructor => paramsToHtml(ctr.valueParams) case dfe: Def => paramsToHtml(dfe.valueParams) case _ => NodeSeq.Empty } } }{ if (isReduced) NodeSeq.Empty else { mbr match { case tpl: DocTemplateEntity if tpl.parentType.isDefined => <span class="result"> extends { typeToHtml(tpl.parentType.get, hasLinks) } case tme: MemberEntity if (tme.isDef || tme.isVal || tme.isLazyVal || tme.isVar) => <span class="result">: { typeToHtml(tme.resultType, hasLinks) } case abt: AbstractType => val b2s = boundsToHtml(abt.hi, abt.lo, hasLinks) if (b2s != NodeSeq.Empty) <span class="result">{ b2s } else NodeSeq.Empty case alt: AliasType => <span class="result"> = { typeToHtml(alt.alias, hasLinks) } case _ => NodeSeq.Empty } }} </span> </xml:group> mbr match { case dte: DocTemplateEntity if !isSelf => <h4 class="signature">{ inside(hasLinks = false, nameLink = relativeLinkTo(dte)) } case _ if isSelf => <h4 id="signature" class="signature">{ inside(hasLinks = true) } case _ => <h4 class="signature">{ inside(hasLinks = true) } } } /** */ def treeToHtml(tree: TreeEntity): NodeSeq = { /** Makes text good looking in the html page : newlines and basic indentation, * You must change this function if you want to improve pretty printing of default Values */ def codeStringToXml(text: String): NodeSeq = { var goodLookingXml: NodeSeq = NodeSeq.Empty var indent = 0 for(c<-text) c match { case '{' => indent+=1 goodLookingXml ++= Text("{") case '}' => indent-=1 goodLookingXml ++= Text("}") case '\n' => goodLookingXml++= <br/> ++ indentation case _ => goodLookingXml ++= Text(c.toString) } def indentation:NodeSeq = { var indentXml = NodeSeq.Empty for (x <- 1 to indent) indentXml ++= Text(" ") indentXml } goodLookingXml } var index = 0 val str = tree.expression val length = str.length var myXml: NodeSeq = NodeSeq.Empty for( (from, (member, to)) <- tree.refEntity.toSeq) { if (index < from) { myXml ++= codeStringToXml(str.substring(index,from)) index = from } if (index == from) { member match { case mbr: DocTemplateEntity => val link = relativeLinkTo(mbr) myXml ++= <span class="name">{str.substring(from, to)} case mbr: MemberEntity => val anchor = "#" + mbr.name + defParamsString(mbr) + ":" + mbr.resultType.name val link = relativeLinkTo(mbr.inTemplate) myXml ++= <span class="name">{str.substring(from, to)} } index = to } } // function used in the MemberEntity case above def defParamsString(mbr: Entity):String = mbr match { case d:MemberEntity with Def => defParamsToString(d) case _ => "" } if (index <= length-1) myXml ++= codeStringToXml(str.substring(index, length )) if(length < 36) <span class="symbol">{ myXml } else <span class="defval" name={ myXml }>{ "..." } } def argumentsToHtml(argss: List[ValueArgument]): NodeSeq = { def argumentsToHtml0(argss: List[ValueArgument]): NodeSeq = argss match { case Nil => NodeSeq.Empty case arg :: Nil => argumentToHtml(arg) case arg :: args => argumentToHtml(arg) ++ xml.Text(", ") ++ argumentsToHtml0(args) } <span class="args">({ argumentsToHtml0(argss) }) } def argumentToHtml(arg: ValueArgument): NodeSeq = { <span> { arg.parameter match { case Some(param) => Text(param.name + " = ") case None => NodeSeq.Empty } } { treeToHtml(arg.value) } </span> } }Other Scala examples (source code examples)Here is a short list of links related to this Scala Template.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.