Skip to content

Commit bdb929e

Browse files
authored
Rollup merge of #118109 - notriddle:notriddle/search-cleanup-2, r=GuillaumeGomez
rustdoc-search: simplify `checkPath` and `sortResults` These two commits reduce the amount of code in search.js with no noticeable change in performance. https://notriddle.com/rustdoc-html-demo-5/profile-5/index.html
2 parents a98698e + 28f17d9 commit bdb929e

File tree

9 files changed

+77
-92
lines changed

9 files changed

+77
-92
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

+15-37
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 === "");
@@ -1840,26 +1828,16 @@ function initSearch(rawSearchIndex) {
18401828

18411829
const length = path.length;
18421830
const clength = contains.length;
1843-
if (clength > length) {
1844-
return maxEditDistance + 1;
1845-
}
1846-
for (let i = 0; i < length; ++i) {
1847-
if (i + clength > length) {
1848-
break;
1849-
}
1831+
pathiter: for (let i = length - clength; i >= 0; i -= 1) {
18501832
let dist_total = 0;
1851-
let aborted = false;
18521833
for (let x = 0; x < clength; ++x) {
18531834
const dist = editDistance(path[i + x], contains[x], maxEditDistance);
18541835
if (dist > maxEditDistance) {
1855-
aborted = true;
1856-
break;
1836+
continue pathiter;
18571837
}
18581838
dist_total += dist;
18591839
}
1860-
if (!aborted) {
1861-
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
1862-
}
1840+
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
18631841
}
18641842
return ret_dist;
18651843
}
@@ -2953,7 +2931,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
29532931
// https://mathiasbynens.be/notes/shapes-ics
29542932
const crateRow = {
29552933
crate: crate,
2956-
ty: 1, // == ExternCrate
2934+
ty: 3, // == ExternCrate
29572935
name: crate,
29582936
path: "",
29592937
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-bindings.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const PARSED = [
8181
pathWithoutLast: [],
8282
pathLast: "never",
8383
generics: [],
84-
typeFilter: 15,
84+
typeFilter: 1,
8585
}]
8686
],
8787
],
@@ -112,7 +112,7 @@ const PARSED = [
112112
pathWithoutLast: [],
113113
pathLast: "[]",
114114
generics: [],
115-
typeFilter: 15,
115+
typeFilter: 1,
116116
}]
117117
],
118118
],
@@ -149,10 +149,10 @@ const PARSED = [
149149
pathWithoutLast: [],
150150
pathLast: "never",
151151
generics: [],
152-
typeFilter: 15,
152+
typeFilter: 1,
153153
},
154154
],
155-
typeFilter: 15,
155+
typeFilter: 1,
156156
}]
157157
],
158158
],

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,

0 commit comments

Comments
 (0)