@@ -833,39 +833,52 @@ function defocusSearchBar() {
833
833
} ;
834
834
}
835
835
836
- function getObjectFromId ( id ) {
836
+ function getObjectNameFromId ( id ) {
837
837
if ( typeof id === "number" ) {
838
- return searchIndex [ id ] ;
838
+ return searchIndex [ id ] . name ;
839
839
}
840
- return { 'name' : id } ;
840
+ return id ;
841
841
}
842
842
843
843
function checkGenerics ( obj , val ) {
844
844
// The names match, but we need to be sure that all generics kinda
845
845
// match as well.
846
+ var tmp_lev , elem_name ;
846
847
if ( val . generics . length > 0 ) {
847
848
if ( obj . length > GENERICS_DATA &&
848
849
obj [ GENERICS_DATA ] . length >= val . generics . length ) {
849
- var elems = obj [ GENERICS_DATA ] . slice ( 0 ) ;
850
+ var elems = Object . create ( null ) ;
851
+ var elength = object [ GENERICS_DATA ] . length ;
852
+ for ( var x = 0 ; x < elength ; ++ x ) {
853
+ elems [ getObjectNameFromId ( obj [ GENERICS_DATA ] [ x ] ) ] += 1 ;
854
+ }
850
855
var total = 0 ;
851
856
var done = 0 ;
852
857
// We need to find the type that matches the most to remove it in order
853
858
// to move forward.
854
859
var vlength = val . generics . length ;
855
- for ( var y = 0 ; y < vlength ; ++ y ) {
856
- var lev = { pos : - 1 , lev : MAX_LEV_DISTANCE + 1 } ;
857
- var firstGeneric = getObjectFromId ( val . generics [ y ] ) . name ;
858
- for ( var x = 0 , elength = elems . length ; x < elength ; ++ x ) {
859
- var tmp_lev = levenshtein ( getObjectFromId ( elems [ x ] ) . name ,
860
- firstGeneric ) ;
861
- if ( tmp_lev < lev . lev ) {
862
- lev . lev = tmp_lev ;
863
- lev . pos = x ;
860
+ for ( x = 0 ; x < vlength ; ++ x ) {
861
+ var lev = MAX_LEV_DISTANCE + 1 ;
862
+ var firstGeneric = getObjectNameFromId ( val . generics [ x ] ) ;
863
+ var match = null ;
864
+ if ( elems [ firstGeneric ] ) {
865
+ match = firstGeneric ;
866
+ lev = 0 ;
867
+ } else {
868
+ for ( elem_name in elems ) {
869
+ tmp_lev = levenshtein ( elem_name , firstGeneric ) ;
870
+ if ( tmp_lev < lev ) {
871
+ lev = tmp_lev ;
872
+ match = elem_name ;
873
+ }
864
874
}
865
875
}
866
- if ( lev . pos !== - 1 ) {
867
- elems . splice ( lev . pos , 1 ) ;
868
- total += lev . lev ;
876
+ if ( match !== null ) {
877
+ elems [ match ] -= 1 ;
878
+ if ( elems [ match ] == 0 ) {
879
+ delete elems [ match ] ;
880
+ }
881
+ total += lev ;
869
882
done += 1 ;
870
883
} else {
871
884
return MAX_LEV_DISTANCE + 1 ;
@@ -880,25 +893,27 @@ function defocusSearchBar() {
880
893
// Check for type name and type generics (if any).
881
894
function checkType ( obj , val , literalSearch ) {
882
895
var lev_distance = MAX_LEV_DISTANCE + 1 ;
883
- var len , x , y , e_len , firstGeneric ;
896
+ var len , x , firstGeneric ;
884
897
if ( obj [ NAME ] === val . name ) {
885
898
if ( literalSearch === true ) {
886
899
if ( val . generics && val . generics . length !== 0 ) {
887
900
if ( obj . length > GENERICS_DATA &&
888
901
obj [ GENERICS_DATA ] . length >= val . generics . length ) {
889
- var elems = obj [ GENERICS_DATA ] . slice ( 0 ) ;
890
- var allFound = true ;
902
+ var elems = Object . create ( null ) ;
903
+ len = obj [ GENERICS_DATA ] . length ;
904
+ for ( x = 0 ; x < len ; ++ x ) {
905
+ elems [ getObjectNameFromId ( obj [ GENERICS_DATA ] [ x ] ) ] += 1 ;
906
+ }
891
907
908
+ var allFound = true ;
892
909
len = val . generics . length ;
893
- for ( y = 0 ; allFound === true && y < len ; ++ y ) {
894
- allFound = false ;
895
- firstGeneric = getObjectFromId ( val . generics [ y ] ) . name ;
896
- e_len = elems . length ;
897
- for ( x = 0 ; allFound === false && x < e_len ; ++ x ) {
898
- allFound = getObjectFromId ( elems [ x ] ) . name === firstGeneric ;
899
- }
900
- if ( allFound === true ) {
901
- elems . splice ( x - 1 , 1 ) ;
910
+ for ( x = 0 ; x < len ; ++ x ) {
911
+ firstGeneric = getObjectNameFromId ( val . generics [ x ] ) ;
912
+ if ( elems [ firstGeneric ] ) {
913
+ elems [ firstGeneric ] -= 1 ;
914
+ } else {
915
+ allFound = false ;
916
+ break ;
902
917
}
903
918
}
904
919
if ( allFound === true ) {
@@ -1066,13 +1081,6 @@ function defocusSearchBar() {
1066
1081
return false ;
1067
1082
}
1068
1083
1069
- function generateId ( ty ) {
1070
- if ( ty . parent && ty . parent . name ) {
1071
- return itemTypes [ ty . ty ] + ty . path + ty . parent . name + ty . name ;
1072
- }
1073
- return itemTypes [ ty . ty ] + ty . path + ty . name ;
1074
- }
1075
-
1076
1084
function createAliasFromItem ( item ) {
1077
1085
return {
1078
1086
crate : item . crate ,
@@ -1158,7 +1166,7 @@ function defocusSearchBar() {
1158
1166
in_args = findArg ( searchIndex [ i ] , val , true , typeFilter ) ;
1159
1167
returned = checkReturned ( searchIndex [ i ] , val , true , typeFilter ) ;
1160
1168
ty = searchIndex [ i ] ;
1161
- fullId = generateId ( ty ) ;
1169
+ fullId = ty . id ;
1162
1170
1163
1171
if ( searchWords [ i ] === val . name
1164
1172
&& typePassesFilter ( typeFilter , searchIndex [ i ] . ty )
@@ -1208,7 +1216,7 @@ function defocusSearchBar() {
1208
1216
if ( ! type ) {
1209
1217
continue ;
1210
1218
}
1211
- fullId = generateId ( ty ) ;
1219
+ fullId = ty . id ;
1212
1220
1213
1221
returned = checkReturned ( ty , output , true , NO_TYPE_FILTER ) ;
1214
1222
if ( output . name === "*" || returned === true ) {
@@ -1292,15 +1300,15 @@ function defocusSearchBar() {
1292
1300
var index = - 1 ;
1293
1301
// we want lev results to go lower than others
1294
1302
lev = MAX_LEV_DISTANCE + 1 ;
1295
- fullId = generateId ( ty ) ;
1303
+ fullId = ty . id ;
1296
1304
1297
1305
if ( searchWords [ j ] . indexOf ( split [ i ] ) > - 1 ||
1298
1306
searchWords [ j ] . indexOf ( val ) > - 1 ||
1299
- searchWords [ j ] . replace ( / _ / g , "" ) . indexOf ( val ) > - 1 )
1307
+ ty . normalizedName . indexOf ( val ) > - 1 )
1300
1308
{
1301
1309
// filter type: ... queries
1302
1310
if ( typePassesFilter ( typeFilter , ty . ty ) && results [ fullId ] === undefined ) {
1303
- index = searchWords [ j ] . replace ( / _ / g , "" ) . indexOf ( val ) ;
1311
+ index = ty . normalizedName . indexOf ( val ) ;
1304
1312
}
1305
1313
}
1306
1314
if ( ( lev = levenshtein ( searchWords [ j ] , val ) ) <= MAX_LEV_DISTANCE ) {
@@ -1828,23 +1836,35 @@ function defocusSearchBar() {
1828
1836
function buildIndex ( rawSearchIndex ) {
1829
1837
searchIndex = [ ] ;
1830
1838
var searchWords = [ ] ;
1831
- var i ;
1839
+ var i , word ;
1832
1840
var currentIndex = 0 ;
1841
+ var id = 0 ;
1833
1842
1834
1843
for ( var crate in rawSearchIndex ) {
1835
1844
if ( ! hasOwnProperty ( rawSearchIndex , crate ) ) { continue ; }
1836
1845
1837
1846
var crateSize = 0 ;
1838
1847
1839
1848
searchWords . push ( crate ) ;
1840
- searchIndex . push ( {
1849
+ var normalizedName = crate . indexOf ( "_" ) === - 1
1850
+ ? crate
1851
+ : crate . replace ( / _ / g, "" ) ;
1852
+ // This object should have exactly the same set of fields as the "row"
1853
+ // object defined below. Your JavaScript runtime will thank you.
1854
+ // https://mathiasbynens.be/notes/shapes-ics
1855
+ var crateRow = {
1841
1856
crate : crate ,
1842
1857
ty : 1 , // == ExternCrate
1843
1858
name : crate ,
1844
1859
path : "" ,
1845
1860
desc : rawSearchIndex [ crate ] . doc ,
1861
+ parent : undefined ,
1846
1862
type : null ,
1847
- } ) ;
1863
+ id : id ,
1864
+ normalizedName : normalizedName ,
1865
+ } ;
1866
+ id += 1 ;
1867
+ searchIndex . push ( crateRow ) ;
1848
1868
currentIndex += 1 ;
1849
1869
1850
1870
// an array of (Number) item types
@@ -1882,6 +1902,18 @@ function defocusSearchBar() {
1882
1902
len = itemTypes . length ;
1883
1903
var lastPath = "" ;
1884
1904
for ( i = 0 ; i < len ; ++ i ) {
1905
+ // This object should have exactly the same set of fields as the "crateRow"
1906
+ // object defined above.
1907
+ if ( typeof itemNames [ i ] === "string" ) {
1908
+ word = itemNames [ i ] . toLowerCase ( ) ;
1909
+ searchWords . push ( word ) ;
1910
+ } else {
1911
+ word = "" ;
1912
+ searchWords . push ( "" ) ;
1913
+ }
1914
+ var normalizedName = word . indexOf ( "_" ) === - 1
1915
+ ? word
1916
+ : word . replace ( / _ / g, "" ) ;
1885
1917
var row = {
1886
1918
crate : crate ,
1887
1919
ty : itemTypes [ i ] ,
@@ -1890,14 +1922,11 @@ function defocusSearchBar() {
1890
1922
desc : itemDescs [ i ] ,
1891
1923
parent : itemParentIdxs [ i ] > 0 ? paths [ itemParentIdxs [ i ] - 1 ] : undefined ,
1892
1924
type : itemFunctionSearchTypes [ i ] ,
1925
+ id : id ,
1926
+ normalizedName : normalizedName ,
1893
1927
} ;
1928
+ id += 1 ;
1894
1929
searchIndex . push ( row ) ;
1895
- if ( typeof row . name === "string" ) {
1896
- var word = row . name . toLowerCase ( ) ;
1897
- searchWords . push ( word ) ;
1898
- } else {
1899
- searchWords . push ( "" ) ;
1900
- }
1901
1930
lastPath = row . path ;
1902
1931
crateSize += 1 ;
1903
1932
}
0 commit comments