diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index c7717ab615..5ef278127e 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -38,10 +38,7 @@ pub(crate) fn completions( original_file.reparse(&edit) }; - let module = match ModuleDescriptor::guess_from_position(db, position)? { - None => return Ok(None), - Some(it) => it, - }; + let module = ctry!(ModuleDescriptor::guess_from_position(db, position)?); let mut res = Vec::new(); let mut has_completions = false; diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 1faa70a18c..858b52e76e 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -13,6 +13,7 @@ use crate::{ descriptors::{ module::{ModuleDescriptor}, function::FnScopes, + Def, Path, }, Cancelable @@ -42,8 +43,7 @@ pub(super) fn completions( let module_scope = module.scope(db)?; acc.extend( module_scope - .items - .iter() + .entries() .filter(|(_name, res)| { // Don't expose this item match res.import { @@ -157,19 +157,20 @@ fn complete_path( return Ok(()); } path.segments.pop(); - let target_module = match module.resolve_path(path) { + let def_id = match module.resolve_path(db, path)? { None => return Ok(()), Some(it) => it, }; + let target_module = match def_id.resolve(db)? { + Def::Module(it) => it, + Def::Item => return Ok(()), + }; let module_scope = target_module.scope(db)?; - let completions = module_scope - .items - .iter() - .map(|(name, _res)| CompletionItem { - label: name.to_string(), - lookup: None, - snippet: None, - }); + let completions = module_scope.entries().map(|(name, _res)| CompletionItem { + label: name.to_string(), + lookup: None, + snippet: None, + }); acc.extend(completions); Ok(()) } diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs index 98094d9ee8..7a1bcf4475 100644 --- a/crates/ra_analysis/src/descriptors/mod.rs +++ b/crates/ra_analysis/src/descriptors/mod.rs @@ -13,9 +13,12 @@ use crate::{ FileId, db::SyntaxDatabase, descriptors::function::{resolve_local_name, FnId, FnScopes}, - descriptors::module::{ModuleId, ModuleTree, ModuleSource, nameres::{ItemMap, InputModuleItems, FileItems}}, + descriptors::module::{ + ModuleId, ModuleTree, ModuleSource, ModuleDescriptor, + nameres::{ItemMap, InputModuleItems, FileItems} + }, input::SourceRootId, - loc2id::IdDatabase, + loc2id::{IdDatabase, DefId, DefLoc}, syntax_ptr::LocalSyntaxPtr, Cancelable, }; @@ -67,6 +70,25 @@ salsa::query_group! { } } +pub(crate) enum Def { + Module(ModuleDescriptor), + Item, +} + +impl DefId { + pub(crate) fn resolve(self, db: &impl DescriptorDatabase) -> Cancelable { + let loc = db.id_maps().def_loc(self); + let res = match loc { + DefLoc::Module { id, source_root } => { + let descr = ModuleDescriptor::new(db, source_root, id)?; + Def::Module(descr) + } + DefLoc::Item { .. } => Def::Item, + }; + Ok(res) + } +} + #[derive(Debug)] pub struct ReferenceDescriptor { pub range: TextRange, diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index a6eaec178c..78911d5d9c 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -17,6 +17,7 @@ use crate::{ descriptors::{Path, PathKind, DescriptorDatabase}, input::SourceRootId, arena::{Arena, Id}, + loc2id::{DefLoc, DefId}, }; pub(crate) use self::nameres::ModuleScope; @@ -76,6 +77,20 @@ impl ModuleDescriptor { Ok(res) } + pub(super) fn new( + db: &impl DescriptorDatabase, + source_root_id: SourceRootId, + module_id: ModuleId, + ) -> Cancelable { + let module_tree = db._module_tree(source_root_id)?; + let res = ModuleDescriptor { + tree: module_tree, + source_root_id, + module_id, + }; + Ok(res) + } + /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. /// Returns `None` for the root module pub fn parent_link_source( @@ -117,6 +132,14 @@ impl ModuleDescriptor { Some(link.name(&self.tree)) } + pub fn def_id(&self, db: &impl DescriptorDatabase) -> DefId { + let def_loc = DefLoc::Module { + id: self.module_id, + source_root: self.source_root_id, + }; + db.id_maps().def_id(def_loc) + } + /// Finds a child module with the specified name. pub fn child(&self, name: &str) -> Option { let child_id = self.module_id.child(&self.tree, name)?; @@ -133,17 +156,28 @@ impl ModuleDescriptor { Ok(res) } - pub(crate) fn resolve_path(&self, path: Path) -> Option { + pub(crate) fn resolve_path( + &self, + db: &impl DescriptorDatabase, + path: Path, + ) -> Cancelable> { let mut curr = match path.kind { PathKind::Crate => self.crate_root(), PathKind::Self_ | PathKind::Plain => self.clone(), - PathKind::Super => self.parent()?, - }; - let segments = path.segments; - for name in segments { - curr = curr.child(&name)?; + PathKind::Super => ctry!(self.parent()), } - Some(curr) + .def_id(db); + + let segments = path.segments; + for name in segments.iter() { + let module = match db.id_maps().def_loc(curr) { + DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?, + _ => return Ok(None), + }; + let scope = module.scope(db)?; + curr = ctry!(ctry!(scope.get(&name)).def_id); + } + Ok(Some(curr)) } pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { diff --git a/crates/ra_analysis/src/descriptors/module/nameres.rs b/crates/ra_analysis/src/descriptors/module/nameres.rs index d347a69b01..d2964f67f9 100644 --- a/crates/ra_analysis/src/descriptors/module/nameres.rs +++ b/crates/ra_analysis/src/descriptors/module/nameres.rs @@ -103,7 +103,16 @@ pub(crate) struct ItemMap { #[derive(Debug, Default, PartialEq, Eq, Clone)] pub(crate) struct ModuleScope { - pub(crate) items: FxHashMap, + items: FxHashMap, +} + +impl ModuleScope { + pub(crate) fn entries<'a>(&'a self) -> impl Iterator + 'a { + self.items.iter() + } + pub(crate) fn get(&self, name: &SmolStr) -> Option<&Resolution> { + self.items.get(name) + } } /// A set of items and imports declared inside a module, without relation to diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index ad6b52371c..8a41b31526 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -450,14 +450,8 @@ impl AnalysisImpl { let syntax = file.syntax(); // Find the calling expression and it's NameRef - let calling_node = match FnCallNode::with_node(syntax, position.offset) { - Some(node) => node, - None => return Ok(None), - }; - let name_ref = match calling_node.name_ref() { - Some(name) => name, - None => return Ok(None), - }; + let calling_node = ctry!(FnCallNode::with_node(syntax, position.offset)); + let name_ref = ctry!(calling_node.name_ref()); // Resolve the function's NameRef (NOTE: this isn't entirely accurate). let file_symbols = self.index_resolve(name_ref)?; diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index cedbd1fc8d..0fbfd8a408 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -9,6 +9,15 @@ extern crate relative_path; extern crate rustc_hash; extern crate salsa; +macro_rules! ctry { + ($expr:expr) => { + match $expr { + None => return Ok(None), + Some(it) => it, + } + }; +} + mod arena; mod db; mod loc2id;