diff options
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 32 |
1 files changed, 32 insertions, 0 deletions
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 | } | ||