aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/parent_module.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-18 17:35:10 +0000
committerAleksey Kladov <[email protected]>2020-02-26 11:55:50 +0000
commitc3a4c4429de83450654795534e64e878a774a088 (patch)
tree12d89798f61b276f8bd640db07276a7d4e92b1c2 /crates/ra_ide/src/parent_module.rs
parent04deae3dba7c9b7054f7a1d64e4b93a05aecc132 (diff)
Refactor primary IDE API
This introduces the new type -- Semantics. Semantics maps SyntaxNodes to various semantic info, such as type, name resolution or macro expansions. To do so, Semantics maintains a HashMap which maps every node it saw to the file from which the node originated. This is enough to get all the necessary hir bits just from syntax.
Diffstat (limited to 'crates/ra_ide/src/parent_module.rs')
-rw-r--r--crates/ra_ide/src/parent_module.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs
index af14d6ab3..2c4bdb039 100644
--- a/crates/ra_ide/src/parent_module.rs
+++ b/crates/ra_ide/src/parent_module.rs
@@ -1,6 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; 3use hir::Semantics;
4use ra_db::{CrateId, FileId, FilePosition};
4use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
5use ra_syntax::{ 6use ra_syntax::{
6 algo::find_node_at_offset, 7 algo::find_node_at_offset,
@@ -13,10 +14,10 @@ use crate::NavigationTarget;
13/// This returns `Vec` because a module may be included from several places. We 14/// This returns `Vec` because a module may be included from several places. We
14/// don't handle this case yet though, so the Vec has length at most one. 15/// don't handle this case yet though, so the Vec has length at most one.
15pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { 16pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
16 let mut sb = hir::SourceBinder::new(db); 17 let sema = Semantics::new(db);
17 let parse = db.parse(position.file_id); 18 let source_file = sema.parse(position.file_id);
18 19
19 let mut module = find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset); 20 let mut module = find_node_at_offset::<ast::Module>(source_file.syntax(), position.offset);
20 21
21 // If cursor is literally on `mod foo`, go to the grandpa. 22 // If cursor is literally on `mod foo`, go to the grandpa.
22 if let Some(m) = &module { 23 if let Some(m) = &module {
@@ -30,8 +31,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
30 } 31 }
31 32
32 let module = match module { 33 let module = match module {
33 Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)), 34 Some(module) => sema.to_def(&module),
34 None => sb.to_module_def(position.file_id), 35 None => sema.to_module_def(position.file_id),
35 }; 36 };
36 let module = match module { 37 let module = match module {
37 None => return Vec::new(), 38 None => return Vec::new(),
@@ -43,8 +44,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
43 44
44/// Returns `Vec` for the same reason as `parent_module` 45/// Returns `Vec` for the same reason as `parent_module`
45pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { 46pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
46 let mut sb = hir::SourceBinder::new(db); 47 let sema = Semantics::new(db);
47 let module = match sb.to_module_def(file_id) { 48 let module = match sema.to_module_def(file_id) {
48 Some(it) => it, 49 Some(it) => it,
49 None => return Vec::new(), 50 None => return Vec::new(),
50 }; 51 };