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.rs83
1 files changed, 44 insertions, 39 deletions
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index cc772de51..aec748abf 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -6,8 +6,8 @@
6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). 6// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
7 7
8use hir::{ 8use hir::{
9 Adt, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef, SourceBinder, 9 Adt, FieldSource, HasSource, ImplBlock, InFile, Local, MacroDef, Module, ModuleDef,
10 StructField, TypeParam, VariantDef, 10 SourceBinder, StructField, TypeParam,
11}; 11};
12use ra_prof::profile; 12use ra_prof::profile;
13use ra_syntax::{ 13use ra_syntax::{
@@ -29,7 +29,6 @@ pub enum NameKind {
29 29
30#[derive(PartialEq, Eq)] 30#[derive(PartialEq, Eq)]
31pub struct NameDefinition { 31pub struct NameDefinition {
32 pub visibility: Option<ast::Visibility>,
33 /// FIXME: this doesn't really make sense. For example, builtin types don't 32 /// FIXME: this doesn't really make sense. For example, builtin types don't
34 /// really have a module. 33 /// really have a module.
35 pub kind: NameKind, 34 pub kind: NameKind,
@@ -46,6 +45,34 @@ impl NameDefinition {
46 NameKind::TypeParam(it) => Some(it.module(db)), 45 NameKind::TypeParam(it) => Some(it.module(db)),
47 } 46 }
48 } 47 }
48
49 pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
50 match self.kind {
51 NameKind::Macro(_) => None,
52 NameKind::StructField(sf) => match sf.source(db).value {
53 FieldSource::Named(it) => it.visibility(),
54 FieldSource::Pos(it) => it.visibility(),
55 },
56 NameKind::ModuleDef(def) => match def {
57 ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(),
58 ModuleDef::Function(it) => it.source(db).value.visibility(),
59 ModuleDef::Adt(adt) => match adt {
60 Adt::Struct(it) => it.source(db).value.visibility(),
61 Adt::Union(it) => it.source(db).value.visibility(),
62 Adt::Enum(it) => it.source(db).value.visibility(),
63 },
64 ModuleDef::Const(it) => it.source(db).value.visibility(),
65 ModuleDef::Static(it) => it.source(db).value.visibility(),
66 ModuleDef::Trait(it) => it.source(db).value.visibility(),
67 ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
68 ModuleDef::EnumVariant(_) => None,
69 ModuleDef::BuiltinType(_) => None,
70 },
71 NameKind::SelfType(_) => None,
72 NameKind::Local(_) => None,
73 NameKind::TypeParam(_) => None,
74 }
75 }
49} 76}
50 77
51pub fn classify_name( 78pub fn classify_name(
@@ -61,65 +88,63 @@ pub fn classify_name(
61 let src = name.with_value(it); 88 let src = name.with_value(it);
62 let local = sb.to_def(src)?; 89 let local = sb.to_def(src)?;
63 Some(NameDefinition { 90 Some(NameDefinition {
64 visibility: None,
65 kind: NameKind::Local(local), 91 kind: NameKind::Local(local),
66 }) 92 })
67 }, 93 },
68 ast::RecordFieldDef(it) => { 94 ast::RecordFieldDef(it) => {
69 let src = name.with_value(it); 95 let src = name.with_value(it);
70 let field: hir::StructField = sb.to_def(src)?; 96 let field: hir::StructField = sb.to_def(src)?;
71 Some(from_struct_field(sb.db, field)) 97 Some(from_struct_field(field))
72 }, 98 },
73 ast::Module(it) => { 99 ast::Module(it) => {
74 let def = sb.to_def(name.with_value(it))?; 100 let def = sb.to_def(name.with_value(it))?;
75 Some(from_module_def(sb.db, def.into())) 101 Some(from_module_def(def.into()))
76 }, 102 },
77 ast::StructDef(it) => { 103 ast::StructDef(it) => {
78 let src = name.with_value(it); 104 let src = name.with_value(it);
79 let def: hir::Struct = sb.to_def(src)?; 105 let def: hir::Struct = sb.to_def(src)?;
80 Some(from_module_def(sb.db, def.into())) 106 Some(from_module_def(def.into()))
81 }, 107 },
82 ast::EnumDef(it) => { 108 ast::EnumDef(it) => {
83 let src = name.with_value(it); 109 let src = name.with_value(it);
84 let def: hir::Enum = sb.to_def(src)?; 110 let def: hir::Enum = sb.to_def(src)?;
85 Some(from_module_def(sb.db, def.into())) 111 Some(from_module_def(def.into()))
86 }, 112 },
87 ast::TraitDef(it) => { 113 ast::TraitDef(it) => {
88 let src = name.with_value(it); 114 let src = name.with_value(it);
89 let def: hir::Trait = sb.to_def(src)?; 115 let def: hir::Trait = sb.to_def(src)?;
90 Some(from_module_def(sb.db, def.into())) 116 Some(from_module_def(def.into()))
91 }, 117 },
92 ast::StaticDef(it) => { 118 ast::StaticDef(it) => {
93 let src = name.with_value(it); 119 let src = name.with_value(it);
94 let def: hir::Static = sb.to_def(src)?; 120 let def: hir::Static = sb.to_def(src)?;
95 Some(from_module_def(sb.db, def.into())) 121 Some(from_module_def(def.into()))
96 }, 122 },
97 ast::EnumVariant(it) => { 123 ast::EnumVariant(it) => {
98 let src = name.with_value(it); 124 let src = name.with_value(it);
99 let def: hir::EnumVariant = sb.to_def(src)?; 125 let def: hir::EnumVariant = sb.to_def(src)?;
100 Some(from_module_def(sb.db, def.into())) 126 Some(from_module_def(def.into()))
101 }, 127 },
102 ast::FnDef(it) => { 128 ast::FnDef(it) => {
103 let src = name.with_value(it); 129 let src = name.with_value(it);
104 let def: hir::Function = sb.to_def(src)?; 130 let def: hir::Function = sb.to_def(src)?;
105 Some(from_module_def(sb.db, def.into())) 131 Some(from_module_def(def.into()))
106 }, 132 },
107 ast::ConstDef(it) => { 133 ast::ConstDef(it) => {
108 let src = name.with_value(it); 134 let src = name.with_value(it);
109 let def: hir::Const = sb.to_def(src)?; 135 let def: hir::Const = sb.to_def(src)?;
110 Some(from_module_def(sb.db, def.into())) 136 Some(from_module_def(def.into()))
111 }, 137 },
112 ast::TypeAliasDef(it) => { 138 ast::TypeAliasDef(it) => {
113 let src = name.with_value(it); 139 let src = name.with_value(it);
114 let def: hir::TypeAlias = sb.to_def(src)?; 140 let def: hir::TypeAlias = sb.to_def(src)?;
115 Some(from_module_def(sb.db, def.into())) 141 Some(from_module_def(def.into()))
116 }, 142 },
117 ast::MacroCall(it) => { 143 ast::MacroCall(it) => {
118 let src = name.with_value(it); 144 let src = name.with_value(it);
119 let def = sb.to_def(src.clone())?; 145 let def = sb.to_def(src.clone())?;
120 146
121 Some(NameDefinition { 147 Some(NameDefinition {
122 visibility: None,
123 kind: NameKind::Macro(def), 148 kind: NameKind::Macro(def),
124 }) 149 })
125 }, 150 },
@@ -127,7 +152,6 @@ pub fn classify_name(
127 let src = name.with_value(it); 152 let src = name.with_value(it);
128 let def = sb.to_def(src)?; 153 let def = sb.to_def(src)?;
129 Some(NameDefinition { 154 Some(NameDefinition {
130 visibility: None,
131 kind: NameKind::TypeParam(def), 155 kind: NameKind::TypeParam(def),
132 }) 156 })
133 }, 157 },
@@ -136,31 +160,12 @@ pub fn classify_name(
136 } 160 }
137} 161}
138 162
139pub fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { 163pub fn from_struct_field(field: StructField) -> NameDefinition {
140 let kind = NameKind::StructField(field); 164 let kind = NameKind::StructField(field);
141 let parent = field.parent_def(db); 165 NameDefinition { kind }
142 let visibility = match parent {
143 VariantDef::Struct(s) => s.source(db).value.visibility(),
144 VariantDef::Union(e) => e.source(db).value.visibility(),
145 VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(),
146 };
147 NameDefinition { kind, visibility }
148} 166}
149 167
150pub fn from_module_def(db: &RootDatabase, def: ModuleDef) -> NameDefinition { 168pub fn from_module_def(def: ModuleDef) -> NameDefinition {
151 let kind = NameKind::ModuleDef(def); 169 let kind = NameKind::ModuleDef(def);
152 let visibility = match def { 170 NameDefinition { kind }
153 ModuleDef::Module(it) => it.declaration_source(db).and_then(|s| s.value.visibility()),
154 ModuleDef::EnumVariant(it) => it.source(db).value.parent_enum().visibility(),
155 ModuleDef::Function(it) => it.source(db).value.visibility(),
156 ModuleDef::Const(it) => it.source(db).value.visibility(),
157 ModuleDef::Static(it) => it.source(db).value.visibility(),
158 ModuleDef::Trait(it) => it.source(db).value.visibility(),
159 ModuleDef::TypeAlias(it) => it.source(db).value.visibility(),
160 ModuleDef::Adt(Adt::Struct(it)) => it.source(db).value.visibility(),
161 ModuleDef::Adt(Adt::Union(it)) => it.source(db).value.visibility(),
162 ModuleDef::Adt(Adt::Enum(it)) => it.source(db).value.visibility(),
163 ModuleDef::BuiltinType(..) => None,
164 };
165 NameDefinition { kind, visibility }
166} 171}