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 ++++++++++++++++++++++++++++-- crates/ra_hir/src/code_model_api.rs | 67 +++++++++++-------------------------- crates/ra_hir/src/lib.rs | 2 +- 3 files changed, 72 insertions(+), 52 deletions(-) (limited to 'crates/ra_hir') 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) } } diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 66c016180..fa3e4baa7 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -5,12 +5,13 @@ use ra_db::{CrateId, Cancelable, FileId}; use ra_syntax::{ast, TreePtr, SyntaxNode}; use crate::{ - Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, + Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, type_ref::TypeRef, nameres::ModuleScope, db::HirDatabase, expr::BodySyntaxMapping, ty::InferenceResult, + adt::VariantData, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -137,58 +138,18 @@ impl Module { } } -/// A single field of an enum variant or struct -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct StructField { - pub(crate) name: Name, - pub(crate) type_ref: TypeRef, + struct_: Struct, + name: Name, } impl StructField { pub fn name(&self) -> &Name { &self.name } - - pub fn type_ref(&self) -> &TypeRef { - &self.type_ref - } -} - -/// 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, - } + pub fn ty(&self, db: &impl HirDatabase) -> Cancelable> { + db.type_for_field(self.struct_.def_id, self.name.clone()) } } @@ -206,8 +167,18 @@ impl Struct { Ok(db.struct_data(self.def_id)?.name.clone()) } - pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id)?.variant_data.clone()) + pub fn fields(&self, db: &impl HirDatabase) -> Cancelable> { + let res = db + .struct_data(self.def_id)? + .variant_data + .fields() + .iter() + .map(|it| StructField { + struct_: self.clone(), + name: it.name.clone(), + }) + .collect(); + Ok(res) } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index ca7f395a8..1b6b72c98 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -56,6 +56,6 @@ pub use self::code_model_api::{ Crate, CrateDependency, Def, Module, ModuleSource, Problem, - Struct, Enum, VariantData, StructField, + Struct, Enum, Function, FnSignature, }; -- cgit v1.2.3