aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/references/name_definition.rs
blob: aca23f79e24b6f5f09c35d34ccca54f8ce285d31 (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
//! `NameDefinition` keeps information about the element we want to search references for.
//! The element is represented by `NameKind`. It's located inside some `container` and
//! has a `visibility`, which defines a search scope.
//! Note that the reference search is possible for not all of the classified items.

use hir::{
    Adt, AssocItem, GenericParam, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty,
    VariantDef,
};
use ra_syntax::{ast, ast::VisibilityOwner};

use crate::db::RootDatabase;

#[derive(Debug, PartialEq, Eq)]
pub enum NameKind {
    Macro(MacroDef),
    Field(StructField),
    AssocItem(AssocItem),
    Def(ModuleDef),
    SelfType(Ty),
    Local(Local),
    GenericParam(GenericParam),
}

#[derive(PartialEq, Eq)]
pub(crate) struct NameDefinition {
    pub visibility: Option<ast::Visibility>,
    pub container: Module,
    pub kind: NameKind,
}

pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition {
    let container = item.module(db);
    let visibility = match item {
        AssocItem::Function(f) => f.source(db).value.visibility(),
        AssocItem::Const(c) => c.source(db).value.visibility(),
        AssocItem::TypeAlias(a) => a.source(db).value.visibility(),
    };
    let kind = NameKind::AssocItem(item);
    NameDefinition { kind, container, visibility }
}

pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition {
    let kind = NameKind::Field(field);
    let parent = field.parent_def(db);
    let container = parent.module(db);
    let visibility = match parent {
        VariantDef::Struct(s) => s.source(db).value.visibility(),
        VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(),
    };
    NameDefinition { kind, container, visibility }
}

pub(super) fn from_module_def(
    db: &RootDatabase,
    def: ModuleDef,
    module: Option<Module>,
) -> NameDefinition {
    let kind = NameKind::Def(def);
    let (container, visibility) = match def {
        ModuleDef::Module(it) => {
            let container = it.parent(db).or_else(|| Some(it)).unwrap();
            let visibility = it.declaration_source(db).and_then(|s| s.value.visibility());
            (container, visibility)
        }
        ModuleDef::EnumVariant(it) => {
            let container = it.module(db);
            let visibility = it.source(db).value.parent_enum().visibility();
            (container, visibility)
        }
        ModuleDef::Function(it) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Const(it) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Static(it) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Trait(it) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).value.visibility()),
        ModuleDef::BuiltinType(..) => (module.unwrap(), None),
    };
    NameDefinition { kind, container, visibility }
}