Skip to content

Commit b603303

Browse files
committed
chore: tidy up
1 parent a629d6f commit b603303

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

crates/ide-completion/src/render.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ fn main() {
20422042
}
20432043

20442044
#[test]
2045-
fn colon_complete_preferred_order_relevances() {
2045+
fn associated_fn_relevanes() {
20462046
check_relevance(
20472047
r#"
20482048
struct AStruct;
@@ -2078,29 +2078,36 @@ fn test() {
20782078
"#]],
20792079
);
20802080

2081-
// // Generic
2082-
// check_relevance(
2083-
// r#"
2084-
// struct A<T>{item: T}
2085-
// struct ABuilder;
2086-
// impl A {
2087-
// fn foo(&self) {}
2088-
// fn new_1<T>(input: u32, l: T) -> A<T> { A }
2089-
// fn new_2<T>() -> Self { A { item: <_>::default()} }
2090-
// fn aaaabuilder<T>() -> ABuilder<T> { A }
2091-
// }
2092-
2093-
// fn test() {
2094-
// let a = A::$0;
2095-
// }
2096-
// "#,
2097-
// expect![[r#"
2098-
// fn new_2() [type_could_unify]
2099-
// fn aaaabuilder() [type_could_unify]
2100-
// fn new_1(…) [type_could_unify]
2101-
// me foo(…) [type_could_unify]
2102-
// "#]],
2103-
// );
2081+
// Generic
2082+
check_relevance(
2083+
r#"
2084+
struct AStruct<T: Default>(T);
2085+
struct AStructBuilder;
2086+
struct Res<T>(T);
2087+
2088+
impl<T: Default> AStruct<T> {
2089+
fn fn_no_ret(&self) {}
2090+
fn fn_ctr_with_args(input: T) -> AStruct<T> { AStruct(input) }
2091+
fn fn_direct_ctr() -> Self { AStruct(<_>::default()) }
2092+
fn fn_ctr() -> Res<Self> { Res(Self::fn_direct_ctr()) }
2093+
fn fn_other() -> Res<u32> { Res(0) }
2094+
fn fn_builder() -> AStructBuilder { AStructBuilder }
2095+
}
2096+
2097+
fn test() {
2098+
//let a : Res<AStruct<u32>> = self::AStruct::fn_ctr();
2099+
let a = self::AStruct::<u32>::$0;
2100+
}
2101+
"#,
2102+
expect![[r#"
2103+
fn fn_direct_ctr() [type_could_unify]
2104+
fn fn_ctr_with_args(…) [type_could_unify]
2105+
fn fn_builder() [type_could_unify]
2106+
fn fn_ctr() [type_could_unify]
2107+
fn fn_other() [type_could_unify]
2108+
me fn_no_ret(…) [type_could_unify]
2109+
"#]],
2110+
);
21042111
}
21052112

21062113
#[test]

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,25 @@ fn compute_associated_fn(
169169
return None;
170170
}
171171

172+
let has_args = !func.assoc_fn_params(db).is_empty();
172173
let ret_type = func.ret_type(db);
173174
let ret_unit_type = ret_type.is_unit();
174-
let self_ty = match func_kind {
175+
let self_type = match func_kind {
175176
FuncKind::Function(PathCompletionCtx {
176177
qualified: Qualified::With { path, .. }, ..
177-
}) => path.segment().map(|s| s.to_string()),
178+
}) => path.segment().and_then(|ps| ps.name_ref()).map(|n| n.to_string()),
178179
_ => None,
179-
};
180-
let (returns_self, returns_self_wrapped) = self_ty
181-
.map(|self_ty| {
180+
}; // self type without any generic args
181+
let (returns_self, returns_self_wrapped) = self_type
182+
.map(|self_type| {
182183
let ret_ty = ret_type.display(db).to_string();
183-
let ret_self_wrapped = ret_ty.contains(&format!("<{self_ty}>"));
184184

185-
(ret_ty == self_ty || ret_self_wrapped, ret_self_wrapped)
185+
// the second checks are needed when Self has generic args
186+
let ret_self_only = ret_ty == self_type || ret_ty.starts_with(&format!("{self_type}<"));
187+
let ret_self_wrapped = ret_ty.ends_with(&format!("<{self_type}>"))
188+
|| ret_ty.contains(&format!("<{self_type}<"));
189+
190+
(ret_self_only || ret_self_wrapped, ret_self_wrapped)
186191
})
187192
.unwrap_or_else(|| (false, false));
188193

@@ -191,22 +196,22 @@ fn compute_associated_fn(
191196
&& ctx.completion.expected_type.as_ref() == Some(&ret_type)
192197
{
193198
// impl Foo { fn baz(&self) -> u32 { 0 } }
194-
// let _: u32 = foo.$0; // baz is preffered as it returns expected u32
199+
// let _: u32 = foo.$0; // baz is preferred as it returns expected u32
195200
CompletionRelevanceAssociatedFnType::ReturnsExpectedType
196201
} else if ret_type.display(db).to_string().ends_with("Builder") {
197202
// -> [..]Builder
198203
CompletionRelevanceAssociatedFnType::Builder
199204
} else if returns_self_wrapped {
200-
// fn() -> Result<A>
205+
// fn([..]) -> Result<A>
201206
CompletionRelevanceAssociatedFnType::Constructor
202207
} else if returns_self {
203-
// fn() -> A
208+
// fn([..]) -> A
204209
CompletionRelevanceAssociatedFnType::DirectConstructor
205210
} else {
206211
CompletionRelevanceAssociatedFnType::Other
207212
};
208213

209-
Some(CompletionRelevanceAssociatedFn { ty, has_args: !func.assoc_fn_params(db).is_empty() })
214+
Some(CompletionRelevanceAssociatedFn { ty, has_args })
210215
}
211216

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

0 commit comments

Comments
 (0)