Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ fn expected_type_and_name<'db>(
cov_mark::hit!(expected_type_fn_param);
ActiveParameter::at_token(
sema,
token.clone(),
token.clone(),
).map(|ap| {
let name = ap.ident().map(NameOrNameRef::Name);
(Some(ap.ty), name)
Expand Down
14 changes: 14 additions & 0 deletions crates/ide-completion/src/context/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ fn bar(x: u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
check_expected_type_and_name(
r#"
fn foo() { bar(, $0); }
fn bar(x: u32, y: i32) {}
"#,
expect![[r#"ty: i32, name: y"#]],
);
check_expected_type_and_name(
r#"
fn foo() { bar(, c$0); }
fn bar(x: u32, y: i32) {}
"#,
expect![[r#"ty: i32, name: y"#]],
);
}

#[test]
Expand Down
17 changes: 12 additions & 5 deletions crates/ide-db/src/active_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hir::{InFile, Semantics, Type};
use parser::T;
use span::TextSize;
use syntax::{
AstNode, NodeOrToken, SyntaxToken,
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
ast::{self, AstChildren, HasArgList, HasAttrs, HasName},
match_ast,
};
Expand Down Expand Up @@ -102,8 +102,7 @@ pub fn callable_for_node<'db>(
arg_list
.syntax()
.children_with_tokens()
.filter_map(NodeOrToken::into_token)
.filter(|t| t.kind() == T![,])
.filter_map(into_comma)
.take_while(|t| t.text_range().start() <= offset)
.count()
});
Expand Down Expand Up @@ -162,8 +161,7 @@ pub fn generic_def_for_node(
let active_param = generic_arg_list
.syntax()
.children_with_tokens()
.filter_map(NodeOrToken::into_token)
.filter(|t| t.kind() == T![,])
.filter_map(into_comma)
.take_while(|t| t.text_range().start() <= token.text_range().start())
.count();

Expand All @@ -174,3 +172,12 @@ pub fn generic_def_for_node(

Some((def, active_param, first_arg_is_non_lifetime, variant))
}

fn into_comma(it: NodeOrToken<SyntaxNode, SyntaxToken>) -> Option<SyntaxToken> {
let token = match it {
NodeOrToken::Token(it) => it,
NodeOrToken::Node(node) if node.kind() == SyntaxKind::ERROR => node.first_token()?,
NodeOrToken::Node(_) => return None,
};
(token.kind() == T![,]).then_some(token)
}