Skip to content

Commit 5164917

Browse files
committed
chore: tidy up
1 parent 3e4f969 commit 5164917

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
@@ -2045,7 +2045,7 @@ fn main() {
20452045
}
20462046

20472047
#[test]
2048-
fn colon_complete_preferred_order_relevances() {
2048+
fn associated_fn_relevanes() {
20492049
check_relevance(
20502050
r#"
20512051
struct AStruct;
@@ -2081,29 +2081,36 @@ fn test() {
20812081
"#]],
20822082
);
20832083

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

21092116
#[test]

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,25 @@ fn compute_associated_fn(
173173
return None;
174174
}
175175

176+
let has_args = !func.assoc_fn_params(db).is_empty();
176177
let ret_type = func.ret_type(db);
177178
let ret_unit_type = ret_type.is_unit();
178-
let self_ty = match func_kind {
179+
let self_type = match func_kind {
179180
FuncKind::Function(PathCompletionCtx {
180181
qualified: Qualified::With { path, .. }, ..
181-
}) => path.segment().map(|s| s.to_string()),
182+
}) => path.segment().and_then(|ps| ps.name_ref()).map(|n| n.to_string()),
182183
_ => None,
183-
};
184-
let (returns_self, returns_self_wrapped) = self_ty
185-
.map(|self_ty| {
184+
}; // self type without any generic args
185+
let (returns_self, returns_self_wrapped) = self_type
186+
.map(|self_type| {
186187
let ret_ty = ret_type.display(db).to_string();
187-
let ret_self_wrapped = ret_ty.contains(&format!("<{self_ty}>"));
188188

189-
(ret_ty == self_ty || ret_self_wrapped, ret_self_wrapped)
189+
// the second checks are needed when Self has generic args
190+
let ret_self_only = ret_ty == self_type || ret_ty.starts_with(&format!("{self_type}<"));
191+
let ret_self_wrapped = ret_ty.ends_with(&format!("<{self_type}>"))
192+
|| ret_ty.contains(&format!("<{self_type}<"));
193+
194+
(ret_self_only || ret_self_wrapped, ret_self_wrapped)
190195
})
191196
.unwrap_or_else(|| (false, false));
192197

@@ -195,22 +200,22 @@ fn compute_associated_fn(
195200
&& ctx.completion.expected_type.as_ref() == Some(&ret_type)
196201
{
197202
// impl Foo { fn baz(&self) -> u32 { 0 } }
198-
// let _: u32 = foo.$0; // baz is preffered as it returns expected u32
203+
// let _: u32 = foo.$0; // baz is preferred as it returns expected u32
199204
CompletionRelevanceAssociatedFnType::ReturnsExpectedType
200205
} else if ret_type.display(db).to_string().ends_with("Builder") {
201206
// -> [..]Builder
202207
CompletionRelevanceAssociatedFnType::Builder
203208
} else if returns_self_wrapped {
204-
// fn() -> Result<A>
209+
// fn([..]) -> Result<A>
205210
CompletionRelevanceAssociatedFnType::Constructor
206211
} else if returns_self {
207-
// fn() -> A
212+
// fn([..]) -> A
208213
CompletionRelevanceAssociatedFnType::DirectConstructor
209214
} else {
210215
CompletionRelevanceAssociatedFnType::Other
211216
};
212217

213-
Some(CompletionRelevanceAssociatedFn { ty, has_args: !func.assoc_fn_params(db).is_empty() })
218+
Some(CompletionRelevanceAssociatedFn { ty, has_args })
214219
}
215220

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

0 commit comments

Comments
 (0)