diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-09 14:17:55 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-09 14:17:55 +0100 |
commit | 793f7e69f298ccb14936a33c59d7df1527b39af6 (patch) | |
tree | b3d92530c2f9b2caa4015f70803008de47ec088f /crates/ra_hir | |
parent | cc2f2a74f66a131bcee63656ec94424b61895ec5 (diff) | |
parent | aa2f58550a3ea135035118da92a0d7733bad2e1a (diff) |
Merge #1976
1976: Add `module` methods r=matklad a=viorina
Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-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 | } | ||