aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/adt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r--crates/ra_hir/src/adt.rs54
1 files changed, 33 insertions, 21 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index 3caf60ee6..baf853a3a 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -13,7 +13,7 @@ use crate::{
13 HirDatabase, DefKind, 13 HirDatabase, DefKind,
14 SourceItemId, 14 SourceItemId,
15 type_ref::TypeRef, 15 type_ref::TypeRef,
16 ids::{StructLoc}, 16 ids::{StructLoc, EnumLoc},
17}; 17};
18 18
19impl Struct { 19impl Struct {
@@ -33,6 +33,19 @@ impl Struct {
33 } 33 }
34} 34}
35 35
36impl Enum {
37 pub(crate) fn from_ast(
38 db: &impl HirDatabase,
39 module: Module,
40 file_id: HirFileId,
41 ast: &ast::EnumDef,
42 ) -> Enum {
43 let loc: EnumLoc = EnumLoc::from_ast(db, module, file_id, ast);
44 let id = loc.id(db);
45 Enum { id }
46 }
47}
48
36#[derive(Debug, Clone, PartialEq, Eq)] 49#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct StructData { 50pub struct StructData {
38 pub(crate) name: Option<Name>, 51 pub(crate) name: Option<Name>,
@@ -55,20 +68,20 @@ impl StructData {
55 68
56fn get_def_id( 69fn get_def_id(
57 db: &impl HirDatabase, 70 db: &impl HirDatabase,
58 same_file_loc: &DefLoc, 71 module: Module,
72 file_id: HirFileId,
59 node: &SyntaxNode, 73 node: &SyntaxNode,
60 expected_kind: DefKind, 74 expected_kind: DefKind,
61) -> DefId { 75) -> DefId {
62 let file_id = same_file_loc.source_item_id.file_id;
63 let file_items = db.file_items(file_id); 76 let file_items = db.file_items(file_id);
64 77
65 let item_id = file_items.id_of(file_id, node); 78 let item_id = file_items.id_of(file_id, node);
66 let source_item_id = SourceItemId { 79 let source_item_id = SourceItemId {
80 file_id,
67 item_id: Some(item_id), 81 item_id: Some(item_id),
68 ..same_file_loc.source_item_id
69 }; 82 };
70 let loc = DefLoc { 83 let loc = DefLoc {
71 module: same_file_loc.module, 84 module,
72 kind: expected_kind, 85 kind: expected_kind,
73 source_item_id, 86 source_item_id,
74 }; 87 };
@@ -87,19 +100,22 @@ impl EnumData {
87 EnumData { name, variants } 100 EnumData { name, variants }
88 } 101 }
89 102
90 pub(crate) fn enum_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<EnumData> { 103 pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
91 let def_loc = def_id.loc(db); 104 let (file_id, enum_def) = e.source(db);
92 assert!(def_loc.kind == DefKind::Enum); 105 let module = e.module(db);
93 let syntax = db.file_item(def_loc.source_item_id);
94 let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node");
95 let variants = if let Some(vl) = enum_def.variant_list() { 106 let variants = if let Some(vl) = enum_def.variant_list() {
96 vl.variants() 107 vl.variants()
97 .filter_map(|variant_def| { 108 .filter_map(|variant_def| {
98 let name = variant_def.name().map(|n| n.as_name()); 109 let name = variant_def.name().map(|n| n.as_name());
99 110
100 name.map(|n| { 111 name.map(|n| {
101 let def_id = 112 let def_id = get_def_id(
102 get_def_id(db, &def_loc, variant_def.syntax(), DefKind::EnumVariant); 113 db,
114 module,
115 file_id,
116 variant_def.syntax(),
117 DefKind::EnumVariant,
118 );
103 (n, EnumVariant::new(def_id)) 119 (n, EnumVariant::new(def_id))
104 }) 120 })
105 }) 121 })
@@ -107,7 +123,7 @@ impl EnumData {
107 } else { 123 } else {
108 Vec::new() 124 Vec::new()
109 }; 125 };
110 Arc::new(EnumData::new(enum_def, variants)) 126 Arc::new(EnumData::new(&*enum_def, variants))
111 } 127 }
112} 128}
113 129
@@ -139,14 +155,10 @@ impl EnumVariantData {
139 let syntax = db.file_item(def_loc.source_item_id); 155 let syntax = db.file_item(def_loc.source_item_id);
140 let variant_def = ast::EnumVariant::cast(&syntax) 156 let variant_def = ast::EnumVariant::cast(&syntax)
141 .expect("enum variant def should point to EnumVariant node"); 157 .expect("enum variant def should point to EnumVariant node");
142 let enum_node = syntax 158 let enum_def = variant_def.parent_enum();
143 .parent() 159 let e = Enum::from_ast(db, def_loc.module, def_loc.source_item_id.file_id, enum_def);
144 .expect("enum variant should have enum variant list ancestor") 160
145 .parent() 161 Arc::new(EnumVariantData::new(variant_def, e))
146 .expect("enum variant list should have enum ancestor");
147 let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
148
149 Arc::new(EnumVariantData::new(variant_def, Enum::new(enum_def_id)))
150 } 162 }
151} 163}
152 164