aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs23
1 files changed, 21 insertions, 2 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index a0165aef2..a0d1daf71 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -8,14 +8,14 @@
8use ra_db::{FileId, FilePosition, Cancelable}; 8use ra_db::{FileId, FilePosition, Cancelable};
9use ra_editor::find_node_at_offset; 9use ra_editor::find_node_at_offset;
10use ra_syntax::{ 10use ra_syntax::{
11 ast::{self, AstNode}, 11 ast::{self, AstNode, NameOwner},
12 SyntaxNodeRef, 12 SyntaxNodeRef,
13}; 13};
14 14
15use crate::{ 15use crate::{
16 HirDatabase, Module, Function, SourceItemId, 16 HirDatabase, Module, Function, SourceItemId,
17 module::ModuleSource, 17 module::ModuleSource,
18 DefKind, DefLoc 18 DefKind, DefLoc, AsName,
19}; 19};
20 20
21/// Locates the module by `FileId`. Picks topmost module in the file. 21/// Locates the module by `FileId`. Picks topmost module in the file.
@@ -24,6 +24,25 @@ pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable
24 module_from_source(db, module_source) 24 module_from_source(db, module_source)
25} 25}
26 26
27/// Locates the child module by `mod child;` declaration.
28pub fn module_from_declaration(
29 db: &impl HirDatabase,
30 file_id: FileId,
31 decl: ast::Module,
32) -> Cancelable<Option<Module>> {
33 let parent_module = module_from_file_id(db, file_id)?;
34 let child_name = decl.name();
35 match (parent_module, child_name) {
36 (Some(parent_module), Some(child_name)) => {
37 if let Some(child) = parent_module.child(&child_name.as_name()) {
38 return Ok(Some(child));
39 }
40 }
41 _ => (),
42 }
43 Ok(None)
44}
45
27/// Locates the module by position in the source code. 46/// Locates the module by position in the source code.
28pub fn module_from_position( 47pub fn module_from_position(
29 db: &impl HirDatabase, 48 db: &impl HirDatabase,