Skip to content

Commit

Permalink
rustdoc-search: make primitives and keywords less special
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
notriddle committed Nov 20, 2023
1 parent 63164b7 commit 43078ee
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 67 deletions.
44 changes: 22 additions & 22 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ use crate::clean;
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
#[repr(u8)]
pub(crate) enum ItemType {
Module = 0,
ExternCrate = 1,
Import = 2,
Struct = 3,
Enum = 4,
Function = 5,
TypeAlias = 6,
Static = 7,
Trait = 8,
Impl = 9,
TyMethod = 10,
Method = 11,
StructField = 12,
Variant = 13,
Macro = 14,
Primitive = 15,
AssocType = 16,
Constant = 17,
AssocConst = 18,
Union = 19,
ForeignType = 20,
Keyword = 21,
Keyword = 0,
Primitive = 1,
Module = 2,
ExternCrate = 3,
Import = 4,
Struct = 5,
Enum = 6,
Function = 7,
TypeAlias = 8,
Static = 9,
Trait = 10,
Impl = 11,
TyMethod = 12,
Method = 13,
StructField = 14,
Variant = 15,
Macro = 16,
AssocType = 17,
Constant = 18,
AssocConst = 19,
Union = 20,
ForeignType = 21,
OpaqueTy = 22,
ProcAttribute = 23,
ProcDerive = 24,
Expand Down
20 changes: 4 additions & 16 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if (!Array.prototype.toSpliced) {
// This mapping table should match the discriminants of
// `rustdoc::formats::item_type::ItemType` type in Rust.
const itemTypes = [
"keyword",
"primitive",
"mod",
"externcrate",
"import",
Expand All @@ -33,13 +35,11 @@ const itemTypes = [
"structfield",
"variant",
"macro",
"primitive",
"associatedtype",
"constant",
"associatedconstant",
"union",
"foreigntype",
"keyword",
"existential",
"attr",
"derive",
Expand All @@ -48,6 +48,8 @@ const itemTypes = [
];

const longItemTypes = [
"keyword",
"primitive type",
"module",
"extern crate",
"re-export",
Expand All @@ -63,22 +65,18 @@ const longItemTypes = [
"struct field",
"enum variant",
"macro",
"primitive type",
"assoc type",
"constant",
"assoc const",
"union",
"foreign type",
"keyword",
"existential type",
"attribute macro",
"derive macro",
"trait alias",
];

// used for special search precedence
const TY_PRIMITIVE = itemTypes.indexOf("primitive");
const TY_KEYWORD = itemTypes.indexOf("keyword");
const TY_GENERIC = itemTypes.indexOf("generic");
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";

Expand Down Expand Up @@ -1251,16 +1249,6 @@ function initSearch(rawSearchIndex) {
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)
a = (aaa.item.desc === "");
b = (bbb.item.desc === "");
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-js-std/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const EXPECTED = {
'query': 'fn',
'others': [
{ 'path': 'std', 'name': 'fn', ty: 15 }, // 15 is for primitive types
{ 'path': 'std', 'name': 'fn', ty: 21 }, // 21 is for keywords
{ 'path': 'std', 'name': 'fn', ty: 1 }, // 1 is for primitive types
{ 'path': 'std', 'name': 'fn', ty: 0 }, // 0 is for keywords
],
};
4 changes: 2 additions & 2 deletions tests/rustdoc-js-std/macro-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const EXPECTED = {
'query': 'panic',
'others': [
{ 'path': 'std', 'name': 'panic', ty: 14 }, // 15 is for macros
{ 'path': 'std', 'name': 'panic', ty: 0 }, // 0 is for modules
{ 'path': 'std', 'name': 'panic', ty: 16 }, // 16 is for macros
{ 'path': 'std', 'name': 'panic', ty: 2 }, // 2 is for modules
],
};
22 changes: 11 additions & 11 deletions tests/rustdoc-js-std/parser-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "foo",
generics: [],
typeFilter: 5,
typeFilter: 7,
}],
foundElems: 1,
original: "fn:foo",
Expand All @@ -23,7 +23,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "foo",
generics: [],
typeFilter: 4,
typeFilter: 6,
}],
foundElems: 1,
original: "enum : foo",
Expand All @@ -48,7 +48,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "macro",
generics: [],
typeFilter: 14,
typeFilter: 16,
}],
foundElems: 1,
original: "macro!",
Expand All @@ -64,7 +64,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "mac",
generics: [],
typeFilter: 14,
typeFilter: 16,
}],
foundElems: 1,
original: "macro:mac!",
Expand All @@ -80,7 +80,7 @@ const PARSED = [
pathWithoutLast: ["a"],
pathLast: "mac",
generics: [],
typeFilter: 14,
typeFilter: 16,
}],
foundElems: 1,
original: "a::mac!",
Expand All @@ -99,7 +99,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "foo",
generics: [],
typeFilter: 5,
typeFilter: 7,
}],
userQuery: "-> fn:foo",
error: null,
Expand All @@ -121,10 +121,10 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "bar",
generics: [],
typeFilter: 5,
typeFilter: 7,
}
],
typeFilter: 5,
typeFilter: 7,
}],
userQuery: "-> fn:foo<fn:bar>",
error: null,
Expand All @@ -146,18 +146,18 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "bar",
generics: [],
typeFilter: 5,
typeFilter: 7,
},
{
name: "baz::fuzz",
fullPath: ["baz", "fuzz"],
pathWithoutLast: ["baz"],
pathLast: "fuzz",
generics: [],
typeFilter: 4,
typeFilter: 6,
},
],
typeFilter: 5,
typeFilter: 7,
}],
userQuery: "-> fn:foo<fn:bar, enum : baz::fuzz>",
error: null,
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-js-std/parser-ident.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 15,
typeFilter: 1,
},
],
typeFilter: -1,
Expand All @@ -32,7 +32,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 15,
typeFilter: 1,
}],
foundElems: 1,
original: "!",
Expand All @@ -48,7 +48,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "a",
generics: [],
typeFilter: 14,
typeFilter: 16,
}],
foundElems: 1,
original: "a!",
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-js-std/parser-returned.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 15,
typeFilter: 1,
}],
userQuery: "-> !",
error: null,
Expand Down
20 changes: 10 additions & 10 deletions tests/rustdoc-js-std/parser-slice-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "[]",
generics: [],
typeFilter: 15,
typeFilter: 1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand All @@ -70,7 +70,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "[]",
generics: [],
typeFilter: 15,
typeFilter: 1,
},
{
name: "u8",
Expand Down Expand Up @@ -105,7 +105,7 @@ const PARSED = [
typeFilter: -1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand Down Expand Up @@ -140,7 +140,7 @@ const PARSED = [
typeFilter: -1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand Down Expand Up @@ -176,7 +176,7 @@ const PARSED = [
typeFilter: -1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand All @@ -194,7 +194,7 @@ const PARSED = [
pathWithoutLast: [],
pathLast: "[]",
generics: [],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand Down Expand Up @@ -284,7 +284,7 @@ const PARSED = [
typeFilter: -1,
},
],
typeFilter: 15,
typeFilter: 1,
},
],
foundElems: 1,
Expand Down

0 comments on commit 43078ee

Please sign in to comment.