resolve_path works with DefIds
This commit is contained in:
parent
aa7fd563a4
commit
192e2bbb0e
3 changed files with 46 additions and 11 deletions
crates/ra_analysis/src
|
@ -13,6 +13,7 @@ use crate::{
|
|||
descriptors::{
|
||||
module::{ModuleDescriptor},
|
||||
function::FnScopes,
|
||||
Def,
|
||||
Path,
|
||||
},
|
||||
Cancelable
|
||||
|
@ -156,10 +157,14 @@ fn complete_path(
|
|||
return Ok(());
|
||||
}
|
||||
path.segments.pop();
|
||||
let target_module = match module.resolve_path(db, 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.entries().map(|(name, _res)| CompletionItem {
|
||||
label: name.to_string(),
|
||||
|
|
|
@ -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<Def> {
|
||||
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,
|
||||
|
|
|
@ -77,7 +77,7 @@ impl ModuleDescriptor {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
fn new(
|
||||
pub(super) fn new(
|
||||
db: &impl DescriptorDatabase,
|
||||
source_root_id: SourceRootId,
|
||||
module_id: ModuleId,
|
||||
|
@ -132,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<ModuleDescriptor> {
|
||||
let child_id = self.module_id.child(&self.tree, name)?;
|
||||
|
@ -152,23 +160,23 @@ impl ModuleDescriptor {
|
|||
&self,
|
||||
db: &impl DescriptorDatabase,
|
||||
path: Path,
|
||||
) -> Cancelable<Option<ModuleDescriptor>> {
|
||||
) -> Cancelable<Option<DefId>> {
|
||||
let mut curr = match path.kind {
|
||||
PathKind::Crate => self.crate_root(),
|
||||
PathKind::Self_ | PathKind::Plain => self.clone(),
|
||||
PathKind::Super => ctry!(self.parent()),
|
||||
};
|
||||
}
|
||||
.def_id(db);
|
||||
|
||||
let segments = path.segments;
|
||||
for name in segments {
|
||||
let scope = curr.scope(db)?;
|
||||
let def_id = ctry!(ctry!(scope.get(&name)).def_id);
|
||||
curr = match db.id_maps().def_loc(def_id) {
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue