diff options
Diffstat (limited to 'crates/ra_ide/src/parent_module.rs')
-rw-r--r-- | crates/ra_ide/src/parent_module.rs | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs index f5a788c07..2dbccfc3b 100644 --- a/crates/ra_ide/src/parent_module.rs +++ b/crates/ra_ide/src/parent_module.rs | |||
@@ -1,17 +1,23 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; | 3 | use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; |
4 | use ra_syntax::{ | ||
5 | algo::find_node_at_offset, | ||
6 | ast::{self, AstNode}, | ||
7 | }; | ||
4 | 8 | ||
5 | use crate::{db::RootDatabase, NavigationTarget}; | 9 | use crate::{db::RootDatabase, NavigationTarget}; |
6 | 10 | ||
7 | /// This returns `Vec` because a module may be included from several places. We | 11 | /// This returns `Vec` because a module may be included from several places. We |
8 | /// don't handle this case yet though, so the Vec has length at most one. | 12 | /// don't handle this case yet though, so the Vec has length at most one. |
9 | pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { | 13 | pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { |
10 | let src = hir::ModuleSource::from_position(db, position); | 14 | let mut sb = hir::SourceBinder::new(db); |
11 | let module = match hir::Module::from_definition( | 15 | let parse = db.parse(position.file_id); |
12 | db, | 16 | let module = match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) { |
13 | hir::InFile { file_id: position.file_id.into(), value: src }, | 17 | Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)), |
14 | ) { | 18 | None => sb.to_module_def(position.file_id), |
19 | }; | ||
20 | let module = match module { | ||
15 | None => return Vec::new(), | 21 | None => return Vec::new(), |
16 | Some(it) => it, | 22 | Some(it) => it, |
17 | }; | 23 | }; |
@@ -21,14 +27,11 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na | |||
21 | 27 | ||
22 | /// Returns `Vec` for the same reason as `parent_module` | 28 | /// Returns `Vec` for the same reason as `parent_module` |
23 | pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { | 29 | pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { |
24 | let source_file = db.parse(file_id).tree(); | 30 | let mut sb = hir::SourceBinder::new(db); |
25 | let src = hir::ModuleSource::SourceFile(source_file); | 31 | let module = match sb.to_module_def(file_id) { |
26 | let module = | 32 | Some(it) => it, |
27 | match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) | 33 | None => return Vec::new(), |
28 | { | 34 | }; |
29 | Some(it) => it, | ||
30 | None => return Vec::new(), | ||
31 | }; | ||
32 | let krate = module.krate(); | 35 | let krate = module.krate(); |
33 | vec![krate.into()] | 36 | vec![krate.into()] |
34 | } | 37 | } |