mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-05 03:23:25 +00:00
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.
This commit is contained in:
parent
978de5cf8b
commit
e78286c8e8
@ -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<Name>,
|
||||
// TODO: keep track of names also since we already have them?
|
||||
// then we won't need additional db lookups
|
||||
pub(crate) variants: Option<Vec<EnumVariant>>,
|
||||
pub(crate) variants: Vec<(Name, EnumVariant)>,
|
||||
}
|
||||
|
||||
impl EnumData {
|
||||
fn new(enum_def: &ast::EnumDef, variants: Option<Vec<EnumVariant>>) -> 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)))
|
||||
}
|
||||
}
|
||||
|
@ -201,7 +201,7 @@ impl Enum {
|
||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
||||
}
|
||||
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Option<Vec<EnumVariant>>> {
|
||||
pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> {
|
||||
Ok(db.enum_data(self.def_id)?.variants.clone())
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
});
|
||||
let matching_variant =
|
||||
e.variants(db)?.into_iter().find(|(n, _variant)| n == name);
|
||||
|
||||
if let Some(Some(variant)) = matching_variant {
|
||||
if let Some((_n, variant)) = matching_variant {
|
||||
return Ok(PerNs::both(variant.def_id(), e.def_id()));
|
||||
} else {
|
||||
return Ok(PerNs::none());
|
||||
|
@ -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())
|
||||
.for_each(|(variant_name, _variant)| {
|
||||
CompletionItem::new(CompletionKind::Reference, variant_name.to_string())
|
||||
.kind(CompletionItemKind::EnumVariant)
|
||||
.add_to(acc)
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user