Skip to content

Commit 91f184c

Browse files
committed
rustdoc search: add new crate: syntax to search a single
crate alternative to rust-lang#129769
1 parent d4822c2 commit 91f184c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

+35
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ const itemTypes = [
5656
"derive",
5757
"traitalias", // 25
5858
"generic",
59+
"crate",
5960
];
6061

6162
// used for special search precedence
6263
const TY_PRIMITIVE = itemTypes.indexOf("primitive");
6364
const TY_GENERIC = itemTypes.indexOf("generic");
6465
const TY_IMPORT = itemTypes.indexOf("import");
6566
const TY_TRAIT = itemTypes.indexOf("trait");
67+
// minor hack to implement the `crate:` syntax
68+
const TY_CRATE = itemTypes.indexOf("crate");
6669
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";
6770

6871
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -291,6 +294,20 @@ function getFilteredNextElem(query, parserState, elems, isInGenerics) {
291294
parserState.pos += 1;
292295
parserState.totalElems -= 1;
293296
query.literalSearch = false;
297+
if (parserState.typeFilter === "crate") {
298+
while (parserState.userQuery[parserState.pos] === " ") {
299+
parserState.pos += 1;
300+
}
301+
const start = parserState.pos;
302+
const foundCrate = consumeIdent(parserState);
303+
if (!foundCrate) {
304+
throw ["Expected ident after ", "crate:", ", found ", parserState.userQuery[start]];
305+
}
306+
const name = parserState.userQuery.substring(start, parserState.pos);
307+
elems.push(makePrimitiveElement(name, { typeFilter: "crate" }));
308+
parserState.typeFilter = null;
309+
return getFilteredNextElem(query, parserState, elems, isInGenerics);
310+
}
294311
getNextElem(query, parserState, elems, isInGenerics);
295312
}
296313
}
@@ -1870,6 +1887,7 @@ class DocSearch {
18701887
correction: null,
18711888
proposeCorrectionFrom: null,
18721889
proposeCorrectionTo: null,
1890+
filterCrates: null,
18731891
// bloom filter build from type ids
18741892
typeFingerprint: new Uint32Array(4),
18751893
};
@@ -1996,6 +2014,20 @@ class DocSearch {
19962014
query.error = err;
19972015
return query;
19982016
}
2017+
2018+
function handleCrateFilters(elem) {
2019+
if (elem.typeFilter === TY_CRATE) {
2020+
query.filterCrates = elem.name;
2021+
return false;
2022+
}
2023+
return true;
2024+
2025+
}
2026+
const nonCrateElems = query.elems.filter(handleCrateFilters);
2027+
if (nonCrateElems.length !== query.elems.length) {
2028+
query.elems = nonCrateElems;
2029+
}
2030+
19992031
if (!query.literalSearch) {
20002032
// If there is more than one element in the query, we switch to literalSearch in any
20012033
// case.
@@ -4428,6 +4460,9 @@ async function search(forced) {
44284460

44294461
const params = searchState.getQueryStringParams();
44304462

4463+
if (query.filterCrates !== null) {
4464+
filterCrates = query.filterCrates;
4465+
}
44314466
// In case we have no information about the saved crate and there is a URL query parameter,
44324467
// we override it with the URL query parameter.
44334468
if (filterCrates === null && params["filter-crate"] !== undefined) {

0 commit comments

Comments
 (0)