Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,13 @@ fn expected_type_and_name<'db>(
.map(|c| (Some(c.return_type()), None))
.unwrap_or((None, None))
},
ast::ParamList(_) => (None, None),
ast::ParamList(it) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it inside a ParamList? Couldn't this degrade completions for some other cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it inside a ParamList?

When completing || $0, the token is | and token.parent() is the ParamList

Couldn't this degrade completions for some other cases?

The difference should only be when ParamList is used as a closure parameter, because .and_then(ast::ClosureExpr::cast)

let closure = it.syntax().parent().and_then(ast::ClosureExpr::cast);
let ty = closure.and_then(|it| sema.type_of_expr(&it.into()));
ty.and_then(|ty| ty.original.as_callable(sema.db))
.map(|c| (Some(c.return_type()), None))
.unwrap_or((None, None))
},
ast::Stmt(_) => (None, None),
ast::Item(_) => (None, None),
_ => {
Expand Down Expand Up @@ -1938,6 +1944,7 @@ fn prev_special_biased_token_at_trivia(mut token: SyntaxToken) -> SyntaxToken {
| T![|=]
| T![&=]
| T![^=]
| T![|]
| T![return]
| T![break]
| T![continue] = prev.kind()
Expand Down
13 changes: 12 additions & 1 deletion crates/ide-completion/src/context/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,25 @@ fn foo() -> u32 {

#[test]
fn expected_type_closure_param_return() {
// FIXME: make this work with `|| $0`
check_expected_type_and_name(
r#"
//- minicore: fn
fn foo() {
bar(|| a$0);
}

fn bar(f: impl FnOnce() -> u32) {}
"#,
expect![[r#"ty: u32, name: ?"#]],
);

check_expected_type_and_name(
r#"
//- minicore: fn
fn foo() {
bar(|| $0);
}

fn bar(f: impl FnOnce() -> u32) {}
"#,
expect![[r#"ty: u32, name: ?"#]],
Expand Down