Skip to content

Commit 5c7e7df

Browse files
committed
rustdoc-search: pass original names through AST
1 parent 2cc1c0c commit 5c7e7df

19 files changed

Lines changed: 295 additions & 487 deletions

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function getFilteredNextElem(query, parserState, elems, isInGenerics) {
276276
// The type filter doesn't count as an element since it's a modifier.
277277
consttypeFilterElem=elems.pop();
278278
checkExtraTypeFilterCharacters(start,parserState);
279-
parserState.typeFilter=typeFilterElem.name;
279+
parserState.typeFilter=typeFilterElem.normalizedPathLast;
280280
parserState.pos+=1;
281281
parserState.totalElems-=1;
282282
query.literalSearch=false;
@@ -686,7 +686,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
686686
}elseif(quadcolon!==null){
687687
throw["Unexpected ",quadcolon[0]];
688688
}
689-
constpathSegments=path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/);
689+
constpathSegments=path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/).map(x=>x.toLowerCase());
690690
// In case we only have something like `<p>`, there is no name.
691691
if(pathSegments.length===0
692692
||(pathSegments.length===1&&pathSegments[0]==="")){
@@ -726,7 +726,10 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
726726
if(gen.name!==null){
727727
gen.bindingName.generics.unshift(gen);
728728
}
729-
bindings.set(gen.bindingName.name,gen.bindingName.generics);
729+
bindings.set(
730+
gen.bindingName.name.toLowerCase().replace(/_/g,""),
731+
gen.bindingName.generics,
732+
);
730733
returnfalse;
731734
}
732735
returntrue;
@@ -1786,8 +1789,7 @@ class DocSearch {
17861789
*/
17871790
functionnewParsedQuery(userQuery){
17881791
return{
1789-
original: userQuery,
1790-
userQuery: userQuery.toLowerCase(),
1792+
userQuery,
17911793
elems: [],
17921794
returned: [],
17931795
// Total number of "top" elements (does not include generics).
@@ -1909,7 +1911,7 @@ class DocSearch {
19091911
genericsElems: 0,
19101912
typeFilter: null,
19111913
isInBinding: null,
1912-
userQuery: userQuery.toLowerCase(),
1914+
userQuery,
19131915
};
19141916
letquery=newParsedQuery(userQuery);
19151917

@@ -2097,7 +2099,7 @@ class DocSearch {
20972099
*/
20982100
constsortResults=async(results,isType,preferredCrate)=>{
20992101
constuserQuery=parsedQuery.userQuery;
2100-
constcasedUserQuery=parsedQuery.original;
2102+
constnormalizedUserQuery=parsedQuery.userQuery.toLowerCase();
21012103
constresult_list=[];
21022104
for(constresultofresults.values()){
21032105
result.item=this.searchIndex[result.id];
@@ -2109,15 +2111,15 @@ class DocSearch {
21092111
leta,b;
21102112

21112113
// sort by exact case-sensitive match
2112-
a=(aaa.item.name!==casedUserQuery);
2113-
b=(bbb.item.name!==casedUserQuery);
2114+
a=(aaa.item.name!==userQuery);
2115+
b=(bbb.item.name!==userQuery);
21142116
if(a!==b){
21152117
returna-b;
21162118
}
21172119

21182120
// sort by exact match with regard to the last word (mismatch goes later)
2119-
a=(aaa.word!==userQuery);
2120-
b=(bbb.word!==userQuery);
2121+
a=(aaa.word!==normalizedUserQuery);
2122+
b=(bbb.word!==normalizedUserQuery);
21212123
if(a!==b){
21222124
returna-b;
21232125
}
@@ -3163,21 +3165,25 @@ class DocSearch {
31633165
if((elem.id===null&&parsedQuery.totalElems>1&&elem.typeFilter===-1
31643166
&&elem.generics.length===0&&elem.bindings.size===0)
31653167
||elem.typeFilter===TY_GENERIC){
3166-
if(genericSymbols.has(elem.name)){
3167-
elem.id=genericSymbols.get(elem.name);
3168+
if(genericSymbols.has(elem.normalizedPathLast)){
3169+
elem.id=genericSymbols.get(elem.normalizedPathLast);
31683170
}else{
31693171
elem.id=-(genericSymbols.size+1);
3170-
genericSymbols.set(elem.name,elem.id);
3172+
genericSymbols.set(elem.normalizedPathLast,elem.id);
31713173
}
3172-
if(elem.typeFilter===-1&&elem.name.length>=3){
3174+
if(elem.typeFilter===-1&&elem.normalizedPathLast.length>=3){
31733175
// Silly heuristic to catch if the user probably meant
31743176
// to not write a generic parameter. We don't use it,
31753177
// just bring it up.
3176-
constmaxPartDistance=Math.floor(elem.name.length/3);
3178+
constmaxPartDistance=Math.floor(elem.normalizedPathLast.length/3);
31773179
letmatchDist=maxPartDistance+1;
31783180
letmatchName="";
31793181
for(constnameofthis.typeNameIdMap.keys()){
3180-
constdist=editDistance(name,elem.name,maxPartDistance);
3182+
constdist=editDistance(
3183+
name,
3184+
elem.normalizedPathLast,
3185+
maxPartDistance,
3186+
);
31813187
if(dist<=matchDist&&dist<=maxPartDistance){
31823188
if(dist===matchDist&&matchName>name){
31833189
continue;
@@ -3289,7 +3295,7 @@ class DocSearch {
32893295
sorted_returned,
32903296
sorted_others,
32913297
parsedQuery);
3292-
awaithandleAliases(ret,parsedQuery.original.replace(/"/g,""),
3298+
awaithandleAliases(ret,parsedQuery.userQuery.replace(/"/g,""),
32933299
filterCrates,currentCrate);
32943300
awaitPromise.all([ret.others,ret.returned,ret.in_args].map(asynclist=>{
32953301
constdescs=awaitPromise.all(list.map(result=>{
@@ -3709,11 +3715,11 @@ async function search(forced) {
37093715
}
37103716

37113717
// Update document title to maintain a meaningful browser history
3712-
searchState.title="\""+query.original+"\" Search - Rust";
3718+
searchState.title="\""+query.userQuery+"\" Search - Rust";
37133719

37143720
// Because searching is incremental by character, only the most
37153721
// recent search query is added to the browser history.
3716-
updateSearchHistory(buildUrl(query.original,filterCrates));
3722+
updateSearchHistory(buildUrl(query.userQuery,filterCrates));
37173723

37183724
awaitshowResults(
37193725
awaitdocSearch.execQuery(query,filterCrates,window.currentCrate),

‎src/tools/rustdoc-js/tester.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
8484
if(fullPath.length===0){
8585
fieldsToCheck=[
8686
"foundElems",
87-
"original",
8887
"returned",
8988
"userQuery",
9089
"error",

‎tests/rustdoc-gui/search-corrections.goml‎

Lines changed: 6 additions & 6 deletions
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‎

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ const PARSED = [
33
query: 'A<B=C>',
44
elems: [
55
{
6-
name: "a",
6+
name: "A",
77
fullPath: ["a"],
88
pathWithoutLast: [],
99
pathLast: "a",
10+
normalizedPathLast: "a",
1011
generics: [],
1112
bindings: [
1213
[
1314
'b',
1415
[
1516
{
16-
name: "c",
17+
name: "C",
1718
fullPath: ["c"],
1819
pathWithoutLast: [],
1920
pathLast: "c",
21+
normalizedPathLast: "c",
2022
generics: [],
2123
typeFilter: -1,
2224
},
@@ -27,16 +29,15 @@ const PARSED = [
2729
},
2830
],
2931
foundElems: 1,
30-
original: 'A<B=C>',
32+
userQuery: 'A<B=C>',
3133
returned: [],
32-
userQuery: 'a<b=c>',
3334
error: null,
3435
},
3536
{
3637
query: 'A<B = C>',
3738
elems: [
3839
{
39-
name: "a",
40+
name: "A",
4041
fullPath: ["a"],
4142
pathWithoutLast: [],
4243
pathLast: "a",
@@ -45,7 +46,7 @@ const PARSED = [
4546
[
4647
'b',
4748
[{
48-
name: "c",
49+
name: "C",
4950
fullPath: ["c"],
5051
pathWithoutLast: [],
5152
pathLast: "c",
@@ -58,16 +59,15 @@ const PARSED = [
5859
},
5960
],
6061
foundElems: 1,
61-
original: 'A<B = C>',
62+
userQuery: 'A<B = C>',
6263
returned: [],
63-
userQuery: 'a<b = c>',
6464
error: null,
6565
},
6666
{
6767
query: 'A<B=!>',
6868
elems: [
6969
{
70-
name: "a",
70+
name: "A",
7171
fullPath: ["a"],
7272
pathWithoutLast: [],
7373
pathLast: "a",
@@ -89,16 +89,15 @@ const PARSED = [
8989
},
9090
],
9191
foundElems: 1,
92-
original: 'A<B=!>',
92+
userQuery: 'A<B=!>',
9393
returned: [],
94-
userQuery: 'a<b=!>',
9594
error: null,
9695
},
9796
{
9897
query: 'A<B=[]>',
9998
elems: [
10099
{
101-
name: "a",
100+
name: "A",
102101
fullPath: ["a"],
103102
pathWithoutLast: [],
104103
pathLast: "a",
@@ -120,16 +119,15 @@ const PARSED = [
120119
},
121120
],
122121
foundElems: 1,
123-
original: 'A<B=[]>',
122+
userQuery: 'A<B=[]>',
124123
returned: [],
125-
userQuery: 'a<b=[]>',
126124
error: null,
127125
},
128126
{
129127
query: 'A<B=[!]>',
130128
elems: [
131129
{
132-
name: "a",
130+
name: "A",
133131
fullPath: ["a"],
134132
pathWithoutLast: [],
135133
pathLast: "a",
@@ -160,52 +158,47 @@ const PARSED = [
160158
},
161159
],
162160
foundElems: 1,
163-
original: 'A<B=[!]>',
161+
userQuery: 'A<B=[!]>',
164162
returned: [],
165-
userQuery: 'a<b=[!]>',
166163
error: null,
167164
},
168165
{
169166
query: 'A<B=C=>',
170167
elems: [],
171168
foundElems: 0,
172-
original: 'A<B=C=>',
169+
userQuery: 'A<B=C=>',
173170
returned: [],
174-
userQuery: 'a<b=c=>',
175171
error: "Cannot write `=` twice in a binding",
176172
},
177173
{
178174
query: 'A<B=>',
179175
elems: [],
180176
foundElems: 0,
181-
original: 'A<B=>',
177+
userQuery: 'A<B=>',
182178
returned: [],
183-
userQuery: 'a<b=>',
184179
error: "Unexpected `>` after `=`",
185180
},
186181
{
187182
query: 'B=C',
188183
elems: [],
189184
foundElems: 0,
190-
original: 'B=C',
185+
userQuery: 'B=C',
191186
returned: [],
192-
userQuery: 'b=c',
193187
error: "Type parameter `=` must be within generics list",
194188
},
195189
{
196190
query: '[B=C]',
197191
elems: [],
198192
foundElems: 0,
199-
original: '[B=C]',
193+
userQuery: '[B=C]',
200194
returned: [],
201-
userQuery: '[b=c]',
202195
error: "Type parameter `=` cannot be within slice `[]`",
203196
},
204197
{
205198
query: 'A<B<X>=C>',
206199
elems: [
207200
{
208-
name: "a",
201+
name: "A",
209202
fullPath: ["a"],
210203
pathWithoutLast: [],
211204
pathLast: "a",
@@ -215,15 +208,15 @@ const PARSED = [
215208
'b',
216209
[
217210
{
218-
name: "c",
211+
name: "C",
219212
fullPath: ["c"],
220213
pathWithoutLast: [],
221214
pathLast: "c",
222215
generics: [],
223216
typeFilter: -1,
224217
},
225218
{
226-
name: "x",
219+
name: "X",
227220
fullPath: ["x"],
228221
pathWithoutLast: [],
229222
pathLast: "x",
@@ -237,9 +230,8 @@ const PARSED = [
237230
},
238231
],
239232
foundElems: 1,
240-
original: 'A<B<X>=C>',
233+
userQuery: 'A<B<X>=C>',
241234
returned: [],
242-
userQuery: 'a<b<x>=c>',
243235
error: null,
244236
},
245237
];

0 commit comments

Comments
 (0)