@@ -166,6 +166,8 @@ pub struct CompletionRelevance {
166
166
pub postfix_match : Option < CompletionRelevancePostfixMatch > ,
167
167
/// This is set for type inference results
168
168
pub is_definite : bool ,
169
+ /// This is set for items that are function (associated or method)
170
+ pub function : Option < CompletionRelevanceFn > ,
169
171
}
170
172
171
173
#[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
@@ -207,6 +209,24 @@ pub enum CompletionRelevancePostfixMatch {
207
209
Exact ,
208
210
}
209
211
212
+ #[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
213
+ pub struct CompletionRelevanceFn {
214
+ pub has_params : bool ,
215
+ pub has_self_param : bool ,
216
+ pub return_type : CompletionRelevanceReturnType ,
217
+ }
218
+
219
+ #[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
220
+ pub enum CompletionRelevanceReturnType {
221
+ Other ,
222
+ /// Returns the Self type of the impl/trait
223
+ DirectConstructor ,
224
+ /// Returns something that indirectly constructs the `Self` type of the impl/trait e.g. `Result<Self, ()>`, `Option<Self>`
225
+ Constructor ,
226
+ /// Returns a possible builder for the type
227
+ Builder ,
228
+ }
229
+
210
230
impl CompletionRelevance {
211
231
/// Provides a relevance score. Higher values are more relevant.
212
232
///
@@ -231,6 +251,7 @@ impl CompletionRelevance {
231
251
postfix_match,
232
252
is_definite,
233
253
is_item_from_notable_trait,
254
+ function,
234
255
} = self ;
235
256
236
257
// lower rank private things
@@ -275,6 +296,33 @@ impl CompletionRelevance {
275
296
if is_definite {
276
297
score += 10 ;
277
298
}
299
+
300
+ score += function
301
+ . map ( |asf| {
302
+ let mut fn_score = match asf. return_type {
303
+ CompletionRelevanceReturnType :: DirectConstructor => 15 ,
304
+ CompletionRelevanceReturnType :: Builder => 10 ,
305
+ CompletionRelevanceReturnType :: Constructor => 5 ,
306
+ CompletionRelevanceReturnType :: Other => 0 ,
307
+ } ;
308
+
309
+ // When a fn is bumped due to return type:
310
+ // Bump Constructor or Builder methods with no arguments,
311
+ // over them tha with self arguments
312
+ if fn_score > 0 {
313
+ if !asf. has_params {
314
+ // bump associated functions
315
+ fn_score += 1 ;
316
+ } else if asf. has_self_param {
317
+ // downgrade methods (below Constructor)
318
+ fn_score = 1 ;
319
+ }
320
+ }
321
+
322
+ fn_score
323
+ } )
324
+ . unwrap_or_default ( ) ;
325
+
278
326
score
279
327
}
280
328
0 commit comments