diff options
Diffstat (limited to 'crates/ra_hir/src/krate.rs')
-rw-r--r-- | crates/ra_hir/src/krate.rs | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/crates/ra_hir/src/krate.rs b/crates/ra_hir/src/krate.rs index 367ddbd21..61007cc29 100644 --- a/crates/ra_hir/src/krate.rs +++ b/crates/ra_hir/src/krate.rs | |||
@@ -1,15 +1,37 @@ | |||
1 | use crate::FileId; | 1 | use crate::{HirDatabase, Module, Cancelable}; |
2 | 2 | ||
3 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 3 | pub use ra_db::CrateId; |
4 | pub struct CrateId(u32); | ||
5 | 4 | ||
5 | /// hir::Crate describes a single crate. It's the main inteface with which | ||
6 | /// crate's dependencies interact. Mostly, it should be just a proxy for the | ||
7 | /// root module. | ||
6 | #[derive(Debug)] | 8 | #[derive(Debug)] |
7 | pub struct Crate { | 9 | pub struct Crate { |
8 | root: FileId, | 10 | crate_id: CrateId, |
9 | } | 11 | } |
10 | 12 | ||
11 | impl Crate { | 13 | impl Crate { |
12 | pub fn dependencies(&self) -> Vec<CrateId> { | 14 | pub(crate) fn new(crate_id: CrateId) -> Crate { |
13 | Vec::new() | 15 | Crate { crate_id } |
16 | } | ||
17 | pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<Crate> { | ||
18 | let crate_graph = db.crate_graph(); | ||
19 | crate_graph | ||
20 | .dependencies(self.crate_id) | ||
21 | .map(|dep| Crate::new(dep.crate_id())) | ||
22 | .collect() | ||
23 | } | ||
24 | pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | ||
25 | let crate_graph = db.crate_graph(); | ||
26 | let file_id = crate_graph.crate_root(self.crate_id); | ||
27 | let source_root_id = db.file_source_root(file_id); | ||
28 | let module_tree = db.module_tree(source_root_id)?; | ||
29 | // FIXME: teach module tree about crate roots instead of guessing | ||
30 | let (module_id, _) = ctry!(module_tree | ||
31 | .modules_with_sources() | ||
32 | .find(|(_, src)| src.file_id() == file_id)); | ||
33 | |||
34 | let module = Module::new(db, source_root_id, module_id)?; | ||
35 | Ok(Some(module)) | ||
14 | } | 36 | } |
15 | } | 37 | } |