rustdoc-search: make primitives and keywords less special

The search sorting code already sorts by item type discriminant,
putting things with smaller discriminants first. There was
also a special case for sorting keywords and primitives earlier,
and this commit removes it by giving them lower discriminants.

The sorting code has another criteria where items with descriptions
appear earlier than items without, and that criteria has higher
priority than the item type. This shouldn't matter, though,
because primitives and keywords normally only appear in the
standard library, and it always gives them descriptions.
This commit is contained in:
Michael Howell 2023-11-20 13:37:57 -07:00
parent d82a08537a
commit 28f17d97a9
9 changed files with 74 additions and 79 deletions

View file

@ -16,6 +16,13 @@ use crate::clean;
/// Consequently, every change to this type should be synchronized to /// Consequently, every change to this type should be synchronized to
/// the `itemTypes` mapping table in `html/static/js/search.js`. /// the `itemTypes` mapping table in `html/static/js/search.js`.
/// ///
/// The search engine in search.js also uses item type numbers as a tie breaker when
/// sorting results. Keywords and primitives are given first because we want them to be easily
/// found by new users who don't know about advanced features like type filters. The rest are
/// mostly in an arbitrary order, but it's easier to test the search engine when
/// it's deterministic, and these are strictly finer-grained than language namespaces, so
/// using the path and the item type together to sort ensures that search sorting is stable.
///
/// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and /// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints /// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
/// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an /// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
@ -23,28 +30,28 @@ use crate::clean;
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)] #[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
#[repr(u8)] #[repr(u8)]
pub(crate) enum ItemType { pub(crate) enum ItemType {
Module = 0, Keyword = 0,
ExternCrate = 1, Primitive = 1,
Import = 2, Module = 2,
Struct = 3, ExternCrate = 3,
Enum = 4, Import = 4,
Function = 5, Struct = 5,
TypeAlias = 6, Enum = 6,
Static = 7, Function = 7,
Trait = 8, TypeAlias = 8,
Impl = 9, Static = 9,
TyMethod = 10, Trait = 10,
Method = 11, Impl = 11,
StructField = 12, TyMethod = 12,
Variant = 13, Method = 13,
Macro = 14, StructField = 14,
Primitive = 15, Variant = 15,
AssocType = 16, Macro = 16,
Constant = 17, AssocType = 17,
AssocConst = 18, Constant = 18,
Union = 19, AssocConst = 19,
ForeignType = 20, Union = 20,
Keyword = 21, ForeignType = 21,
OpaqueTy = 22, OpaqueTy = 22,
ProcAttribute = 23, ProcAttribute = 23,
ProcDerive = 24, ProcDerive = 24,

View file

@ -18,28 +18,28 @@ if (!Array.prototype.toSpliced) {
// This mapping table should match the discriminants of // This mapping table should match the discriminants of
// `rustdoc::formats::item_type::ItemType` type in Rust. // `rustdoc::formats::item_type::ItemType` type in Rust.
const itemTypes = [ const itemTypes = [
"keyword",
"primitive",
"mod", "mod",
"externcrate", "externcrate",
"import", "import",
"struct", "struct", // 5
"enum", "enum",
"fn", // 5 "fn",
"type", "type",
"static", "static",
"trait", "trait", // 10
"impl", "impl",
"tymethod", // 10 "tymethod",
"method", "method",
"structfield", "structfield",
"variant", "variant", // 15
"macro", "macro",
"primitive", // 15
"associatedtype", "associatedtype",
"constant", "constant",
"associatedconstant", "associatedconstant",
"union", "union", // 20
"foreigntype", // 20 "foreigntype",
"keyword",
"existential", "existential",
"attr", "attr",
"derive", "derive",
@ -48,6 +48,8 @@ const itemTypes = [
]; ];
const longItemTypes = [ const longItemTypes = [
"keyword",
"primitive type",
"module", "module",
"extern crate", "extern crate",
"re-export", "re-export",
@ -63,13 +65,11 @@ const longItemTypes = [
"struct field", "struct field",
"enum variant", "enum variant",
"macro", "macro",
"primitive type",
"assoc type", "assoc type",
"constant", "constant",
"assoc const", "assoc const",
"union", "union",
"foreign type", "foreign type",
"keyword",
"existential type", "existential type",
"attribute macro", "attribute macro",
"derive macro", "derive macro",
@ -77,8 +77,6 @@ const longItemTypes = [
]; ];
// used for special search precedence // used for special search precedence
const TY_PRIMITIVE = itemTypes.indexOf("primitive");
const TY_KEYWORD = itemTypes.indexOf("keyword");
const TY_GENERIC = itemTypes.indexOf("generic"); const TY_GENERIC = itemTypes.indexOf("generic");
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../"; const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";
@ -1317,16 +1315,6 @@ function initSearch(rawSearchIndex) {
return (a > b ? +1 : -1); return (a > b ? +1 : -1);
} }
// special precedence for primitive and keyword pages
if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
(aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
return -1;
}
if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
(bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
return 1;
}
// sort by description (no description goes later) // sort by description (no description goes later)
a = (aaa.item.desc === ""); a = (aaa.item.desc === "");
b = (bbb.item.desc === ""); b = (bbb.item.desc === "");
@ -2943,7 +2931,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
// https://mathiasbynens.be/notes/shapes-ics // https://mathiasbynens.be/notes/shapes-ics
const crateRow = { const crateRow = {
crate: crate, crate: crate,
ty: 1, // == ExternCrate ty: 3, // == ExternCrate
name: crate, name: crate,
path: "", path: "",
desc: crateCorpus.doc, desc: crateCorpus.doc,

View file

@ -3,7 +3,7 @@
const EXPECTED = { const EXPECTED = {
'query': 'fn', 'query': 'fn',
'others': [ 'others': [
{ 'path': 'std', 'name': 'fn', ty: 15 }, // 15 is for primitive types { 'path': 'std', 'name': 'fn', ty: 1 }, // 1 is for primitive types
{ 'path': 'std', 'name': 'fn', ty: 21 }, // 21 is for keywords { 'path': 'std', 'name': 'fn', ty: 0 }, // 0 is for keywords
], ],
}; };

View file

@ -3,7 +3,7 @@
const EXPECTED = { const EXPECTED = {
'query': 'panic', 'query': 'panic',
'others': [ 'others': [
{ 'path': 'std', 'name': 'panic', ty: 14 }, // 15 is for macros { 'path': 'std', 'name': 'panic', ty: 16 }, // 16 is for macros
{ 'path': 'std', 'name': 'panic', ty: 0 }, // 0 is for modules { 'path': 'std', 'name': 'panic', ty: 2 }, // 2 is for modules
], ],
}; };

View file

@ -81,7 +81,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "never", pathLast: "never",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}] }]
], ],
], ],
@ -112,7 +112,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "[]", pathLast: "[]",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}] }]
], ],
], ],
@ -149,10 +149,10 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "never", pathLast: "never",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}] }]
], ],
], ],

