Skip to content

Commit c11d24b

Browse files
[rustdoc search] Simplify itemTypes and filter "dependencies"
1 parent f2bae99 commit c11d24b

File tree

1 file changed

+88
-102
lines changed

1 file changed

+88
-102
lines changed

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

Lines changed: 88 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -91,54 +91,55 @@ if (!Promise.withResolvers) {
9191
// ==================== Core search logic begin ====================
9292
// This mapping table should match the discriminants of
9393
// `rustdoc::formats::item_type::ItemType` type in Rust.
94-
const itemTypes = [
95-
"keyword",
96-
"primitive",
97-
"mod",
98-
"externcrate",
99-
"import",
100-
"struct", // 5
101-
"enum",
102-
"fn",
103-
"type",
104-
"static",
105-
"trait", // 10
106-
"impl",
107-
"tymethod",
108-
"method",
109-
"structfield",
110-
"variant", // 15
111-
"macro",
112-
"associatedtype",
113-
"constant",
114-
"associatedconstant",
115-
"union", // 20
116-
"foreigntype",
117-
"existential",
118-
"attr",
119-
"derive",
120-
"traitalias", // 25
121-
"generic",
122-
"attribute",
123-
];
94+
const itemTypes = Object.freeze({
95+
keyword: 0,
96+
primitive: 1,
97+
mod: 2,
98+
externcrate: 3,
99+
import: 4,
100+
struct: 5,
101+
enum: 6,
102+
fn: 7,
103+
type: 8,
104+
static: 9,
105+
trait: 10,
106+
impl: 11,
107+
tymethod: 12,
108+
method: 13,
109+
structfield: 14,
110+
variant: 15,
111+
macro: 16,
112+
associatedtype: 17,
113+
constant: 18,
114+
associatedconstant: 19,
115+
union: 20,
116+
foreigntype: 21,
117+
existential: 22,
118+
attr: 23,
119+
derive: 24,
120+
traitalias: 25,
121+
generic: 26,
122+
attribute: 27,
123+
});
124+
const itemTypesName = Array.from(Object.keys(itemTypes));
125+
126+
// When filtering, some types might be included as well. For example, when you filter on `constant`,
127+
// we also include associated constant items.
128+
//
129+
// This map is built as follows: the first item of the array is the type to be included when the
130+
// second type of the array is used as filter.
131+
const itemParents = new Map([
132+
[itemTypes.associatedconstant, itemTypes.constant],
133+
[itemTypes.method, itemTypes.fn],
134+
[itemTypes.tymethod, itemTypes.fn],
135+
[itemTypes.primitive, itemTypes.type],
136+
[itemTypes.associatedtype, itemTypes.type],
137+
[itemTypes.traitalias, itemTypes.trait],
138+
[itemTypes.attr, itemTypes.macro],
139+
[itemTypes.derive, itemTypes.macro],
140+
[itemTypes.externcrate, itemTypes.import],
141+
]);
124142

125-
// used for special search precedence
126-
/** @type {rustdoc.ItemType} */
127-
const TY_PRIMITIVE = 1;
128-
/** @type {rustdoc.ItemType} */
129-
const TY_GENERIC = 26;
130-
/** @type {rustdoc.ItemType} */
131-
const TY_IMPORT = 4;
132-
/** @type {rustdoc.ItemType} */
133-
const TY_TRAIT = 10;
134-
/** @type {rustdoc.ItemType} */
135-
const TY_FN = 7;
136-
/** @type {rustdoc.ItemType} */
137-
const TY_METHOD = 13;
138-
/** @type {rustdoc.ItemType} */
139-
const TY_TYMETHOD = 12;
140-
/** @type {rustdoc.ItemType} */
141-
const TY_ASSOCTYPE = 17;
142143
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";
143144

144145
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -302,7 +303,7 @@ function isEndCharacter(c) {
302303
* @returns
303304
*/
304305
function isFnLikeTy(ty) {
305-
return ty === TY_FN || ty === TY_METHOD || ty === TY_TYMETHOD;
306+
return ty === itemTypes.fn || ty === itemTypes.method || ty === itemTypes.tymethod;
306307
}
307308

308309
/**
@@ -1205,8 +1206,9 @@ function itemTypeFromName(typename) {
12051206
if (typename === null) {
12061207
return NO_TYPE_FILTER;
12071208
}
1208-
const index = itemTypes.findIndex(i => i === typename);
1209-
if (index < 0) {
1209+
// @ts-ignore
1210+
const index = itemTypes[typename];
1211+
if (index === undefined) {
12101212
throw ["Unknown type filter ", typename];
12111213
}
12121214
return index;
@@ -1329,21 +1331,21 @@ class DocSearch {
13291331
}
13301332
return -1;
13311333
};
1332-
const typeNameIdOfOutput = await first(output, TY_ASSOCTYPE, "");
1333-
const typeNameIdOfFnPtr = await first(fn, TY_PRIMITIVE, "");
1334-
const typeNameIdOfFn = await first(fn, TY_TRAIT, "core::ops");
1335-
const typeNameIdOfFnMut = await first(fnMut, TY_TRAIT, "core::ops");
1336-
const typeNameIdOfFnOnce = await first(fnOnce, TY_TRAIT, "core::ops");
1337-
const typeNameIdOfArray = await first(array, TY_PRIMITIVE, "");
1338-
const typeNameIdOfSlice = await first(slice, TY_PRIMITIVE, "");
1339-
const typeNameIdOfArrayOrSlice = await first(arrayOrSlice, TY_PRIMITIVE, "");
1340-
const typeNameIdOfTuple = await first(tuple, TY_PRIMITIVE, "");
1341-
const typeNameIdOfUnit = await first(unit, TY_PRIMITIVE, "");
1342-
const typeNameIdOfTupleOrUnit = await first(tupleOrUnit, TY_PRIMITIVE, "");
1343-
const typeNameIdOfReference = await first(reference, TY_PRIMITIVE, "");
1344-
const typeNameIdOfPointer = await first(pointer, TY_PRIMITIVE, "");
1345-
const typeNameIdOfHof = await first(hof, TY_PRIMITIVE, "");
1346-
const typeNameIdOfNever = await first(never, TY_PRIMITIVE, "");
1334+
const typeNameIdOfOutput = await first(output, itemTypes.associatedtype, "");
1335+
const typeNameIdOfFnPtr = await first(fn, itemTypes.primitive, "");
1336+
const typeNameIdOfFn = await first(fn, itemTypes.trait, "core::ops");
1337+
const typeNameIdOfFnMut = await first(fnMut, itemTypes.trait, "core::ops");
1338+
const typeNameIdOfFnOnce = await first(fnOnce, itemTypes.trait, "core::ops");
1339+
const typeNameIdOfArray = await first(array, itemTypes.primitive, "");
1340+
const typeNameIdOfSlice = await first(slice, itemTypes.primitive, "");
1341+
const typeNameIdOfArrayOrSlice = await first(arrayOrSlice, itemTypes.primitive, "");
1342+
const typeNameIdOfTuple = await first(tuple, itemTypes.primitive, "");
1343+
const typeNameIdOfUnit = await first(unit, itemTypes.primitive, "");
1344+
const typeNameIdOfTupleOrUnit = await first(tupleOrUnit, itemTypes.primitive, "");
1345+
const typeNameIdOfReference = await first(reference, itemTypes.primitive, "");
1346+
const typeNameIdOfPointer = await first(pointer, itemTypes.primitive, "");
1347+
const typeNameIdOfHof = await first(hof, itemTypes.primitive, "");
1348+
const typeNameIdOfNever = await first(never, itemTypes.primitive, "");
13471349
this.typeNameIds = {
13481350
typeNameIdOfOutput,
13491351
typeNameIdOfFnPtr,
@@ -1520,7 +1522,7 @@ class DocSearch {
15201522
/** @param {rustdoc.ParserQueryElement} elem */
15211523
const checkTypeFilter = elem => {
15221524
const ty = itemTypeFromName(elem.typeFilter);
1523-
if (ty === TY_GENERIC && elem.generics.length !== 0) {
1525+
if (ty === itemTypes.generic && elem.generics.length !== 0) {
15241526
throw [
15251527
"Generic type parameter ",
15261528
elem.name,
@@ -2033,7 +2035,7 @@ class DocSearch {
20332035
result = {
20342036
id,
20352037
name: "",
2036-
ty: TY_GENERIC,
2038+
ty: itemTypes.generic,
20372039
path: null,
20382040
exactPath: null,
20392041
generics,
@@ -2045,7 +2047,7 @@ class DocSearch {
20452047
result = {
20462048
id: null,
20472049
name: "",
2048-
ty: TY_GENERIC,
2050+
ty: itemTypes.generic,
20492051
path: null,
20502052
exactPath: null,
20512053
generics,
@@ -2062,7 +2064,7 @@ class DocSearch {
20622064
return {
20632065
id: null,
20642066
name: "",
2065-
ty: TY_GENERIC,
2067+
ty: itemTypes.generic,
20662068
path: null,
20672069
exactPath: null,
20682070
generics,
@@ -2149,7 +2151,7 @@ class DocSearch {
21492151
let displayPath;
21502152
let href;
21512153
let traitPath = null;
2152-
const type = itemTypes[item.ty];
2154+
const type = itemTypesName[item.ty];
21532155
const name = item.name;
21542156
let path = item.modulePath;
21552157
let exactPath = item.exactModulePath;
@@ -2173,7 +2175,7 @@ class DocSearch {
21732175
} else if (item.parent) {
21742176
const myparent = item.parent;
21752177
let anchor = type + "." + name;
2176-
const parentType = itemTypes[myparent.path.ty];
2178+
const parentType = itemTypesName[myparent.path.ty];
21772179
let pageType = parentType;
21782180
let pageName = myparent.name;
21792181
exactPath = `${myparent.path.exactModulePath}::${myparent.name}`;
@@ -2520,11 +2522,11 @@ class DocSearch {
25202522
whereClause.set(fnParamNames[-1 - fnType.id], where);
25212523
}
25222524
} else {
2523-
if (fnType.ty === TY_PRIMITIVE) {
2525+
if (fnType.ty === itemTypes.primitive) {
25242526
if (await writeSpecialPrimitive(fnType, result)) {
25252527
return;
25262528
}
2527-
} else if (fnType.ty === TY_TRAIT && (
2529+
} else if (fnType.ty === itemTypes.trait && (
25282530
fnType.id === typeNameIds.typeNameIdOfFn ||
25292531
fnType.id === typeNameIds.typeNameIdOfFnMut ||
25302532
fnType.id === typeNameIds.typeNameIdOfFnOnce ||
@@ -2691,8 +2693,8 @@ class DocSearch {
26912693
// unlike other items, methods have a different ty when they are
26922694
// in an impl block vs a trait. want to normalize this away.
26932695
let ty = obj.item.ty;
2694-
if (ty === TY_TYMETHOD) {
2695-
ty = TY_METHOD;
2696+
if (ty === itemTypes.tymethod) {
2697+
ty = itemTypes.method;
26962698
}
26972699
// To be sure than it some items aren't considered as duplicate.
26982700
obj.fullPath = res[2] + "|" + ty;
@@ -2714,10 +2716,10 @@ class DocSearch {
27142716

27152717
// Exports are specifically not shown if the items they point at
27162718
// are already in the results.
2717-
if (obj.item.ty === TY_IMPORT && duplicates.has(res[2])) {
2719+
if (obj.item.ty === itemTypes.import && duplicates.has(res[2])) {
27182720
continue;
27192721
}
2720-
if (duplicates.has(res[2] + "|" + TY_IMPORT)) {
2722+
if (duplicates.has(res[2] + "|" + itemTypes.import)) {
27212723
continue;
27222724
}
27232725
duplicates.add(obj.fullPath);
@@ -3894,24 +3896,8 @@ class DocSearch {
38943896
if (filter <= NO_TYPE_FILTER || filter === type) return true;
38953897

38963898
// Match related items
3897-
const name = itemTypes[type];
3898-
switch (itemTypes[filter]) {
3899-
case "constant":
3900-
return name === "associatedconstant";
3901-
case "fn":
3902-
return name === "method" || name === "tymethod";
3903-
case "type":
3904-
return name === "primitive" || name === "associatedtype";
3905-
case "trait":
3906-
return name === "traitalias";
3907-
case "macro":
3908-
return name === "attr" || name === "derive";
3909-
case "import":
3910-
return name === "externcrate";
3911-
}
3912-
3913-
// No match
3914-
return false;
3899+
// @ts-ignore
3900+
return filter === itemParents.get(type);
39153901
}
39163902

39173903
const innerRunNameQuery =
@@ -4246,7 +4232,7 @@ class DocSearch {
42464232
* ]>[]}
42474233
* */
42484234
const typePromises = [];
4249-
if (typeFilter !== TY_GENERIC && searchResults) {
4235+
if (typeFilter !== itemTypes.generic && searchResults) {
42504236
for (const id of searchResults.matches().entries()) {
42514237
typePromises.push(Promise.all([
42524238
this.getName(id),
@@ -4262,22 +4248,22 @@ class DocSearch {
42624248
ty && !ty[polarity].every(bitmap => {
42634249
return bitmap.isEmpty();
42644250
}) &&
4265-
path && path.ty !== TY_ASSOCTYPE &&
4251+
path && path.ty !== itemTypes.associatedtype &&
42664252
(elem.pathWithoutLast.length === 0 ||
42674253
checkPath(
42684254
elem.pathWithoutLast,
42694255
path.modulePath.split("::"),
42704256
) === 0),
42714257
);
42724258
if (types.length === 0) {
4273-
const areGenericsAllowed = typeFilter === TY_GENERIC || (
4259+
const areGenericsAllowed = typeFilter === itemTypes.generic || (
42744260
typeFilter === -1 &&
42754261
(parsedQuery.totalElems > 1 || parsedQuery.hasReturnArrow) &&
42764262
elem.pathWithoutLast.length === 0 &&
42774263
elem.generics.length === 0 &&
42784264
elem.bindings.size === 0
42794265
);
4280-
if (typeFilter !== TY_GENERIC &&
4266+
if (typeFilter !== itemTypes.generic &&
42814267
(elem.name.length >= 3 || !areGenericsAllowed)
42824268
) {
42834269
/** @type {string|null} */
@@ -4301,7 +4287,7 @@ class DocSearch {
43014287
!ty[polarity].every(bitmap => {
43024288
return bitmap.isEmpty();
43034289
}) &&
4304-
path.ty !== TY_ASSOCTYPE
4290+
path.ty !== itemTypes.associatedtype
43054291
) {
43064292
let dist = editDistance(
43074293
name,
@@ -4363,7 +4349,7 @@ class DocSearch {
43634349
queryElem: {
43644350
name: elem.name,
43654351
id: (-genericId) - 1,
4366-
typeFilter: TY_GENERIC,
4352+
typeFilter: itemTypes.generic,
43674353
generics: [],
43684354
bindings: EMPTY_BINDINGS_MAP,
43694355
fullPath: elem.fullPath,
@@ -4930,7 +4916,7 @@ async function addTab(results, query, display, finishedCallback, isTypeSearch) {
49304916
count += 1;
49314917

49324918
const name = obj.item.name;
4933-
const type = itemTypes[obj.item.ty];
4919+
const type = itemTypesName[obj.item.ty];
49344920
const longType = longItemTypes[obj.item.ty];
49354921
const typeName = longType.length !== 0 ? `${longType}` : "?";
49364922

0 commit comments

Comments
 (0)