@@ -63,6 +63,12 @@ const TY_PRIMITIVE = itemTypes.indexOf("primitive");
63
63
const TY_GENERIC = itemTypes . indexOf ( "generic" ) ;
64
64
const TY_IMPORT = itemTypes . indexOf ( "import" ) ;
65
65
const TY_TRAIT = itemTypes . indexOf ( "trait" ) ;
66
+ // used for isType
67
+ const TY_STRUCT = itemTypes . indexOf ( "struct" ) ;
68
+ const TY_ENUM = itemTypes . indexOf ( "enum" ) ;
69
+ const TY_UNION = itemTypes . indexOf ( "union" ) ;
70
+ const TY_PRIMATIVE = itemTypes . indexOf ( "primative" ) ;
71
+ const TY_FOREIGN_TYPE = itemTypes . indexOf ( "foreigntype" ) ;
66
72
const ROOT_PATH = typeof window !== "undefined" ? window . rootPath : "../" ;
67
73
68
74
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -250,6 +256,18 @@ function prevIs(parserState, lookingFor) {
250
256
return false ;
251
257
}
252
258
259
+ function isType ( ty ) {
260
+ return ty === TY_STRUCT || ty === TY_ENUM || ty === TY_UNION ||
261
+ ty === TY_PRIMATIVE || ty === TY_FOREIGN_TYPE || ty === - 1 ;
262
+ }
263
+
264
+ /**
265
+ * This function removes any queryElem that cannot be a function argument.
266
+ */
267
+ function filterOnlyTypes ( elems ) {
268
+ return elems . filter ( elem => isType ( elem . typeFilter ) ) ;
269
+ }
270
+
253
271
/**
254
272
* Returns `true` if the last element in the `elems` argument has generics.
255
273
*
@@ -1870,6 +1888,8 @@ class DocSearch {
1870
1888
correction : null ,
1871
1889
proposeCorrectionFrom : null ,
1872
1890
proposeCorrectionTo : null ,
1891
+ // used for type-and-name searches
1892
+ extraNameElem : null ,
1873
1893
// bloom filter build from type ids
1874
1894
typeFingerprint : new Uint32Array ( 4 ) ,
1875
1895
} ;
@@ -2002,6 +2022,17 @@ class DocSearch {
2002
2022
query . literalSearch = parserState . totalElems > 1 ;
2003
2023
}
2004
2024
query . foundElems = query . elems . length + query . returned . length ;
2025
+ if ( query . returned . length > 0 || query . elems . length > 1 ) {
2026
+ for ( const elem of query . elems ) {
2027
+ if ( ! isType ( elem . typeFilter ) ) {
2028
+ query . extraNameElem = elem ;
2029
+ query . elems = filterOnlyTypes ( query . elems ) ;
2030
+ query . hasReturnArrow = true ;
2031
+ console . log ( query . elems ) ;
2032
+ break ;
2033
+ }
2034
+ }
2035
+ }
2005
2036
query . totalElems = parserState . totalElems ;
2006
2037
return query ;
2007
2038
}
@@ -3687,7 +3718,7 @@ class DocSearch {
3687
3718
* @param {integer } pos - Position in the `searchIndex`.
3688
3719
* @param {Object } results
3689
3720
*/
3690
- function handleArgs ( row , pos , results ) {
3721
+ function handleArgs ( row , pos , results , maxEditDistance ) {
3691
3722
if ( ! row || ( filterCrates !== null && row . crate !== filterCrates ) || ! row . type ) {
3692
3723
return ;
3693
3724
}
@@ -3724,8 +3755,26 @@ class DocSearch {
3724
3755
return ;
3725
3756
}
3726
3757
3758
+ let name_dist = 0 ;
3759
+ let path_dist = tfpDist ;
3760
+ const name_elem = parsedQuery . extraNameElem ;
3761
+ if ( name_elem !== null ) {
3762
+ if ( ! typePassesFilter ( name_elem . typeFilter , row . ty ) ) {
3763
+ return ;
3764
+ }
3765
+ name_dist = editDistance (
3766
+ row . normalizedName , name_elem . normalizedPathLast , maxEditDistance ) ;
3767
+ if ( row . normalizedName . includes ( name_elem . normalizedPathLast ) ) {
3768
+ name_dist = name_dist / 3 ;
3769
+ }
3770
+ if ( name_dist > maxEditDistance ) {
3771
+ return ;
3772
+ }
3773
+ const real_path_dist = checkPath ( name_elem . fullPath , row ) ;
3774
+ path_dist = ( path_dist + real_path_dist ) / 2 ;
3775
+ }
3727
3776
results . max_dist = Math . max ( results . max_dist || 0 , tfpDist ) ;
3728
- addIntoResults ( results , row . id , pos , 0 , tfpDist , 0 , Number . MAX_VALUE ) ;
3777
+ addIntoResults ( results , row . id , pos , name_dist , path_dist , 0 , Number . MAX_VALUE ) ;
3729
3778
}
3730
3779
3731
3780
/**
@@ -3928,7 +3977,7 @@ class DocSearch {
3928
3977
parsedQuery . elems . sort ( sortQ ) ;
3929
3978
parsedQuery . returned . sort ( sortQ ) ;
3930
3979
for ( let i = 0 , nSearchIndex = this . searchIndex . length ; i < nSearchIndex ; ++ i ) {
3931
- handleArgs ( this . searchIndex [ i ] , i , results_others ) ;
3980
+ handleArgs ( this . searchIndex [ i ] , i , results_others , maxEditDistance ) ;
3932
3981
}
3933
3982
}
3934
3983
} ;
0 commit comments