Skip to content

Commit 415a168

Browse files
committed
Introduce searchable docs, decreasing index by 40x
1 parent 6443005 commit 415a168

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

doc-tool/resources/_layouts/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h1>Member Results</h1>
3333
// Parse parameters from URL:
3434
var parameters = [];
3535
var apiSearch = undefined;
36-
var docs = {{ docs | json }};
36+
var docs = {{ searchableDocs | json }};
3737
(function() {
3838
var pairs = location.search.substring(1).split('&');
3939
for (var i = 0; i < pairs.length; i++) {

doc-tool/resources/js/api-search.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ onmessage = function(e) {
4747
var searchRegex = regexForTerm(searchTerm);
4848

4949
var filterPackages = function(entity) {
50-
return entity.kind != "package";
50+
switch(entity.kind) {
51+
case "val":
52+
case "def":
53+
case "type":
54+
case "package":
55+
return false;
56+
default:
57+
return true;
58+
}
5159
};
5260

5361
// look at this higher order function, such syntax:
@@ -78,7 +86,7 @@ onmessage = function(e) {
7886
};
7987

8088
docs.forEach(function(pack) {
81-
pack.children
89+
pack.members
8290
.filter(filterPackages)
8391
.forEach(messageParentIfMatches(pack));
8492
});

doc-tool/src/dotty/tools/dottydoc/model/JavaConverters.scala

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,41 @@ object JavaConverters {
241241
}
242242

243243
implicit class JavaMap(val map: collection.Map[String, Package]) extends AnyVal {
244-
def toJavaList: LinkedList[AnyRef] = {
245-
map.toList
246-
.sortBy(_._1)
247-
.foldLeft(new LinkedList[AnyRef]()) { case (list, (_, pkg)) =>
248-
list.add(pkg.asJava())
249-
list
250-
}
244+
def toJavaList: LinkedList[AnyRef] =
245+
convertToList(map.mapValues(_.asJava))
246+
247+
def flattened: LinkedList[AnyRef] =
248+
convertToList(map.mapValues(flattenEntity))
249+
250+
private[this] def convertToList(ms: collection.Map[String, AnyRef]): LinkedList[AnyRef] =
251+
ms.toList.sortBy(_._1)
252+
.foldLeft(new LinkedList[AnyRef]()) { case (list, (_, value)) =>
253+
list.add(value); list
254+
}
255+
256+
private[this] def flattenEntity(e: Entity): JMap[String, _] = {
257+
def entity(e: Entity) =
258+
Map("name" -> e.name, "path" -> e.path.asJava, "kind" -> e.kind)
259+
260+
def members(e: Entity with Members) =
261+
Map("members" -> e.members.map(flattenEntity).asJava)
262+
263+
def companion(e: Companion) = Map(
264+
"hasCompanion" -> e.hasCompanion,
265+
"companionPath" -> e.companionPath.asJava
266+
)
267+
268+
(e match {
269+
case e: Package => entity(e) ++ members(e)
270+
case e: Class => entity(e) ++ members(e) ++ companion(e)
271+
case e: CaseClass => entity(e) ++ members(e) ++ companion(e)
272+
case e: Trait => entity(e) ++ members(e) ++ companion(e)
273+
case e: Object => entity(e) ++ members(e) ++ companion(e)
274+
case e: TypeAlias => entity(e)
275+
case e: Def => entity(e)
276+
case e: Val => entity(e)
277+
})
278+
.asJava
251279
}
252280
}
253281
}

doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.collection.JavaConverters._
1111

1212
case class DefaultParams(
1313
docs: JList[_],
14+
docsFlattened: JList[_],
1415
originalDocs: Map[String, Package],
1516
page: PageInfo,
1617
site: SiteInfo,
@@ -23,6 +24,8 @@ case class DefaultParams(
2324
val base = Map(
2425
"docs" -> docs,
2526

27+
"searchableDocs" -> docsFlattened,
28+
2629
"originalDocs" -> originalDocs,
2730

2831
"page" -> Map(

doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ case class Site(
4141
documentation.toJavaList
4242
}
4343

44+
private val docsFlattened: JList[_] = {
45+
import model.JavaConverters._
46+
documentation.flattened
47+
}
48+
4449
/** All files that are considered static in this context, this can be
4550
* anything from CSS, JS to images and other files.
4651
*
@@ -166,7 +171,7 @@ case class Site(
166171
}
167172

168173
DefaultParams(
169-
docs, documentation, PageInfo(pathFromRoot),
174+
docs, docsFlattened, documentation, PageInfo(pathFromRoot),
170175
SiteInfo(baseUrl, projectTitle, projectVersion, projectUrl, Array()),
171176
sidebar
172177
)

0 commit comments

Comments
 (0)