mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Fix active parameter analysis once more
This commit is contained in:
parent
d2cf8c234a
commit
9738f97f8c
2 changed files with 44 additions and 32 deletions
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{Semantics, Type};
|
use hir::{Semantics, Type};
|
||||||
|
use parser::T;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::non_trivia_sibling,
|
|
||||||
ast::{self, HasArgList, HasName},
|
ast::{self, HasArgList, HasName},
|
||||||
AstNode, Direction, SyntaxToken, TextRange,
|
AstNode, NodeOrToken, SyntaxToken,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::RootDatabase;
|
use crate::RootDatabase;
|
||||||
|
@ -59,7 +59,7 @@ pub fn callable_for_node(
|
||||||
calling_node: &ast::CallableExpr,
|
calling_node: &ast::CallableExpr,
|
||||||
token: &SyntaxToken,
|
token: &SyntaxToken,
|
||||||
) -> Option<(hir::Callable, Option<usize>)> {
|
) -> Option<(hir::Callable, Option<usize>)> {
|
||||||
let callable = match &calling_node {
|
let callable = match calling_node {
|
||||||
ast::CallableExpr::Call(call) => {
|
ast::CallableExpr::Call(call) => {
|
||||||
let expr = call.expr()?;
|
let expr = call.expr()?;
|
||||||
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
|
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
|
||||||
|
@ -67,24 +67,15 @@ pub fn callable_for_node(
|
||||||
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
|
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
|
||||||
}?;
|
}?;
|
||||||
let active_param = if let Some(arg_list) = calling_node.arg_list() {
|
let active_param = if let Some(arg_list) = calling_node.arg_list() {
|
||||||
let account_for_ws = |arg: &ast::Expr| {
|
Some(
|
||||||
let node = arg.syntax().clone();
|
|
||||||
let left = non_trivia_sibling(node.clone().into(), Direction::Prev)
|
|
||||||
.and_then(|it| it.into_token())?
|
|
||||||
.text_range();
|
|
||||||
let right = non_trivia_sibling(node.into(), Direction::Next)
|
|
||||||
.and_then(|it| it.into_token())?
|
|
||||||
.text_range();
|
|
||||||
Some(TextRange::new(left.end(), right.start()))
|
|
||||||
};
|
|
||||||
arg_list
|
arg_list
|
||||||
.args()
|
.syntax()
|
||||||
.position(|arg| {
|
.children_with_tokens()
|
||||||
account_for_ws(&arg)
|
.filter_map(NodeOrToken::into_token)
|
||||||
.unwrap_or(arg.syntax().text_range())
|
.filter(|t| t.kind() == T![,])
|
||||||
.contains(token.text_range().start())
|
.take_while(|t| t.text_range().start() <= token.text_range().start())
|
||||||
})
|
.count(),
|
||||||
.or(Some(0))
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,7 +12,7 @@ use stdx::format_to;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo,
|
algo,
|
||||||
ast::{self, HasArgList},
|
ast::{self, HasArgList},
|
||||||
match_ast, AstNode, Direction, SyntaxKind, SyntaxToken, TextRange, TextSize,
|
match_ast, AstNode, Direction, SyntaxToken, TextRange, TextSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::RootDatabase;
|
use crate::RootDatabase;
|
||||||
|
@ -105,10 +105,10 @@ pub(crate) fn signature_help(db: &RootDatabase, position: FilePosition) -> Optio
|
||||||
// Stop at multi-line expressions, since the signature of the outer call is not very
|
// Stop at multi-line expressions, since the signature of the outer call is not very
|
||||||
// helpful inside them.
|
// helpful inside them.
|
||||||
if let Some(expr) = ast::Expr::cast(node.clone()) {
|
if let Some(expr) = ast::Expr::cast(node.clone()) {
|
||||||
if expr.syntax().text().contains_char('\n')
|
if !matches!(expr, ast::Expr::RecordExpr(..))
|
||||||
&& expr.syntax().kind() != SyntaxKind::RECORD_EXPR
|
&& expr.syntax().text().contains_char('\n')
|
||||||
{
|
{
|
||||||
return None;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,18 +122,16 @@ fn signature_help_for_call(
|
||||||
token: SyntaxToken,
|
token: SyntaxToken,
|
||||||
) -> Option<SignatureHelp> {
|
) -> Option<SignatureHelp> {
|
||||||
// Find the calling expression and its NameRef
|
// Find the calling expression and its NameRef
|
||||||
let mut node = arg_list.syntax().parent()?;
|
let mut nodes = arg_list.syntax().ancestors().skip(1);
|
||||||
let calling_node = loop {
|
let calling_node = loop {
|
||||||
if let Some(callable) = ast::CallableExpr::cast(node.clone()) {
|
if let Some(callable) = ast::CallableExpr::cast(nodes.next()?) {
|
||||||
if callable
|
let inside_callable = callable
|
||||||
.arg_list()
|
.arg_list()
|
||||||
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()))
|
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()));
|
||||||
{
|
if inside_callable {
|
||||||
break callable;
|
break callable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node = node.parent()?;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (callable, active_parameter) = callable_for_node(sema, &calling_node, &token)?;
|
let (callable, active_parameter) = callable_for_node(sema, &calling_node, &token)?;
|
||||||
|
@ -1594,4 +1592,27 @@ impl S {
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_enum_in_nested_method_in_lambda() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
enum A {
|
||||||
|
A,
|
||||||
|
B
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar(_: A) { }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let foo = Foo;
|
||||||
|
std::thread::spawn(move || { bar(A:$0) } );
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
fn bar(_: A)
|
||||||
|
^^^^
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue