aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_impl.rs')
-rw-r--r--crates/ra_hir/src/code_model_impl.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
new file mode 100644
index 000000000..75d4e04c1
--- /dev/null
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -0,0 +1,34 @@
1use ra_db::{CrateId, Cancelable};
2
3use crate::{Module, HirFileId, db::HirDatabase, Crate, CrateDependency, AsName};
4
5impl Crate {
6 pub(crate) fn new(crate_id: CrateId) -> Crate {
7 Crate { crate_id }
8 }
9 pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
10 let crate_graph = db.crate_graph();
11 crate_graph
12 .dependencies(self.crate_id)
13 .map(|dep| {
14 let krate = Crate::new(dep.crate_id());
15 let name = dep.as_name();
16 CrateDependency { krate, name }
17 })
18 .collect()
19 }
20 pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
21 let crate_graph = db.crate_graph();
22 let file_id = crate_graph.crate_root(self.crate_id);
23 let source_root_id = db.file_source_root(file_id);
24 let file_id = HirFileId::from(file_id);
25 let module_tree = db.module_tree(source_root_id)?;
26 // FIXME: teach module tree about crate roots instead of guessing
27 let (module_id, _) = ctry!(module_tree
28 .modules_with_sources()
29 .find(|(_, src)| src.file_id() == file_id));
30
31 let module = Module::new(db, source_root_id, module_id)?;
32 Ok(Some(module))
33 }
34}