diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r-- | crates/ra_hir_def/src/nameres.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index c01e020ef..6723465a5 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs | |||
@@ -58,7 +58,10 @@ mod tests; | |||
58 | 58 | ||
59 | use std::sync::Arc; | 59 | use std::sync::Arc; |
60 | 60 | ||
61 | use hir_expand::{ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, MacroDefId}; | 61 | use hir_expand::{ |
62 | ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId, | ||
63 | Source, | ||
64 | }; | ||
62 | use once_cell::sync::Lazy; | 65 | use once_cell::sync::Lazy; |
63 | use ra_arena::Arena; | 66 | use ra_arena::Arena; |
64 | use ra_db::{CrateId, Edition, FileId}; | 67 | use ra_db::{CrateId, Edition, FileId}; |
@@ -116,12 +119,15 @@ pub struct ModuleData { | |||
116 | pub parent: Option<CrateModuleId>, | 119 | pub parent: Option<CrateModuleId>, |
117 | pub children: FxHashMap<Name, CrateModuleId>, | 120 | pub children: FxHashMap<Name, CrateModuleId>, |
118 | pub scope: ModuleScope, | 121 | pub scope: ModuleScope, |
122 | |||
123 | // FIXME: these can't be both null, we need a three-state enum here. | ||
119 | /// None for root | 124 | /// None for root |
120 | pub declaration: Option<AstId<ast::Module>>, | 125 | pub declaration: Option<AstId<ast::Module>>, |
121 | /// None for inline modules. | 126 | /// None for inline modules. |
122 | /// | 127 | /// |
123 | /// Note that non-inline modules, by definition, live inside non-macro file. | 128 | /// Note that non-inline modules, by definition, live inside non-macro file. |
124 | pub definition: Option<FileId>, | 129 | pub definition: Option<FileId>, |
130 | |||
125 | pub impls: Vec<ImplId>, | 131 | pub impls: Vec<ImplId>, |
126 | } | 132 | } |
127 | 133 | ||
@@ -285,6 +291,29 @@ impl CrateDefMap { | |||
285 | } | 291 | } |
286 | } | 292 | } |
287 | 293 | ||
294 | impl ModuleData { | ||
295 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | ||
296 | pub fn definition_source( | ||
297 | &self, | ||
298 | db: &impl DefDatabase2, | ||
299 | ) -> Source<Either<ast::SourceFile, ast::Module>> { | ||
300 | if let Some(file_id) = self.definition { | ||
301 | let sf = db.parse(file_id).tree(); | ||
302 | return Source::new(file_id.into(), Either::A(sf)); | ||
303 | } | ||
304 | let decl = self.declaration.unwrap(); | ||
305 | Source::new(decl.file_id(), Either::B(decl.to_node(db))) | ||
306 | } | ||
307 | |||
308 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | ||
309 | /// `None` for the crate root. | ||
310 | pub fn declaration_source(&self, db: &impl DefDatabase2) -> Option<Source<ast::Module>> { | ||
311 | let decl = self.declaration?; | ||
312 | let value = decl.to_node(db); | ||
313 | Some(Source { file_id: decl.file_id(), value }) | ||
314 | } | ||
315 | } | ||
316 | |||
288 | mod diagnostics { | 317 | mod diagnostics { |
289 | use hir_expand::diagnostics::DiagnosticSink; | 318 | use hir_expand::diagnostics::DiagnosticSink; |
290 | use ra_db::RelativePathBuf; | 319 | use ra_db::RelativePathBuf; |