aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/defs.rs80
1 files changed, 37 insertions, 43 deletions
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 030f44f86..cc772de51 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -32,10 +32,22 @@ pub struct NameDefinition {
32 pub visibility: Option<ast::Visibility>, 32 pub visibility: Option<ast::Visibility>,
33 /// FIXME: this doesn't really make sense. For example, builtin types don't 33 /// FIXME: this doesn't really make sense. For example, builtin types don't
34 /// really have a module. 34 /// really have a module.
35 pub container: Module,
36 pub kind: NameKind, 35 pub kind: NameKind,
37} 36}
38 37
38impl NameDefinition {
39 pub fn module(&self, db: &RootDatabase) -> Option<Module> {
40 match self.kind {
41 NameKind::Macro(it) => it.module(db),
42 NameKind::StructField(it) => Some(it.parent_def(db).module(db)),
43 NameKind::ModuleDef(it) => it.module(db),
44 NameKind::SelfType(it) => Some(it.module(db)),
45 NameKind::Local(it) => Some(it.module(db)),
46 NameKind::TypeParam(it) => Some(it.module(db)),
47 }
48 }
49}
50
39pub fn classify_name( 51pub fn classify_name(
40 sb: &mut SourceBinder<RootDatabase>, 52 sb: &mut SourceBinder<RootDatabase>,
41 name: InFile<&ast::Name>, 53 name: InFile<&ast::Name>,
@@ -50,7 +62,6 @@ pub fn classify_name(
50 let local = sb.to_def(src)?; 62 let local = sb.to_def(src)?;
51 Some(NameDefinition { 63 Some(NameDefinition {
52 visibility: None, 64 visibility: None,
53 container: local.module(sb.db),
54 kind: NameKind::Local(local), 65 kind: NameKind::Local(local),
55 }) 66 })
56 }, 67 },
@@ -61,57 +72,54 @@ pub fn classify_name(
61 }, 72 },
62 ast::Module(it) => { 73 ast::Module(it) => {
63 let def = sb.to_def(name.with_value(it))?; 74 let def = sb.to_def(name.with_value(it))?;
64 Some(from_module_def(sb.db, def.into(), None)) 75 Some(from_module_def(sb.db, def.into()))
65 }, 76 },
66 ast::StructDef(it) => { 77 ast::StructDef(it) => {
67 let src = name.with_value(it); 78 let src = name.with_value(it);
68 let def: hir::Struct = sb.to_def(src)?; 79 let def: hir::Struct = sb.to_def(src)?;
69 Some(from_module_def(sb.db, def.into(), None)) 80 Some(from_module_def(sb.db, def.into()))
70 }, 81 },
71 ast::EnumDef(it) => { 82 ast::EnumDef(it) => {
72 let src = name.with_value(it); 83 let src = name.with_value(it);
73 let def: hir::Enum = sb.to_def(src)?; 84 let def: hir::Enum = sb.to_def(src)?;
74 Some(from_module_def(sb.db, def.into(), None)) 85 Some(from_module_def(sb.db, def.into()))
75 }, 86 },
76 ast::TraitDef(it) => { 87 ast::TraitDef(it) => {
77 let src = name.with_value(it); 88 let src = name.with_value(it);
78 let def: hir::Trait = sb.to_def(src)?; 89 let def: hir::Trait = sb.to_def(src)?;
79 Some(from_module_def(sb.db, def.into(), None)) 90 Some(from_module_def(sb.db, def.into()))
80 }, 91 },
81 ast::StaticDef(it) => { 92 ast::StaticDef(it) => {
82 let src = name.with_value(it); 93 let src = name.with_value(it);
83 let def: hir::Static = sb.to_def(src)?; 94 let def: hir::Static = sb.to_def(src)?;
84 Some(from_module_def(sb.db, def.into(), None)) 95 Some(from_module_def(sb.db, def.into()))
85 }, 96 },
86 ast::EnumVariant(it) => { 97 ast::EnumVariant(it) => {
87 let src = name.with_value(it); 98 let src = name.with_value(it);
88 let def: hir::EnumVariant = sb.to_def(src)?; 99 let def: hir::EnumVariant = sb.to_def(src)?;
89 Some(from_module_def(sb.db, def.into(), None)) 100 Some(from_module_def(sb.db, def.into()))
90 }, 101 },
91 ast::FnDef(it) => { 102 ast::FnDef(it) => {
92 let src = name.with_value(it); 103 let src = name.with_value(it);
93 let def: hir::Function = sb.to_def(src)?; 104 let def: hir::Function = sb.to_def(src)?;
94 Some(from_module_def(sb.db, def.into(), None)) 105 Some(from_module_def(sb.db, def.into()))
95 }, 106 },
96 ast::ConstDef(it) => { 107 ast::ConstDef(it) => {
97 let src = name.with_value(it); 108 let src = name.with_value(it);
98 let def: hir::Const = sb.to_def(src)?; 109 let def: hir::Const = sb.to_def(src)?;
99 Some(from_module_def(sb.db, def.into(), None)) 110 Some(from_module_def(sb.db, def.into()))
100 }, 111 },
101 ast::TypeAliasDef(it) => { 112 ast::TypeAliasDef(it) => {
102 let src = name.with_value(it); 113 let src = name.with_value(it);
103 let def: hir::TypeAlias = sb.to_def(src)?; 114 let def: hir::TypeAlias = sb.to_def(src)?;
104 Some(from_module_def(sb.db, def.into(), None)) 115 Some(from_module_def(sb.db, def.into()))
105 }, 116 },
106 ast::MacroCall(it) => { 117 ast::MacroCall(it) => {
107 let src = name.with_value(it); 118 let src = name.with_value(it);
108 let def = sb.to_def(src.clone())?; 119 let def = sb.to_def(src.clone())?;
109 120
110 let module = sb.to_module_def(src.file_id.original_file(sb.db))?;
111
112 Some(NameDefinition { 121 Some(NameDefinition {
113 visibility: None, 122 visibility: None,
114 container: module,
115 kind: NameKind::Macro(def), 123 kind: NameKind::Macro(def),
116 }) 124 })
117 }, 125 },
@@ -120,7 +128,6 @@ pub fn classify_name(
120 let def = sb.to_def(src)?; 128 let def = sb.to_def(src)?;
121 Some(NameDefinition { 129 Some(NameDefinition {
122 visibility: None, 130 visibility: None,
123 container: def.module(sb.db),
124 kind: NameKind::TypeParam(def), 131 kind: NameKind::TypeParam(def),
125 }) 132 })
126 }, 133 },
@@ -132,41 +139,28 @@ pub fn classify_name(
132pub fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { 139pub fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition {
133 let kind = NameKind::StructField(field); 140 let kind = NameKind::StructField(field);
134 let parent = field.parent_def(db); 141 let parent = field.parent_def(db);
135 let container = parent.module(db);
136 let visibility = match parent { 142 let visibility = match parent {
137 VariantDef::Struct(s) => s.source(db).value.visibility(), 143 VariantDef::Struct(s) => s.source(db).value.visibility(),
138 VariantDef::Union(e) => e.source(db).value.visibility(), 144 VariantDef::Union(e) => e.source(db).value.visibility(),
139 VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(), 145 VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(),
140 }; 146 };
141 NameDefinition { kind, container, visibility } 147 NameDefinition { kind, visibility }
142} 148}
143 149
144pub fn from_module_def( 150pub fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition {
145 db: &RootDatabase,
146 def: ModuleDef,
147 module: Option<Module>,
148) -> NameDefinition {
149 let kind = NameKind::ModuleDef(def); 151 let kind = NameKind::ModuleDef(def);
150 let (container, visibility) = match def { 152 let visibility = match def {
151 ModuleDef::Module(it) => { 153 ModuleDef::Module(it) => it.declaration_source(db).and_then(|s| s.value.visibility()),
152 let container = it.parent(db).or_else(|| Some(it)).unwrap(); 154 ModuleDef::EnumVariant(it) => it.source(db).value.parent_enum().visibility(),
153 let visibility = it.declaration_source(db).and_then(|s| s.value.visibility()); 155 ModuleDef::Function(it) => it.source(db).value.visibility(),
154 (container, visibility) 156 ModuleDef::Const(it) => it.source(db).value.visibility(),
155 } 157 ModuleDef::Static(it) => it.source(db).value.visibility(),
156 ModuleDef::EnumVariant(it) => { 158 ModuleDef::Trait(it) => it.source(db).value.visibility(),
157 let container = it.module(db); 159 ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
158 let visibility = it.source(db).value.parent_enum().visibility(); 160 ModuleDef::Adt(Adt::Struct(it)) => it.source(db).value.visibility(),
159 (container, visibility) 161 ModuleDef::Adt(Adt::Union(it)) => it.source(db).value.visibility(),
160 } 162 ModuleDef::Adt(Adt::Enum(it)) => it.source(db).value.visibility(),
161 ModuleDef::Function(it) => (it.module(db), it.source(db).value.visibility()), 163 ModuleDef::BuiltinType(..) => None,
162 ModuleDef::Const(it) => (it.module(db), it.source(db).value.visibility()),
163 ModuleDef::Static(it) => (it.module(db), it.source(db).value.visibility()),
164 ModuleDef::Trait(it) => (it.module(db), it.source(db).value.visibility()),
165 ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).value.visibility()),
166 ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).value.visibility()),
167 ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).value.visibility()),
168 ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).value.visibility()),
169 ModuleDef::BuiltinType(..) => (module.unwrap(), None),
170 }; 164 };
171 NameDefinition { kind, container, visibility } 165 NameDefinition { kind, visibility }
172} 166}