diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 50 | ||||
-rw-r--r-- | crates/ra_hir/src/from_id.rs | 19 |
2 files changed, 26 insertions, 43 deletions
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index fc466c1f0..a45c062bf 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -1,13 +1,13 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_def::{HasSource as _, Lookup}; | 3 | use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; |
4 | use ra_syntax::ast::{self, AstNode}; | 4 | use ra_syntax::ast::{self, AstNode}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | db::{AstDatabase, DefDatabase, HirDatabase}, | 7 | db::{AstDatabase, DefDatabase, HirDatabase}, |
8 | ids::AstItemDef, | 8 | ids::AstItemDef, |
9 | Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, | 9 | Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, |
10 | ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, | 10 | ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub use hir_expand::Source; | 13 | pub use hir_expand::Source; |
@@ -46,33 +46,12 @@ impl Module { | |||
46 | impl HasSource for StructField { | 46 | impl HasSource for StructField { |
47 | type Ast = FieldSource; | 47 | type Ast = FieldSource; |
48 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { | 48 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<FieldSource> { |
49 | let var_data = self.parent.variant_data(db); | 49 | let var = VariantId::from(self.parent); |
50 | let fields = var_data.fields().unwrap(); | 50 | let src = var.child_source(db); |
51 | let ss; | 51 | src.map(|it| match it[self.id].clone() { |
52 | let es; | 52 | Either::A(it) => FieldSource::Pos(it), |
53 | let (file_id, struct_kind) = match self.parent { | 53 | Either::B(it) => FieldSource::Named(it), |
54 | VariantDef::Struct(s) => { | 54 | }) |
55 | ss = s.source(db); | ||
56 | (ss.file_id, ss.value.kind()) | ||
57 | } | ||
58 | VariantDef::EnumVariant(e) => { | ||
59 | es = e.source(db); | ||
60 | (es.file_id, es.value.kind()) | ||
61 | } | ||
62 | }; | ||
63 | |||
64 | let field_sources = match struct_kind { | ||
65 | ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(), | ||
66 | ast::StructKind::Record(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), | ||
67 | ast::StructKind::Unit => Vec::new(), | ||
68 | }; | ||
69 | let value = field_sources | ||
70 | .into_iter() | ||
71 | .zip(fields.iter()) | ||
72 | .find(|(_syntax, (id, _))| *id == self.id) | ||
73 | .unwrap() | ||
74 | .0; | ||
75 | Source { file_id, value } | ||
76 | } | 55 | } |
77 | } | 56 | } |
78 | impl HasSource for Struct { | 57 | impl HasSource for Struct { |
@@ -96,18 +75,7 @@ impl HasSource for Enum { | |||
96 | impl HasSource for EnumVariant { | 75 | impl HasSource for EnumVariant { |
97 | type Ast = ast::EnumVariant; | 76 | type Ast = ast::EnumVariant; |
98 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { | 77 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::EnumVariant> { |
99 | let enum_data = db.enum_data(self.parent.id); | 78 | self.parent.id.child_source(db).map(|map| map[self.id].clone()) |
100 | let src = self.parent.id.source(db); | ||
101 | let value = src | ||
102 | .value | ||
103 | .variant_list() | ||
104 | .into_iter() | ||
105 | .flat_map(|it| it.variants()) | ||
106 | .zip(enum_data.variants.iter()) | ||
107 | .find(|(_syntax, (id, _))| *id == self.id) | ||
108 | .unwrap() | ||
109 | .0; | ||
110 | Source { file_id: src.file_id, value } | ||
111 | } | 79 | } |
112 | } | 80 | } |
113 | impl HasSource for Function { | 81 | impl HasSource for Function { |
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index e8ed04056..8900fc1f2 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs | |||
@@ -5,13 +5,13 @@ | |||
5 | 5 | ||
6 | use hir_def::{ | 6 | use hir_def::{ |
7 | AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, | 7 | AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, |
8 | ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, | 8 | ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, VariantId, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | ty::{CallableDef, TypableDef}, | 12 | ty::{CallableDef, TypableDef}, |
13 | Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, | 13 | Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, |
14 | Static, TypeAlias, | 14 | Static, TypeAlias, VariantDef, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | impl From<ra_db::CrateId> for Crate { | 17 | impl From<ra_db::CrateId> for Crate { |
@@ -70,6 +70,12 @@ impl From<EnumVariantId> for EnumVariant { | |||
70 | } | 70 | } |
71 | } | 71 | } |
72 | 72 | ||
73 | impl From<EnumVariant> for EnumVariantId { | ||
74 | fn from(def: EnumVariant) -> Self { | ||
75 | EnumVariantId { parent: def.parent.id, local_id: def.id } | ||
76 | } | ||
77 | } | ||
78 | |||
73 | impl From<ModuleDefId> for ModuleDef { | 79 | impl From<ModuleDefId> for ModuleDef { |
74 | fn from(id: ModuleDefId) -> Self { | 80 | fn from(id: ModuleDefId) -> Self { |
75 | match id { | 81 | match id { |
@@ -219,3 +225,12 @@ impl From<CallableDef> for GenericDefId { | |||
219 | } | 225 | } |
220 | } | 226 | } |
221 | } | 227 | } |
228 | |||
229 | impl From<VariantDef> for VariantId { | ||
230 | fn from(def: VariantDef) -> Self { | ||
231 | match def { | ||
232 | VariantDef::Struct(it) => VariantId::StructId(it.id), | ||
233 | VariantDef::EnumVariant(it) => VariantId::EnumVariantId(it.into()), | ||
234 | } | ||
235 | } | ||
236 | } | ||