show field types in completion

This commit is contained in:
Aleksey Kladov 2019-01-09 18:46:02 +03:00
parent ddf2a8a948
commit 56b2138d82
5 changed files with 88 additions and 61 deletions

View file

@ -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<Arc<VariantData>> {
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<StructField>),
Tuple(Vec<StructField>),
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)
}
}