@@ -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<Array<any>> } */
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,61 @@ 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
+ // @ts -ignore
2305
+ is_alias : true ,
2306
+ ty : original . ty ,
2307
+ paramNames : [ ] ,
2308
+ word,
2309
+ id,
2310
+ parent : undefined ,
2311
+ original,
2312
+ path : "" ,
2313
+ // @ts -ignore
2314
+ implDisambiguator : original . implDisambiguator ,
2315
+ // Needed to load the description of the original item.
2316
+ // @ts -ignore
2317
+ descShard : original . descShard ,
2318
+ // @ts -ignore
2319
+ descIndex : original . descIndex ,
2320
+ // @ts -ignore
2321
+ bitIndex : original . bitIndex ,
2322
+ } ;
2323
+ aliases_map . push ( row ) ;
2324
+ this . nameTrie . insert ( normalizedName , id , this . tailTable ) ;
2325
+ id += 1 ;
2326
+ searchIndex . push ( row ) ;
2327
+ }
2328
+ }
2329
+ }
2295
2330
// Drop the (rather large) hash table used for reusing function items
2296
2331
this . TYPES_POOL = new Map ( ) ;
2297
2332
return searchIndex ;
@@ -2536,6 +2571,8 @@ class DocSearch {
2536
2571
parsedQuery . elems . reduce ( ( acc , next ) => acc + next . pathLast . length , 0 ) +
2537
2572
parsedQuery . returned . reduce ( ( acc , next ) => acc + next . pathLast . length , 0 ) ;
2538
2573
const maxEditDistance = Math . floor ( queryLen / 3 ) ;
2574
+ // We reinitialize the `FOUND_ALIASES` map.
2575
+ this . FOUND_ALIASES . clear ( ) ;
2539
2576
2540
2577
/**
2541
2578
* @type {Map<string, number> }
@@ -2695,6 +2732,10 @@ class DocSearch {
2695
2732
const buildHrefAndPath = item => {
2696
2733
let displayPath ;
2697
2734
let href ;
2735
+ if ( item . is_alias ) {
2736
+ this . FOUND_ALIASES . add ( item . word ) ;
2737
+ item = item . original ;
2738
+ }
2698
2739
const type = itemTypes [ item . ty ] ;
2699
2740
const name = item . name ;
2700
2741
let path = item . path ;
@@ -3198,8 +3239,7 @@ class DocSearch {
3198
3239
result . item = this . searchIndex [ result . id ] ;
3199
3240
result . word = this . searchIndex [ result . id ] . word ;
3200
3241
if ( isReturnTypeQuery ) {
3201
- // we are doing a return-type based search,
3202
- // deprioritize "clone-like" results,
3242
+ // We are doing a return-type based search, deprioritize "clone-like" results,
3203
3243
// ie. functions that also take the queried type as an argument.
3204
3244
const resultItemType = result . item && result . item . type ;
3205
3245
if ( ! resultItemType ) {
@@ -4259,28 +4299,13 @@ class DocSearch {
4259
4299
return false ;
4260
4300
}
4261
4301
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
4302
// @ts -expect-error
4282
4303
const handleAliases = async ( ret , query , filterCrates , currentCrate ) => {
4283
4304
const lowerQuery = query . toLowerCase ( ) ;
4305
+ if ( this . FOUND_ALIASES . has ( lowerQuery ) ) {
4306
+ return ;
4307
+ }
4308
+ this . FOUND_ALIASES . add ( lowerQuery ) ;
4284
4309
// We separate aliases and crate aliases because we want to have current crate
4285
4310
// aliases to be before the others in the displayed results.
4286
4311
// @ts -expect-error
@@ -4292,7 +4317,7 @@ class DocSearch {
4292
4317
&& this . ALIASES . get ( filterCrates ) . has ( lowerQuery ) ) {
4293
4318
const query_aliases = this . ALIASES . get ( filterCrates ) . get ( lowerQuery ) ;
4294
4319
for ( const alias of query_aliases ) {
4295
- aliases . push ( createAliasFromItem ( this . searchIndex [ alias ] ) ) ;
4320
+ aliases . push ( alias ) ;
4296
4321
}
4297
4322
}
4298
4323
} else {
@@ -4302,17 +4327,17 @@ class DocSearch {
4302
4327
const pushTo = crate === currentCrate ? crateAliases : aliases ;
4303
4328
const query_aliases = crateAliasesIndex . get ( lowerQuery ) ;
4304
4329
for ( const alias of query_aliases ) {
4305
- pushTo . push ( createAliasFromItem ( this . searchIndex [ alias ] ) ) ;
4330
+ pushTo . push ( alias ) ;
4306
4331
}
4307
4332
}
4308
4333
}
4309
4334
}
4310
4335
4311
4336
// @ts -expect-error
4312
4337
const sortFunc = ( aaa , bbb ) => {
4313
- if ( aaa . path < bbb . path ) {
4338
+ if ( aaa . original . path < bbb . original . path ) {
4314
4339
return 1 ;
4315
- } else if ( aaa . path === bbb . path ) {
4340
+ } else if ( aaa . original . path === bbb . original . path ) {
4316
4341
return 0 ;
4317
4342
}
4318
4343
return - 1 ;
@@ -4321,21 +4346,10 @@ class DocSearch {
4321
4346
crateAliases . sort ( sortFunc ) ;
4322
4347
aliases . sort ( sortFunc ) ;
4323
4348
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
4349
// @ts -expect-error
4337
4350
const pushFunc = alias => {
4338
- alias . alias = query ;
4351
+ // Cloning `alias` to prevent its fields to be updated.
4352
+ alias = { ...alias } ;
4339
4353
const res = buildHrefAndPath ( alias ) ;
4340
4354
alias . displayPath = pathSplitter ( res [ 0 ] ) ;
4341
4355
alias . fullPath = alias . displayPath + alias . name ;
@@ -4347,16 +4361,8 @@ class DocSearch {
4347
4361
}
4348
4362
} ;
4349
4363
4350
- aliases . forEach ( ( alias , i ) => {
4351
- // @ts -expect-error
4352
- alias . desc = descs [ i ] ;
4353
- } ) ;
4354
4364
aliases . forEach ( pushFunc ) ;
4355
4365
// @ts -expect-error
4356
- crateAliases . forEach ( ( alias , i ) => {
4357
- alias . desc = crateDescs [ i ] ;
4358
- } ) ;
4359
- // @ts -expect-error
4360
4366
crateAliases . forEach ( pushFunc ) ;
4361
4367
} ;
4362
4368
@@ -4802,7 +4808,7 @@ async function addTab(array, query, display) {
4802
4808
output . className = "search-results " + extraClass ;
4803
4809
4804
4810
const lis = Promise . all ( array . map ( async item => {
4805
- const name = item . name ;
4811
+ const name = item . is_alias ? item . original . name : item . name ;
4806
4812
const type = itemTypes [ item . ty ] ;
4807
4813
const longType = longItemTypes [ item . ty ] ;
4808
4814
const typeName = longType . length !== 0 ? `${ longType } ` : "?" ;
@@ -4822,7 +4828,7 @@ async function addTab(array, query, display) {
4822
4828
let alias = " " ;
4823
4829
if ( item . is_alias ) {
4824
4830
alias = ` <div class="alias">\
4825
- <b>${ item . alias } </b><i class="grey"> - see </i>\
4831
+ <b>${ item . name } </b><i class="grey"> - see </i>\
4826
4832
</div>` ;
4827
4833
}
4828
4834
resultName . insertAdjacentHTML (
@@ -5201,6 +5207,7 @@ function registerSearchEvents() {
5201
5207
if ( searchState . input . value . length === 0 ) {
5202
5208
searchState . hideResults ( ) ;
5203
5209
} else {
5210
+ // @ts -ignore
5204
5211
searchState . timeout = setTimeout ( search , 500 ) ;
5205
5212
}
5206
5213
} ;
@@ -5842,8 +5849,8 @@ Lev1TParametricDescription.prototype.offsetIncrs3 = /*2 bits per value */ new In
5842
5849
// be called ONLY when the whole file has been parsed and loaded.
5843
5850
5844
5851
// @ts -expect-error
5845
- function initSearch ( searchIndx ) {
5846
- rawSearchIndex = searchIndx ;
5852
+ function initSearch ( searchIndex ) {
5853
+ rawSearchIndex = searchIndex ;
5847
5854
if ( typeof window !== "undefined" ) {
5848
5855
// @ts -expect-error
5849
5856
docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
0 commit comments