View file

@ -7,7 +7,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "foo", pathLast: "foo",
generics: [], generics: [],
typeFilter: 5, typeFilter: 7,
}], }],
foundElems: 1, foundElems: 1,
original: "fn:foo", original: "fn:foo",
@ -23,7 +23,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "foo", pathLast: "foo",
generics: [], generics: [],
typeFilter: 4, typeFilter: 6,
}], }],
foundElems: 1, foundElems: 1,
original: "enum : foo", original: "enum : foo",
@ -48,7 +48,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "macro", pathLast: "macro",
generics: [], generics: [],
typeFilter: 14, typeFilter: 16,
}], }],
foundElems: 1, foundElems: 1,
original: "macro!", original: "macro!",
@ -64,7 +64,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "mac", pathLast: "mac",
generics: [], generics: [],
typeFilter: 14, typeFilter: 16,
}], }],
foundElems: 1, foundElems: 1,
original: "macro:mac!", original: "macro:mac!",
@ -80,7 +80,7 @@ const PARSED = [
pathWithoutLast: ["a"], pathWithoutLast: ["a"],
pathLast: "mac", pathLast: "mac",
generics: [], generics: [],
typeFilter: 14, typeFilter: 16,
}], }],
foundElems: 1, foundElems: 1,
original: "a::mac!", original: "a::mac!",
@ -99,7 +99,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "foo", pathLast: "foo",
generics: [], generics: [],
typeFilter: 5, typeFilter: 7,
}], }],
userQuery: "-> fn:foo", userQuery: "-> fn:foo",
error: null, error: null,
@ -121,10 +121,10 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "bar", pathLast: "bar",
generics: [], generics: [],
typeFilter: 5, typeFilter: 7,
} }
], ],
typeFilter: 5, typeFilter: 7,
}], }],
userQuery: "-> fn:foo<fn:bar>", userQuery: "-> fn:foo<fn:bar>",
error: null, error: null,
@ -146,7 +146,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "bar", pathLast: "bar",
generics: [], generics: [],
typeFilter: 5, typeFilter: 7,
}, },
{ {
name: "baz::fuzz", name: "baz::fuzz",
@ -154,10 +154,10 @@ const PARSED = [
pathWithoutLast: ["baz"], pathWithoutLast: ["baz"],
pathLast: "fuzz", pathLast: "fuzz",
generics: [], generics: [],
typeFilter: 4, typeFilter: 6,
}, },
], ],
typeFilter: 5, typeFilter: 7,
}], }],
userQuery: "-> fn:foo<fn:bar, enum : baz::fuzz>", userQuery: "-> fn:foo<fn:bar, enum : baz::fuzz>",
error: null, error: null,

View file

@ -13,7 +13,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "never", pathLast: "never",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}, },
], ],
typeFilter: -1, typeFilter: -1,
@ -32,7 +32,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "never", pathLast: "never",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}], }],
foundElems: 1, foundElems: 1,
original: "!", original: "!",
@ -48,7 +48,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "a", pathLast: "a",
generics: [], generics: [],
typeFilter: 14, typeFilter: 16,
}], }],
foundElems: 1, foundElems: 1,
original: "a!", original: "a!",

View file

@ -89,7 +89,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "never", pathLast: "never",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}], }],
userQuery: "-> !", userQuery: "-> !",
error: null, error: null,

View file

@ -43,16 +43,16 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "[]", pathLast: "[]",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,
@ -70,7 +70,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "[]", pathLast: "[]",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}, },
{ {
name: "u8", name: "u8",
@ -105,7 +105,7 @@ const PARSED = [
typeFilter: -1, typeFilter: -1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,
@ -140,7 +140,7 @@ const PARSED = [
typeFilter: -1, typeFilter: -1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,
@ -176,7 +176,7 @@ const PARSED = [
typeFilter: -1, typeFilter: -1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,
@ -194,7 +194,7 @@ const PARSED = [
pathWithoutLast: [], pathWithoutLast: [],
pathLast: "[]", pathLast: "[]",
generics: [], generics: [],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,
@ -284,7 +284,7 @@ const PARSED = [
typeFilter: -1, typeFilter: -1,
}, },
], ],
typeFilter: 15, typeFilter: 1,
}, },
], ],
foundElems: 1, foundElems: 1,