aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs44
1 files changed, 37 insertions, 7 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index e3a7e8e3c..8eb3c577d 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -5,7 +5,7 @@ pub(crate) mod docs;
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use ra_db::{CrateId, Edition, FileId, SourceRootId}; 8use ra_db::{CrateId, Edition, FileId};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
10 10
11use crate::{ 11use crate::{
@@ -24,7 +24,7 @@ use crate::{
24 U8, USIZE, 24 U8, USIZE,
25 }, 25 },
26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace}, 26 nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
27 resolve::{Resolver, TypeNs}, 27 resolve::{Resolver, Scope, TypeNs},
28 traits::TraitData, 28 traits::TraitData,
29 ty::{ 29 ty::{
30 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, 30 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness},
@@ -76,10 +76,8 @@ impl Crate {
76 crate_graph.edition(self.crate_id) 76 crate_graph.edition(self.crate_id)
77 } 77 }
78 78
79 // FIXME: should this be in source_binder? 79 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
80 pub fn source_root_crates(db: &impl DefDatabase, source_root: SourceRootId) -> Vec<Crate> { 80 db.crate_graph().iter().map(|crate_id| Crate { crate_id }).collect()
81 let crate_ids = db.source_root_crates(source_root);
82 crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect()
83 } 81 }
84} 82}
85 83
@@ -465,7 +463,7 @@ impl Enum {
465 // ...and add generic params, if present 463 // ...and add generic params, if present
466 let p = self.generic_params(db); 464 let p = self.generic_params(db);
467 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r }; 465 let r = if !p.params.is_empty() { r.push_generic_params_scope(p) } else { r };
468 r 466 r.push_scope(Scope::AdtScope(self.into()))
469 } 467 }
470} 468}
471 469
@@ -569,6 +567,14 @@ impl DefWithBody {
569 DefWithBody::Static(s) => s.krate(db), 567 DefWithBody::Static(s) => s.krate(db),
570 } 568 }
571 } 569 }
570
571 pub fn module(self, db: &impl HirDatabase) -> Module {
572 match self {
573 DefWithBody::Const(c) => c.module(db),
574 DefWithBody::Function(f) => f.module(db),
575 DefWithBody::Static(s) => s.module(db),
576 }
577 }
572} 578}
573 579
574pub trait HasBody: Copy { 580pub trait HasBody: Copy {
@@ -789,6 +795,20 @@ impl Const {
789 ImplBlock::containing(module_impls, self.into()) 795 ImplBlock::containing(module_impls, self.into())
790 } 796 }
791 797
798 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
799 db.trait_items_index(self.module(db)).get_parent_trait(self.into())
800 }
801
802 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
803 if let Some(impl_block) = self.impl_block(db) {
804 Some(impl_block.into())
805 } else if let Some(trait_) = self.parent_trait(db) {
806 Some(trait_.into())
807 } else {
808 None
809 }
810 }
811
792 // FIXME: move to a more general type for 'body-having' items 812 // FIXME: move to a more general type for 'body-having' items
793 /// Builds a resolver for code inside this item. 813 /// Builds a resolver for code inside this item.
794 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { 814 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
@@ -1075,3 +1095,13 @@ impl From<AssocItem> for crate::generics::GenericDef {
1075 } 1095 }
1076 } 1096 }
1077} 1097}
1098
1099impl AssocItem {
1100 pub fn module(self, db: &impl DefDatabase) -> Module {
1101 match self {
1102 AssocItem::Function(f) => f.module(db),
1103 AssocItem::Const(c) => c.module(db),
1104 AssocItem::TypeAlias(t) => t.module(db),
1105 }
1106 }
1107}