Skip to content
Closed
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
15 changes: 12 additions & 3 deletions crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,10 @@ fn analyze<'db>(
/// Calculate the expected type and name of the cursor position.
fn expected_type_and_name<'db>(
sema: &Semantics<'db, RootDatabase>,
token: &SyntaxToken,
self_token: &SyntaxToken,
name_like: &ast::NameLike,
) -> (Option<Type<'db>>, Option<NameOrNameRef>) {
let token = prev_special_biased_token_at_trivia(token.clone());
let token = prev_special_biased_token_at_trivia(self_token.clone());
let mut node = match token.parent() {
Some(it) => it,
None => return (None, None),
Expand Down Expand Up @@ -756,7 +756,15 @@ 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)

Copy link
Contributor

Choose a reason for hiding this comment

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

But won't it also trigger when inside the param list, e.g. |$0| ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But won't it also trigger when inside the param list, e.g. |$0| ...?

Will expected_type_and_name be called when completing this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, not sure.

let closure = it.syntax().parent().and_then(ast::ClosureExpr::cast);
let ty = closure
.filter(|_| it.syntax().text_range().end() <= self_token.text_range().start())
.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 +1946,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
Loading