aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-24 20:32:41 +0000
committerAleksey Kladov <[email protected]>2019-01-24 20:32:41 +0000
commit4c514a3e02b019cdd3a17c9bcd78d93c210ab267 (patch)
treeacd4a571cec5c44d1ccbc705578f47a898da4139 /crates/ra_hir/src/code_model_api.rs
parent11dda8a0fb905a1b41a64001d564632eb062b9aa (diff)
move enum variant to the new API
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs29
1 files changed, 13 insertions, 16 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 4b79358e4..5e927e41d 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -16,7 +16,7 @@ use crate::{
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, StructId, EnumId}, 19 ids::{FunctionId, StructId, EnumId, EnumVariantId},
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,9 +68,11 @@ pub enum ModuleDef {
68 Function(Function), 68 Function(Function),
69 Struct(Struct), 69 Struct(Struct),
70 Enum(Enum), 70 Enum(Enum),
71 // Can't be directly declared, but can be imported.
72 EnumVariant(EnumVariant),
71 Def(DefId), 73 Def(DefId),
72} 74}
73impl_froms!(ModuleDef: Module, Function, Struct, Enum); 75impl_froms!(ModuleDef: Module, Function, Struct, Enum, EnumVariant);
74 76
75impl From<DefId> for ModuleDef { 77impl From<DefId> for ModuleDef {
76 fn from(it: DefId) -> ModuleDef { 78 fn from(it: DefId) -> ModuleDef {
@@ -264,30 +266,25 @@ impl Docs for Enum {
264 } 266 }
265} 267}
266 268
267#[derive(Debug, Clone, PartialEq, Eq, Hash)] 269#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
268pub struct EnumVariant { 270pub struct EnumVariant {
269 pub(crate) def_id: DefId, 271 pub(crate) id: EnumVariantId,
270} 272}
271 273
272impl EnumVariant { 274impl EnumVariant {
273 pub(crate) fn new(def_id: DefId) -> Self { 275 pub fn module(&self, db: &impl HirDatabase) -> Module {
274 EnumVariant { def_id } 276 self.id.loc(db).module
275 }
276
277 pub fn def_id(&self) -> DefId {
278 self.def_id
279 } 277 }
280
281 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { 278 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
282 db.enum_variant_data(self.def_id).parent_enum.clone() 279 db.enum_variant_data(*self).parent_enum.clone()
283 } 280 }
284 281
285 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { 282 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
286 db.enum_variant_data(self.def_id).name.clone() 283 db.enum_variant_data(*self).name.clone()
287 } 284 }
288 285
289 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> { 286 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
290 db.enum_variant_data(self.def_id).variant_data.clone() 287 db.enum_variant_data(*self).variant_data.clone()
291 } 288 }
292 289
293 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { 290 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
@@ -295,14 +292,14 @@ impl EnumVariant {
295 .fields() 292 .fields()
296 .iter() 293 .iter()
297 .map(|it| StructField { 294 .map(|it| StructField {
298 parent: self.def_id.into(), 295 parent: (*self).into(),
299 name: it.name.clone(), 296 name: it.name.clone(),
300 }) 297 })
301 .collect() 298 .collect()
302 } 299 }
303 300
304 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) { 301 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
305 def_id_to_ast(db, self.def_id) 302 self.id.loc(db).source(db)
306 } 303 }
307} 304}
308 305