module-scoped defloc
This commit is contained in:
parent
45fce90349
commit
947e3350e0
3 changed files with 43 additions and 24 deletions
crates/ra_hir/src
|
@ -61,15 +61,18 @@ impl FnId {
|
||||||
pub struct DefId(u32);
|
pub struct DefId(u32);
|
||||||
ra_db::impl_numeric_id!(DefId);
|
ra_db::impl_numeric_id!(DefId);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub(crate) enum DefKind {
|
||||||
|
Module,
|
||||||
|
Item,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum DefLoc {
|
pub struct DefLoc {
|
||||||
Module {
|
pub(crate) kind: DefKind,
|
||||||
id: ModuleId,
|
source_root_id: SourceRootId,
|
||||||
source_root: SourceRootId,
|
module_id: ModuleId,
|
||||||
},
|
source_item_id: SourceItemId,
|
||||||
Item {
|
|
||||||
source_item_id: SourceItemId,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefId {
|
impl DefId {
|
||||||
|
@ -92,12 +95,12 @@ pub enum Def {
|
||||||
impl DefId {
|
impl DefId {
|
||||||
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
|
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
|
||||||
let loc = self.loc(db);
|
let loc = self.loc(db);
|
||||||
let res = match loc {
|
let res = match loc.kind {
|
||||||
DefLoc::Module { id, source_root } => {
|
DefKind::Module => {
|
||||||
let descr = Module::new(db, source_root, id)?;
|
let descr = Module::new(db, loc.source_root_id, loc.module_id)?;
|
||||||
Def::Module(descr)
|
Def::Module(descr)
|
||||||
}
|
}
|
||||||
DefLoc::Item { .. } => Def::Item,
|
DefKind::Item => Def::Item,
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ use ra_db::{SourceRootId, FileId, FilePosition, Cancelable};
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
|
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
|
||||||
arena::{Arena, Id},
|
arena::{Arena, Id},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -127,9 +127,11 @@ impl Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
|
pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
|
||||||
let def_loc = DefLoc::Module {
|
let def_loc = DefLoc {
|
||||||
id: self.module_id,
|
kind: DefKind::Module,
|
||||||
source_root: self.source_root_id,
|
source_root_id: self.source_root_id,
|
||||||
|
module_id: self.module_id,
|
||||||
|
source_item_id: self.module_id.source(&self.tree).0,
|
||||||
};
|
};
|
||||||
def_loc.id(db)
|
def_loc.id(db)
|
||||||
}
|
}
|
||||||
|
@ -161,7 +163,12 @@ impl Module {
|
||||||
let segments = path.segments;
|
let segments = path.segments;
|
||||||
for name in segments.iter() {
|
for name in segments.iter() {
|
||||||
let module = match curr.loc(db) {
|
let module = match curr.loc(db) {
|
||||||
DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?,
|
DefLoc {
|
||||||
|
kind: DefKind::Module,
|
||||||
|
source_root_id,
|
||||||
|
module_id,
|
||||||
|
..
|
||||||
|
} => Module::new(db, source_root_id, module_id)?,
|
||||||
_ => return Ok(None),
|
_ => return Ok(None),
|
||||||
};
|
};
|
||||||
let scope = module.scope(db)?;
|
let scope = module.scope(db)?;
|
||||||
|
|
|
@ -28,7 +28,7 @@ use ra_db::SourceRootId;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Cancelable, FileId,
|
Cancelable, FileId,
|
||||||
DefId, DefLoc,
|
DefId, DefLoc, DefKind,
|
||||||
SourceItemId, SourceFileItemId, SourceFileItems,
|
SourceItemId, SourceFileItemId, SourceFileItems,
|
||||||
Path, PathKind,
|
Path, PathKind,
|
||||||
HirDatabase,
|
HirDatabase,
|
||||||
|
@ -247,7 +247,10 @@ where
|
||||||
// handle submodules separatelly
|
// handle submodules separatelly
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let def_loc = DefLoc::Item {
|
let def_loc = DefLoc {
|
||||||
|
kind: DefKind::Item,
|
||||||
|
source_root_id: self.source_root,
|
||||||
|
module_id,
|
||||||
source_item_id: SourceItemId {
|
source_item_id: SourceItemId {
|
||||||
file_id,
|
file_id,
|
||||||
item_id: item.id,
|
item_id: item.id,
|
||||||
|
@ -261,10 +264,12 @@ where
|
||||||
module_items.items.insert(item.name.clone(), resolution);
|
module_items.items.insert(item.name.clone(), resolution);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, mod_id) in module_id.children(&self.module_tree) {
|
for (name, module_id) in module_id.children(&self.module_tree) {
|
||||||
let def_loc = DefLoc::Module {
|
let def_loc = DefLoc {
|
||||||
id: mod_id,
|
kind: DefKind::Module,
|
||||||
source_root: self.source_root,
|
source_root_id: self.source_root,
|
||||||
|
module_id,
|
||||||
|
source_item_id: module_id.source(&self.module_tree).0,
|
||||||
};
|
};
|
||||||
let def_id = def_loc.id(self.db);
|
let def_id = def_loc.id(self.db);
|
||||||
let resolution = Resolution {
|
let resolution = Resolution {
|
||||||
|
@ -316,7 +321,11 @@ where
|
||||||
|
|
||||||
if !is_last {
|
if !is_last {
|
||||||
curr = match def_id.loc(self.db) {
|
curr = match def_id.loc(self.db) {
|
||||||
DefLoc::Module { id, .. } => id,
|
DefLoc {
|
||||||
|
kind: DefKind::Module,
|
||||||
|
module_id,
|
||||||
|
..
|
||||||
|
} => module_id,
|
||||||
_ => return,
|
_ => return,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue