From aa2f58550a3ea135035118da92a0d7733bad2e1a Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Wed, 9 Oct 2019 14:59:47 +0300 Subject: add `module` methods --- crates/ra_hir/src/adt.rs | 12 ++++++++++-- crates/ra_hir/src/code_model.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 99d286215..3e9cd3c63 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -9,7 +9,7 @@ use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, type_ref::TypeRef, - AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField, + AsName, Enum, EnumVariant, FieldSource, HasSource, Module, Name, Source, Struct, StructField, }; impl Struct { @@ -170,12 +170,20 @@ impl VariantDef { } } - pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option { + pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option { match self { VariantDef::Struct(it) => it.field(db, name), VariantDef::EnumVariant(it) => it.field(db, name), } } + + pub fn module(self, db: &impl HirDatabase) -> Module { + match self { + VariantDef::Struct(it) => it.module(db), + VariantDef::EnumVariant(it) => it.module(db), + } + } + pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { match self { VariantDef::Struct(it) => it.variant_data(db), diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 58db6832d..8055a07db 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -569,6 +569,14 @@ impl DefWithBody { DefWithBody::Static(s) => s.krate(db), } } + + pub fn module(self, db: &impl HirDatabase) -> Module { + match self { + DefWithBody::Const(c) => c.module(db), + DefWithBody::Function(f) => f.module(db), + DefWithBody::Static(s) => s.module(db), + } + } } pub trait HasBody: Copy { @@ -789,6 +797,20 @@ impl Const { ImplBlock::containing(module_impls, self.into()) } + pub fn parent_trait(self, db: &impl DefDatabase) -> Option { + db.trait_items_index(self.module(db)).get_parent_trait(self.into()) + } + + pub fn container(self, db: &impl DefDatabase) -> Option { + if let Some(impl_block) = self.impl_block(db) { + Some(impl_block.into()) + } else if let Some(trait_) = self.parent_trait(db) { + Some(trait_.into()) + } else { + None + } + } + // FIXME: move to a more general type for 'body-having' items /// Builds a resolver for code inside this item. pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { @@ -1075,3 +1097,13 @@ impl From for crate::generics::GenericDef { } } } + +impl AssocItem { + pub fn module(self, db: &impl DefDatabase) -> Module { + match self { + AssocItem::Function(f) => f.module(db), + AssocItem::Const(c) => c.module(db), + AssocItem::TypeAlias(t) => t.module(db), + } + } +} -- cgit v1.2.3