From 11122e29b7ec5bc2e08822deaa6fdf9a1cc8ffca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 21:06:08 +0300 Subject: completion for enum variants --- crates/ra_hir/src/adt.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_hir/src/adt.rs') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 65c461148..6cdaa1888 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -73,6 +73,10 @@ impl Enum { pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.enum_data(self.def_id)?.name.clone()) } + + pub fn variants(&self, db: &impl HirDatabase) -> Cancelable)>> { + Ok(db.enum_data(self.def_id)?.variants.clone()) + } } #[derive(Debug, Clone, PartialEq, Eq)] -- cgit v1.2.3 From 792899587647f5aa0293c2588173677682187c0a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 21:34:58 +0300 Subject: nameify structs&enums --- crates/ra_hir/src/adt.rs | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'crates/ra_hir/src/adt.rs') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 6cdaa1888..e839a5a90 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -1,10 +1,10 @@ use std::sync::Arc; -use ra_syntax::{SmolStr, ast::{self, NameOwner, StructFlavor}}; +use ra_syntax::ast::{self, NameOwner, StructFlavor}; use crate::{ - DefId, Cancelable, - db::{HirDatabase}, + DefId, Cancelable, Name, AsName, + db::HirDatabase, type_ref::TypeRef, }; @@ -29,26 +29,26 @@ impl Struct { Ok(db.struct_data(self.def_id)?) } - pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.struct_data(self.def_id)?.name.clone()) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructData { - name: Option, + name: Option, variant_data: Arc, } impl StructData { pub(crate) fn new(struct_def: ast::StructDef) -> StructData { - let name = struct_def.name().map(|n| n.text()); + let name = struct_def.name().map(|n| n.as_name()); let variant_data = VariantData::new(struct_def.flavor()); let variant_data = Arc::new(variant_data); StructData { name, variant_data } } - pub fn name(&self) -> Option<&SmolStr> { + pub fn name(&self) -> Option<&Name> { self.name.as_ref() } @@ -70,31 +70,29 @@ impl Enum { self.def_id } - pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { 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()) } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct EnumData { - name: Option, - variants: Vec<(SmolStr, Arc)>, + name: Option, + variants: Vec<(Name, Arc)>, } impl EnumData { pub(crate) fn new(enum_def: ast::EnumDef) -> Self { - let name = enum_def.name().map(|n| n.text()); + let name = enum_def.name().map(|n| n.as_name()); let variants = if let Some(evl) = enum_def.variant_list() { evl.variants() .map(|v| { ( - v.name() - .map(|n| n.text()) - .unwrap_or_else(|| SmolStr::new("[error]")), + v.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), Arc::new(VariantData::new(v.flavor())), ) }) @@ -109,12 +107,12 @@ impl EnumData { /// A single field of an enum variant or struct #[derive(Debug, Clone, PartialEq, Eq)] pub struct StructField { - name: SmolStr, + name: Name, type_ref: TypeRef, } impl StructField { - pub fn name(&self) -> SmolStr { + pub fn name(&self) -> Name { self.name.clone() } pub fn type_ref(&self) -> &TypeRef { @@ -138,7 +136,7 @@ impl VariantData { .fields() .enumerate() .map(|(i, fd)| StructField { - name: SmolStr::new(i.to_string()), + name: Name::tuple_field_name(i), type_ref: TypeRef::from_ast_opt(fd.type_ref()), }) .collect(); @@ -148,10 +146,7 @@ impl VariantData { let fields = fl .fields() .map(|fd| StructField { - name: fd - .name() - .map(|n| n.text()) - .unwrap_or_else(|| SmolStr::new("[error]")), + name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), type_ref: TypeRef::from_ast_opt(fd.type_ref()), }) .collect(); @@ -161,10 +156,10 @@ impl VariantData { } } - pub(crate) fn get_field_type_ref(&self, field_name: &str) -> Option<&TypeRef> { + pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { self.fields() .iter() - .find(|f| f.name == field_name) + .find(|f| f.name == *field_name) .map(|f| &f.type_ref) } -- cgit v1.2.3