Skip to content

Commit b50f1b6

Browse files
committed
rustdoc-search: build highlighted function signature
1 parent 1341782 commit b50f1b6

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

Diff for: src/librustdoc/html/static/js/search.js

+60-24
Original file line numberDiff line numberDiff line change
@@ -1502,10 +1502,10 @@ function initSearch(rawSearchIndex) {
15021502
* This function checks if a list of search query `queryElems` can all be found in the
15031503
* search index (`fnTypes`).
15041504
*
1505-
* This function returns `true` on a match, or `false` if none. If `solutionCb` is
1505+
* This function returns highlighted results on a match, or `null`. If `solutionCb` is
15061506
* supplied, it will call that function with mgens, and that callback can accept or
1507-
* reject the result bu returning `true` or `false`. If the callback returns false,
1508-
* then this function will try with a different solution, or bail with false if it
1507+
* reject the result by returning `true` or `false`. If the callback returns false,
1508+
* then this function will try with a different solution, or bail with null if it
15091509
* runs out of candidates.
15101510
*
15111511
* @param {Array<FunctionType>} fnTypesIn - The objects to check.
@@ -1518,7 +1518,7 @@ function initSearch(rawSearchIndex) {
15181518
* - Limit checks that Ty matches Vec<Ty>,
15191519
* but not Vec<ParamEnvAnd<WithInfcx<ConstTy<Interner<Ty=Ty>>>>>
15201520
*
1521-
* @return {boolean} - Returns true if a match, false otherwise.
1521+
* @return {[FunctionType]|null} - Returns highlighed results if a match, null otherwise.
15221522
*/
15231523
function unifyFunctionTypes(
15241524
fnTypesIn,
@@ -1529,17 +1529,17 @@ function initSearch(rawSearchIndex) {
15291529
unboxingDepth,
15301530
) {
15311531
if (unboxingDepth >= UNBOXING_LIMIT) {
1532-
return false;
1532+
return null;
15331533
}
15341534
/**
15351535
* @type Map<integer, integer>|null
15361536
*/
15371537
const mgens = mgensIn === null ? null : new Map(mgensIn);
15381538
if (queryElems.length === 0) {
1539-
return !solutionCb || solutionCb(mgens);
1539+
return (!solutionCb || solutionCb(mgens)) ? fnTypesIn : null;
15401540
}
15411541
if (!fnTypesIn || fnTypesIn.length === 0) {
1542-
return false;
1542+
return null;
15431543
}
15441544
const ql = queryElems.length;
15451545
const fl = fnTypesIn.length;
@@ -1548,7 +1548,7 @@ function initSearch(rawSearchIndex) {
15481548
if (ql === 1 && queryElems[0].generics.length === 0
15491549
&& queryElems[0].bindings.size === 0) {
15501550
const queryElem = queryElems[0];
1551-
for (const fnType of fnTypesIn) {
1551+
for (const [i, fnType] of fnTypesIn.entries()) {
15521552
if (!unifyFunctionTypeIsMatchCandidate(fnType, queryElem, mgens)) {
15531553
continue;
15541554
}
@@ -1560,14 +1560,18 @@ function initSearch(rawSearchIndex) {
15601560
const mgensScratch = new Map(mgens);
15611561
mgensScratch.set(fnType.id, queryElem.id);
15621562
if (!solutionCb || solutionCb(mgensScratch)) {
1563-
return true;
1563+
const highlighted = [...fnTypesIn];
1564+
highlighted[i] = Object.assign({ highlighted: true }, fnType);
1565+
return highlighted;
15641566
}
15651567
} else if (!solutionCb || solutionCb(mgens ? new Map(mgens) : null)) {
15661568
// unifyFunctionTypeIsMatchCandidate already checks that ids match
1567-
return true;
1569+
const highlighted = [...fnTypesIn];
1570+
highlighted[i] = Object.assign({ highlighted: true }, fnType);
1571+
return highlighted;
15681572
}
15691573
}
1570-
for (const fnType of fnTypesIn) {
1574+
for (const [i, fnType] of fnTypesIn.entries()) {
15711575
if (!unifyFunctionTypeIsUnboxCandidate(
15721576
fnType,
15731577
queryElem,
@@ -1592,17 +1596,28 @@ function initSearch(rawSearchIndex) {
15921596
solutionCb,
15931597
unboxingDepth + 1,
15941598
)) {
1595-
return true;
1599+
const highlighted = [...fnTypesIn];
1600+
highlighted[i] = Object.assign({ highlighted: true }, fnType);
1601+
return highlighted;
1602+
}
1603+
} else {
1604+
const highlightedGenerics = unifyFunctionTypes(
1605+
[...fnType.generics, ...Array.from(fnType.bindings.values()).flat() ],
1606+
queryElems,
1607+
whereClause,
1608+
mgens ? new Map(mgens) : null,
1609+
solutionCb,
1610+
unboxingDepth + 1,
1611+
);
1612+
if (highlightedGenerics) {
1613+
const highlighted = [...fnTypesIn];
1614+
highlighted[i] = Object.assign({
1615+
generics: highlightedGenerics,
1616+
bindings: new Map(),
1617+
highlighted: false,
1618+
}, fnType);
1619+
return highlighted;
15961620
}
1597-
} else if (unifyFunctionTypes(
1598-
[...fnType.generics, ...Array.from(fnType.bindings.values()).flat() ],
1599-
queryElems,
1600-
whereClause,
1601-
mgens ? new Map(mgens) : null,
1602-
solutionCb,
1603-
unboxingDepth + 1,
1604-
)) {
1605-
return true;
16061621
}
16071622
}
16081623
return false;
@@ -1698,7 +1713,11 @@ function initSearch(rawSearchIndex) {
16981713
unboxingDepth,
16991714
);
17001715
if (passesUnification) {
1701-
return true;
1716+
return fnTypesIn.map(innerFnType => {
1717+
const highlighted = fnType === innerFnType ||
1718+
passesUnification.indexOf(innerFnType) !== -1;
1719+
return Object.assign({ highlighted }, innerFnType);
1720+
});
17021721
}
17031722
// backtrack
17041723
fnTypes[flast] = fnTypes[i];
@@ -1741,10 +1760,27 @@ function initSearch(rawSearchIndex) {
17411760
unboxingDepth + 1,
17421761
);
17431762
if (passesUnification) {
1744-
return true;
1763+
return fnTypesIn.map(innerFnType => {
1764+
if (fnType === innerFnType) {
1765+
return Object.assign({
1766+
generics: unifyFunctionTypes(
1767+
[...generics, ...bindings],
1768+
[queryElem],
1769+
whereClause,
1770+
mgensScratch,
1771+
solutionCb,
1772+
unboxingDepth + 1,
1773+
) || [],
1774+
bindings: new Map(),
1775+
highlighted: false,
1776+
}, innerFnType);
1777+
}
1778+
const highlighted = passesUnification.indexOf(innerFnType) !== -1;
1779+
return Object.assign({ highlighted }, innerFnType);
1780+
});
17451781
}
17461782
}
1747-
return false;
1783+
return null;
17481784
}
17491785
/**
17501786
* Check if this function is a match candidate.

0 commit comments

Comments
 (0)