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.rs51
1 files changed, 46 insertions, 5 deletions
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs
index 7f5669c8f..4f4b506dd 100644
--- a/crates/ra_hir/src/code_model_impl.rs
+++ b/crates/ra_hir/src/code_model_impl.rs
@@ -1,12 +1,13 @@
1use ra_db::{CrateId, Cancelable, SourceRootId}; 1use ra_db::{CrateId, Cancelable, SourceRootId, FileId};
2use ra_syntax::{ast, SyntaxNode, AstNode};
2 3
3use crate::{ 4use crate::{
4 HirFileId, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId, 5 HirFileId, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId,
5 module::{ModuleSource, ModuleScope}, 6 module::{ModuleScope, Problem},
6 db::HirDatabase, 7 db::HirDatabase,
7}; 8};
8 9
9use crate::code_model_api::Module; 10use crate::code_model_api::{Module, ModuleSource};
10 11
11impl Crate { 12impl Crate {
12 pub(crate) fn new(crate_id: CrateId) -> Crate { 13 pub(crate) fn new(crate_id: CrateId) -> Crate {
@@ -68,9 +69,44 @@ impl Module {
68 Ok(module) 69 Ok(module)
69 } 70 }
70 71
71 pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> ModuleSource { 72 pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
72 let loc = self.def_id.loc(db); 73 let loc = self.def_id.loc(db);
73 ModuleSource(loc.source_item_id) 74 let module_tree = db.module_tree(loc.source_root_id)?;
75 let link = ctry!(loc.module_id.parent_link(&module_tree));
76 Ok(Some(link.name(&module_tree).clone()))
77 }
78
79 pub fn defenition_source_impl(
80 &self,
81 db: &impl HirDatabase,
82 ) -> Cancelable<(FileId, ModuleSource)> {
83 let loc = self.def_id.loc(db);
84 let file_id = loc.source_item_id.file_id.as_original_file();
85 let syntax_node = db.file_item(loc.source_item_id);
86 let syntax_node = syntax_node.borrowed();
87 let module_source = if let Some(source_file) = ast::SourceFile::cast(syntax_node) {
88 ModuleSource::SourceFile(source_file.owned())
89 } else {
90 let module = ast::Module::cast(syntax_node).unwrap();
91 ModuleSource::Module(module.owned())
92 };
93 Ok((file_id, module_source))
94 }
95
96 pub fn declaration_source_impl(
97 &self,
98 db: &impl HirDatabase,
99 ) -> Cancelable<Option<(FileId, ast::ModuleNode)>> {
100 let loc = self.def_id.loc(db);
101 let module_tree = db.module_tree(loc.source_root_id)?;
102 let link = ctry!(loc.module_id.parent_link(&module_tree));
103 let file_id = link
104 .owner(&module_tree)
105 .source(&module_tree)
106 .file_id()
107 .as_original_file();
108 let src = link.bind_source(&module_tree, db);
109 Ok(Some((file_id, src)))
74 } 110 }
75 111
76 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { 112 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
@@ -150,4 +186,9 @@ impl Module {
150 } 186 }
151 Ok(curr_per_ns) 187 Ok(curr_per_ns)
152 } 188 }
189 pub fn problems_impl(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SyntaxNode, Problem)>> {
190 let loc = self.def_id.loc(db);
191 let module_tree = db.module_tree(loc.source_root_id)?;
192 Ok(loc.module_id.problems(&module_tree, db))
193 }
153} 194}