mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Move path completion to descriptors
This commit is contained in:
parent
11f19b7849
commit
7ffc7d3308
3 changed files with 36 additions and 18 deletions
|
@ -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]
|
#[test]
|
||||||
fn test_completion_mod_scope_nested() {
|
fn test_completion_mod_scope_nested() {
|
||||||
check_scope_completion(
|
check_scope_completion(
|
||||||
|
|
|
@ -13,7 +13,7 @@ use crate::{
|
||||||
descriptors::{
|
descriptors::{
|
||||||
module::{ModuleDescriptor},
|
module::{ModuleDescriptor},
|
||||||
function::FnScopes,
|
function::FnScopes,
|
||||||
Path, PathKind,
|
Path,
|
||||||
},
|
},
|
||||||
Cancelable
|
Cancelable
|
||||||
};
|
};
|
||||||
|
@ -148,9 +148,13 @@ fn complete_path(
|
||||||
acc: &mut Vec<CompletionItem>,
|
acc: &mut Vec<CompletionItem>,
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
module: &ModuleDescriptor,
|
module: &ModuleDescriptor,
|
||||||
path: Path,
|
mut path: Path,
|
||||||
) -> Cancelable<()> {
|
) -> 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(()),
|
None => return Ok(()),
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
};
|
};
|
||||||
|
@ -167,19 +171,6 @@ fn complete_path(
|
||||||
Ok(())
|
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>) {
|
fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) {
|
||||||
acc.push(CompletionItem {
|
acc.push(CompletionItem {
|
||||||
label: "tfn".to_string(),
|
label: "tfn".to_string(),
|
||||||
|
|
|
@ -14,11 +14,11 @@ use relative_path::RelativePathBuf;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable,
|
db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable,
|
||||||
descriptors::DescriptorDatabase,
|
descriptors::{Path, PathKind, DescriptorDatabase},
|
||||||
input::SourceRootId
|
input::SourceRootId
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) use self::{nameres::ModuleScope};
|
pub(crate) use self::nameres::ModuleScope;
|
||||||
|
|
||||||
/// `ModuleDescriptor` is API entry point to get all the information
|
/// `ModuleDescriptor` is API entry point to get all the information
|
||||||
/// about a particular module.
|
/// about a particular module.
|
||||||
|
@ -131,6 +131,19 @@ impl ModuleDescriptor {
|
||||||
Ok(res)
|
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)> {
|
pub fn problems(&self, db: &impl DescriptorDatabase) -> Vec<(SyntaxNode, Problem)> {
|
||||||
self.module_id.problems(&self.tree, db)
|
self.module_id.problems(&self.tree, db)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue