Skip to content

Commit 217363c

Browse files
committed
tweak: prefer no self args
1 parent bdc470c commit 217363c

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

crates/ide-completion/src/item.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub enum CompletionRelevancePostfixMatch {
212212
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
213213
pub struct CompletionRelevanceAssociatedFn {
214214
pub has_args: bool,
215+
pub has_self_arg: bool,
215216
pub ty: CompletionRelevanceAssociatedFnType,
216217
}
217218

@@ -299,13 +300,21 @@ impl CompletionRelevance {
299300
}
300301

301302
score += associated_fn
302-
.map(|asf| match asf.ty {
303-
CompletionRelevanceAssociatedFnType::ReturnsExpectedType => 20,
304-
CompletionRelevanceAssociatedFnType::DirectConstructor => 15,
305-
CompletionRelevanceAssociatedFnType::Builder => 10,
306-
CompletionRelevanceAssociatedFnType::Constructor => 5,
307-
CompletionRelevanceAssociatedFnType::Other => 0,
308-
} + if !asf.has_args { 5 } else { 0 })
303+
.map(|asf| {
304+
let mut score = match asf.ty {
305+
CompletionRelevanceAssociatedFnType::ReturnsExpectedType => 20,
306+
CompletionRelevanceAssociatedFnType::DirectConstructor => 15,
307+
CompletionRelevanceAssociatedFnType::Builder => 10,
308+
CompletionRelevanceAssociatedFnType::Constructor => 5,
309+
CompletionRelevanceAssociatedFnType::Other => 0,
310+
};
311+
312+
// Prefer functions with no arguments, then functions with self arguments
313+
score += if !asf.has_args { 2 } else { 0 };
314+
score -= if score > 0 && asf.has_self_arg { 1 } else { 0 };
315+
316+
score
317+
})
309318
.unwrap_or_default();
310319

311320
score

crates/ide-completion/src/render.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ impl S {
13331333
associated_fn: Some(
13341334
CompletionRelevanceAssociatedFn {
13351335
has_args: true,
1336+
has_self_arg: true,
13361337
ty: Other,
13371338
},
13381339
),
@@ -1456,6 +1457,7 @@ fn foo(s: S) { s.$0 }
14561457
associated_fn: Some(
14571458
CompletionRelevanceAssociatedFn {
14581459
has_args: true,
1460+
has_self_arg: true,
14591461
ty: Other,
14601462
},
14611463
),
@@ -2224,6 +2226,7 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
22242226
associated_fn: Some(
22252227
CompletionRelevanceAssociatedFn {
22262228
has_args: true,
2229+
has_self_arg: true,
22272230
ty: Other,
22282231
},
22292232
),
@@ -2354,6 +2357,7 @@ fn main() {
23542357
associated_fn: Some(
23552358
CompletionRelevanceAssociatedFn {
23562359
has_args: false,
2360+
has_self_arg: false,
23572361
ty: Other,
23582362
},
23592363
),

crates/ide-completion/src/render/function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ fn compute_associated_fn(
175175

176176
let has_args = !func.assoc_fn_params(db).is_empty();
177177
let ret_type = func.ret_type(db);
178+
let has_self_arg = func.self_param(db).is_some();
178179
let ret_unit_type = ret_type.is_unit();
179180
let self_type = match func_kind {
180181
FuncKind::Function(PathCompletionCtx {
@@ -215,7 +216,7 @@ fn compute_associated_fn(
215216
CompletionRelevanceAssociatedFnType::Other
216217
};
217218

218-
Some(CompletionRelevanceAssociatedFn { ty, has_args })
219+
Some(CompletionRelevanceAssociatedFn { ty, has_args, has_self_arg })
219220
}
220221

221222
pub(super) fn add_call_parens<'b>(

0 commit comments

Comments
 (0)