diff options
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index d56570754..8639bbd5c 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::ast::{self, NameOwner, StructFlavor}; | 3 | use ra_db::Cancelable; |
4 | use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; | ||
4 | 5 | ||
5 | use crate::{ | 6 | use crate::{ |
6 | DefId, Name, AsName, Struct, Enum, VariantData, StructField, | 7 | DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, |
7 | type_ref::TypeRef, | 8 | type_ref::TypeRef, |
8 | }; | 9 | }; |
9 | 10 | ||
@@ -20,12 +21,24 @@ pub struct StructData { | |||
20 | } | 21 | } |
21 | 22 | ||
22 | impl StructData { | 23 | impl StructData { |
23 | pub(crate) fn new(struct_def: &ast::StructDef) -> StructData { | 24 | fn new(struct_def: &ast::StructDef) -> StructData { |
24 | let name = struct_def.name().map(|n| n.as_name()); | 25 | let name = struct_def.name().map(|n| n.as_name()); |
25 | let variant_data = VariantData::new(struct_def.flavor()); | 26 | let variant_data = VariantData::new(struct_def.flavor()); |
26 | let variant_data = Arc::new(variant_data); | 27 | let variant_data = Arc::new(variant_data); |
27 | StructData { name, variant_data } | 28 | StructData { name, variant_data } |
28 | } | 29 | } |
30 | |||
31 | pub(crate) fn struct_data_query( | ||
32 | db: &impl HirDatabase, | ||
33 | def_id: DefId, | ||
34 | ) -> Cancelable<Arc<StructData>> { | ||
35 | let def_loc = def_id.loc(db); | ||
36 | assert!(def_loc.kind == DefKind::Struct); | ||
37 | let syntax = db.file_item(def_loc.source_item_id); | ||
38 | let struct_def = | ||
39 | ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); | ||
40 | Ok(Arc::new(StructData::new(struct_def))) | ||
41 | } | ||
29 | } | 42 | } |
30 | 43 | ||
31 | impl Enum { | 44 | impl Enum { |
@@ -41,7 +54,7 @@ pub struct EnumData { | |||
41 | } | 54 | } |
42 | 55 | ||
43 | impl EnumData { | 56 | impl EnumData { |
44 | pub(crate) fn new(enum_def: &ast::EnumDef) -> Self { | 57 | fn new(enum_def: &ast::EnumDef) -> Self { |
45 | let name = enum_def.name().map(|n| n.as_name()); | 58 | let name = enum_def.name().map(|n| n.as_name()); |
46 | let variants = if let Some(evl) = enum_def.variant_list() { | 59 | let variants = if let Some(evl) = enum_def.variant_list() { |
47 | evl.variants() | 60 | evl.variants() |
@@ -57,6 +70,17 @@ impl EnumData { | |||
57 | }; | 70 | }; |
58 | EnumData { name, variants } | 71 | EnumData { name, variants } |
59 | } | 72 | } |
73 | |||
74 | pub(crate) fn enum_data_query( | ||
75 | db: &impl HirDatabase, | ||
76 | def_id: DefId, | ||
77 | ) -> Cancelable<Arc<EnumData>> { | ||
78 | let def_loc = def_id.loc(db); | ||
79 | assert!(def_loc.kind == DefKind::Enum); | ||
80 | let syntax = db.file_item(def_loc.source_item_id); | ||
81 | let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); | ||
82 | Ok(Arc::new(EnumData::new(enum_def))) | ||
83 | } | ||
60 | } | 84 | } |
61 | 85 | ||
62 | impl VariantData { | 86 | impl VariantData { |