aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/defs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db/src/defs.rs')
-rw-r--r--crates/ra_ide_db/src/defs.rs55
1 files changed, 20 insertions, 35 deletions
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index aec748abf..04c214624 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -18,7 +18,7 @@ use ra_syntax::{
18use crate::RootDatabase; 18use crate::RootDatabase;
19 19
20#[derive(Debug, PartialEq, Eq)] 20#[derive(Debug, PartialEq, Eq)]
21pub enum NameKind { 21pub enum NameDefinition {
22 Macro(MacroDef), 22 Macro(MacroDef),
23 StructField(StructField), 23 StructField(StructField),
24 ModuleDef(ModuleDef), 24 ModuleDef(ModuleDef),
@@ -27,33 +27,26 @@ pub enum NameKind {
27 TypeParam(TypeParam), 27 TypeParam(TypeParam),
28} 28}
29 29
30#[derive(PartialEq, Eq)]
31pub struct NameDefinition {
32 /// FIXME: this doesn't really make sense. For example, builtin types don't
33 /// really have a module.
34 pub kind: NameKind,
35}
36
37impl NameDefinition { 30impl NameDefinition {
38 pub fn module(&self, db: &RootDatabase) -> Option<Module> { 31 pub fn module(&self, db: &RootDatabase) -> Option<Module> {
39 match self.kind { 32 match self {
40 NameKind::Macro(it) => it.module(db), 33 NameDefinition::Macro(it) => it.module(db),
41 NameKind::StructField(it) => Some(it.parent_def(db).module(db)), 34 NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)),
42 NameKind::ModuleDef(it) => it.module(db), 35 NameDefinition::ModuleDef(it) => it.module(db),
43 NameKind::SelfType(it) => Some(it.module(db)), 36 NameDefinition::SelfType(it) => Some(it.module(db)),
44 NameKind::Local(it) => Some(it.module(db)), 37 NameDefinition::Local(it) => Some(it.module(db)),
45 NameKind::TypeParam(it) => Some(it.module(db)), 38 NameDefinition::TypeParam(it) => Some(it.module(db)),
46 } 39 }
47 } 40 }
48 41
49 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> { 42 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
50 match self.kind { 43 match self {
51 NameKind::Macro(_) => None, 44 NameDefinition::Macro(_) => None,
52 NameKind::StructField(sf) => match sf.source(db).value { 45 NameDefinition::StructField(sf) => match sf.source(db).value {
53 FieldSource::Named(it) => it.visibility(), 46 FieldSource::Named(it) => it.visibility(),
54 FieldSource::Pos(it) => it.visibility(), 47 FieldSource::Pos(it) => it.visibility(),
55 }, 48 },
56 NameKind::ModuleDef(def) => match def { 49 NameDefinition::ModuleDef(def) => match def {
57 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), 50 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
58 ModuleDef::Function(it) => it.source(db).value.visibility(), 51 ModuleDef::Function(it) => it.source(db).value.visibility(),
59 ModuleDef::Adt(adt) => match adt { 52 ModuleDef::Adt(adt) => match adt {
@@ -68,9 +61,9 @@ impl NameDefinition {
68 ModuleDef::EnumVariant(_) => None, 61 ModuleDef::EnumVariant(_) => None,
69 ModuleDef::BuiltinType(_) => None, 62 ModuleDef::BuiltinType(_) => None,
70 }, 63 },
71 NameKind::SelfType(_) => None, 64 NameDefinition::SelfType(_) => None,
72 NameKind::Local(_) => None, 65 NameDefinition::Local(_) => None,
73 NameKind::TypeParam(_) => None, 66 NameDefinition::TypeParam(_) => None,
74 } 67 }
75 } 68 }
76} 69}
@@ -87,9 +80,7 @@ pub fn classify_name(
87 ast::BindPat(it) => { 80 ast::BindPat(it) => {
88 let src = name.with_value(it); 81 let src = name.with_value(it);
89 let local = sb.to_def(src)?; 82 let local = sb.to_def(src)?;
90 Some(NameDefinition { 83 Some(NameDefinition::Local(local))
91 kind: NameKind::Local(local),
92 })
93 }, 84 },
94 ast::RecordFieldDef(it) => { 85 ast::RecordFieldDef(it) => {
95 let src = name.with_value(it); 86 let src = name.with_value(it);
@@ -144,16 +135,12 @@ pub fn classify_name(
144 let src = name.with_value(it); 135 let src = name.with_value(it);
145 let def = sb.to_def(src.clone())?; 136 let def = sb.to_def(src.clone())?;
146 137
147 Some(NameDefinition { 138 Some(NameDefinition::Macro(def))
148 kind: NameKind::Macro(def),
149 })
150 }, 139 },
151 ast::TypeParam(it) => { 140 ast::TypeParam(it) => {
152 let src = name.with_value(it); 141 let src = name.with_value(it);
153 let def = sb.to_def(src)?; 142 let def = sb.to_def(src)?;
154 Some(NameDefinition { 143 Some(NameDefinition::TypeParam(def))
155 kind: NameKind::TypeParam(def),
156 })
157 }, 144 },
158 _ => None, 145 _ => None,
159 } 146 }
@@ -161,11 +148,9 @@ pub fn classify_name(
161} 148}
162 149
163pub fn from_struct_field(field: StructField) -> NameDefinition { 150pub fn from_struct_field(field: StructField) -> NameDefinition {
164 let kind = NameKind::StructField(field); 151 NameDefinition::StructField(field)
165 NameDefinition { kind }
166} 152}
167 153
168pub fn from_module_def(def: ModuleDef) -> NameDefinition { 154pub fn from_module_def(def: ModuleDef) -> NameDefinition {
169 let kind = NameKind::ModuleDef(def); 155 NameDefinition::ModuleDef(def)
170 NameDefinition { kind }
171} 156}