Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5246eb8

Browse files
committedNov 21, 2023
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.
1 parent d82a085 commit 5246eb8

File tree

8 files changed

+70
-75
lines changed

8 files changed

+70
-75
lines changed
 

‎src/librustdoc/formats/item_type.rs

+29-22
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,42 @@ use crate::clean;
1616
/// Consequently, every change to this type should be synchronized to
1717
/// the `itemTypes` mapping table in `html/static/js/search.js`.
1818
///
19+
/// The search engine in search.js also uses item type numbers as a tie breaker when
20+
/// sorting results. Keywords and primitives are given first because we want them to be easily
21+
/// found by new users who don't know about advanced features like type filters. The rest are
22+
/// mostly in an arbitrary order, but it's easier to test the search engine when
23+
/// it's deterministic, and these are strictly finer-grained than language namespaces, so
24+
/// using the path and the item type together to sort ensures that search sorting is stable.
25+
///
1926
/// In addition, code in `html::render` uses this enum to generate CSS classes, page prefixes, and
2027
/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
2128
/// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
2229
/// ordering based on a helper function inside `item_module`, in the same file.
2330
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
2431
#[repr(u8)]
2532
pub(crate) enum ItemType {
26-
Module = 0,
27-
ExternCrate = 1,
28-
Import = 2,
29-
Struct = 3,
30-
Enum = 4,
31-
Function = 5,
32-
TypeAlias = 6,
33-
Static = 7,
34-
Trait = 8,
35-
Impl = 9,
36-
TyMethod = 10,
37-
Method = 11,
38-
StructField = 12,
39-
Variant = 13,
40-
Macro = 14,
41-
Primitive = 15,
42-
AssocType = 16,
43-
Constant = 17,
44-
AssocConst = 18,
45-
Union = 19,
46-
ForeignType = 20,
47-
Keyword = 21,
33+
Keyword = 0,
34+
Primitive = 1,
35+
Module = 2,
36+
ExternCrate = 3,
37+
Import = 4,
38+
Struct = 5,
39+
Enum = 6,
40+
Function = 7,
41+
TypeAlias = 8,
42+
Static = 9,
43+
Trait = 10,
44+
Impl = 11,
45+
TyMethod = 12,
46+
Method = 13,
47+
StructField = 14,
48+
Variant = 15,
49+
Macro = 16,
50+
AssocType = 17,
51+
Constant = 18,
52+
AssocConst = 19,
53+
Union = 20,
54+
ForeignType = 21,
4855
OpaqueTy = 22,
4956
ProcAttribute = 23,
5057
ProcDerive = 24,

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

+12-24
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ if (!Array.prototype.toSpliced) {
1818
// This mapping table should match the discriminants of
1919
// `rustdoc::formats::item_type::ItemType` type in Rust.
2020
const itemTypes = [
21+
"keyword",
22+
"primitive",
2123
"mod",
2224
"externcrate",
2325
"import",
24-
"struct",
26+
"struct", // 5
2527
"enum",
26-
"fn", // 5
28+
"fn",
2729
"type",
2830
"static",
29-
"trait",
31+
"trait", // 10
3032
"impl",
31-
"tymethod", // 10
33+
"tymethod",
3234
"method",
3335
"structfield",
34-
"variant",
36+
"variant", // 15
3537
"macro",
36-
"primitive", // 15
3738
"associatedtype",
3839
"constant",
3940
"associatedconstant",
40-
"union",
41-
"foreigntype", // 20
42-
"keyword",
41+
"union", // 20
42+
"foreigntype",
4343
"existential",
4444
"attr",
4545
"derive",
@@ -48,6 +48,8 @@ const itemTypes = [
4848
];
4949

5050
const longItemTypes = [
51+
"keyword",
52+
"primitive type",
5153
"module",
5254
"extern crate",
5355
"re-export",
@@ -63,22 +65,18 @@ const longItemTypes = [
6365
"struct field",
6466
"enum variant",
6567
"macro",
66-
"primitive type",
6768
"assoc type",
6869
"constant",
6970
"assoc const",
7071
"union",
7172
"foreign type",
72-
"keyword",
7373
"existential type",
7474
"attribute macro",
7575
"derive macro",
7676
"trait alias",
7777
];
7878

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

@@ -1317,16 +1315,6 @@ function initSearch(rawSearchIndex) {
13171315
return (a > b ? +1 : -1);
13181316
}
13191317

1320-
// special precedence for primitive and keyword pages
1321-
if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
1322-
(aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
1323-
return -1;
1324-
}
1325-
if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
1326-
(bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
1327-
return 1;
1328-
}
1329-
13301318
// sort by description (no description goes later)
13311319
a = (aaa.item.desc === "");
13321320
b = (bbb.item.desc === "");
@@ -2943,7 +2931,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
29432931
// https://mathiasbynens.be/notes/shapes-ics
29442932
const crateRow = {
29452933
crate: crate,
2946-
ty: 1, // == ExternCrate
2934+
ty: 3, // == ExternCrate
29472935
name: crate,
29482936
path: "",
29492937
desc: crateCorpus.doc,

‎tests/rustdoc-js-std/keyword.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const EXPECTED = {
44
'query': 'fn',
55
'others': [
6-
{ 'path': 'std', 'name': 'fn', ty: 15 }, // 15 is for primitive types
7-
{ 'path': 'std', 'name': 'fn', ty: 21 }, // 21 is for keywords
6+
{ 'path': 'std', 'name': 'fn', ty: 1 }, // 1 is for primitive types
7+
{ 'path': 'std', 'name': 'fn', ty: 0 }, // 0 is for keywords
88
],
99
};

‎tests/rustdoc-js-std/macro-check.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const EXPECTED = {
44
'query': 'panic',
55
'others': [
6-
{ 'path': 'std', 'name': 'panic', ty: 14 }, // 15 is for macros
7-
{ 'path': 'std', 'name': 'panic', ty: 0 }, // 0 is for modules
6+
{ 'path': 'std', 'name': 'panic', ty: 16 }, // 16 is for macros
7+
{ 'path': 'std', 'name': 'panic', ty: 2 }, // 2 is for modules
88
],
99
};

‎tests/rustdoc-js-std/parser-filter.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const PARSED = [
77
pathWithoutLast: [],
88
pathLast: "foo",
99
generics: [],
10-
typeFilter: 5,
10+
typeFilter: 7,
1111
}],
1212
foundElems: 1,
1313
original: "fn:foo",
@@ -23,7 +23,7 @@ const PARSED = [
2323
pathWithoutLast: [],
2424
pathLast: "foo",
2525
generics: [],
26-
typeFilter: 4,
26+
typeFilter: 6,
2727
}],
2828
foundElems: 1,
2929
original: "enum : foo",
@@ -48,7 +48,7 @@ const PARSED = [
4848
pathWithoutLast: [],
4949
pathLast: "macro",
5050
generics: [],
51-
typeFilter: 14,
51+
typeFilter: 16,
5252
}],
5353
foundElems: 1,
5454
original: "macro!",
@@ -64,7 +64,7 @@ const PARSED = [
6464
pathWithoutLast: [],
6565
pathLast: "mac",
6666
generics: [],
67-
typeFilter: 14,
67+
typeFilter: 16,
6868
}],
6969
foundElems: 1,
7070
original: "macro:mac!",
@@ -80,7 +80,7 @@ const PARSED = [
8080
pathWithoutLast: ["a"],
8181
pathLast: "mac",
8282
generics: [],
83-
typeFilter: 14,
83+
typeFilter: 16,
8484
}],
8585
foundElems: 1,
8686
original: "a::mac!",
@@ -99,7 +99,7 @@ const PARSED = [
9999
pathWithoutLast: [],
100100
pathLast: "foo",
101101
generics: [],
102-
typeFilter: 5,
102+
typeFilter: 7,
103103
}],
104104
userQuery: "-> fn:foo",
105105
error: null,
@@ -121,10 +121,10 @@ const PARSED = [
121121
pathWithoutLast: [],
122122
pathLast: "bar",
123123
generics: [],
124-
typeFilter: 5,
124+
typeFilter: 7,
125125
}
126126
],
127-
typeFilter: 5,
127+
typeFilter: 7,
128128
}],
129129
userQuery: "-> fn:foo<fn:bar>",
130130
error: null,
@@ -146,18 +146,18 @@ const PARSED = [
146146
pathWithoutLast: [],
147147
pathLast: "bar",
148148
generics: [],
149-
typeFilter: 5,
149+
typeFilter: 7,
150150
},
151151
{
152152
name: "baz::fuzz",
153153
fullPath: ["baz", "fuzz"],
154154
pathWithoutLast: ["baz"],
155155
pathLast: "fuzz",
156156
generics: [],
157-
typeFilter: 4,
157+
typeFilter: 6,
158158
},
159159
],
160-
typeFilter: 5,
160+
typeFilter: 7,
161161
}],
162162
userQuery: "-> fn:foo<fn:bar, enum : baz::fuzz>",
163163
error: null,

