From 7ffc7d33082475ffd3b8768be001af5b8988a54b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 21 Nov 2018 18:20:44 +0300 Subject: Move path completion to descriptors --- crates/ra_analysis/src/completion/mod.rs | 14 +++++++++++++ .../src/completion/reference_completion.rs | 23 +++++++--------------- crates/ra_analysis/src/descriptors/module/mod.rs | 17 ++++++++++++++-- 3 files changed, 36 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index a8a752fc7..8034060de 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs @@ -220,6 +220,20 @@ mod tests { ); } + #[test] + fn test_completion_self_path() { + check_scope_completion( + r" + use self::m::B<|>; + + mod m { + struct Bar; + } + ", + r#"[CompletionItem { label: "Bar", lookup: None, snippet: None }]"#, + ); + } + #[test] fn test_completion_mod_scope_nested() { check_scope_completion( diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index a96570415..d301a3c02 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -13,7 +13,7 @@ use crate::{ descriptors::{ module::{ModuleDescriptor}, function::FnScopes, - Path, PathKind, + Path, }, Cancelable }; @@ -148,9 +148,13 @@ fn complete_path( acc: &mut Vec, db: &RootDatabase, module: &ModuleDescriptor, - path: Path, + mut path: Path, ) -> Cancelable<()> { - let target_module = match find_target_module(module, path) { + if path.segments.is_empty() { + return Ok(()); + } + path.segments.pop(); + let target_module = match module.resolve_path(path) { None => return Ok(()), Some(it) => it, }; @@ -167,19 +171,6 @@ fn complete_path( Ok(()) } -fn find_target_module(module: &ModuleDescriptor, path: Path) -> Option { - if path.kind != PathKind::Crate { - return None; - } - let mut segments = path.segments; - segments.pop(); - let mut target_module = module.crate_root(); - for name in segments { - target_module = target_module.child(&name)?; - } - Some(target_module) -} - fn complete_mod_item_snippets(acc: &mut Vec) { acc.push(CompletionItem { label: "tfn".to_string(), diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index cfdffcdbc..a7e41e3db 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -14,11 +14,11 @@ use relative_path::RelativePathBuf; use crate::{ db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, - descriptors::DescriptorDatabase, + descriptors::{Path, PathKind, DescriptorDatabase}, input::SourceRootId }; -pub(crate) use self::{nameres::ModuleScope}; +pub(crate) use self::nameres::ModuleScope; /// `ModuleDescriptor` is API entry point to get all the information /// about a particular module. @@ -131,6 +131,19 @@ impl ModuleDescriptor { Ok(res) } + pub(crate) fn resolve_path(&self, path: Path) -> Option { + 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)?; + } + Some(curr) + } + pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> { self.module_id.problems(&self.tree, db) } -- cgit v1.2.3