2021-11-22 06:19:46 -06:00
|
|
|
//! This module provides functionality for querying callable information about a token.
|
|
|
|
|
|
|
|
use either::Either;
|
|
|
|
use hir::{Semantics, Type};
|
2023-02-12 10:25:32 -06:00
|
|
|
use parser::T;
|
2021-11-22 06:19:46 -06:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, HasArgList, HasName},
|
2023-02-12 10:25:32 -06:00
|
|
|
AstNode, NodeOrToken, SyntaxToken,
|
2021-11-22 06:19:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::RootDatabase;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ActiveParameter {
|
|
|
|
pub ty: Type,
|
2022-09-02 11:58:24 -05:00
|
|
|
pub pat: Option<Either<ast::SelfParam, ast::Pat>>,
|
2021-11-22 06:19:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveParameter {
|
|
|
|
/// Returns information about the call argument this token is part of.
|
2022-07-20 08:02:08 -05:00
|
|
|
pub fn at_token(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Self> {
|
2021-11-22 06:19:46 -06:00
|
|
|
let (signature, active_parameter) = callable_for_token(sema, token)?;
|
|
|
|
|
|
|
|
let idx = active_parameter?;
|
|
|
|
let mut params = signature.params(sema.db);
|
|
|
|
if !(idx < params.len()) {
|
|
|
|
cov_mark::hit!(too_many_arguments);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let (pat, ty) = params.swap_remove(idx);
|
2022-09-02 11:58:24 -05:00
|
|
|
Some(ActiveParameter { ty, pat })
|
2021-11-22 06:19:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ident(&self) -> Option<ast::Name> {
|
2022-09-02 11:58:24 -05:00
|
|
|
self.pat.as_ref().and_then(|param| match param {
|
|
|
|
Either::Right(ast::Pat::IdentPat(ident)) => ident.name(),
|
2021-11-22 06:19:46 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a [`hir::Callable`] this token is a part of and its argument index of said callable.
|
|
|
|
pub fn callable_for_token(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-11-22 06:19:46 -06:00
|
|
|
token: SyntaxToken,
|
|
|
|
) -> Option<(hir::Callable, Option<usize>)> {
|
2022-03-22 10:05:24 -05:00
|
|
|
// Find the calling expression and its NameRef
|
2021-11-22 06:19:46 -06:00
|
|
|
let parent = token.parent()?;
|
|
|
|
let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| {
|
|
|
|
it.arg_list()
|
|
|
|
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()))
|
|
|
|
})?;
|
|
|
|
|
2022-03-22 10:05:24 -05:00
|
|
|
callable_for_node(sema, &calling_node, &token)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn callable_for_node(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2022-03-22 10:05:24 -05:00
|
|
|
calling_node: &ast::CallableExpr,
|
|
|
|
token: &SyntaxToken,
|
|
|
|
) -> Option<(hir::Callable, Option<usize>)> {
|
2023-02-12 10:25:32 -06:00
|
|
|
let callable = match calling_node {
|
2021-11-22 06:19:46 -06:00
|
|
|
ast::CallableExpr::Call(call) => {
|
|
|
|
let expr = call.expr()?;
|
|
|
|
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
|
|
|
|
}
|
|
|
|
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
|
|
|
|
}?;
|
|
|
|
let active_param = if let Some(arg_list) = calling_node.arg_list() {
|
2023-02-12 10:25:32 -06:00
|
|
|
Some(
|
|
|
|
arg_list
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter_map(NodeOrToken::into_token)
|
|
|
|
.filter(|t| t.kind() == T![,])
|
|
|
|
.take_while(|t| t.text_range().start() <= token.text_range().start())
|
|
|
|
.count(),
|
|
|
|
)
|
2021-11-22 06:19:46 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
Some((callable, active_param))
|
|
|
|
}
|