1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! This module provides functionality for querying callable information about a token.

use either::Either;
use hir::{InFile, Semantics, Type};
use parser::T;
use syntax::{
    ast::{self, HasArgList, HasName},
    match_ast, AstNode, NodeOrToken, SyntaxToken,
};

use crate::RootDatabase;

#[derive(Debug)]
pub struct ActiveParameter {
    pub ty: Type,
    pub src: Option<InFile<Either<ast::SelfParam, ast::Param>>>,
}

impl ActiveParameter {
    /// Returns information about the call argument this token is part of.
    pub fn at_token(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Self> {
        let (signature, active_parameter) = callable_for_token(sema, token)?;

        let idx = active_parameter?;
        let mut params = signature.params();
        if idx >= params.len() {
            cov_mark::hit!(too_many_arguments);
            return None;
        }
        let param = params.swap_remove(idx);
        Some(ActiveParameter { ty: param.ty().clone(), src: sema.source(param) })
    }

    pub fn ident(&self) -> Option<ast::Name> {
        self.src.as_ref().and_then(|param| match param.value.as_ref().right()?.pat()? {
            ast::Pat::IdentPat(ident) => ident.name(),
            _ => None,
        })
    }
}

/// Returns a [`hir::Callable`] this token is a part of and its argument index of said callable.
pub fn callable_for_token(
    sema: &Semantics<'_, RootDatabase>,
    token: SyntaxToken,
) -> Option<(hir::Callable, Option<usize>)> {
    // Find the calling expression and its NameRef
    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()))
    })?;

    callable_for_node(sema, &calling_node, &token)
}

pub fn callable_for_node(
    sema: &Semantics<'_, RootDatabase>,
    calling_node: &ast::CallableExpr,
    token: &SyntaxToken,
) -> Option<(hir::Callable, Option<usize>)> {
    let callable = match calling_node {
        ast::CallableExpr::Call(call) => sema.resolve_expr_as_callable(&call.expr()?),
        ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
    }?;
    let active_param = calling_node.arg_list().map(|arg_list| {
        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()
    });
    Some((callable, active_param))
}

pub fn generic_def_for_node(
    sema: &Semantics<'_, RootDatabase>,
    generic_arg_list: &ast::GenericArgList,
    token: &SyntaxToken,
) -> Option<(hir::GenericDef, usize, bool, Option<hir::Variant>)> {
    let parent = generic_arg_list.syntax().parent()?;
    let mut variant = None;
    let def = match_ast! {
        match parent {
            ast::PathSegment(ps) => {
                let res = sema.resolve_path(&ps.parent_path())?;
                let generic_def: hir::GenericDef = match res {
                    hir::PathResolution::Def(hir::ModuleDef::Adt(it)) => it.into(),
                    hir::PathResolution::Def(hir::ModuleDef::Function(it)) => it.into(),
                    hir::PathResolution::Def(hir::ModuleDef::Trait(it)) => it.into(),
                    hir::PathResolution::Def(hir::ModuleDef::TraitAlias(it)) => it.into(),
                    hir::PathResolution::Def(hir::ModuleDef::TypeAlias(it)) => it.into(),
                    hir::PathResolution::Def(hir::ModuleDef::Variant(it)) => {
                        variant = Some(it);
                        it.parent_enum(sema.db).into()
                    },
                    hir::PathResolution::Def(hir::ModuleDef::BuiltinType(_))
                    | hir::PathResolution::Def(hir::ModuleDef::Const(_))
                    | hir::PathResolution::Def(hir::ModuleDef::Macro(_))
                    | hir::PathResolution::Def(hir::ModuleDef::Module(_))
                    | hir::PathResolution::Def(hir::ModuleDef::Static(_)) => return None,
                    hir::PathResolution::BuiltinAttr(_)
                    | hir::PathResolution::ToolModule(_)
                    | hir::PathResolution::Local(_)
                    | hir::PathResolution::TypeParam(_)
                    | hir::PathResolution::ConstParam(_)
                    | hir::PathResolution::SelfType(_)
                    | hir::PathResolution::DeriveHelper(_) => return None,
                };

                generic_def
            },
            ast::AssocTypeArg(_) => {
                // FIXME: We don't record the resolutions for this anywhere atm
                return None;
            },
            ast::MethodCallExpr(mcall) => {
                // recv.method::<$0>()
                let method = sema.resolve_method_call(&mcall)?;
                method.into()
            },
            _ => return None,
        }
    };

    let active_param = generic_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();

    let first_arg_is_non_lifetime = generic_arg_list
        .generic_args()
        .next()
        .map_or(false, |arg| !matches!(arg, ast::GenericArg::LifetimeArg(_)));

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