Skip to content

Commit 3259007

Browse files
committed
rustdoc search: add new crate: syntax to search a single crate
alternative to rust-lang#129769 limitations: currently, the crate filter has to be followed by a comma, so it's `crate:std, vec`.
1 parent a48861a commit 3259007

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ const itemTypes = [
4747
"derive",
4848
"traitalias", // 25
4949
"generic",
50+
"crate",
5051
];
5152

5253
// used for special search precedence
5354
const TY_GENERIC = itemTypes.indexOf("generic");
5455
const TY_IMPORT = itemTypes.indexOf("import");
56+
// minor hack to implement the `crate:` syntax
57+
const TY_CRATE = itemTypes.indexOf("crate");
5558
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";
5659

5760
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -1799,6 +1802,7 @@ class DocSearch {
17991802
correction: null,
18001803
proposeCorrectionFrom: null,
18011804
proposeCorrectionTo: null,
1805+
filterCrates: null,
18021806
// bloom filter build from type ids
18031807
typeFingerprint: new Uint32Array(4),
18041808
};
@@ -1926,6 +1930,19 @@ class DocSearch {
19261930
query.error = err;
19271931
return query;
19281932
}
1933+
1934+
const nonCrateElems = query.elems.filter(function (elem) {
1935+
if (elem.typeFilter === TY_CRATE) {
1936+
query.filterCrates = elem.name;
1937+
return false;
1938+
}
1939+
return true;
1940+
});
1941+
if (nonCrateElems.length !== query.elems.length) {
1942+
query.elems = nonCrateElems;
1943+
query.userQuery = query.elems.join(", ");
1944+
}
1945+
19291946
if (!query.literalSearch) {
19301947
// If there is more than one element in the query, we switch to literalSearch in any
19311948
// case.
@@ -3713,6 +3730,9 @@ async function search(forced) {
37133730

37143731
const params = searchState.getQueryStringParams();
37153732

3733+
if (query.filterCrates !== null) {
3734+
filterCrates = query.filterCrates;
3735+
}
37163736
// In case we have no information about the saved crate and there is a URL query parameter,
37173737
// we override it with the URL query parameter.
37183738
if (filterCrates === null && params["filter-crate"] !== undefined) {

0 commit comments

Comments
 (0)