From fd4456d0ec88e3433a7a8be6f27d8af9afedefe5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 6 Jan 2019 17:33:27 +0300 Subject: flatten module structure --- crates/ra_hir/src/code_model_impl/krate.rs | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 crates/ra_hir/src/code_model_impl/krate.rs (limited to 'crates/ra_hir/src/code_model_impl/krate.rs') diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs new file mode 100644 index 000000000..591a81597 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/krate.rs @@ -0,0 +1,45 @@ +use ra_db::{CrateId, Cancelable}; + +use crate::{ + HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, + db::HirDatabase, +}; + +impl Crate { + pub(crate) fn new(crate_id: CrateId) -> Crate { + Crate { crate_id } + } + pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec { + let crate_graph = db.crate_graph(); + crate_graph + .dependencies(self.crate_id) + .map(|dep| { + let krate = Crate::new(dep.crate_id()); + let name = dep.as_name(); + CrateDependency { krate, name } + }) + .collect() + } + pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable> { + let crate_graph = db.crate_graph(); + let file_id = crate_graph.crate_root(self.crate_id); + let source_root_id = db.file_source_root(file_id); + let file_id = HirFileId::from(file_id); + let module_tree = db.module_tree(source_root_id)?; + // FIXME: teach module tree about crate roots instead of guessing + let (module_id, _) = ctry!(module_tree + .modules_with_sources() + .find(|(_, src)| src.file_id() == file_id)); + + let def_loc = DefLoc { + kind: DefKind::Module, + source_root_id, + module_id, + source_item_id: module_id.source(&module_tree).0, + }; + let def_id = def_loc.id(db); + + let module = Module::new(def_id); + Ok(Some(module)) + } +} -- cgit v1.2.3