aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/krate.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/krate.rs')
-rw-r--r--crates/ra_hir/src/krate.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/crates/ra_hir/src/krate.rs b/crates/ra_hir/src/krate.rs
new file mode 100644
index 000000000..1196dcef1
--- /dev/null
+++ b/crates/ra_hir/src/krate.rs
@@ -0,0 +1,48 @@
1use ra_syntax::SmolStr;
2pub use ra_db::CrateId;
3
4use crate::{HirDatabase, Module, Cancelable};
5
6/// hir::Crate describes a single crate. It's the main inteface with which
7/// crate's dependencies interact. Mostly, it should be just a proxy for the
8/// root module.
9#[derive(Debug)]
10pub struct Crate {
11 crate_id: CrateId,
12}
13
14#[derive(Debug)]
15pub struct CrateDependency {
16 pub krate: Crate,
17 pub name: SmolStr,
18}
19
20impl Crate {
21 pub(crate) fn new(crate_id: CrateId) -> Crate {
22 Crate { crate_id }
23 }
24 pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
25 let crate_graph = db.crate_graph();
26 crate_graph
27 .dependencies(self.crate_id)
28 .map(|dep| {
29 let krate = Crate::new(dep.crate_id());
30 let name = dep.name.clone();
31 CrateDependency { krate, name }
32 })
33 .collect()
34 }
35 pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
36 let crate_graph = db.crate_graph();
37 let file_id = crate_graph.crate_root(self.crate_id);
38 let source_root_id = db.file_source_root(file_id);
39 let module_tree = db.module_tree(source_root_id)?;
40 // FIXME: teach module tree about crate roots instead of guessing
41 let (module_id, _) = ctry!(module_tree
42 .modules_with_sources()
43 .find(|(_, src)| src.file_id() == file_id));
44
45 let module = Module::new(db, source_root_id, module_id)?;
46 Ok(Some(module))
47 }
48}