Skip to content

Commit

Permalink
rustdoc search: add new crate: syntax to search a single crate
Browse files Browse the repository at this point in the history
alternative to rust-lang#129769

limitations: currently, the crate filter has to be followed by a
comma, so it's `crate:std, vec`.
  • Loading branch information
lolbinarycat committed Sep 2, 2024
1 parent a48861a commit 3259007
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ const itemTypes = [
"derive",
"traitalias", // 25
"generic",
"crate",
];

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

// Hard limit on how deep to recurse into generics when doing type-driven search.
Expand Down Expand Up @@ -1799,6 +1802,7 @@ class DocSearch {
correction: null,
proposeCorrectionFrom: null,
proposeCorrectionTo: null,
filterCrates: null,
// bloom filter build from type ids
typeFingerprint: new Uint32Array(4),
};
Expand Down Expand Up @@ -1926,6 +1930,19 @@ class DocSearch {
query.error = err;
return query;
}

const nonCrateElems = query.elems.filter(function (elem) {
if (elem.typeFilter === TY_CRATE) {
query.filterCrates = elem.name;
return false;
}
return true;
});
if (nonCrateElems.length !== query.elems.length) {
query.elems = nonCrateElems;
query.userQuery = query.elems.join(", ");
}

if (!query.literalSearch) {
// If there is more than one element in the query, we switch to literalSearch in any
// case.
Expand Down Expand Up @@ -3713,6 +3730,9 @@ async function search(forced) {

const params = searchState.getQueryStringParams();

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

0 comments on commit 3259007

Please sign in to comment.