aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/nameres.rs68
1 files changed, 66 insertions, 2 deletions
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index ca8cbcd42..feb2a4d32 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -63,9 +63,12 @@ use hir_expand::{
63}; 63};
64use once_cell::sync::Lazy; 64use once_cell::sync::Lazy;
65use ra_arena::Arena; 65use ra_arena::Arena;
66use ra_db::{CrateId, Edition, FileId}; 66use ra_db::{CrateId, Edition, FileId, FilePosition};
67use ra_prof::profile; 67use ra_prof::profile;
68use ra_syntax::ast; 68use ra_syntax::{
69 ast::{self, AstNode},
70 SyntaxNode,
71};
69use rustc_hash::FxHashMap; 72use rustc_hash::FxHashMap;
70 73
71use crate::{ 74use crate::{
@@ -361,6 +364,67 @@ impl ModuleData {
361 } 364 }
362} 365}
363 366
367pub enum ModuleSource {
368 SourceFile(ast::SourceFile),
369 Module(ast::Module),
370 Block(ast::Block),
371}
372
373impl ModuleSource {
374 pub fn new(
375 db: &impl DefDatabase,
376 file_id: Option<FileId>,
377 decl_id: Option<AstId<ast::Module>>,
378 ) -> ModuleSource {
379 match (file_id, decl_id) {
380 (Some(file_id), _) => {
381 let source_file = db.parse(file_id).tree();
382 ModuleSource::SourceFile(source_file)
383 }
384 (None, Some(item_id)) => {
385 let module = item_id.to_node(db);
386 assert!(module.item_list().is_some(), "expected inline module");
387 ModuleSource::Module(module)
388 }
389 (None, None) => panic!(),
390 }
391 }
392
393 // FIXME: this methods do not belong here
394 pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource {
395 let parse = db.parse(position.file_id);
396 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
397 parse.tree().syntax(),
398 position.offset,
399 ) {
400 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
401 _ => {
402 let source_file = parse.tree();
403 ModuleSource::SourceFile(source_file)
404 }
405 }
406 }
407
408 pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource {
409 if let Some(m) =
410 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
411 {
412 ModuleSource::Module(m)
413 } else if let Some(b) = child.value.ancestors().filter_map(ast::Block::cast).next() {
414 ModuleSource::Block(b)
415 } else {
416 let file_id = child.file_id.original_file(db);
417 let source_file = db.parse(file_id).tree();
418 ModuleSource::SourceFile(source_file)
419 }
420 }
421
422 pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
423 let source_file = db.parse(file_id).tree();
424 ModuleSource::SourceFile(source_file)
425 }
426}
427
364mod diagnostics { 428mod diagnostics {
365 use hir_expand::diagnostics::DiagnosticSink; 429 use hir_expand::diagnostics::DiagnosticSink;
366 use ra_db::RelativePathBuf; 430 use ra_db::RelativePathBuf;