Skip to content

Commit 1090876

Browse files
committed
tweak: prefer no self args
1 parent 5b9da60 commit 1090876

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
@@ -210,6 +210,7 @@ pub enum CompletionRelevancePostfixMatch {
210210
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
211211
pub struct CompletionRelevanceAssociatedFn {
212212
pub has_args: bool,
213+
pub has_self_arg: bool,
213214
pub ty: CompletionRelevanceAssociatedFnType,
214215
}
215216

@@ -293,13 +294,21 @@ impl CompletionRelevance {
293294
}
294295

295296
score += associated_fn
296-
.map(|asf| match asf.ty {
297-
CompletionRelevanceAssociatedFnType::ReturnsExpectedType => 20,
298-
CompletionRelevanceAssociatedFnType::DirectConstructor => 15,
299-
CompletionRelevanceAssociatedFnType::Builder => 10,
300-
CompletionRelevanceAssociatedFnType::Constructor => 5,
301-
CompletionRelevanceAssociatedFnType::Other => 0,
302-
} + if !asf.has_args { 5 } else { 0 })
297+
.map(|asf| {
298+
let mut score = match asf.ty {
299+
CompletionRelevanceAssociatedFnType::ReturnsExpectedType => 20,
300+
CompletionRelevanceAssociatedFnType::DirectConstructor => 15,
301+
CompletionRelevanceAssociatedFnType::Builder => 10,
302+
CompletionRelevanceAssociatedFnType::Constructor => 5,
303+
CompletionRelevanceAssociatedFnType::Other => 0,
304+
};
305+
306+
// Prefer functions with no arguments, then functions with self arguments
307+
score += if !asf.has_args { 2 } else { 0 };
308+
score -= if score > 0 && asf.has_self_arg { 1 } else { 0 };
309+
310+
score
311+
})
303312
.unwrap_or_default();
304313

305314
score

crates/ide-completion/src/render.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ impl S {
13301330
associated_fn: Some(
13311331
CompletionRelevanceAssociatedFn {
13321332
has_args: true,
1333+
has_self_arg: true,
13331334
ty: Other,
13341335
},
13351336
),
@@ -1453,6 +1454,7 @@ fn foo(s: S) { s.$0 }
14531454
associated_fn: Some(
14541455
CompletionRelevanceAssociatedFn {
14551456
has_args: true,
1457+
has_self_arg: true,
14561458
ty: Other,
14571459
},
14581460
),
@@ -2221,6 +2223,7 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
22212223
associated_fn: Some(
22222224
CompletionRelevanceAssociatedFn {
22232225
has_args: true,
2226+
has_self_arg: true,
22242227
ty: Other,
22252228
},
22262229
),
@@ -2350,6 +2353,7 @@ fn main() {
23502353
associated_fn: Some(
23512354
CompletionRelevanceAssociatedFn {
23522355
has_args: false,
2356+
has_self_arg: false,
23532357
ty: Other,
23542358
},
23552359
),

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

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

172172
let has_args = !func.assoc_fn_params(db).is_empty();
173173
let ret_type = func.ret_type(db);
174+
let has_self_arg = func.self_param(db).is_some();
174175
let ret_unit_type = ret_type.is_unit();
175176
let self_type = match func_kind {
176177
FuncKind::Function(PathCompletionCtx {
@@ -211,7 +212,7 @@ fn compute_associated_fn(
211212
CompletionRelevanceAssociatedFnType::Other
212213
};
213214

214-
Some(CompletionRelevanceAssociatedFn { ty, has_args })
215+
Some(CompletionRelevanceAssociatedFn { ty, has_args, has_self_arg })
215216
}
216217

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

0 commit comments

Comments
 (0)