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.rs59
1 files changed, 57 insertions, 2 deletions
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
index 75d4e04c1..22079ba33 100644
--- a/crates/ra_hir/src/code_model_impl.rs
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -1,6 +1,9 @@
1use ra_db::{CrateId, Cancelable}; 1use ra_db::{CrateId, Cancelable};
2 2
3use crate::{Module, HirFileId, db::HirDatabase, Crate, CrateDependency, AsName}; 3use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name};
4
5use crate::code_model_api::Module;
6
4 7
5impl Crate { 8impl Crate {
6 pub(crate) fn new(crate_id: CrateId) -> Crate { 9 pub(crate) fn new(crate_id: CrateId) -> Crate {
@@ -28,7 +31,59 @@ impl Crate {
28 .modules_with_sources() 31 .modules_with_sources()
29 .find(|(_, src)| src.file_id() == file_id)); 32 .find(|(_, src)| src.file_id() == file_id));
30 33
31 let module = Module::new(db, source_root_id, module_id)?; 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}
46
47impl Module {
48 fn new(def_id: DefId) -> Self {
49 crate::code_model_api::Module { def_id }
50 }
51
52 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
53 let root = self.crate_root(db)?;
54 let loc = root.def_id.loc(db);
55 let file_id = loc.source_item_id.file_id.as_original_file();
56
57 let crate_graph = db.crate_graph();
58 let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id));
59 Ok(Some(Crate::new(crate_id)))
60 }
61
62 pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> {
63 let loc = self.def_id.loc(db);
64 let module_tree = db.module_tree(loc.source_root_id)?;
65 let module_id = loc.module_id.crate_root(&module_tree);
66 let def_loc = DefLoc {
67 module_id,
68 source_item_id: module_id.source(&module_tree).0,
69 ..loc
70 };
71 let def_id = def_loc.id(db);
72 let module = Module::new(def_id);
73 Ok(module)
74 }
75 /// Finds a child module with the specified name.
76 pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
77 let loc = self.def_id.loc(db);
78 let module_tree = db.module_tree(loc.source_root_id)?;
79 let child_id = ctry!(loc.module_id.child(&module_tree, name));
80 let def_loc = DefLoc {
81 module_id: child_id,
82 source_item_id: child_id.source(&module_tree).0,
83 ..loc
84 };
85 let def_id = def_loc.id(db);
86 let module = Module::new(def_id);
32 Ok(Some(module)) 87 Ok(Some(module))
33 } 88 }
34} 89}