@@ -830,7 +830,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
830
830
*/
831
831
function makePrimitiveElement ( name , extra ) {
832
832
return Object . assign ( {
833
- name : name ,
833
+ name,
834
834
id : null ,
835
835
fullPath : [ name ] ,
836
836
pathWithoutLast : [ ] ,
@@ -1483,6 +1483,7 @@ class DocSearch {
1483
1483
*/
1484
1484
this . assocTypeIdNameMap = new Map ( ) ;
1485
1485
this . ALIASES = new Map ( ) ;
1486
+ this . FOUND_ALIASES = new Set ( ) ;
1486
1487
this . rootPath = rootPath ;
1487
1488
this . searchState = searchState ;
1488
1489
@@ -2030,6 +2031,8 @@ class DocSearch {
2030
2031
// normalized names, type signature objects and fingerprints, and aliases.
2031
2032
id = 0 ;
2032
2033
2034
+ /** @type {Array<[string, { [key: string]: Array<number> }, number]> } */
2035
+ const allAliases = [ ] ;
2033
2036
for ( const [ crate , crateCorpus ] of rawSearchIndex ) {
2034
2037
// a string representing the lengths of each description shard
2035
2038
// a string representing the list of function types
@@ -2178,10 +2181,10 @@ class DocSearch {
2178
2181
paths [ i ] = { ty, name, path, exactPath, unboxFlag } ;
2179
2182
}
2180
2183
2181
- // convert `item*` into an object form, and construct word indices.
2184
+ // Convert `item*` into an object form, and construct word indices.
2182
2185
//
2183
- // before any analysis is performed lets gather the search terms to
2184
- // search against apart from the rest of the data. This is a quick
2186
+ // Before any analysis is performed, let's gather the search terms to
2187
+ // search against apart from the rest of the data. This is a quick
2185
2188
// operation that is cached for the life of the page state so that
2186
2189
// all other search operations have access to this cached data for
2187
2190
// faster analysis operations
@@ -2269,29 +2272,58 @@ class DocSearch {
2269
2272
}
2270
2273
2271
2274
if ( aliases ) {
2272
- const currentCrateAliases = new Map ( ) ;
2273
- this . ALIASES . set ( crate , currentCrateAliases ) ;
2274
- for ( const alias_name in aliases ) {
2275
- if ( ! Object . prototype . hasOwnProperty . call ( aliases , alias_name ) ) {
2276
- continue ;
2277
- }
2278
-
2279
- /** @type {number[] } */
2280
- let currentNameAliases ;
2281
- if ( currentCrateAliases . has ( alias_name ) ) {
2282
- currentNameAliases = currentCrateAliases . get ( alias_name ) ;
2283
- } else {
2284
- currentNameAliases = [ ] ;
2285
- currentCrateAliases . set ( alias_name , currentNameAliases ) ;
2286
- }
2287
- for ( const local_alias of aliases [ alias_name ] ) {
2288
- currentNameAliases . push ( local_alias + currentIndex ) ;
2289
- }
2290
- }
2275
+ // We need to add the aliases in `searchIndex` after we finished filling it
2276
+ // to not mess up indexes.
2277
+ allAliases . push ( [ crate , aliases , currentIndex ] ) ;
2291
2278
}
2292
2279
currentIndex += itemTypes . length ;
2293
2280
this . searchState . descShards . set ( crate , descShardList ) ;
2294
2281
}
2282
+
2283
+ for ( const [ crate , aliases , index ] of allAliases ) {
2284
+ for ( const [ alias_name , alias_refs ] of Object . entries ( aliases ) ) {
2285
+ if ( ! this . ALIASES . has ( crate ) ) {
2286
+ this . ALIASES . set ( crate , new Map ( ) ) ;
2287
+ }
2288
+ const word = alias_name . toLowerCase ( ) ;
2289
+ const crate_alias_map = this . ALIASES . get ( crate ) ;
2290
+ if ( ! crate_alias_map . has ( word ) ) {
2291
+ crate_alias_map . set ( word , [ ] ) ;
2292
+ }
2293
+ const aliases_map = crate_alias_map . get ( word ) ;
2294
+
2295
+ const normalizedName = word . indexOf ( "_" ) === - 1 ? word : word . replace ( / _ / g, "" ) ;
2296
+ for ( const alias of alias_refs ) {
2297
+ const originalIndex = alias + index ;
2298
+ const original = searchIndex [ originalIndex ] ;
2299
+ /** @type {rustdoc.Row } */
2300
+ const row = {
2301
+ crate,
2302
+ name : alias_name ,
2303
+ normalizedName,
2304
+ is_alias : true ,
2305
+ ty : original . ty ,
2306
+ type : original . type ,
2307
+ paramNames : [ ] ,
2308
+ word,
2309
+ id,
2310
+ parent : undefined ,
2311
+ original,
2312
+ path : "" ,
2313
+ implDisambiguator : original . implDisambiguator ,
2314
+ // Needed to load the description of the original item.
2315
+ // @ts -ignore
2316
+ descShard : original . descShard ,
2317
+ descIndex : original . descIndex ,
2318
+ bitIndex : original . bitIndex ,
2319
+ } ;
2320
+ aliases_map . push ( row ) ;
2321
+ this . nameTrie . insert ( normalizedName , id , this . tailTable ) ;
2322
+ id += 1 ;
2323
+ searchIndex . push ( row ) ;
2324
+ }
2325
+ }
2326
+ }
2295
2327
// Drop the (rather large) hash table used for reusing function items
2296
2328
this . TYPES_POOL = new Map ( ) ;
2297
2329
return searchIndex ;
@@ -2536,6 +2568,8 @@ class DocSearch {
2536
2568
parsedQuery . elems . reduce ( ( acc , next ) => acc + next . pathLast . length , 0 ) +
2537
2569
parsedQuery . returned . reduce ( ( acc , next ) => acc + next . pathLast . length , 0 ) ;
2538
2570
const maxEditDistance = Math . floor ( queryLen / 3 ) ;
2571
+ // We reinitialize the `FOUND_ALIASES` map.
2572
+ this . FOUND_ALIASES . clear ( ) ;
2539
2573
2540
2574
/**
2541
2575
* @type {Map<string, number> }
@@ -2695,6 +2729,10 @@ class DocSearch {
2695
2729
const buildHrefAndPath = item => {
2696
2730
let displayPath ;
2697
2731
let href ;
2732
+ if ( item . is_alias ) {
2733
+ this . FOUND_ALIASES . add ( item . word ) ;
2734
+ item = item . original ;
2735
+ }
2698
2736
const type = itemTypes [ item . ty ] ;
2699
2737
const name = item . name ;
2700
2738
let path = item . path ;
@@ -3198,8 +3236,7 @@ class DocSearch {
3198
3236
result . item = this . searchIndex [ result . id ] ;
3199
3237
result . word = this . searchIndex [ result . id ] . word ;
3200
3238
if ( isReturnTypeQuery ) {
3201
- // we are doing a return-type based search,
3202
- // deprioritize "clone-like" results,
3239
+ // We are doing a return-type based search, deprioritize "clone-like" results,
3203
3240
// ie. functions that also take the queried type as an argument.
3204
3241
const resultItemType = result . item && result . item . type ;
3205
3242
if ( ! resultItemType ) {
@@ -4259,28 +4296,13 @@ class DocSearch {
4259
4296
return false ;
4260
4297
}
4261
4298
4262
- // this does not yet have a type in `rustdoc.d.ts`.
4263
- // @ts -expect-error
4264
- function createAliasFromItem ( item ) {
4265
- return {
4266
- crate : item . crate ,
4267
- name : item . name ,
4268
- path : item . path ,
4269
- descShard : item . descShard ,
4270
- descIndex : item . descIndex ,
4271
- exactPath : item . exactPath ,
4272
- ty : item . ty ,
4273
- parent : item . parent ,
4274
- type : item . type ,
4275
- is_alias : true ,
4276
- bitIndex : item . bitIndex ,
4277
- implDisambiguator : item . implDisambiguator ,
4278
- } ;
4279
- }
4280
-
4281
4299
// @ts -expect-error
4282
4300
const handleAliases = async ( ret , query , filterCrates , currentCrate ) => {
4283
4301
const lowerQuery = query . toLowerCase ( ) ;
4302
+ if ( this . FOUND_ALIASES . has ( lowerQuery ) ) {
4303
+ return ;
4304
+ }
4305
+ this . FOUND_ALIASES . add ( lowerQuery ) ;
4284
4306
// We separate aliases and crate aliases because we want to have current crate
4285
4307
// aliases to be before the others in the displayed results.
4286
4308
// @ts -expect-error
@@ -4292,7 +4314,7 @@ class DocSearch {
4292
4314
&& this . ALIASES . get ( filterCrates ) . has ( lowerQuery ) ) {
4293
4315
const query_aliases = this . ALIASES . get ( filterCrates ) . get ( lowerQuery ) ;
4294
4316
for ( const alias of query_aliases ) {
4295
- aliases . push ( createAliasFromItem ( this . searchIndex [ alias ] ) ) ;
4317
+ aliases . push ( alias ) ;
4296
4318
}
4297
4319
}
4298
4320
} else {
@@ -4302,17 +4324,17 @@ class DocSearch {
4302
4324
const pushTo = crate === currentCrate ? crateAliases : aliases ;
4303
4325
const query_aliases = crateAliasesIndex . get ( lowerQuery ) ;
4304
4326
for ( const alias of query_aliases ) {
4305
- pushTo . push ( createAliasFromItem ( this . searchIndex [ alias ] ) ) ;
4327
+ pushTo . push ( alias ) ;
4306
4328
}
4307
4329
}
4308
4330
}
4309
4331
}
4310
4332
4311
4333
// @ts -expect-error
4312
4334
const sortFunc = ( aaa , bbb ) => {
4313
- if ( aaa . path < bbb . path ) {
4335
+ if ( aaa . original . path < bbb . original . path ) {
4314
4336
return 1 ;
4315
- } else if ( aaa . path === bbb . path ) {
4337
+ } else if ( aaa . original . path === bbb . original . path ) {
4316
4338
return 0 ;
4317
4339
}
4318
4340
return - 1 ;
@@ -4321,21 +4343,10 @@ class DocSearch {
4321
4343
crateAliases . sort ( sortFunc ) ;
4322
4344
aliases . sort ( sortFunc ) ;
4323
4345
4324
- // @ts -expect-error
4325
- const fetchDesc = alias => {
4326
- // @ts -expect-error
4327
- return this . searchIndexEmptyDesc . get ( alias . crate ) . contains ( alias . bitIndex ) ?
4328
- "" : this . searchState . loadDesc ( alias ) ;
4329
- } ;
4330
- const [ crateDescs , descs ] = await Promise . all ( [
4331
- // @ts -expect-error
4332
- Promise . all ( crateAliases . map ( fetchDesc ) ) ,
4333
- Promise . all ( aliases . map ( fetchDesc ) ) ,
4334
- ] ) ;
4335
-
4336
4346
// @ts -expect-error
4337
4347
const pushFunc = alias => {
4338
- alias . alias = query ;
4348
+ // Cloning `alias` to prevent its fields to be updated.
4349
+ alias = { ...alias } ;
4339
4350
const res = buildHrefAndPath ( alias ) ;
4340
4351
alias . displayPath = pathSplitter ( res [ 0 ] ) ;
4341
4352
alias . fullPath = alias . displayPath + alias . name ;
@@ -4347,16 +4358,8 @@ class DocSearch {
4347
4358
}
4348
4359
} ;
4349
4360
4350
- aliases . forEach ( ( alias , i ) => {
4351
- // @ts -expect-error
4352
- alias . desc = descs [ i ] ;
4353
- } ) ;
4354
4361
aliases . forEach ( pushFunc ) ;
4355
4362
// @ts -expect-error
4356
- crateAliases . forEach ( ( alias , i ) => {
4357
- alias . desc = crateDescs [ i ] ;
4358
- } ) ;
4359
- // @ts -expect-error
4360
4363
crateAliases . forEach ( pushFunc ) ;
4361
4364
} ;
4362
4365
@@ -4802,7 +4805,7 @@ async function addTab(array, query, display) {
4802
4805
output . className = "search-results " + extraClass ;
4803
4806
4804
4807
const lis = Promise . all ( array . map ( async item => {
4805
- const name = item . name ;
4808
+ const name = item . is_alias ? item . original . name : item . name ;
4806
4809
const type = itemTypes [ item . ty ] ;
4807
4810
const longType = longItemTypes [ item . ty ] ;
4808
4811
const typeName = longType . length !== 0 ? `${ longType } ` : "?" ;
@@ -4822,7 +4825,7 @@ async function addTab(array, query, display) {
4822
4825
let alias = " " ;
4823
4826
if ( item . is_alias ) {
4824
4827
alias = ` <div class="alias">\
4825
- <b>${ item . alias } </b><i class="grey"> - see </i>\
4828
+ <b>${ item . name } </b><i class="grey"> - see </i>\
4826
4829
</div>` ;
4827
4830
}
4828
4831
resultName . insertAdjacentHTML (
@@ -5201,6 +5204,7 @@ function registerSearchEvents() {
5201
5204
if ( searchState . input . value . length === 0 ) {
5202
5205
searchState . hideResults ( ) ;
5203
5206
} else {
5207
+ // @ts -ignore
5204
5208
searchState . timeout = setTimeout ( search , 500 ) ;
5205
5209
}
5206
5210
} ;
@@ -5842,8 +5846,8 @@ Lev1TParametricDescription.prototype.offsetIncrs3 = /*2 bits per value */ new In
5842
5846
// be called ONLY when the whole file has been parsed and loaded.
5843
5847
5844
5848
// @ts -expect-error
5845
- function initSearch ( searchIndx ) {
5846
- rawSearchIndex = searchIndx ;
5849
+ function initSearch ( searchIndex ) {
5850
+ rawSearchIndex = searchIndex ;
5847
5851
if ( typeof window !== "undefined" ) {
5848
5852
// @ts -expect-error
5849
5853
docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
0 commit comments