diff options
author | Ekaterina Babshukova <[email protected]> | 2019-10-09 12:59:47 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-10-09 12:59:47 +0100 |
commit | aa2f58550a3ea135035118da92a0d7733bad2e1a (patch) | |
tree | bc0c803db3ca24675a8e4bd84c75e86acfc848a0 | |
parent | afc871199d423f7a59c64c7a79191911bd879c89 (diff) |
add `module` methods
-rw-r--r-- | crates/ra_hir/src/adt.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 32 |
2 files changed, 42 insertions, 2 deletions
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}; | |||
9 | use crate::{ | 9 | use crate::{ |
10 | db::{AstDatabase, DefDatabase, HirDatabase}, | 10 | db::{AstDatabase, DefDatabase, HirDatabase}, |
11 | type_ref::TypeRef, | 11 | type_ref::TypeRef, |
12 | AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField, | 12 | AsName, Enum, EnumVariant, FieldSource, HasSource, Module, Name, Source, Struct, StructField, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | impl Struct { | 15 | impl Struct { |
@@ -170,12 +170,20 @@ impl VariantDef { | |||
170 | } | 170 | } |
171 | } | 171 | } |
172 | 172 | ||
173 | pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { | 173 | pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> { |
174 | match self { | 174 | match self { |
175 | VariantDef::Struct(it) => it.field(db, name), | 175 | VariantDef::Struct(it) => it.field(db, name), |
176 | VariantDef::EnumVariant(it) => it.field(db, name), | 176 | VariantDef::EnumVariant(it) => it.field(db, name), |
177 | } | 177 | } |
178 | } | 178 | } |
179 | |||
180 | pub fn module(self, db: &impl HirDatabase) -> Module { | ||
181 | match self { | ||
182 | VariantDef::Struct(it) => it.module(db), | ||
183 | VariantDef::EnumVariant(it) => it.module(db), | ||
184 | } | ||
185 | } | ||
186 | |||
179 | pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { | 187 | pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { |
180 | match self { | 188 | match self { |
181 | VariantDef::Struct(it) => it.variant_data(db), | 189 | 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 { | |||
569 | DefWithBody::Static(s) => s.krate(db), | 569 | DefWithBody::Static(s) => s.krate(db), |
570 | } | 570 | } |
571 | } | 571 | } |
572 | |||
573 | pub fn module(self, db: &impl HirDatabase) -> Module { | ||
574 | match self { | ||
575 | DefWithBody::Const(c) => c.module(db), | ||
576 | DefWithBody::Function(f) => f.module(db), | ||
577 | DefWithBody::Static(s) => s.module(db), | ||
578 | } | ||
579 | } | ||
572 | } | 580 | } |
573 | 581 | ||
574 | pub trait HasBody: Copy { | 582 | pub trait HasBody: Copy { |
@@ -789,6 +797,20 @@ impl Const { | |||
789 | ImplBlock::containing(module_impls, self.into()) | 797 | ImplBlock::containing(module_impls, self.into()) |
790 | } | 798 | } |
791 | 799 | ||
800 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | ||
801 | db.trait_items_index(self.module(db)).get_parent_trait(self.into()) | ||
802 | } | ||
803 | |||
804 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | ||
805 | if let Some(impl_block) = self.impl_block(db) { | ||
806 | Some(impl_block.into()) | ||
807 | } else if let Some(trait_) = self.parent_trait(db) { | ||
808 | Some(trait_.into()) | ||
809 | } else { | ||
810 | None | ||
811 | } | ||
812 | } | ||
813 | |||
792 | // FIXME: move to a more general type for 'body-having' items | 814 | // FIXME: move to a more general type for 'body-having' items |
793 | /// Builds a resolver for code inside this item. | 815 | /// Builds a resolver for code inside this item. |
794 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { | 816 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
@@ -1075,3 +1097,13 @@ impl From<AssocItem> for crate::generics::GenericDef { | |||
1075 | } | 1097 | } |
1076 | } | 1098 | } |
1077 | } | 1099 | } |
1100 | |||
1101 | impl AssocItem { | ||
1102 | pub fn module(self, db: &impl DefDatabase) -> Module { | ||
1103 | match self { | ||
1104 | AssocItem::Function(f) => f.module(db), | ||
1105 | AssocItem::Const(c) => c.module(db), | ||
1106 | AssocItem::TypeAlias(t) => t.module(db), | ||
1107 | } | ||
1108 | } | ||
1109 | } | ||