Skip to content

Commit bccb9f7

Browse files
committed
rustdoc: fold path to lower case, but preserve original name
This fixes the "About This Result" page so that it shows the case of type parameters in the query.
1 parent 5522634 commit bccb9f7

19 files changed

+174
-359
lines changed

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

+19-13
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ function initSearch(rawSearchIndex) {
585585
} else if (quadcolon !== null) {
586586
throw ["Unexpected ", quadcolon[0]];
587587
}
588-
const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/);
588+
const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/).map(x => x.toLowerCase());
589589
// In case we only have something like `<p>`, there is no name.
590590
if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) {
591591
if (generics.length > 0 || prevIs(parserState, ">")) {
@@ -624,7 +624,10 @@ function initSearch(rawSearchIndex) {
624624
if (gen.name !== null) {
625625
gen.bindingName.generics.unshift(gen);
626626
}
627-
bindings.set(gen.bindingName.name, gen.bindingName.generics);
627+
bindings.set(
628+
gen.bindingName.name.toLowerCase().replace(/_/g, ""),
629+
gen.bindingName.generics,
630+
);
628631
return false;
629632
}
630633
return true;
@@ -1204,8 +1207,7 @@ function initSearch(rawSearchIndex) {
12041207
*/
12051208
function newParsedQuery(userQuery) {
12061209
return {
1207-
original: userQuery,
1208-
userQuery: userQuery.toLowerCase(),
1210+
userQuery: userQuery,
12091211
elems: [],
12101212
returned: [],
12111213
// Total number of "top" elements (does not include generics).
@@ -1297,7 +1299,7 @@ function initSearch(rawSearchIndex) {
12971299
genericsElems: 0,
12981300
typeFilter: null,
12991301
isInBinding: null,
1300-
userQuery: userQuery.toLowerCase(),
1302+
userQuery,
13011303
};
13021304
let query = newParsedQuery(userQuery);
13031305

@@ -2781,21 +2783,25 @@ function initSearch(rawSearchIndex) {
27812783
if ((elem.id === null && parsedQuery.totalElems > 1 && elem.typeFilter === -1
27822784
&& elem.generics.length === 0 && elem.bindings.size === 0)
27832785
|| elem.typeFilter === TY_GENERIC) {
2784-
if (genericSymbols.has(elem.name)) {
2785-
elem.id = genericSymbols.get(elem.name);
2786+
if (genericSymbols.has(elem.normalizedPathLast)) {
2787+
elem.id = genericSymbols.get(elem.normalizedPathLast);
27862788
} else {
27872789
elem.id = -(genericSymbols.size + 1);
2788-
genericSymbols.set(elem.name, elem.id);
2790+
genericSymbols.set(elem.normalizedPathLast, elem.id);
27892791
}
2790-
if (elem.typeFilter === -1 && elem.name.length >= 3) {
2792+
if (elem.typeFilter === -1 && elem.normalizedPathLast.length >= 3) {
27912793
// Silly heuristic to catch if the user probably meant
27922794
// to not write a generic parameter. We don't use it,
27932795
// just bring it up.
2794-
const maxPartDistance = Math.floor(elem.name.length / 3);
2796+
const maxPartDistance = Math.floor(elem.normalizedPathLast.length / 3);
27952797
let matchDist = maxPartDistance + 1;
27962798
let matchName = "";
27972799
for (const name of typeNameIdMap.keys()) {
2798-
const dist = editDistance(name, elem.name, maxPartDistance);
2800+
const dist = editDistance(
2801+
name,
2802+
elem.normalizedPathLast,
2803+
maxPartDistance,
2804+
);
27992805
if (dist <= matchDist && dist <= maxPartDistance) {
28002806
if (dist === matchDist && matchName > name) {
28012807
continue;
@@ -3356,11 +3362,11 @@ ${item.displayPath}<span class="${type}">${name}</span>\
33563362
}
33573363

33583364
// Update document title to maintain a meaningful browser history
3359-
searchState.title = "Results for " + query.original + " - Rust";
3365+
searchState.title = "Results for " + query.userQuery + " - Rust";
33603366

33613367
// Because searching is incremental by character, only the most
33623368
// recent search query is added to the browser history.
3363-
updateSearchHistory(buildUrl(query.original, filterCrates));
3369+
updateSearchHistory(buildUrl(query.userQuery, filterCrates));
33643370

33653371
await showResults(
33663372
await execQuery(query, filterCrates, window.currentCrate),

src/tools/rustdoc-js/tester.js

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
9191
if (fullPath.length === 0) {
9292
fieldsToCheck = [
9393
"foundElems",
94-
"original",
9594
"returned",
9695
"userQuery",
9796
"error",

tests/rustdoc-gui/search-corrections.goml

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ assert-css: (".search-corrections", {
2424
})
2525
assert-text: (
2626
".search-corrections",
27-
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
27+
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
2828
)
2929

3030
// Corrections do get shown on the "In Return Type" tab.
@@ -35,7 +35,7 @@ assert-css: (".search-corrections", {
3535
})
3636
assert-text: (
3737
".search-corrections",
38-
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
38+
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
3939
)
4040

4141
// Now, explicit return values
@@ -52,7 +52,7 @@ assert-css: (".search-corrections", {
5252
})
5353
assert-text: (
5454
".search-corrections",
55-
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
55+
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
5656
)
5757

5858
// Now, generic correction
@@ -69,7 +69,7 @@ assert-css: (".search-corrections", {
6969
})
7070
assert-text: (
7171
".search-corrections",
72-
"Type \"notablestructwithlongnamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
72+
"Type \"NotableStructWithLongNamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
7373
)
7474

7575
// Now, generic correction plus error
@@ -86,7 +86,7 @@ assert-css: (".search-corrections", {
8686
})
8787
assert-text: (
8888
".search-corrections",
89-
"Type \"notablestructwithlongnamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
89+
"Type \"NotableStructWithLongNamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
9090
)
9191

9292
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
@@ -102,5 +102,5 @@ assert-css: (".error", {
102102
})
103103
assert-text: (
104104
".error",
105-
"Query parser error: \"Generic type parameter notablestructwithlongnamr does not accept generic parameters\"."
105+
"Query parser error: \"Generic type parameter NotableStructWithLongNamr does not accept generic parameters\"."
106106
)

tests/rustdoc-js-std/parser-bindings.js

+20-30
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const PARSED = [
33
query: 'A<B=C>',
44
elems: [
55
{
6-
name: "a",
6+
name: "A",
77
fullPath: ["a"],
88
pathWithoutLast: [],
99
pathLast: "a",
@@ -13,7 +13,7 @@ const PARSED = [
1313
'b',
1414
[
1515
{
16-
name: "c",
16+
name: "C",
1717
fullPath: ["c"],
1818
pathWithoutLast: [],
1919
pathLast: "c",
@@ -27,16 +27,15 @@ const PARSED = [
2727
},
2828
],
2929
foundElems: 1,
30-
original: 'A<B=C>',
30+
userQuery: 'A<B=C>',
3131
returned: [],
32-
userQuery: 'a<b=c>',
3332
error: null,
3433
},
3534
{
3635
query: 'A<B = C>',
3736
elems: [
3837
{
39-
name: "a",
38+
name: "A",
4039
fullPath: ["a"],
4140
pathWithoutLast: [],
4241
pathLast: "a",
@@ -45,7 +44,7 @@ const PARSED = [
4544
[
4645
'b',
4746
[{
48-
name: "c",
47+
name: "C",
4948
fullPath: ["c"],
5049
pathWithoutLast: [],
5150
pathLast: "c",
@@ -58,16 +57,15 @@ const PARSED = [
5857
},
5958
],
6059
foundElems: 1,
61-
original: 'A<B = C>',
60+
userQuery: 'A<B = C>',
6261
returned: [],
63-
userQuery: 'a<b = c>',
6462
error: null,
6563
},
6664
{
6765
query: 'A<B=!>',
6866
elems: [
6967
{
70-
name: "a",
68+
name: "A",
7169
fullPath: ["a"],
7270
pathWithoutLast: [],
7371
pathLast: "a",
@@ -89,16 +87,15 @@ const PARSED = [
8987
},
9088
],
9189
foundElems: 1,
92-
original: 'A<B=!>',
90+
userQuery: 'A<B=!>',
9391
returned: [],
94-
userQuery: 'a<b=!>',
9592
error: null,
9693
},
9794
{
9895
query: 'A<B=[]>',
9996
elems: [
10097
{
101-
name: "a",
98+
name: "A",
10299
fullPath: ["a"],
103100
pathWithoutLast: [],
104101
pathLast: "a",
@@ -120,16 +117,15 @@ const PARSED = [
120117
},
121118
],
122119
foundElems: 1,
123-
original: 'A<B=[]>',
120+
userQuery: 'A<B=[]>',
124121
returned: [],
125-
userQuery: 'a<b=[]>',
126122
error: null,
127123
},
128124
{
129125
query: 'A<B=[!]>',
130126
elems: [
131127
{
132-
name: "a",
128+
name: "A",
133129
fullPath: ["a"],
134130
pathWithoutLast: [],
135131
pathLast: "a",
@@ -160,52 +156,47 @@ const PARSED = [
160156
},
161157
],
162158
foundElems: 1,
163-
original: 'A<B=[!]>',
159+
userQuery: 'A<B=[!]>',
164160
returned: [],
165-
userQuery: 'a<b=[!]>',
166161
error: null,
167162
},
168163
{
169164
query: 'A<B=C=>',
170165
elems: [],
171166
foundElems: 0,
172-
original: 'A<B=C=>',
167+
userQuery: 'A<B=C=>',
173168
returned: [],
174-
userQuery: 'a<b=c=>',
175169
error: "Cannot write `=` twice in a binding",
176170
},
177171
{
178172
query: 'A<B=>',
179173
elems: [],
180174
foundElems: 0,
181-
original: 'A<B=>',
175+
userQuery: 'A<B=>',
182176
returned: [],
183-
userQuery: 'a<b=>',
184177
error: "Unexpected `>` after `=`",
185178
},
186179
{
187180
query: 'B=C',
188181
elems: [],
189182
foundElems: 0,
190-
original: 'B=C',
183+
userQuery: 'B=C',
191184
returned: [],
192-
userQuery: 'b=c',
193185
error: "Type parameter `=` must be within generics list",
194186
},
195187
{
196188
query: '[B=C]',
197189
elems: [],
198190
foundElems: 0,
199-
original: '[B=C]',
191+
userQuery: '[B=C]',
200192
returned: [],
201-
userQuery: '[b=c]',
202193
error: "Type parameter `=` cannot be within slice `[]`",
203194
},
204195
{
205196
query: 'A<B<X>=C>',
206197
elems: [
207198
{
208-
name: "a",
199+
name: "A",
209200
fullPath: ["a"],
210201
pathWithoutLast: [],
211202
pathLast: "a",
@@ -215,15 +206,15 @@ const PARSED = [
215206
'b',
216207
[
217208
{
218-
name: "c",
209+
name: "C",
219210
fullPath: ["c"],
220211
pathWithoutLast: [],
221212
pathLast: "c",
222213
generics: [],
223214
typeFilter: -1,
224215
},
225216
{
226-
name: "x",
217+
name: "X",
227218
fullPath: ["x"],
228219
pathWithoutLast: [],
229220
pathLast: "x",
@@ -237,9 +228,8 @@ const PARSED = [
237228
},
238229
],
239230
foundElems: 1,
240-
original: 'A<B<X>=C>',
231+
userQuery: 'A<B<X>=C>',
241232
returned: [],
242-
userQuery: 'a<b<x>=c>',
243233
error: null,
244234
},
245235
];

0 commit comments

Comments
 (0)