mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
resolve_path works with DefIds
This commit is contained in:
parent
aa7fd563a4
commit
192e2bbb0e
3 changed files with 46 additions and 11 deletions
|
@ -13,6 +13,7 @@ use crate::{
|
||||||
descriptors::{
|
descriptors::{
|
||||||
module::{ModuleDescriptor},
|
module::{ModuleDescriptor},
|
||||||
function::FnScopes,
|
function::FnScopes,
|
||||||
|
Def,
|
||||||
Path,
|
Path,
|
||||||
},
|
},
|
||||||
Cancelable
|
Cancelable
|
||||||
|
@ -156,10 +157,14 @@ fn complete_path(
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
path.segments.pop();
|
path.segments.pop();
|
||||||
let target_module = match module.resolve_path(db, path)? {
|
let def_id = match module.resolve_path(db, path)? {
|
||||||
None => return Ok(()),
|
None => return Ok(()),
|
||||||
Some(it) => it,
|
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 module_scope = target_module.scope(db)?;
|
||||||
let completions = module_scope.entries().map(|(name, _res)| CompletionItem {
|
let completions = module_scope.entries().map(|(name, _res)| CompletionItem {
|
||||||
label: name.to_string(),
|
label: name.to_string(),
|
||||||
|
|
|
@ -13,9 +13,12 @@ use crate::{
|
||||||
FileId,
|
FileId,
|
||||||
db::SyntaxDatabase,
|
db::SyntaxDatabase,
|
||||||
descriptors::function::{resolve_local_name, FnId, FnScopes},
|
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,
|
input::SourceRootId,
|
||||||
loc2id::IdDatabase,
|
loc2id::{IdDatabase, DefId, DefLoc},
|
||||||
syntax_ptr::LocalSyntaxPtr,
|
syntax_ptr::LocalSyntaxPtr,
|
||||||
Cancelable,
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct ReferenceDescriptor {
|
pub struct ReferenceDescriptor {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
|
|
|
@ -77,7 +77,7 @@ impl ModuleDescriptor {
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new(
|
pub(super) fn new(
|
||||||
db: &impl DescriptorDatabase,
|
db: &impl DescriptorDatabase,
|
||||||
source_root_id: SourceRootId,
|
source_root_id: SourceRootId,
|
||||||
module_id: ModuleId,
|
module_id: ModuleId,
|
||||||
|
@ -132,6 +132,14 @@ impl ModuleDescriptor {
|
||||||
Some(link.name(&self.tree))
|
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.
|
/// Finds a child module with the specified name.
|
||||||
pub fn child(&self, name: &str) -> Option<ModuleDescriptor> {
|
pub fn child(&self, name: &str) -> Option<ModuleDescriptor> {
|
||||||
let child_id = self.module_id.child(&self.tree, name)?;
|
let child_id = self.module_id.child(&self.tree, name)?;
|
||||||
|
@ -152,23 +160,23 @@ impl ModuleDescriptor {
|
||||||
&self,
|
&self,
|
||||||
db: &impl DescriptorDatabase,
|
db: &impl DescriptorDatabase,
|
||||||
path: Path,
|
path: Path,
|
||||||
) -> Cancelable<Option<ModuleDescriptor>> {
|
) -> Cancelable<Option<DefId>> {
|
||||||
let mut curr = match path.kind {
|
let mut curr = match path.kind {
|
||||||
PathKind::Crate => self.crate_root(),
|
PathKind::Crate => self.crate_root(),
|
||||||
PathKind::Self_ | PathKind::Plain => self.clone(),
|
PathKind::Self_ | PathKind::Plain => self.clone(),
|
||||||
PathKind::Super => ctry!(self.parent()),
|
PathKind::Super => ctry!(self.parent()),
|
||||||
};
|
}
|
||||||
|
.def_id(db);
|
||||||
|
|
||||||
let segments = path.segments;
|
let segments = path.segments;
|
||||||
for name in segments {
|
for name in segments.iter() {
|
||||||
let scope = curr.scope(db)?;
|
let module = match db.id_maps().def_loc(curr) {
|
||||||
let def_id = ctry!(ctry!(scope.get(&name)).def_id);
|
|
||||||
curr = match db.id_maps().def_loc(def_id) {
|
|
||||||
DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?,
|
DefLoc::Module { id, source_root } => ModuleDescriptor::new(db, source_root, id)?,
|
||||||
_ => return Ok(None),
|
_ => return Ok(None),
|
||||||
};
|
};
|
||||||
|
let scope = module.scope(db)?;
|
||||||
|
curr = ctry!(ctry!(scope.get(&name)).def_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(curr))
|
Ok(Some(curr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue