Skip to content

Commit 8fe7dd4

Browse files
committed
rustdoc-search: clean up formatting, add comments
1 parent 5afdfb1 commit 8fe7dd4

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/librustdoc/html/static/js/main.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,17 @@ function preLoadCss(cssUrl) {
358358
loadParamNames: async function(crate) {
359359
if (this.paramNameShards.has(crate)) {
360360
return this.paramNameShards.get(crate);
361-
} else {
362-
const promise = new Promise((resolve, reject) => {
363-
this.paramNameResolvers.set(crate, resolve);
364-
const url = resourcePath(
365-
`search.desc/${crate}/${crate}-param-names`,
366-
".js",
367-
);
368-
loadScript(url, reject);
369-
});
370-
this.paramNameShards.set(crate, promise);
371-
return promise;
372361
}
362+
const promise = new Promise((resolve, reject) => {
363+
this.paramNameResolvers.set(crate, resolve);
364+
const url = resourcePath(
365+
`search.desc/${crate}/${crate}-param-names`,
366+
".js",
367+
);
368+
loadScript(url, reject);
369+
});
370+
this.paramNameShards.set(crate, promise);
371+
return promise;
373372
},
374373
loadedParamNames: function(crate, data) {
375374
this.paramNameResolvers.get(crate)(JSON.parse(data));

src/librustdoc/html/static/js/search.js

+37-4
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,16 @@ function initSearch(rawSearchIndex) {
14891489

14901490
const fnParamNames = (await searchState.loadParamNames(obj.crate))[obj.bitIndex - 1];
14911491
const queryParamNames = [];
1492-
/** @param {QueryElement} queryElem */
1492+
/**
1493+
* Recursively writes a map of IDs to query generic names,
1494+
* which are later used to map query generic names to function generic names.
1495+
* For example, when the user writes `X -> Option<X>` and the function
1496+
* is actually written as `T -> Option<T>`, this function stores the
1497+
* mapping `(-1, "X")`, and the writeFn function looks up the entry
1498+
* for -1 to form the final, user-visible mapping of "X is T".
1499+
*
1500+
* @param {QueryElement} queryElem
1501+
*/
14931502
function remapQuery(queryElem) {
14941503
if (queryElem.id < 0) {
14951504
queryParamNames[-1 - queryElem.id] = queryElem.name;
@@ -1503,9 +1512,22 @@ function initSearch(rawSearchIndex) {
15031512
}
15041513
parsedQuery.elems.forEach(remapQuery);
15051514
parsedQuery.returned.forEach(remapQuery);
1515+
/**
1516+
* Write text to a highlighting array.
1517+
* Index 0 is not highlighted, index 1 is highlighted,
1518+
* index 2 is not highlighted, etc.
1519+
*
1520+
* @param {{name: string, highlighted: bool|undefined}} fnType - input
1521+
* @param {[string]} result
1522+
*/
15061523
function pushText(fnType, result) {
15071524
// If !!(result.length % 2) == false, then pushing a new slot starts an even
15081525
// numbered slot. Even numbered slots are not highlighted.
1526+
//
1527+
// `highlighted` will not be defined if an entire subtree is not highlighted,
1528+
// so `!!` is used to coerce it to boolean. `result.length % 2` is used to
1529+
// check if the number is even, but it evaluates to a number, so it also
1530+
// needs coerced to a boolean.
15091531
if (!!(result.length % 2) === !!fnType.highlighted) {
15101532
result.push("");
15111533
} else if (result.length === 0 && !!fnType.highlighted) {
@@ -1514,6 +1536,13 @@ function initSearch(rawSearchIndex) {
15141536
}
15151537
result[result.length - 1] += fnType.name;
15161538
}
1539+
/**
1540+
* Write a higher order function type: either a function pointer
1541+
* or a trait bound on Fn, FnMut, or FnOnce.
1542+
*
1543+
* @param {FunctionType} fnType - input
1544+
* @param {[string]} result
1545+
*/
15171546
function writeHof(fnType, result) {
15181547
const hofOutput = fnType.bindings.get(typeNameIdOfOutput) || [];
15191548
const hofInputs = fnType.generics;
@@ -1547,6 +1576,10 @@ function initSearch(rawSearchIndex) {
15471576
}
15481577
}
15491578
/**
1579+
* Write a type. This function checks for special types,
1580+
* like slices, with their own formatting. It also handles
1581+
* updating the where clause and generic type param map.
1582+
*
15501583
* @param {FunctionType} fnType
15511584
* @param {[string]} result
15521585
*/
@@ -2397,15 +2430,15 @@ function initSearch(rawSearchIndex) {
23972430
if (row.id > 0 && elem.id > 0 && elem.pathWithoutLast.length === 0 &&
23982431
row.generics.length === 0 && elem.generics.length === 0 &&
23992432
row.bindings.size === 0 && elem.bindings.size === 0 &&
2400-
// special case
2433+
// special case for types that can be matched without actually
2434+
// using the heavyweight unification machinery
24012435
elem.id !== typeNameIdOfArrayOrSlice &&
24022436
elem.id !== typeNameIdOfHof &&
24032437
elem.id !== typeNameIdOfTupleOrUnit
24042438
) {
24052439
return row.id === elem.id && typePassesFilter(elem.typeFilter, row.ty);
2406-
} else {
2407-
return unifyFunctionTypes([row], [elem], whereClause, mgens, null, unboxingDepth);
24082440
}
2441+
return unifyFunctionTypes([row], [elem], whereClause, mgens, null, unboxingDepth);
24092442
}
24102443

24112444
/**

0 commit comments

Comments
 (0)