diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 6b13b464d24..e0f5156422e 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -3,11 +3,14 @@ use std::sync::Arc; -use ra_syntax::ast::{self, NameOwner, StructFlavor}; +use ra_syntax::{ + TreeArc, + ast::{self, NameOwner, StructFlavor} +}; use crate::{ Name, AsName, Struct, Enum, EnumVariant, Crate, - HirDatabase, + HirDatabase, HirFileId, type_ref::TypeRef, ids::LocationCtx, }; @@ -55,6 +58,24 @@ pub(crate) fn struct_data_query(db: &impl HirDatabase, struct_: Struct) -> Arc impl Iterator { + enum_def + .variant_list() + .into_iter() + .flat_map(|it| it.variants()) +} + +impl EnumVariant { + pub fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { + let (file_id, enum_def) = self.parent.source(db); + let var = variants(&*enum_def) + .nth(self.idx as usize) + .unwrap() + .to_owned(); + (file_id, var) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub(crate) name: Option, @@ -68,22 +89,19 @@ fn new(enum_def: &ast::EnumDef, variants: Vec<(Name, EnumVariant)>) -> Self { } pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc { - let (file_id, enum_def) = e.source(db); - let module = e.module(db); - let ctx = LocationCtx::new(db, module, file_id); - let variants = if let Some(vl) = enum_def.variant_list() { - vl.variants() - .filter_map(|variant_def| { - let name = variant_def.name()?.as_name(); - let var = EnumVariant { - id: ctx.to_def(variant_def), - }; - Some((name, var)) - }) - .collect() - } else { - Vec::new() - }; + let (_file_id, enum_def) = e.source(db); + let variants = variants(&*enum_def) + .enumerate() + .filter_map(|(idx, variant_def)| { + let name = variant_def.name()?.as_name(); + let var = EnumVariant { + parent: e, + idx: idx as u32, + }; + Some((name, var)) + }) + .collect(); + Arc::new(EnumData::new(&*enum_def, variants)) } } diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index a469ad47709..4fc47aba5d8 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -15,7 +15,7 @@ generics::GenericParams, docs::{Documentation, Docs, docs_from_ast}, module_tree::ModuleId, - ids::{FunctionId, StructId, EnumId, EnumVariantId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, + ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -269,18 +269,19 @@ fn docs(&self, db: &impl HirDatabase) -> Option { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct EnumVariant { - pub(crate) id: EnumVariantId, + pub(crate) parent: Enum, + pub(crate) idx: u32, } impl EnumVariant { pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { - self.id.source(db) + self.source_impl(db) } pub fn module(&self, db: &impl HirDatabase) -> Module { - self.id.module(db) + self.parent.module(db) } - pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum { - db.enum_variant_data(*self).parent_enum.clone() + pub fn parent_enum(&self, _db: &impl HirDatabase) -> Enum { + self.parent } pub fn name(&self, db: &impl HirDatabase) -> Option { diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b125a151840..5272656ec2c 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -18,7 +18,6 @@ pub struct HirInterner { fns: LocationIntener, FunctionId>, structs: LocationIntener, StructId>, enums: LocationIntener, EnumId>, - enum_variants: LocationIntener, EnumVariantId>, consts: LocationIntener, ConstId>, statics: LocationIntener, StaticId>, traits: LocationIntener, TraitId>, @@ -31,7 +30,6 @@ pub fn len(&self) -> usize { + self.fns.len() + self.structs.len() + self.enums.len() - + self.enum_variants.len() + self.consts.len() + self.statics.len() + self.traits.len() @@ -261,15 +259,6 @@ fn interner(interner: &HirInterner) -> &LocationIntener, S } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct EnumVariantId(RawId); -impl_arena_id!(EnumVariantId); -impl AstItemDef for EnumVariantId { - fn interner(interner: &HirInterner) -> &LocationIntener, Self> { - &interner.enum_variants - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ConstId(RawId); impl_arena_id!(ConstId); @@ -342,9 +331,7 @@ fn init(&mut self, source_file: &SourceFile) { // change parent's id. This means that, say, adding a new function to a // trait does not chage ids of top-level items, which helps caching. bfs(source_file.syntax(), |it| { - if let Some(enum_variant) = ast::EnumVariant::cast(it) { - self.alloc(enum_variant.syntax().to_owned()); - } else if let Some(module_item) = ast::ModuleItem::cast(it) { + if let Some(module_item) = ast::ModuleItem::cast(it) { self.alloc(module_item.syntax().to_owned()); } else if let Some(macro_call) = ast::MacroCall::cast(it) { self.alloc(macro_call.syntax().to_owned());