aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module.rs')
-rw-r--r--crates/ra_hir/src/module.rs40
1 files changed, 21 insertions, 19 deletions
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs
index dde036f2c..26171d27c 100644
--- a/crates/ra_hir/src/module.rs
+++ b/crates/ra_hir/src/module.rs
@@ -15,7 +15,7 @@ use relative_path::RelativePathBuf;
15use crate::{ 15use crate::{
16 Def, DefKind, DefLoc, DefId, 16 Def, DefKind, DefLoc, DefId,
17 Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate, 17 Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
18 MFileId, 18 HirFileId,
19 arena::{Arena, Id}, 19 arena::{Arena, Id},
20}; 20};
21 21
@@ -49,13 +49,17 @@ impl Module {
49 /// Returns `None` for the root module 49 /// Returns `None` for the root module
50 pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> { 50 pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> {
51 let link = self.module_id.parent_link(&self.tree)?; 51 let link = self.module_id.parent_link(&self.tree)?;
52 let file_id = link.owner(&self.tree).source(&self.tree).file_id(); 52 let file_id = link
53 .owner(&self.tree)
54 .source(&self.tree)
55 .file_id()
56 .as_original_file();
53 let src = link.bind_source(&self.tree, db); 57 let src = link.bind_source(&self.tree, db);
54 Some((file_id, src)) 58 Some((file_id, src))
55 } 59 }
56 60
57 pub fn source(&self) -> ModuleSource { 61 pub fn file_id(&self) -> FileId {
58 self.module_id.source(&self.tree) 62 self.source().file_id().as_original_file()
59 } 63 }
60 64
61 /// Parent module. Returns `None` if this is a root module. 65 /// Parent module. Returns `None` if this is a root module.
@@ -70,7 +74,7 @@ impl Module {
70 /// Returns the crate this module is part of. 74 /// Returns the crate this module is part of.
71 pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> { 75 pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
72 let root_id = self.module_id.crate_root(&self.tree); 76 let root_id = self.module_id.crate_root(&self.tree);
73 let file_id = root_id.source(&self.tree).file_id(); 77 let file_id = root_id.source(&self.tree).file_id().as_original_file();
74 let crate_graph = db.crate_graph(); 78 let crate_graph = db.crate_graph();
75 let crate_id = crate_graph.crate_id_for_crate_root(file_id)?; 79 let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
76 Some(Crate::new(crate_id)) 80 Some(Crate::new(crate_id))
@@ -163,6 +167,10 @@ impl Module {
163 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { 167 pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> {
164 self.module_id.problems(&self.tree, db) 168 self.module_id.problems(&self.tree, db)
165 } 169 }
170
171 pub(crate) fn source(&self) -> ModuleSource {
172 self.module_id.source(&self.tree)
173 }
166} 174}
167 175
168/// Physically, rust source is organized as a set of files, but logically it is 176/// Physically, rust source is organized as a set of files, but logically it is
@@ -292,34 +300,28 @@ pub struct ModuleData {
292 300
293impl ModuleSource { 301impl ModuleSource {
294 // precondition: item_id **must** point to module 302 // precondition: item_id **must** point to module
295 fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource { 303 fn new(file_id: HirFileId, item_id: Option<SourceFileItemId>) -> ModuleSource {
296 let source_item_id = SourceItemId { 304 let source_item_id = SourceItemId { file_id, item_id };
297 mfile_id: file_id.into(),
298 item_id,
299 };
300 ModuleSource(source_item_id) 305 ModuleSource(source_item_id)
301 } 306 }
302 307
303 pub(crate) fn new_file(file_id: FileId) -> ModuleSource { 308 pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource {
304 ModuleSource::new(file_id, None) 309 ModuleSource::new(file_id, None)
305 } 310 }
306 311
307 pub(crate) fn new_inline( 312 pub(crate) fn new_inline(
308 db: &impl HirDatabase, 313 db: &impl HirDatabase,
309 file_id: FileId, 314 file_id: HirFileId,
310 m: ast::Module, 315 m: ast::Module,
311 ) -> ModuleSource { 316 ) -> ModuleSource {
312 assert!(!m.has_semi()); 317 assert!(!m.has_semi());
313 let file_items = db.file_items(file_id.into()); 318 let file_items = db.file_items(file_id);
314 let item_id = file_items.id_of(file_id.into(), m.syntax()); 319 let item_id = file_items.id_of(file_id, m.syntax());
315 ModuleSource::new(file_id, Some(item_id)) 320 ModuleSource::new(file_id, Some(item_id))
316 } 321 }
317 322
318 pub fn file_id(self) -> FileId { 323 pub(crate) fn file_id(self) -> HirFileId {
319 match self.0.mfile_id { 324 self.0.file_id
320 MFileId::File(file_id) => file_id,
321 MFileId::Macro(_) => unreachable!(),
322 }
323 } 325 }
324 326
325 pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { 327 pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {