@@ -52,6 +52,12 @@ const itemTypes = [
52
52
// used for special search precedence
53
53
const TY_GENERIC = itemTypes . indexOf ( "generic" ) ;
54
54
const TY_IMPORT = itemTypes . indexOf ( "import" ) ;
55
+ // used for isType
56
+ const TY_STRUCT = itemTypes . indexOf ( "struct" ) ;
57
+ const TY_ENUM = itemTypes . indexOf ( "enum" ) ;
58
+ const TY_UNION = itemTypes . indexOf ( "union" ) ;
59
+ const TY_PRIMATIVE = itemTypes . indexOf ( "primative" ) ;
60
+ const TY_FOREIGN_TYPE = itemTypes . indexOf ( "foreigntype" ) ;
55
61
const ROOT_PATH = typeof window !== "undefined" ? window . rootPath : "../" ;
56
62
57
63
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -239,6 +245,18 @@ function prevIs(parserState, lookingFor) {
239
245
return false ;
240
246
}
241
247
248
+ function isType ( ty ) {
249
+ return ty === TY_STRUCT || ty === TY_ENUM || ty === TY_UNION ||
250
+ ty === TY_PRIMATIVE || ty === TY_FOREIGN_TYPE || ty === - 1 ;
251
+ }
252
+
253
+ /**
254
+ * This function removes any queryElem that cannot be a function argument.
255
+ */
256
+ function filterOnlyTypes ( elems ) {
257
+ return elems . filter ( elem => isType ( elem . typeFilter ) ) ;
258
+ }
259
+
242
260
/**
243
261
* Returns `true` if the last element in the `elems` argument has generics.
244
262
*
@@ -1800,6 +1818,8 @@ class DocSearch {
1800
1818
correction : null ,
1801
1819
proposeCorrectionFrom : null ,
1802
1820
proposeCorrectionTo : null ,
1821
+ // used for type-and-name searches
1822
+ extraNameElem : null ,
1803
1823
// bloom filter build from type ids
1804
1824
typeFingerprint : new Uint32Array ( 4 ) ,
1805
1825
} ;
@@ -1932,6 +1952,17 @@ class DocSearch {
1932
1952
query . literalSearch = parserState . totalElems > 1 ;
1933
1953
}
1934
1954
query . foundElems = query . elems . length + query . returned . length ;
1955
+ if ( query . returned . length > 0 || query . elems . length > 1 ) {
1956
+ for ( const elem of query . elems ) {
1957
+ if ( ! isType ( elem . typeFilter ) ) {
1958
+ query . extraNameElem = elem ;
1959
+ query . elems = filterOnlyTypes ( query . elems ) ;
1960
+ query . hasReturnArrow = true ;
1961
+ console . log ( query . elems ) ;
1962
+ break ;
1963
+ }
1964
+ }
1965
+ }
1935
1966
query . totalElems = parserState . totalElems ;
1936
1967
return query ;
1937
1968
}
@@ -3047,7 +3078,7 @@ class DocSearch {
3047
3078
* @param {integer } pos - Position in the `searchIndex`.
3048
3079
* @param {Object } results
3049
3080
*/
3050
- function handleArgs ( row , pos , results ) {
3081
+ function handleArgs ( row , pos , results , maxEditDistance ) {
3051
3082
if ( ! row || ( filterCrates !== null && row . crate !== filterCrates ) || ! row . type ) {
3052
3083
return ;
3053
3084
}
@@ -3084,8 +3115,26 @@ class DocSearch {
3084
3115
return ;
3085
3116
}
3086
3117
3118
+ let name_dist = 0 ;
3119
+ let path_dist = tfpDist ;
3120
+ const name_elem = parsedQuery . extraNameElem ;
3121
+ if ( name_elem !== null ) {
3122
+ if ( ! typePassesFilter ( name_elem . typeFilter , row . ty ) ) {
3123
+ return ;
3124
+ }
3125
+ name_dist = editDistance (
3126
+ row . normalizedName , name_elem . normalizedPathLast , maxEditDistance ) ;
3127
+ if ( row . normalizedName . includes ( name_elem . normalizedPathLast ) ) {
3128
+ name_dist = name_dist / 3 ;
3129
+ }
3130
+ if ( name_dist > maxEditDistance ) {
3131
+ return ;
3132
+ }
3133
+ const real_path_dist = checkPath ( name_elem . fullPath , row ) ;
3134
+ path_dist = ( path_dist + real_path_dist ) / 2 ;
3135
+ }
3087
3136
results . max_dist = Math . max ( results . max_dist || 0 , tfpDist ) ;
3088
- addIntoResults ( results , row . id , pos , 0 , tfpDist , 0 , Number . MAX_VALUE ) ;
3137
+ addIntoResults ( results , row . id , pos , name_dist , path_dist , 0 , Number . MAX_VALUE ) ;
3089
3138
}
3090
3139
3091
3140
/**
@@ -3285,7 +3334,7 @@ class DocSearch {
3285
3334
parsedQuery . elems . sort ( sortQ ) ;
3286
3335
parsedQuery . returned . sort ( sortQ ) ;
3287
3336
for ( let i = 0 , nSearchIndex = this . searchIndex . length ; i < nSearchIndex ; ++ i ) {
3288
- handleArgs ( this . searchIndex [ i ] , i , results_others ) ;
3337
+ handleArgs ( this . searchIndex [ i ] , i , results_others , maxEditDistance ) ;
3289
3338
}
3290
3339
}
3291
3340
} ;
0 commit comments