]> git.lizzy.rs Git - rust.git/commitdiff
add `module` methods
authorEkaterina Babshukova <ekaterina.babshukova@yandex.ru>
Wed, 9 Oct 2019 11:59:47 +0000 (14:59 +0300)
committerEkaterina Babshukova <ekaterina.babshukova@yandex.ru>
Wed, 9 Oct 2019 11:59:47 +0000 (14:59 +0300)
crates/ra_hir/src/adt.rs
crates/ra_hir/src/code_model.rs

index 99d28621509a673b2e23c0891a21526d4cbf19d3..3e9cd3c63d46f8d96f2104362e504e2e375bee9b 100644 (file)
@@ -9,7 +9,7 @@
 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 @@ pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
         }
     }
 
-    pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
+    pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
         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<VariantData> {
         match self {
             VariantDef::Struct(it) => it.variant_data(db),
index 58db6832d1a5c786bb538a6d1bba062b11492b16..8055a07dbe26396a8bd1105a09dbf6fdff7f6db9 100644 (file)
@@ -569,6 +569,14 @@ pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
             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 @@ pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
         ImplBlock::containing(module_impls, self.into())
     }
 
+    pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
+        db.trait_items_index(self.module(db)).get_parent_trait(self.into())
+    }
+
+    pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
+        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 @@ fn from(item: AssocItem) -> Self {
         }
     }
 }
+
+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),
+        }
+    }
+}