|
1 | 1 | //! FIXME: write short doc here
|
2 |
| -use either::Either; |
3 |
| - |
4 | 2 | use hir_def::{
|
5 |
| - child_from_source::ChildFromSource, nameres::ModuleSource, AstItemDef, EnumVariantId, ImplId, |
6 |
| - LocationCtx, ModuleId, TraitId, VariantId, |
| 3 | + child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, |
| 4 | + EnumVariantId, LocationCtx, ModuleId, VariantId, |
7 | 5 | };
|
8 | 6 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
|
9 | 7 | use ra_syntax::{
|
@@ -53,35 +51,39 @@ impl FromSource for Trait {
|
53 | 51 | impl FromSource for Function {
|
54 | 52 | type Ast = ast::FnDef;
|
55 | 53 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
56 |
| - Container::find(db, src.as_ref().map(|it| it.syntax()))? |
57 |
| - .child_from_source(db, src) |
| 54 | + Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::FUNCTION] |
| 55 | + .get(&src) |
| 56 | + .copied() |
58 | 57 | .map(Function::from)
|
59 | 58 | }
|
60 | 59 | }
|
61 | 60 |
|
62 | 61 | impl FromSource for Const {
|
63 | 62 | type Ast = ast::ConstDef;
|
64 | 63 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
65 |
| - Container::find(db, src.as_ref().map(|it| it.syntax()))? |
66 |
| - .child_from_source(db, src) |
| 64 | + Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::CONST] |
| 65 | + .get(&src) |
| 66 | + .copied() |
67 | 67 | .map(Const::from)
|
68 | 68 | }
|
69 | 69 | }
|
70 | 70 | impl FromSource for Static {
|
71 | 71 | type Ast = ast::StaticDef;
|
72 | 72 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
73 |
| - match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
74 |
| - Container::Module(it) => it.id.child_from_source(db, src).map(Static::from), |
75 |
| - Container::Trait(_) | Container::ImplBlock(_) => None, |
76 |
| - } |
| 73 | + Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::STATIC] |
| 74 | + .get(&src) |
| 75 | + .copied() |
| 76 | + .map(Static::from) |
77 | 77 | }
|
78 | 78 | }
|
79 | 79 |
|
80 | 80 | impl FromSource for TypeAlias {
|
81 | 81 | type Ast = ast::TypeAliasDef;
|
82 | 82 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
83 |
| - Container::find(db, src.as_ref().map(|it| it.syntax()))? |
84 |
| - .child_from_source(db, src) |
| 83 | + Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db) |
| 84 | + [keys::TYPE_ALIAS] |
| 85 | + .get(&src) |
| 86 | + .copied() |
85 | 87 | .map(TypeAlias::from)
|
86 | 88 | }
|
87 | 89 | }
|
@@ -116,32 +118,41 @@ impl FromSource for EnumVariant {
|
116 | 118 | let parent_enum = src.value.parent_enum();
|
117 | 119 | let src_enum = InFile { file_id: src.file_id, value: parent_enum };
|
118 | 120 | let parent_enum = Enum::from_source(db, src_enum)?;
|
119 |
| - parent_enum.id.child_from_source(db, src).map(EnumVariant::from) |
| 121 | + parent_enum.id.child_by_source(db)[keys::ENUM_VARIANT] |
| 122 | + .get(&src) |
| 123 | + .copied() |
| 124 | + .map(EnumVariant::from) |
120 | 125 | }
|
121 | 126 | }
|
122 | 127 |
|
123 | 128 | impl FromSource for StructField {
|
124 | 129 | type Ast = FieldSource;
|
125 | 130 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
|
| 131 | + let src = src.as_ref(); |
| 132 | + |
| 133 | + // FIXME this is buggy |
126 | 134 | let variant_id: VariantId = match src.value {
|
127 |
| - FieldSource::Named(ref field) => { |
| 135 | + FieldSource::Named(field) => { |
128 | 136 | let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?;
|
129 | 137 | let src = InFile { file_id: src.file_id, value };
|
130 | 138 | let def = Struct::from_source(db, src)?;
|
131 | 139 | def.id.into()
|
132 | 140 | }
|
133 |
| - FieldSource::Pos(ref field) => { |
| 141 | + FieldSource::Pos(field) => { |
134 | 142 | let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?;
|
135 | 143 | let src = InFile { file_id: src.file_id, value };
|
136 | 144 | let def = EnumVariant::from_source(db, src)?;
|
137 | 145 | EnumVariantId::from(def).into()
|
138 | 146 | }
|
139 | 147 | };
|
140 |
| - let src = src.map(|field_source| match field_source { |
141 |
| - FieldSource::Pos(it) => Either::Left(it), |
142 |
| - FieldSource::Named(it) => Either::Right(it), |
143 |
| - }); |
144 |
| - variant_id.child_from_source(db, src).map(StructField::from) |
| 148 | + |
| 149 | + let dyn_map = variant_id.child_by_source(db); |
| 150 | + match src.value { |
| 151 | + FieldSource::Pos(it) => dyn_map[keys::TUPLE_FIELD].get(&src.with_value(it.clone())), |
| 152 | + FieldSource::Named(it) => dyn_map[keys::RECORD_FIELD].get(&src.with_value(it.clone())), |
| 153 | + } |
| 154 | + .copied() |
| 155 | + .map(StructField::from) |
145 | 156 | }
|
146 | 157 | }
|
147 | 158 |
|
@@ -255,21 +266,12 @@ impl Container {
|
255 | 266 | }
|
256 | 267 | }
|
257 | 268 |
|
258 |
| -impl<CHILD, SOURCE> ChildFromSource<CHILD, SOURCE> for Container |
259 |
| -where |
260 |
| - TraitId: ChildFromSource<CHILD, SOURCE>, |
261 |
| - ImplId: ChildFromSource<CHILD, SOURCE>, |
262 |
| - ModuleId: ChildFromSource<CHILD, SOURCE>, |
263 |
| -{ |
264 |
| - fn child_from_source( |
265 |
| - &self, |
266 |
| - db: &impl DefDatabase, |
267 |
| - child_source: InFile<SOURCE>, |
268 |
| - ) -> Option<CHILD> { |
| 269 | +impl ChildBySource for Container { |
| 270 | + fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { |
269 | 271 | match self {
|
270 |
| - Container::Trait(it) => it.id.child_from_source(db, child_source), |
271 |
| - Container::ImplBlock(it) => it.id.child_from_source(db, child_source), |
272 |
| - Container::Module(it) => it.id.child_from_source(db, child_source), |
| 272 | + Container::Trait(it) => it.id.child_by_source(db), |
| 273 | + Container::ImplBlock(it) => it.id.child_by_source(db), |
| 274 | + Container::Module(it) => it.id.child_by_source(db), |
273 | 275 | }
|
274 | 276 | }
|
275 | 277 | }
|
0 commit comments