From 4344264024d374bb9cdc6f388f13c90b48c6c22e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 5 Dec 2018 13:16:20 +0300 Subject: move fuzzy source binding to a separete mode --- crates/ra_hir/src/module/mod.rs | 71 +++---------------------------------- crates/ra_hir/src/module/nameres.rs | 4 ++- 2 files changed, 7 insertions(+), 68 deletions(-) (limited to 'crates/ra_hir/src/module') diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index 11e6e8e75..9ab7dbff5 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs @@ -3,14 +3,12 @@ pub(super) mod nameres; use std::sync::Arc; -use ra_editor::find_node_at_offset; - use ra_syntax::{ algo::generate, ast::{self, AstNode, NameOwner}, - SmolStr, SyntaxNode, SyntaxNodeRef, + SmolStr, SyntaxNode, }; -use ra_db::{SourceRootId, FileId, FilePosition, Cancelable}; +use ra_db::{SourceRootId, FileId, Cancelable}; use relative_path::RelativePathBuf; use crate::{ @@ -30,68 +28,6 @@ pub struct Module { } impl Module { - /// Lookup `Module` by `FileId`. Note that this is inherently - /// lossy transformation: in general, a single source might correspond to - /// several modules. - pub fn guess_from_file_id( - db: &impl HirDatabase, - file_id: FileId, - ) -> Cancelable> { - let module_source = ModuleSource::new_file(db, file_id); - Module::guess_from_source(db, module_source) - } - - /// Lookup `Module` by position in the source code. Note that this - /// is inherently lossy transformation: in general, a single source might - /// correspond to several modules. - pub fn guess_from_position( - db: &impl HirDatabase, - position: FilePosition, - ) -> Cancelable> { - let file = db.source_file(position.file_id); - let module_source = match find_node_at_offset::(file.syntax(), position.offset) - { - Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), - _ => ModuleSource::new_file(db, position.file_id), - }; - Module::guess_from_source(db, module_source) - } - - pub fn guess_from_child_node( - db: &impl HirDatabase, - file_id: FileId, - node: SyntaxNodeRef, - ) -> Cancelable> { - let module_source = if let Some(m) = node - .ancestors() - .filter_map(ast::Module::cast) - .find(|it| !it.has_semi()) - { - ModuleSource::new_inline(db, file_id, m) - } else { - ModuleSource::new_file(db, file_id) - }; - Module::guess_from_source(db, module_source) - } - - fn guess_from_source( - db: &impl HirDatabase, - module_source: ModuleSource, - ) -> Cancelable> { - let source_root_id = db.file_source_root(module_source.file_id()); - let module_tree = db.module_tree(source_root_id)?; - - let res = match module_tree.any_module_for_source(module_source) { - None => None, - Some(module_id) => Some(Module { - tree: module_tree, - source_root_id, - module_id, - }), - }; - Ok(res) - } - pub(super) fn new( db: &impl HirDatabase, source_root_id: SourceRootId, @@ -225,7 +161,8 @@ impl ModuleTree { .collect() } - fn any_module_for_source(&self, source: ModuleSource) -> Option { + //TODO: move to source binders? + pub(crate) fn any_module_for_source(&self, source: ModuleSource) -> Option { self.modules_for_source(source).pop() } } diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index c2b380a80..61a1acfe6 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -363,7 +363,9 @@ mod tests { fn item_map(fixture: &str) -> (Arc, hir::ModuleId) { let (db, pos) = MockDatabase::with_position(fixture); let source_root = db.file_source_root(pos.file_id); - let module = hir::Module::guess_from_position(&db, pos).unwrap().unwrap(); + let module = hir::source_binder::module_from_position(&db, pos) + .unwrap() + .unwrap(); let module_id = module.module_id; (db.item_map(source_root).unwrap(), module_id) } -- cgit v1.2.3