‎tests/rustdoc-js-std/parser-ident.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const PARSED = [
1313
pathWithoutLast: [],
1414
pathLast: "never",
1515
generics: [],
16-
typeFilter: 15,
16+
typeFilter: 1,
1717
},
1818
],
1919
typeFilter: -1,
@@ -32,7 +32,7 @@ const PARSED = [
3232
pathWithoutLast: [],
3333
pathLast: "never",
3434
generics: [],
35-
typeFilter: 15,
35+
typeFilter: 1,
3636
}],
3737
foundElems: 1,
3838
original: "!",
@@ -48,7 +48,7 @@ const PARSED = [
4848
pathWithoutLast: [],
4949
pathLast: "a",
5050
generics: [],
51-
typeFilter: 14,
51+
typeFilter: 16,
5252
}],
5353
foundElems: 1,
5454
original: "a!",

‎tests/rustdoc-js-std/parser-returned.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const PARSED = [
8989
pathWithoutLast: [],
9090
pathLast: "never",
9191
generics: [],
92-
typeFilter: 15,
92+
typeFilter: 1,
9393
}],
9494
userQuery: "-> !",
9595
error: null,

‎tests/rustdoc-js-std/parser-slice-array.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ const PARSED = [
4343
pathWithoutLast: [],
4444
pathLast: "[]",
4545
generics: [],
46-
typeFilter: 15,
46+
typeFilter: 1,
4747
},
4848
],
49-
typeFilter: 15,
49+
typeFilter: 1,
5050
},
5151
],
52-
typeFilter: 15,
52+
typeFilter: 1,
5353
},
5454
],
55-
typeFilter: 15,
55+
typeFilter: 1,
5656
},
5757
],
5858
foundElems: 1,
@@ -70,7 +70,7 @@ const PARSED = [
7070
pathWithoutLast: [],
7171
pathLast: "[]",
7272
generics: [],
73-
typeFilter: 15,
73+
typeFilter: 1,
7474
},
7575
{
7676
name: "u8",
@@ -105,7 +105,7 @@ const PARSED = [
105105
typeFilter: -1,
106106
},
107107
],
108-
typeFilter: 15,
108+
typeFilter: 1,
109109
},
110110
],
111111
foundElems: 1,
@@ -140,7 +140,7 @@ const PARSED = [
140140
typeFilter: -1,
141141
},
142142
],
143-
typeFilter: 15,
143+
typeFilter: 1,
144144
},
145145
],
146146
foundElems: 1,
@@ -176,7 +176,7 @@ const PARSED = [
176176
typeFilter: -1,
177177
},
178178
],
179-
typeFilter: 15,
179+
typeFilter: 1,
180180
},
181181
],
182182
foundElems: 1,
@@ -194,7 +194,7 @@ const PARSED = [
194194
pathWithoutLast: [],
195195
pathLast: "[]",
196196
generics: [],
197-
typeFilter: 15,
197+
typeFilter: 1,
198198
},
199199
],
200200
foundElems: 1,
@@ -284,7 +284,7 @@ const PARSED = [
284284
typeFilter: -1,
285285
},
286286
],
287-
typeFilter: 15,
287+
typeFilter: 1,
288288
},
289289
],
290290
foundElems: 1,

0 commit comments

Comments
 (0)
Please sign in to comment.