aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-23 08:14:10 +0000
committerAleksey Kladov <[email protected]>2019-11-23 08:14:40 +0000
commit552ba868afc8f72202ac834d07bbeb330aca007d (patch)
treecd57fc21ce442e84bdc41241a70ece9995d826af /crates/ra_hir_def/src/nameres.rs
parent81bfbd26bef1a63ccbeba33430e6b07a53c8e7d9 (diff)
Move attrs query to hir_def
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r--crates/ra_hir_def/src/nameres.rs31
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
59use std::sync::Arc; 59use std::sync::Arc;
60 60
61use hir_expand::{ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, MacroDefId}; 61use hir_expand::{
62 ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId,
63 Source,
64};
62use once_cell::sync::Lazy; 65use once_cell::sync::Lazy;
63use ra_arena::Arena; 66use ra_arena::Arena;
64use ra_db::{CrateId, Edition, FileId}; 67use 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
294impl 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
288mod diagnostics { 317mod diagnostics {
289 use hir_expand::diagnostics::DiagnosticSink; 318 use hir_expand::diagnostics::DiagnosticSink;
290 use ra_db::RelativePathBuf; 319 use ra_db::RelativePathBuf;