aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/references/classify.rs
blob: ca5750521c2f3b8f17afea79cfcd63921250296a (plain)
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
//! Functions that are used to classify an element from its definition or reference.

use hir::{InFile, PathResolution, SourceBinder};
use ra_prof::profile;
use ra_syntax::{ast, AstNode};
use test_utils::tested_by;

use super::{NameDefinition, NameKind};
use ra_ide_db::RootDatabase;

pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field};

pub(crate) fn classify_name_ref(
    sb: &mut SourceBinder<RootDatabase>,
    name_ref: InFile<&ast::NameRef>,
) -> Option<NameDefinition> {
    let _p = profile("classify_name_ref");

    let parent = name_ref.value.syntax().parent()?;
    let analyzer = sb.analyze(name_ref.map(|it| it.syntax()), None);

    if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
        tested_by!(goto_def_for_methods);
        if let Some(func) = analyzer.resolve_method_call(&method_call) {
            return Some(from_module_def(func.into()));
        }
    }

    if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
        tested_by!(goto_def_for_fields);
        if let Some(field) = analyzer.resolve_field(&field_expr) {
            return Some(from_struct_field(field));
        }
    }

    if let Some(record_field) = ast::RecordField::cast(parent.clone()) {
        tested_by!(goto_def_for_record_fields);
        tested_by!(goto_def_for_field_init_shorthand);
        if let Some(field_def) = analyzer.resolve_record_field(&record_field) {
            return Some(from_struct_field(field_def));
        }
    }

    if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
        tested_by!(goto_def_for_macros);
        if let Some(macro_def) =
            analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call))
        {
            let kind = NameKind::Macro(macro_def);
            return Some(NameDefinition { kind });
        }
    }

    let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?;
    let resolved = analyzer.resolve_path(sb.db, &path)?;
    let res = match resolved {
        PathResolution::Def(def) => from_module_def(def),
        PathResolution::AssocItem(item) => {
            let def = match item {
                hir::AssocItem::Function(it) => it.into(),
                hir::AssocItem::Const(it) => it.into(),
                hir::AssocItem::TypeAlias(it) => it.into(),
            };
            from_module_def(def)
        }
        PathResolution::Local(local) => {
            let kind = NameKind::Local(local);
            NameDefinition { kind }
        }
        PathResolution::TypeParam(par) => {
            let kind = NameKind::TypeParam(par);
            NameDefinition { kind }
        }
        PathResolution::Macro(def) => {
            let kind = NameKind::Macro(def);
            NameDefinition { kind }
        }
        PathResolution::SelfType(impl_block) => {
            let kind = NameKind::SelfType(impl_block);
            NameDefinition { kind }
        }
    };
    Some(res)
}