aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-08 11:01:45 +0000
committerAleksey Kladov <[email protected]>2019-12-08 11:02:53 +0000
commit08d3166c8b23b5a342f32e088f7e0d6e032ec17b (patch)
treeb46a1eb2d61c81312912200f568239e61ce5a845 /crates/ra_hir
parentffcdd25cc8e7f1fd7fb4e3e8478c4cd1adfbf843 (diff)
Cleanup Crate API
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs48
-rw-r--r--crates/ra_hir/src/debug.rs4
-rw-r--r--crates/ra_hir/src/from_id.rs4
-rw-r--r--crates/ra_hir/src/from_source.rs2
4 files changed, 36 insertions, 22 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 4578a0ba8..3695115e4 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -25,7 +25,7 @@ use hir_expand::{
25 MacroDefId, 25 MacroDefId,
26}; 26};
27use hir_ty::expr::ExprValidator; 27use hir_ty::expr::ExprValidator;
28use ra_db::{CrateId, Edition}; 28use ra_db::{CrateId, Edition, FileId};
29use ra_syntax::ast; 29use ra_syntax::ast;
30 30
31use crate::{ 31use crate::{
@@ -40,7 +40,7 @@ use crate::{
40/// root module. 40/// root module.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub struct Crate { 42pub struct Crate {
43 pub(crate) crate_id: CrateId, 43 pub(crate) id: CrateId,
44} 44}
45 45
46#[derive(Debug)] 46#[derive(Debug)]
@@ -50,33 +50,47 @@ pub struct CrateDependency {
50} 50}
51 51
52impl Crate { 52impl Crate {
53 pub fn crate_id(self) -> CrateId {
54 self.crate_id
55 }
56
57 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { 53 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
58 db.crate_graph() 54 db.crate_graph()
59 .dependencies(self.crate_id) 55 .dependencies(self.id)
60 .map(|dep| { 56 .map(|dep| {
61 let krate = Crate { crate_id: dep.crate_id() }; 57 let krate = Crate { id: dep.crate_id() };
62 let name = dep.as_name(); 58 let name = dep.as_name();
63 CrateDependency { krate, name } 59 CrateDependency { krate, name }
64 }) 60 })
65 .collect() 61 .collect()
66 } 62 }
67 63
64 // FIXME: add `transitive_reverse_dependencies`.
65 pub fn reverse_dependencies(self, db: &impl DefDatabase) -> Vec<Crate> {
66 let crate_graph = db.crate_graph();
67 crate_graph
68 .iter()
69 .filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id))
70 .map(|id| Crate { id })
71 .collect()
72 }
73
68 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 74 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
69 let module_id = db.crate_def_map(self.crate_id).root; 75 let module_id = db.crate_def_map(self.id).root;
70 Some(Module::new(self, module_id)) 76 Some(Module::new(self, module_id))
71 } 77 }
72 78
79 pub fn root_file(self, db: &impl DefDatabase) -> FileId {
80 db.crate_graph().crate_root(self.id)
81 }
82
73 pub fn edition(self, db: &impl DefDatabase) -> Edition { 83 pub fn edition(self, db: &impl DefDatabase) -> Edition {
74 let crate_graph = db.crate_graph(); 84 let crate_graph = db.crate_graph();
75 crate_graph.edition(self.crate_id) 85 crate_graph.edition(self.id)
76 } 86 }
77 87
78 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 88 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
79 db.crate_graph().iter().map(|crate_id| Crate { crate_id }).collect() 89 db.crate_graph().iter().map(|id| Crate { id }).collect()
90 }
91
92 pub fn crate_id(self) -> CrateId {
93 self.id
80 } 94 }
81} 95}
82 96
@@ -115,7 +129,7 @@ pub use hir_def::attr::Attrs;
115 129
116impl Module { 130impl Module {
117 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { 131 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
118 Module { id: ModuleId { krate: krate.crate_id, local_id: crate_module_id } } 132 Module { id: ModuleId { krate: krate.id, local_id: crate_module_id } }
119 } 133 }
120 134
121 /// Name of this module. 135 /// Name of this module.
@@ -133,7 +147,7 @@ impl Module {
133 147
134 /// Returns the crate this module is part of. 148 /// Returns the crate this module is part of.
135 pub fn krate(self) -> Crate { 149 pub fn krate(self) -> Crate {
136 Crate { crate_id: self.id.krate } 150 Crate { id: self.id.krate }
137 } 151 }
138 152
139 /// Topmost parent of this module. Every module has a `crate_root`, but some 153 /// Topmost parent of this module. Every module has a `crate_root`, but some
@@ -878,11 +892,11 @@ pub struct ImplBlock {
878 892
879impl ImplBlock { 893impl ImplBlock {
880 pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplBlock> { 894 pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplBlock> {
881 let impls = db.impls_in_crate(krate.crate_id); 895 let impls = db.impls_in_crate(krate.id);
882 impls.all_impls().map(Self::from).collect() 896 impls.all_impls().map(Self::from).collect()
883 } 897 }
884 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> { 898 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
885 let impls = db.impls_in_crate(krate.crate_id); 899 let impls = db.impls_in_crate(krate.id);
886 impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect() 900 impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
887 } 901 }
888 902
@@ -915,7 +929,7 @@ impl ImplBlock {
915 } 929 }
916 930
917 pub fn krate(&self, db: &impl DefDatabase) -> Crate { 931 pub fn krate(&self, db: &impl DefDatabase) -> Crate {
918 Crate { crate_id: self.module(db).id.krate } 932 Crate { id: self.module(db).id.krate }
919 } 933 }
920} 934}
921 935
@@ -1053,7 +1067,7 @@ impl Type {
1053 krate: Crate, 1067 krate: Crate,
1054 mut callback: impl FnMut(AssocItem) -> Option<T>, 1068 mut callback: impl FnMut(AssocItem) -> Option<T>,
1055 ) -> Option<T> { 1069 ) -> Option<T> {
1056 for krate in self.ty.value.def_crates(db, krate.crate_id)? { 1070 for krate in self.ty.value.def_crates(db, krate.id)? {
1057 let impls = db.impls_in_crate(krate); 1071 let impls = db.impls_in_crate(krate);
1058 1072
1059 for impl_block in impls.lookup_impl_blocks(&self.ty.value) { 1073 for impl_block in impls.lookup_impl_blocks(&self.ty.value) {
diff --git a/crates/ra_hir/src/debug.rs b/crates/ra_hir/src/debug.rs
index 7a2810f71..6cd5c8cb9 100644
--- a/crates/ra_hir/src/debug.rs
+++ b/crates/ra_hir/src/debug.rs
@@ -57,9 +57,9 @@ pub trait HirDebugDatabase {
57impl<DB: HirDebugHelper> HirDebugDatabase for DB { 57impl<DB: HirDebugHelper> HirDebugDatabase for DB {
58 fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 58 fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
59 let mut builder = fmt.debug_tuple("Crate"); 59 let mut builder = fmt.debug_tuple("Crate");
60 match self.crate_name(krate.crate_id) { 60 match self.crate_name(krate.id) {
61 Some(name) => builder.field(&name), 61 Some(name) => builder.field(&name),
62 None => builder.field(&krate.crate_id), 62 None => builder.field(&krate.id),
63 } 63 }
64 .finish() 64 .finish()
65 } 65 }
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index e96a18d12..0398d0ee6 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -14,8 +14,8 @@ use crate::{
14}; 14};
15 15
16impl From<ra_db::CrateId> for Crate { 16impl From<ra_db::CrateId> for Crate {
17 fn from(crate_id: ra_db::CrateId) -> Self { 17 fn from(id: ra_db::CrateId) -> Self {
18 Crate { crate_id } 18 Crate { id }
19 } 19 }
20} 20}
21 21
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 4acc038e4..071ca2e5a 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -95,7 +95,7 @@ impl FromSource for MacroDef {
95 95
96 let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); 96 let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
97 let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; 97 let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
98 let krate = Some(module.krate().crate_id()); 98 let krate = Some(module.krate().id);
99 99
100 let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value))); 100 let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
101 101