diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2010-09-30 10:08:20 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2010-09-30 10:08:20 +0000 |
commit | 0f2808227be055fb4603e7a0600a063a5756dfa0 (patch) | |
tree | 4e7aa164f4025a50c7f828ff7154759753adab5c /src/compiler | |
parent | 4697416af31b66997e8f837fc325cfd76096cbd6 (diff) | |
download | scala-0f2808227be055fb4603e7a0600a063a5756dfa0.tar.gz scala-0f2808227be055fb4603e7a0600a063a5756dfa0.tar.bz2 scala-0f2808227be055fb4603e7a0600a063a5756dfa0.zip |
[scaladoc] JavaScript template search uses a st...
[scaladoc] JavaScript template search uses a statically built index to
considerably increase performance. Contributed by Kato Kazuyoshi. Review
by dubochet.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/doc/html/page/Index.scala | 76 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js | 68 |
2 files changed, 103 insertions, 41 deletions
diff --git a/src/compiler/scala/tools/nsc/doc/html/page/Index.scala b/src/compiler/scala/tools/nsc/doc/html/page/Index.scala index 09da71fc1f..9a964ee30f 100644 --- a/src/compiler/scala/tools/nsc/doc/html/page/Index.scala +++ b/src/compiler/scala/tools/nsc/doc/html/page/Index.scala @@ -47,6 +47,21 @@ class Index(universe: Universe, indexModel: IndexModelFactory#IndexModel) extend </div> </body> + + def isExcluded(dtpl: DocTemplateEntity) = { + val qname = dtpl.qualifiedName + ( ( qname.startsWith("scala.Tuple") || qname.startsWith("scala.Product") || + qname.startsWith("scala.Function") || qname.startsWith("scala.runtime.AbstractFunction") + ) && !( + qname == "scala.Tuple1" || qname == "scala.Tuple2" || + qname == "scala.Product" || qname == "scala.Product1" || qname == "scala.Product2" || + qname == "scala.Function" || qname == "scala.Function1" || qname == "scala.Function2" || + qname == "scala.runtime.AbstractFunction0" || qname == "scala.runtime.AbstractFunction1" || + qname == "scala.runtime.AbstractFunction2" + ) + ) + } + def browser = <xml:group> <div id="browser" class="ui-layout-west"> @@ -61,19 +76,6 @@ class Index(universe: Universe, indexModel: IndexModelFactory#IndexModel) extend <div class="ui-west-center"> <div id="filter"></div> <div class="pack" id="tpl">{ - def isExcluded(dtpl: DocTemplateEntity) = { - val qname = dtpl.qualifiedName - ( ( qname.startsWith("scala.Tuple") || qname.startsWith("scala.Product") || - qname.startsWith("scala.Function") || qname.startsWith("scala.runtime.AbstractFunction") - ) && !( - qname == "scala.Tuple1" || qname == "scala.Tuple2" || - qname == "scala.Product" || qname == "scala.Product1" || qname == "scala.Product2" || - qname == "scala.Function" || qname == "scala.Function1" || qname == "scala.Function2" || - qname == "scala.runtime.AbstractFunction0" || qname == "scala.runtime.AbstractFunction1" || - qname == "scala.runtime.AbstractFunction2" - ) - ) - } def packageElem(pack: model.Package): NodeSeq = { <xml:group> { if (!pack.isRootPackage) @@ -116,22 +118,64 @@ class Index(universe: Universe, indexModel: IndexModelFactory#IndexModel) extend placeholderSeq ++ createLink(entry, includePlaceholder = false, includeText = true) } - <li title={ entities.head.qualifiedName }>{ + <li id={ + "template-" + toId(entities.head.qualifiedName) + } title={ entities.head.qualifiedName }>{ itemContents }</li> } }</ol> <ol class="packages"> { for (sp <- pack.packages sortBy (_.name.toLowerCase)) yield - <li class="pack" title={ sp.qualifiedName }>{ packageElem(sp) }</li> + <li id={ + "package-" + toId(sp.qualifiedName) + } class="pack" title={ sp.qualifiedName }>{ packageElem(sp) }</li> }</ol> </xml:group> } packageElem(universe.rootPackage) - }</div></div> + }</div></div>{ scriptElement } </div> </xml:group> + def toId(str: String) = { + val pattern = "[^A-Za-z0-9-]".r + pattern.replaceSomeIn(str, (m: scala.util.matching.Regex.Match) => { + Some("-" + m.group(0).charAt(0).toInt) + }) + } + + def scriptElement = { + val templatesOf = allPackagesWithTemplates + + val elements = templatesOf.keys.map(pack => { + List( + "{ name: '", pack, "', children: ", + templatesOf(pack).map(t => "'" + t + "'").mkString("[", ",", "]"), + "}" + ).mkString("") + }) + + <script type="text/javascript">{ + elements.mkString("Index.PACKAGES = [", ",", "]") + }</script> + } + + def allPackagesWithTemplates: Map[Package, List[DocTemplateEntity]] = { + Map(allPackages.map((key) => { + key -> key.templates.filter(t => !t.isPackage && !isExcluded(t)) + }) : _*) + } + + def allPackages: List[Package] = { + def f(parent: Package): List[Package] = { + parent.packages.flatMap( + p => f(p) :+ p + ) + } + f(universe.rootPackage).sortBy(_.toString) + } + def packageQualifiedName(ety: DocTemplateEntity): String = if (ety.inTemplate.isPackage) ety.name else (packageQualifiedName(ety.inTemplate) + "." + ety.name) diff --git a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js index e598dca727..5c1d29fca6 100644 --- a/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js +++ b/src/compiler/scala/tools/nsc/doc/html/resource/lib/index.js @@ -60,6 +60,24 @@ $(document).ready(function() { }); +var Index = {}; + +(function (ns) { + function toId(name) { + return name.replace(/[^A-Za-z0-9-]/g, function (str) { + return '-' + str.charCodeAt(0); + }); + } + + ns.idOfTemplate = function (name) { + return 'template-' + toId(name); + } + + ns.idOfPackage = function (name) { + return 'package-' + toId(name); + } +})(Index); + function configureEntityList() { kindFilterSync(); configureHideFilter(); @@ -141,37 +159,37 @@ function textFilter() { else { // if query is all lower case make a normal case insensitive search queryRegExp = new RegExp(query, "i"); } - scheduler.addForAll("filter", domCache.packs, function(pack0) { - var pack = $(pack0); - $("> ol.templates > li", pack).each(function(){ - var item = $(this).attr("title"); - if (item == "" || queryRegExp.test(item)) { - $(this).show(); - $(this).removeClass("hide"); - } - else { - $(this).addClass("hide"); - $(this).hide(); + + $.each(Index.PACKAGES, function (i, package) { + var empty = true; + var matchedSet = {}; + + $.each(package.children, function (j, child) { + if (queryRegExp.test(child)) { + matchedSet[child] = 1; + empty = false; } }); - if ($("> ol > li:not(.hide)", pack).length > 0) { - pack.show(); - pack.removeClass("hide"); - } - else { - pack.addClass("hide"); - pack.hide(); - } - if ($("> ol.templates > li:not(.hide)", pack).length > 0) { - $("> h3", pack).show(); - $("> .packhide", pack).show(); - $("> .packfocus", pack).show(); - } - else { + + var pack = $('#' + Index.idOfPackage(package.name)); + if (empty) { $("> h3", pack).hide(); + $("> .templates", pack).hide(); $("> .packhide", pack).hide(); $("> .packfocus", pack).hide(); + + return; } + + $("> h3", pack).show(); + $.each(package.children, function (j, child) { + $('#' + Index.idOfTemplate(child))[ + matchedSet[child] ? 'show' : 'hide' + ](); + }); + $("> .templates", pack).show(); + $("> .packhide", pack).show(); + $("> .packfocus", pack).show(); }); }); } |