diff options
author | Aleksey Kladov <[email protected]> | 2019-01-06 14:33:27 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-06 14:36:10 +0000 |
commit | fd4456d0ec88e3433a7a8be6f27d8af9afedefe5 (patch) | |
tree | 37f1225b085e22756cbfd65875595c60205cc93a /crates/ra_hir/src/code_model_impl/krate.rs | |
parent | 5a505189a8f7ed274893a45aed0d0249083d1277 (diff) |
flatten module structure
Diffstat (limited to 'crates/ra_hir/src/code_model_impl/krate.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_impl/krate.rs | 45 |
1 files changed, 45 insertions, 0 deletions
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 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | ||
2 | |||
3 | use crate::{ | ||
4 | HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, | ||
5 | db::HirDatabase, | ||
6 | }; | ||
7 | |||
8 | impl Crate { | ||
9 | pub(crate) fn new(crate_id: CrateId) -> Crate { | ||
10 | Crate { crate_id } | ||
11 | } | ||
12 | pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> { | ||
13 | let crate_graph = db.crate_graph(); | ||
14 | crate_graph | ||
15 | .dependencies(self.crate_id) | ||
16 | .map(|dep| { | ||
17 | let krate = Crate::new(dep.crate_id()); | ||
18 | let name = dep.as_name(); | ||
19 | CrateDependency { krate, name } | ||
20 | }) | ||
21 | .collect() | ||
22 | } | ||
23 | pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | ||
24 | let crate_graph = db.crate_graph(); | ||
25 | let file_id = crate_graph.crate_root(self.crate_id); | ||
26 | let source_root_id = db.file_source_root(file_id); | ||
27 | let file_id = HirFileId::from(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 def_loc = DefLoc { | ||
35 | kind: DefKind::Module, | ||
36 | source_root_id, | ||
37 | module_id, | ||
38 | source_item_id: module_id.source(&module_tree).0, | ||
39 | }; | ||
40 | let def_id = def_loc.id(db); | ||
41 | |||
42 | let module = Module::new(def_id); | ||
43 | Ok(Some(module)) | ||
44 | } | ||
45 | } | ||