diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 249a4aba9..118562984 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -11,7 +11,7 @@ use crate::{ | |||
11 | db::HirDatabase, | 11 | db::HirDatabase, |
12 | expr::BodySyntaxMapping, | 12 | expr::BodySyntaxMapping, |
13 | ty::{InferenceResult, VariantDef}, | 13 | ty::{InferenceResult, VariantDef}, |
14 | adt::{VariantData, EnumVariantId}, | 14 | adt::{EnumVariantId, StructFieldId}, |
15 | generics::GenericParams, | 15 | generics::GenericParams, |
16 | docs::{Documentation, Docs, docs_from_ast}, | 16 | docs::{Documentation, Docs, docs_from_ast}, |
17 | module_tree::ModuleId, | 17 | module_tree::ModuleId, |
@@ -177,19 +177,25 @@ impl Module { | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 180 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
181 | pub struct StructField { | 181 | pub struct StructField { |
182 | parent: VariantDef, | 182 | parent: VariantDef, |
183 | name: Name, | 183 | pub(crate) id: StructFieldId, |
184 | } | 184 | } |
185 | 185 | ||
186 | impl StructField { | 186 | impl StructField { |
187 | pub fn name(&self) -> &Name { | 187 | pub fn name(&self, db: &impl HirDatabase) -> Name { |
188 | &self.name | 188 | self.parent.variant_data(db).fields().unwrap()[self.id] |
189 | .name | ||
190 | .clone() | ||
189 | } | 191 | } |
190 | 192 | ||
191 | pub fn ty(&self, db: &impl HirDatabase) -> Option<Ty> { | 193 | pub fn ty(&self, db: &impl HirDatabase) -> Ty { |
192 | db.type_for_field(self.parent, self.name.clone()) | 194 | db.type_for_field(*self) |
195 | } | ||
196 | |||
197 | pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { | ||
198 | self.parent | ||
193 | } | 199 | } |
194 | } | 200 | } |
195 | 201 | ||
@@ -215,14 +221,28 @@ impl Struct { | |||
215 | db.struct_data(*self) | 221 | db.struct_data(*self) |
216 | .variant_data | 222 | .variant_data |
217 | .fields() | 223 | .fields() |
218 | .iter() | 224 | .into_iter() |
219 | .map(|it| StructField { | 225 | .flat_map(|it| it.iter()) |
226 | .map(|(id, _)| StructField { | ||
220 | parent: (*self).into(), | 227 | parent: (*self).into(), |
221 | name: it.name.clone(), | 228 | id, |
222 | }) | 229 | }) |
223 | .collect() | 230 | .collect() |
224 | } | 231 | } |
225 | 232 | ||
233 | pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | ||
234 | db.struct_data(*self) | ||
235 | .variant_data | ||
236 | .fields() | ||
237 | .into_iter() | ||
238 | .flat_map(|it| it.iter()) | ||
239 | .find(|(_id, data)| data.name == *name) | ||
240 | .map(|(id, _)| StructField { | ||
241 | parent: (*self).into(), | ||
242 | id, | ||
243 | }) | ||
244 | } | ||
245 | |||
226 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { | 246 | pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { |
227 | db.generic_params((*self).into()) | 247 | db.generic_params((*self).into()) |
228 | } | 248 | } |
@@ -300,22 +320,29 @@ impl EnumVariant { | |||
300 | db.enum_data(self.parent).variants[self.id].name.clone() | 320 | db.enum_data(self.parent).variants[self.id].name.clone() |
301 | } | 321 | } |
302 | 322 | ||
303 | pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> { | ||
304 | db.enum_data(self.parent).variants[self.id] | ||
305 | .variant_data | ||
306 | .clone() | ||
307 | } | ||
308 | |||
309 | pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { | 323 | pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> { |
310 | self.variant_data(db) | 324 | self.variant_data(db) |
311 | .fields() | 325 | .fields() |
312 | .iter() | 326 | .into_iter() |
313 | .map(|it| StructField { | 327 | .flat_map(|it| it.iter()) |
328 | .map(|(id, _)| StructField { | ||
314 | parent: (*self).into(), | 329 | parent: (*self).into(), |
315 | name: it.name.clone(), | 330 | id, |
316 | }) | 331 | }) |
317 | .collect() | 332 | .collect() |
318 | } | 333 | } |
334 | |||
335 | pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | ||
336 | self.variant_data(db) | ||
337 | .fields() | ||
338 | .into_iter() | ||
339 | .flat_map(|it| it.iter()) | ||
340 | .find(|(_id, data)| data.name == *name) | ||
341 | .map(|(id, _)| StructField { | ||
342 | parent: (*self).into(), | ||
343 | id, | ||
344 | }) | ||
345 | } | ||
319 | } | 346 | } |
320 | 347 | ||
321 | impl Docs for EnumVariant { | 348 | impl Docs for EnumVariant { |