aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/references/classify.rs
blob: 93e079cccbad3916f80299edb074d468edf21a96 (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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! FIXME: write short doc here

use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer};
use ra_db::FileId;
use ra_syntax::{ast, match_ast, AstNode, AstPtr};

use super::{
    name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field},
    NameDefinition, NameKind,
};
use crate::db::RootDatabase;

pub(crate) fn classify_name(
    db: &RootDatabase,
    file_id: FileId,
    name: &ast::Name,
) -> Option<NameDefinition> {
    let parent = name.syntax().parent()?;
    let file_id = file_id.into();

    // FIXME: add ast::MacroCall(it)
    match_ast! {
        match parent {
            ast::BindPat(it) => {
                from_pat(db, file_id, AstPtr::new(&it))
            },
            ast::RecordFieldDef(it) => {
                let ast = hir::FieldSource::Named(it);
                let src = hir::Source { file_id, ast };
                let field = hir::StructField::from_source(db, src)?;
                Some(from_struct_field(db, field))
            },
            ast::Module(it) => {
                let ast = hir::ModuleSource::Module(it);
                let src = hir::Source { file_id, ast };
                let def = hir::Module::from_definition(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::StructDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Struct::from_source(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::EnumDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Enum::from_source(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::TraitDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Trait::from_source(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::StaticDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Static::from_source(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::EnumVariant(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::EnumVariant::from_source(db, src)?;
                Some(from_module_def(db, def.into(), None))
            },
            ast::FnDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Function::from_source(db, src)?;
                if parent.parent().and_then(ast::ItemList::cast).is_some() {
                    Some(from_assoc_item(db, def.into()))
                } else {
                    Some(from_module_def(db, def.into(), None))
                }
            },
            ast::ConstDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::Const::from_source(db, src)?;
                if parent.parent().and_then(ast::ItemList::cast).is_some() {
                    Some(from_assoc_item(db, def.into()))
                } else {
                    Some(from_module_def(db, def.into(), None))
                }
            },
            ast::TypeAliasDef(it) => {
                let src = hir::Source { file_id, ast: it };
                let def = hir::TypeAlias::from_source(db, src)?;
                if parent.parent().and_then(ast::ItemList::cast).is_some() {
                    Some(from_assoc_item(db, def.into()))
                } else {
                    Some(from_module_def(db, def.into(), None))
                }
            },
            _ => None,
        }
    }
}

pub(crate) fn classify_name_ref(
    db: &RootDatabase,
    file_id: FileId,
    name_ref: &ast::NameRef,
) -> Option<NameDefinition> {
    use PathResolution::*;

    let parent = name_ref.syntax().parent()?;
    let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);

    if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
        if let Some(func) = analyzer.resolve_method_call(&method_call) {
            return Some(from_assoc_item(db, func.into()));
        }
    }

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

    if let Some(record_field) = ast::RecordField::cast(parent.clone()) {
        if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) {
            let variant_def = analyzer.resolve_record_literal(&record_lit)?;
            let hir_path = Path::from_name_ref(name_ref);
            let hir_name = hir_path.as_ident()?;
            let field = variant_def.field(db, hir_name)?;
            return Some(from_struct_field(db, field));
        }
    }

    let ast = ModuleSource::from_child_node(db, file_id, &parent);
    let file_id = file_id.into();
    // FIXME: find correct container and visibility for each case
    let container = Module::from_definition(db, Source { file_id, ast })?;
    let visibility = None;

    if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
        if let Some(macro_def) = analyzer.resolve_macro_call(db, &macro_call) {
            let kind = NameKind::Macro(macro_def);
            return Some(NameDefinition { kind, container, visibility });
        }
    }

    let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
    let resolved = analyzer.resolve_path(db, &path)?;
    match resolved {
        Def(def) => Some(from_module_def(db, def, Some(container))),
        AssocItem(item) => Some(from_assoc_item(db, item)),
        LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat),
        LocalBinding(Either::B(par)) => {
            let kind = NameKind::SelfParam(par);
            Some(NameDefinition { kind, container, visibility })
        }
        GenericParam(par) => {
            // FIXME: get generic param def
            let kind = NameKind::GenericParam(par);
            Some(NameDefinition { kind, container, visibility })
        }
        Macro(def) => {
            let kind = NameKind::Macro(def);
            Some(NameDefinition { kind, container, visibility })
        }
        SelfType(impl_block) => {
            let ty = impl_block.target_ty(db);
            let kind = NameKind::SelfType(ty);
            let container = impl_block.module();
            Some(NameDefinition { kind, container, visibility })
        }
    }
}