Skip to content

Commit 43078ee

Browse files
committed
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 63164b7 commit 43078ee

File tree

8 files changed

+55
-67
lines changed

8 files changed

+55
-67
lines changed

src/librustdoc/formats/item_type.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ use crate::clean;
2323
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
2424
#[repr(u8)]
2525
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,
26+
Keyword = 0,
27+
Primitive = 1,
28+
Module = 2,
29+
ExternCrate = 3,
30+
Import = 4,
31+
Struct = 5,
32+
Enum = 6,
33+
Function = 7,
34+
TypeAlias = 8,
35+
Static = 9,
36+
Trait = 10,
37+
Impl = 11,
38+
TyMethod = 12,
39+
Method = 13,
40+
StructField = 14,
41+
Variant = 15,
42+
Macro = 16,
43+
AssocType = 17,
44+
Constant = 18,
45+
AssocConst = 19,
46+
Union = 20,
47+
ForeignType = 21,
4848
OpaqueTy = 22,
4949
ProcAttribute = 23,
5050
ProcDerive = 24,

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ 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",
@@ -33,13 +35,11 @@ const itemTypes = [
3335
"structfield",
3436
"variant",
3537
"macro",
36-
"primitive",
3738
"associatedtype",
3839
"constant",
3940
"associatedconstant",
4041
"union",
4142
"foreigntype",
42-
"keyword",
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

@@ -1251,16 +1249,6 @@ function initSearch(rawSearchIndex) {
12511249
return (a > b ? +1 : -1);
12521250
}
12531251

1254-
// special precedence for primitive and keyword pages
1255-
if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
1256-
(aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
1257-
return -1;
1258-
}
1259-
if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
1260-
(bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
1261-
return 1;
1262-
}
1263-
12641252
// sort by description (no description goes later)
12651253
a = (aaa.item.desc === "");
12661254
b = (bbb.item.desc === "");

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)