Skip to content

Commit 02fb433

Browse files
committed
Fix active parameter analysis once more
1 parent d2cf8c2 commit 02fb433

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

crates/ide-db/src/active_parameter.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use either::Either;
44
use hir::{Semantics, Type};
5+
use parser::T;
56
use syntax::{
6-
algo::non_trivia_sibling,
77
ast::{self, HasArgList, HasName},
8-
AstNode, Direction, SyntaxToken, TextRange,
8+
AstNode, NodeOrToken, SyntaxToken,
99
};
1010

1111
use crate::RootDatabase;
@@ -59,32 +59,23 @@ pub fn callable_for_node(
5959
calling_node: &ast::CallableExpr,
6060
token: &SyntaxToken,
6161
) -> Option<(hir::Callable, Option<usize>)> {
62-
let callable = match &calling_node {
62+
let callable = match calling_node {
6363
ast::CallableExpr::Call(call) => {
6464
let expr = call.expr()?;
6565
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
6666
}
6767
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
6868
}?;
6969
let active_param = if let Some(arg_list) = calling_node.arg_list() {
70-
let account_for_ws = |arg: &ast::Expr| {
71-
let node = arg.syntax().clone();
72-
let left = non_trivia_sibling(node.clone().into(), Direction::Prev)
73-
.and_then(|it| it.into_token())?
74-
.text_range();
75-
let right = non_trivia_sibling(node.into(), Direction::Next)
76-
.and_then(|it| it.into_token())?
77-
.text_range();
78-
Some(TextRange::new(left.end(), right.start()))
79-
};
80-
arg_list
81-
.args()
82-
.position(|arg| {
83-
account_for_ws(&arg)
84-
.unwrap_or(arg.syntax().text_range())
85-
.contains(token.text_range().start())
86-
})
87-
.or(Some(0))
70+
Some(
71+
arg_list
72+
.syntax()
73+
.children_with_tokens()
74+
.filter_map(NodeOrToken::into_token)
75+
.filter(|t| t.kind() == T![,])
76+
.take_while(|t| t.text_range().start() <= token.text_range().start())
77+
.count(),
78+
)
8879
} else {
8980
None
9081
};

crates/ide/src/signature_help.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ pub(crate) fn signature_help(db: &RootDatabase, position: FilePosition) -> Optio
105105
// Stop at multi-line expressions, since the signature of the outer call is not very
106106
// helpful inside them.
107107
if let Some(expr) = ast::Expr::cast(node.clone()) {
108-
if expr.syntax().text().contains_char('\n')
109-
&& expr.syntax().kind() != SyntaxKind::RECORD_EXPR
108+
if !matches!(expr, ast::Expr::RecordExpr(..))
109+
&& expr.syntax().text().contains_char('\n')
110110
{
111-
return None;
111+
break;
112112
}
113113
}
114114
}
@@ -122,18 +122,16 @@ fn signature_help_for_call(
122122
token: SyntaxToken,
123123
) -> Option<SignatureHelp> {
124124
// Find the calling expression and its NameRef
125-
let mut node = arg_list.syntax().parent()?;
125+
let mut nodes = arg_list.syntax().ancestors().skip(1);
126126
let calling_node = loop {
127-
if let Some(callable) = ast::CallableExpr::cast(node.clone()) {
128-
if callable
127+
if let Some(callable) = ast::CallableExpr::cast(nodes.next()?) {
128+
let inside_callable = callable
129129
.arg_list()
130-
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()))
131-
{
130+
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()));
131+
if inside_callable {
132132
break callable;
133133
}
134134
}
135-
136-
node = node.parent()?;
137135
};
138136

139137
let (callable, active_parameter) = callable_for_node(sema, &calling_node, &token)?;
@@ -1594,4 +1592,27 @@ impl S {
15941592
"#]],
15951593
);
15961594
}
1595+
1596+
#[test]
1597+
fn test_enum_in_nested_method_in_lambda() {
1598+
check(
1599+
r#"
1600+
enum A {
1601+
A,
1602+
B
1603+
}
1604+
1605+
fn bar(_: A) { }
1606+
1607+
fn main() {
1608+
let foo = Foo;
1609+
std::thread::spawn(move || { bar(A:$0) } );
1610+
}
1611+
"#,
1612+
expect![[r#"
1613+
fn bar(_: A)
1614+
^^^^
1615+
"#]],
1616+
);
1617+
}
15971618
}

0 commit comments

Comments
 (0)