Skip to content

Commit 9345186

Browse files
committed
Refactor completion relevance scoring (pr suggestions)
1 parent d17235a commit 9345186

File tree

3 files changed

+20
-62
lines changed

3 files changed

+20
-62
lines changed

crates/ide-completion/src/item.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ pub struct CompletionRelevanceFn {
219219
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
220220
pub enum CompletionRelevanceFnType {
221221
Other,
222-
/// Returns a type that is expected in this context
223-
ReturnsExpectedType,
224222
/// Returns the Self type of the impl/trait
225223
DirectConstructor,
226224
/// Returns something that indirectly constructs the `Self` type of the impl/trait e.g. `Result<Self, ()>`, `Option<Self>`
@@ -253,7 +251,7 @@ impl CompletionRelevance {
253251
postfix_match,
254252
is_definite,
255253
is_item_from_notable_trait,
256-
function: associated_fn,
254+
function,
257255
} = self;
258256

259257
// lower rank private things
@@ -299,23 +297,29 @@ impl CompletionRelevance {
299297
score += 10;
300298
}
301299

302-
score += associated_fn
300+
score += function
303301
.map(|asf| {
304-
let mut score = match asf.ty {
305-
CompletionRelevanceFnType::ReturnsExpectedType => 20,
302+
let mut fn_score = match asf.ty {
306303
CompletionRelevanceFnType::DirectConstructor => 15,
307304
CompletionRelevanceFnType::Builder => 10,
308305
CompletionRelevanceFnType::Constructor => 5,
309306
CompletionRelevanceFnType::Other => 0,
310307
};
311308

312-
// Prefer functions with no arguments, then functions with self arguments
313-
if score > 0 {
314-
score += if !asf.has_args { 2 } else { 0 };
315-
score -= if asf.has_self_arg { score - 1 } else { 0 };
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_args {
314+
// bump associated functions
315+
fn_score += 1;
316+
} else if asf.has_self_arg {
317+
// downgrade methods (below Constructor)
318+
fn_score = 1;
319+
}
316320
}
317321

318-
score
322+
fn_score
319323
})
320324
.unwrap_or_default();
321325

crates/ide-completion/src/render.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,48 +2085,6 @@ fn test() {
20852085
"#]],
20862086
);
20872087

2088-
// Expected 1
2089-
check_relevance(
2090-
r#"
2091-
struct Random;
2092-
2093-
impl Random {
2094-
fn get_i32(&self) -> i32 {}
2095-
fn get_string(&self) -> String {}
2096-
}
2097-
2098-
fn test() {
2099-
let r = Random;
2100-
let name: String = r.$0;
2101-
}
2102-
"#,
2103-
expect![[r#"
2104-
me get_string() [type]
2105-
me get_i32() [type_could_unify]
2106-
"#]],
2107-
);
2108-
2109-
// Expected 2
2110-
check_relevance(
2111-
r#"
2112-
struct Random;
2113-
2114-
impl Random {
2115-
fn get_i32(&self) -> i32 {}
2116-
fn get_string(&self) -> String {}
2117-
}
2118-
2119-
fn age() -> i32 {
2120-
let r = Random;
2121-
r.$0
2122-
}
2123-
"#,
2124-
expect![[r#"
2125-
me get_i32() [type]
2126-
me get_string() [type_could_unify]
2127-
"#]],
2128-
);
2129-
21302088
// Generic 1
21312089
check_relevance(
21322090
r#"

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ fn compute_associated_fn(
173173
return None;
174174
}
175175

176-
let has_args = !func.assoc_fn_params(db).is_empty();
176+
let has_args = func.num_params(db) > 0;
177177
let ret_type = func.ret_type(db);
178178
let has_self_arg = func.self_param(db).is_some();
179-
let ret_unit_type = ret_type.is_unit();
180179
let self_type = match func_kind {
181180
FuncKind::Function(PathCompletionCtx {
182181
qualified: Qualified::With { path, .. }, ..
@@ -196,14 +195,11 @@ fn compute_associated_fn(
196195
})
197196
.unwrap_or_else(|| (false, false));
198197

199-
let ty = if !returns_self
200-
&& !ret_unit_type
201-
&& ctx.completion.expected_type.as_ref() == Some(&ret_type)
198+
let ty = if ret_type
199+
.as_adt()
200+
.and_then(|adt| adt.name(db).as_str().map(|name| name.ends_with("Builder")))
201+
.unwrap_or(false)
202202
{
203-
// impl Foo { fn baz(&self) -> u32 { 0 } }
204-
// let _: u32 = foo.$0; // baz is preferred as it returns expected u32
205-
CompletionRelevanceFnType::ReturnsExpectedType
206-
} else if ret_type.display(db).to_string().ends_with("Builder") {
207203
// fn([..]) -> [..]Builder
208204
CompletionRelevanceFnType::Builder
209205
} else if returns_self_wrapped {

0 commit comments

Comments
 (0)