From e78286c8e88d31045d354320a1d29bbd75405027 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Thu, 10 Jan 2019 02:07:42 +0100 Subject: Save variant names in EnumData to reduce needless queries We already have their names when anyway, and when in all (current) situations where we're interested in an Enum's variants, we want their names. --- crates/ra_hir/src/adt.rs | 29 ++++++++++++----------- crates/ra_hir/src/code_model_api.rs | 2 +- crates/ra_hir/src/code_model_impl/module.rs | 18 ++++---------- crates/ra_ide_api/src/completion/complete_path.rs | 15 ++++-------- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index f1b98cdd7..bcb705c24 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -7,13 +7,9 @@ use ra_syntax::{ }; use crate::{ -<<<<<<< HEAD - DefId, Name, AsName, Struct, Enum, HirDatabase, DefKind, -======= DefId, DefLoc, Name, AsName, Struct, Enum, EnumVariant, - VariantData, StructField, HirDatabase, DefKind, + HirDatabase, DefKind, SourceItemId, ->>>>>>> 95ac72a3... Implement type inference for enum variants type_ref::TypeRef, }; @@ -79,13 +75,11 @@ fn get_def_id( #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { pub(crate) name: Option, - // TODO: keep track of names also since we already have them? - // then we won't need additional db lookups - pub(crate) variants: Option>, + pub(crate) variants: Vec<(Name, EnumVariant)>, } impl EnumData { - fn new(enum_def: &ast::EnumDef, variants: Option>) -> Self { + fn new(enum_def: &ast::EnumDef, variants: Vec<(Name, EnumVariant)>) -> Self { let name = enum_def.name().map(|n| n.as_name()); EnumData { name, variants } } @@ -98,14 +92,21 @@ impl EnumData { assert!(def_loc.kind == DefKind::Enum); let syntax = db.file_item(def_loc.source_item_id); let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); - let variants = enum_def.variant_list().map(|vl| { + let variants = if let Some(vl) = enum_def.variant_list() { vl.variants() - .map(|ev| { - let def_id = get_def_id(db, &def_loc, ev.syntax(), DefKind::EnumVariant); - EnumVariant::new(def_id) + .filter_map(|variant_def| { + let name = variant_def.name().map(|n| n.as_name()); + + name.map(|n| { + let def_id = + get_def_id(db, &def_loc, variant_def.syntax(), DefKind::EnumVariant); + (n, EnumVariant::new(def_id)) + }) }) .collect() - }); + } else { + Vec::new() + }; Ok(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 c7d1bf0a6..725bc7d80 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -201,7 +201,7 @@ impl Enum { Ok(db.enum_data(self.def_id)?.name.clone()) } - pub fn variants(&self, db: &impl HirDatabase) -> Cancelable>> { + pub fn variants(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.enum_data(self.def_id)?.variants.clone()) } } diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index d7d62e863..878dc37c8 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -142,20 +142,10 @@ impl Module { Def::Enum(e) => { if segments.len() == idx + 1 { // enum variant - let matching_variant = e.variants(db)?.map(|variants| { - variants - .into_iter() - // FIXME: replace by match lol - .find(|variant| { - variant - .name(db) - .map(|o| o.map(|ref n| n == name)) - .unwrap_or(Some(false)) - .unwrap_or(false) - }) - }); - - if let Some(Some(variant)) = matching_variant { + let matching_variant = + e.variants(db)?.into_iter().find(|(n, _variant)| n == name); + + if let Some((_n, variant)) = matching_variant { return Ok(PerNs::both(variant.def_id(), e.def_id())); } else { return Ok(PerNs::none()); diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 6a55670d1..9bfec88d0 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -23,17 +23,12 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C } hir::Def::Enum(e) => { e.variants(ctx.db)? - .unwrap_or(vec![]) .into_iter() - .for_each(|variant| { - let variant_name = variant.name(ctx.db); - - if let Ok(Some(name)) = variant_name { - CompletionItem::new(CompletionKind::Reference, name.to_string()) - .kind(CompletionItemKind::EnumVariant) - .add_to(acc) - } - }) + .for_each(|(variant_name, _variant)| { + CompletionItem::new(CompletionKind::Reference, variant_name.to_string()) + .kind(CompletionItemKind::EnumVariant) + .add_to(acc) + }); } _ => return Ok(()), }; -- cgit v1.2.3