diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 905615127..948718aa6 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -10,13 +10,13 @@ use crate::{ | |||
10 | nameres::{ModuleScope, lower::ImportId}, | 10 | nameres::{ModuleScope, lower::ImportId}, |
11 | db::HirDatabase, | 11 | db::HirDatabase, |
12 | expr::BodySyntaxMapping, | 12 | expr::BodySyntaxMapping, |
13 | ty::InferenceResult, | 13 | ty::{InferenceResult, VariantDef}, |
14 | adt::VariantData, | 14 | adt::VariantData, |
15 | generics::GenericParams, | 15 | generics::GenericParams, |
16 | code_model_impl::def_id_to_ast, | 16 | code_model_impl::def_id_to_ast, |
17 | docs::{Documentation, Docs, docs_from_ast}, | 17 | docs::{Documentation, Docs, docs_from_ast}, |
18 | module_tree::ModuleId, | 18 | module_tree::ModuleId, |
19 | ids::FunctionId, | 19 | ids::{FunctionId, StructId}, |
20 | }; | 20 | }; |
21 | 21 | ||
22 | /// hir::Crate describes a single crate. It's the main interface with which | 22 | /// hir::Crate describes a single crate. It's the main interface with which |
@@ -68,6 +68,7 @@ pub struct Module { | |||
68 | pub enum ModuleDef { | 68 | pub enum ModuleDef { |
69 | Module(Module), | 69 | Module(Module), |
70 | Function(Function), | 70 | Function(Function), |
71 | Struct(Struct), | ||
71 | Def(DefId), | 72 | Def(DefId), |
72 | } | 73 | } |
73 | 74 | ||
@@ -83,6 +84,12 @@ impl Into<ModuleDef> for Function { | |||
83 | } | 84 | } |
84 | } | 85 | } |
85 | 86 | ||
87 | impl Into<ModuleDef> for Struct { | ||
88 | fn into(self) -> ModuleDef { | ||
89 | ModuleDef::Struct(self) | ||
90 | } | ||
91 | } | ||
92 | |||
86 | impl Into<ModuleDef> for DefId { | 93 | impl Into<ModuleDef> for DefId { |
87 | fn into(self) -> ModuleDef { | 94 | fn into(self) -> ModuleDef { |
88 | ModuleDef::Def(self) | 95 | ModuleDef::Def(self) |
@@ -187,7 +194,7 @@ impl Module { | |||
187 | 194 | ||
188 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 195 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
189 | pub struct StructField { | 196 | pub struct StructField { |
190 | parent: DefId, | 197 | parent: VariantDef, |
191 | name: Name, | 198 | name: Name, |
192 | } | 199 | } |
193 | 200 | ||
@@ -201,38 +208,38 @@ impl StructField { | |||
201 | } | 208 | } |
202 | } | 209 | } |
203 | 210 | ||
204 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 211 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
205 | pub struct Struct { | 212 | pub struct Struct { |
206 | pub(crate) def_id: DefId, | 213 | pub(crate) id: StructId, |
207 | } | 214 | } |
208 | 215 | ||
209 | impl Struct { | 216 | impl Struct { |
210 | pub fn def_id(&self) -> DefId { | 217 | pub fn module(&self, db: &impl HirDatabase) -> Module { |
211 | self.def_id | 218 | self.id.loc(db).module |
212 | } | 219 | } |
213 | 220 | ||
214 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | 221 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { |
215 | db.struct_data(self.def_id).name.clone() | 222 | db.struct_data(*self).name.clone() |
216 | } | 223 | } |
217 | 224 | ||
218 | pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { | 225 | pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { |
219 | db.struct_data(self.def_id) | 226 | db.struct_data(*self) |
220 | .variant_data | 227 | .variant_data |
221 | .fields() | 228 | .fields() |
222 | .iter() | 229 | .iter() |
223 | .map(|it| StructField { | 230 | .map(|it| StructField { |
224 | parent: self.def_id, | 231 | parent: (*self).into(), |
225 | name: it.name.clone(), | 232 | name: it.name.clone(), |
226 | }) | 233 | }) |
227 | .collect() | 234 | .collect() |
228 | } | 235 | } |
229 | 236 | ||
230 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { | 237 | pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) { |
231 | def_id_to_ast(db, self.def_id) | 238 | self.id.loc(db).source(db) |
232 | } | 239 | } |
233 | 240 | ||
234 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { | 241 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { |
235 | db.generic_params(self.def_id.into()) | 242 | db.generic_params((*self).into()) |
236 | } | 243 | } |
237 | } | 244 | } |
238 | 245 | ||
@@ -310,7 +317,7 @@ impl EnumVariant { | |||
310 | .fields() | 317 | .fields() |
311 | .iter() | 318 | .iter() |
312 | .map(|it| StructField { | 319 | .map(|it| StructField { |
313 | parent: self.def_id, | 320 | parent: self.def_id.into(), |
314 | name: it.name.clone(), | 321 | name: it.name.clone(), |
315 | }) | 322 | }) |
316 | .collect() | 323 | .collect() |