Move path completion to descriptors

This commit is contained in:
Aleksey Kladov 2018-11-21 18:20:44 +03:00
parent 11f19b7849
commit 7ffc7d3308
3 changed files with 36 additions and 18 deletions

View file

@ -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(

View file

@ -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<CompletionItem>,
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<ModuleDescriptor> {
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<CompletionItem>) {
acc.push(CompletionItem {
label: "tfn".to_string(),

View file

@ -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<ModuleDescriptor> {
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)
}