From 56b2138d82620db946fe08ddc164c5e7e22be625 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 18:46:02 +0300 Subject: show field types in completion --- crates/ra_hir/src/adt.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 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 602e7db74..d30390f25 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -4,7 +4,7 @@ use ra_db::Cancelable; use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; use crate::{ - DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, + DefId, Name, AsName, Struct, Enum, HirDatabase, DefKind, type_ref::TypeRef, }; @@ -12,6 +12,10 @@ impl Struct { pub(crate) fn new(def_id: DefId) -> Self { Struct { def_id } } + + pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { + Ok(db.struct_data(self.def_id)?.variant_data.clone()) + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -83,6 +87,51 @@ impl EnumData { } } +/// A single field of an enum variant or struct +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StructField { + pub(crate) name: Name, + pub(crate) type_ref: TypeRef, +} + +/// Fields of an enum variant or struct +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum VariantData { + Struct(Vec), + Tuple(Vec), + Unit, +} + +impl VariantData { + pub fn fields(&self) -> &[StructField] { + match self { + VariantData::Struct(fields) | VariantData::Tuple(fields) => fields, + _ => &[], + } + } + + pub fn is_struct(&self) -> bool { + match self { + VariantData::Struct(..) => true, + _ => false, + } + } + + pub fn is_tuple(&self) -> bool { + match self { + VariantData::Tuple(..) => true, + _ => false, + } + } + + pub fn is_unit(&self) -> bool { + match self { + VariantData::Unit => true, + _ => false, + } + } +} + impl VariantData { fn new(flavor: StructFlavor) -> Self { match flavor { @@ -114,7 +163,7 @@ impl VariantData { pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { self.fields() .iter() - .find(|f| f.name() == field_name) - .map(|f| f.type_ref()) + .find(|f| f.name == *field_name) + .map(|f| &f.type_ref) } } -- cgit v1.2.3