From c3a4c4429de83450654795534e64e878a774a088 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 18:35:10 +0100 Subject: 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. --- crates/ra_ide/src/parent_module.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide/src/parent_module.rs') 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 @@ //! FIXME: write short doc here -use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; +use hir::Semantics; +use ra_db::{CrateId, FileId, FilePosition}; use ra_ide_db::RootDatabase; use ra_syntax::{ algo::find_node_at_offset, @@ -13,10 +14,10 @@ use crate::NavigationTarget; /// This returns `Vec` because a module may be included from several places. We /// don't handle this case yet though, so the Vec has length at most one. pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec { - let mut sb = hir::SourceBinder::new(db); - let parse = db.parse(position.file_id); + let sema = Semantics::new(db); + let source_file = sema.parse(position.file_id); - let mut module = find_node_at_offset::(parse.tree().syntax(), position.offset); + let mut module = find_node_at_offset::(source_file.syntax(), position.offset); // If cursor is literally on `mod foo`, go to the grandpa. if let Some(m) = &module { @@ -30,8 +31,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec sb.to_def(hir::InFile::new(position.file_id.into(), module)), - None => sb.to_module_def(position.file_id), + Some(module) => sema.to_def(&module), + None => sema.to_module_def(position.file_id), }; let module = match module { None => return Vec::new(), @@ -43,8 +44,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec Vec { - let mut sb = hir::SourceBinder::new(db); - let module = match sb.to_module_def(file_id) { + let sema = Semantics::new(db); + let module = match sema.to_module_def(file_id) { Some(it) => it, None => return Vec::new(), }; -- cgit v1.2.3