@@ -52,6 +52,12 @@ const itemTypes = [
5252// used for special search precedence
5353const TY_GENERIC = itemTypes . indexOf ( "generic" ) ;
5454const 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" ) ;
5561const ROOT_PATH = typeof window !== "undefined" ? window . rootPath : "../" ;
5662
5763// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -239,6 +245,18 @@ function prevIs(parserState, lookingFor) {
239245 return false ;
240246}
241247
248+ function isType ( ty ) {
249+ let r = ty === TY_STRUCT || ty === TY_ENUM || ty === TY_UNION || ty === TY_PRIMATIVE || ty === TY_FOREIGN_TYPE || ty === - 1 ;
250+ return r ;
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+
242260/**
243261 * Returns `true` if the last element in the `elems` argument has generics.
244262 *
@@ -1800,6 +1818,8 @@ class DocSearch {
18001818 correction : null ,
18011819 proposeCorrectionFrom : null ,
18021820 proposeCorrectionTo : null ,
1821+ // used for type-and-name searches
1822+ extraNameElem : null ,
18031823 // bloom filter build from type ids
18041824 typeFingerprint : new Uint32Array ( 4 ) ,
18051825 } ;
@@ -1932,6 +1952,17 @@ class DocSearch {
19321952 query . literalSearch = parserState . totalElems > 1 ;
19331953 }
19341954 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+ }
19351966 query . totalElems = parserState . totalElems ;
19361967 return query ;
19371968 }
@@ -1946,6 +1977,7 @@ class DocSearch {
19461977 * @return {ResultsTable }
19471978 */
19481979 async execQuery ( parsedQuery , filterCrates , currentCrate ) {
1980+ console . log ( parsedQuery ) ;
19491981 const results_others = new Map ( ) , results_in_args = new Map ( ) ,
19501982 results_returned = new Map ( ) ;
19511983
@@ -3047,7 +3079,7 @@ class DocSearch {
30473079 * @param {integer } pos - Position in the `searchIndex`.
30483080 * @param {Object } results
30493081 */
3050- function handleArgs ( row , pos , results ) {
3082+ function handleArgs ( row , pos , results , maxEditDistance ) {
30513083 if ( ! row || ( filterCrates !== null && row . crate !== filterCrates ) || ! row . type ) {
30523084 return ;
30533085 }
@@ -3083,9 +3115,25 @@ class DocSearch {
30833115 ) ) {
30843116 return ;
30853117 }
3086-
3118+
3119+ let name_dist = 0 ;
3120+ let name_elem = parsedQuery . extraNameElem ;
3121+ let path_dist = tfpDist ;
3122+ if ( name_elem !== null ) {
3123+ if ( ! typePassesFilter ( name_elem . typeFilter , row . ty ) ) {
3124+ return ;
3125+ }
3126+ name_dist = editDistance ( 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+ path_dist = checkPath ( name_elem . fullPath , row ) ;
3134+ }
30873135 results . max_dist = Math . max ( results . max_dist || 0 , tfpDist ) ;
3088- addIntoResults ( results , row . id , pos , 0 , tfpDist , 0 , Number . MAX_VALUE ) ;
3136+ addIntoResults ( results , row . id , pos , name_dist , ( path_dist + tfpDist ) / 2 , 0 , Number . MAX_VALUE ) ;
30893137 }
30903138
30913139 /**
@@ -3285,7 +3333,7 @@ class DocSearch {
32853333 parsedQuery . elems . sort ( sortQ ) ;
32863334 parsedQuery . returned . sort ( sortQ ) ;
32873335 for ( let i = 0 , nSearchIndex = this . searchIndex . length ; i < nSearchIndex ; ++ i ) {
3288- handleArgs ( this . searchIndex [ i ] , i , results_others ) ;
3336+ handleArgs ( this . searchIndex [ i ] , i , results_others , maxEditDistance ) ;
32893337 }
32903338 }
32913339 } ;
0 commit comments