Skip to content

Commit 0061a7e

Browse files
committed
pr comments
1 parent 102938e commit 0061a7e

File tree

3 files changed

+38
-62
lines changed

3 files changed

+38
-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: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ impl S {
13241324
type_match: None,
13251325
is_local: false,
13261326
is_item_from_trait: false,
1327+
is_item_from_notable_trait: false,
13271328
is_name_already_imported: false,
13281329
requires_import: false,
13291330
is_op_method: false,
@@ -1448,6 +1449,7 @@ fn foo(s: S) { s.$0 }
14481449
type_match: None,
14491450
is_local: false,
14501451
is_item_from_trait: false,
1452+
is_item_from_notable_trait: false,
14511453
is_name_already_imported: false,
14521454
requires_import: false,
14531455
is_op_method: false,
@@ -2083,48 +2085,6 @@ fn test() {
20832085
"#]],
20842086
);
20852087

2086-
// Expected 1
2087-
check_relevance(
2088-
r#"
2089-
struct Random;
2090-
2091-
impl Random {
2092-
fn get_i32(&self) -> i32 {}
2093-
fn get_string(&self) -> String {}
2094-
}
2095-
2096-
fn test() {
2097-
let r = Random;
2098-
let name: String = r.$0;
2099-
}
2100-
"#,
2101-
expect![[r#"
2102-
me get_string() [type]
2103-
me get_i32() [type_could_unify]
2104-
"#]],
2105-
);
2106-
2107-
// Expected 2
2108-
check_relevance(
2109-
r#"
2110-
struct Random;
2111-
2112-
impl Random {
2113-
fn get_i32(&self) -> i32 {}
2114-
fn get_string(&self) -> String {}
2115-
}
2116-
2117-
fn age() -> i32 {
2118-
let r = Random;
2119-
r.$0
2120-
}
2121-
"#,
2122-
expect![[r#"
2123-
me get_i32() [type]
2124-
me get_string() [type_could_unify]
2125-
"#]],
2126-
);
2127-
21282088
// Generic 1
21292089
check_relevance(
21302090
r#"
@@ -2217,6 +2177,7 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
22172177
type_match: None,
22182178
is_local: false,
22192179
is_item_from_trait: false,
2180+
is_item_from_notable_trait: false,
22202181
is_name_already_imported: false,
22212182
requires_import: false,
22222183
is_op_method: false,
@@ -2348,6 +2309,7 @@ fn main() {
23482309
type_match: None,
23492310
is_local: false,
23502311
is_item_from_trait: false,
2312+
is_item_from_notable_trait: false,
23512313
is_name_already_imported: false,
23522314
requires_import: false,
23532315
is_op_method: false,
@@ -2718,6 +2680,13 @@ fn main() {
27182680
is_private_editable: false,
27192681
postfix_match: None,
27202682
is_definite: false,
2683+
function: Some(
2684+
CompletionRelevanceFn {
2685+
has_args: true,
2686+
has_self_arg: true,
2687+
ty: Other,
2688+
},
2689+
),
27212690
},
27222691
},
27232692
CompletionItem {
@@ -2740,6 +2709,13 @@ fn main() {
27402709
is_private_editable: false,
27412710
postfix_match: None,
27422711
is_definite: false,
2712+
function: Some(
2713+
CompletionRelevanceFn {
2714+
has_args: true,
2715+
has_self_arg: true,
2716+
ty: Other,
2717+
},
2718+
),
27432719
},
27442720
},
27452721
]

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)