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.rs74
1 files changed, 34 insertions, 40 deletions
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
index 678758956..7f5669c8f 100644
--- a/crates/ra_hir/src/code_model_impl.rs
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -1,7 +1,10 @@
1use ra_db::{CrateId, Cancelable, FileId}; 1use ra_db::{CrateId, Cancelable, SourceRootId};
2use ra_syntax::{AstNode, ast};
3 2
4use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def}; 3use crate::{
4 HirFileId, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId,
5 module::{ModuleSource, ModuleScope},
6 db::HirDatabase,
7};
5 8
6use crate::code_model_api::Module; 9use crate::code_model_api::Module;
7 10
@@ -48,21 +51,26 @@ impl Module {
48 pub(crate) fn new(def_id: DefId) -> Self { 51 pub(crate) fn new(def_id: DefId) -> Self {
49 crate::code_model_api::Module { def_id } 52 crate::code_model_api::Module { def_id }
50 } 53 }
54 pub(crate) fn from_module_id(
55 db: &impl HirDatabase,
56 source_root_id: SourceRootId,
57 module_id: ModuleId,
58 ) -> Cancelable<Self> {
59 let module_tree = db.module_tree(source_root_id)?;
60 let def_loc = DefLoc {
61 kind: DefKind::Module,
62 source_root_id,
63 module_id,
64 source_item_id: module_id.source(&module_tree).0,
65 };
66 let def_id = def_loc.id(db);
67 let module = Module::new(def_id);
68 Ok(module)
69 }
51 70
52 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (FileId, Option<ast::ModuleNode>) { 71 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> ModuleSource {
53 let loc = self.def_id.loc(db); 72 let loc = self.def_id.loc(db);
54 let source_item_id = loc.source_item_id; 73 ModuleSource(loc.source_item_id)
55 let module = match source_item_id.item_id {
56 None => None,
57 Some(_) => {
58 let syntax_node = db.file_item(source_item_id);
59 let module = ast::Module::cast(syntax_node.borrowed()).unwrap().owned();
60 Some(module)
61 }
62 };
63 // FIXME: remove `as_original_file` here
64 let file_id = source_item_id.file_id.as_original_file();
65 (file_id, module)
66 } 74 }
67 75
68 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { 76 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
@@ -79,41 +87,27 @@ impl Module {
79 let loc = self.def_id.loc(db); 87 let loc = self.def_id.loc(db);
80 let module_tree = db.module_tree(loc.source_root_id)?; 88 let module_tree = db.module_tree(loc.source_root_id)?;
81 let module_id = loc.module_id.crate_root(&module_tree); 89 let module_id = loc.module_id.crate_root(&module_tree);
82 let def_loc = DefLoc { 90 Module::from_module_id(db, loc.source_root_id, module_id)
83 module_id,
84 source_item_id: module_id.source(&module_tree).0,
85 ..loc
86 };
87 let def_id = def_loc.id(db);
88 let module = Module::new(def_id);
89 Ok(module)
90 } 91 }
91 /// Finds a child module with the specified name. 92 /// Finds a child module with the specified name.
92 pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { 93 pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
93 let loc = self.def_id.loc(db); 94 let loc = self.def_id.loc(db);
94 let module_tree = db.module_tree(loc.source_root_id)?; 95 let module_tree = db.module_tree(loc.source_root_id)?;
95 let child_id = ctry!(loc.module_id.child(&module_tree, name)); 96 let child_id = ctry!(loc.module_id.child(&module_tree, name));
96 let def_loc = DefLoc { 97 Module::from_module_id(db, loc.source_root_id, child_id).map(Some)
97 module_id: child_id,
98 source_item_id: child_id.source(&module_tree).0,
99 ..loc
100 };
101 let def_id = def_loc.id(db);
102 let module = Module::new(def_id);
103 Ok(Some(module))
104 } 98 }
105 pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 99 pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
106 let loc = self.def_id.loc(db); 100 let loc = self.def_id.loc(db);
107 let module_tree = db.module_tree(loc.source_root_id)?; 101 let module_tree = db.module_tree(loc.source_root_id)?;
108 let parent_id = ctry!(loc.module_id.parent(&module_tree)); 102 let parent_id = ctry!(loc.module_id.parent(&module_tree));
109 let def_loc = DefLoc { 103 Module::from_module_id(db, loc.source_root_id, parent_id).map(Some)
110 module_id: parent_id, 104 }
111 source_item_id: parent_id.source(&module_tree).0, 105 /// Returns a `ModuleScope`: a set of items, visible in this module.
112 ..loc 106 pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
113 }; 107 let loc = self.def_id.loc(db);
114 let def_id = def_loc.id(db); 108 let item_map = db.item_map(loc.source_root_id)?;
115 let module = Module::new(def_id); 109 let res = item_map.per_module[&loc.module_id].clone();
116 Ok(Some(module)) 110 Ok(res)
117 } 111 }
118 pub fn resolve_path_impl( 112 pub fn resolve_path_impl(
119 &self, 113 &self,