From 2ee2fba752c0f03ab8d1b7009bc56cc785738413 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 11:35:38 +0300 Subject: switch to positional enum variants --- crates/ra_hir/src/adt.rs | 54 ++++++++++++++++++++++++------------- crates/ra_hir/src/code_model_api.rs | 13 ++++----- crates/ra_hir/src/ids.rs | 15 +---------- 3 files changed, 44 insertions(+), 38 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 6b13b464d..e0f515642 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 @@ impl StructData { } } +fn variants(enum_def: &ast::EnumDef) -> 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 @@ impl EnumData { } 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 a469ad477..4fc47aba5 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -15,7 +15,7 @@ use crate::{ 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 @@ impl Docs for Enum { #[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 b125a1518..5272656ec 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 @@ impl HirInterner { + 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 @@ impl AstItemDef for EnumId { } } -#[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 @@ impl SourceFileItems { // 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()); -- cgit v1.2.3