This commit is contained in:
Aleksey Kladov 2019-01-01 21:17:52 +03:00
parent f8d419ee89
commit 4a3f76d3bb
2 changed files with 49 additions and 47 deletions

View File

@ -25,13 +25,12 @@
use ra_db::SourceRootId;
use crate::{
Cancelable, FileId,
Cancelable, MFileId, FileId,
DefId, DefLoc, DefKind,
SourceItemId, SourceFileItemId, SourceFileItems,
Path, PathKind,
HirDatabase, Crate,
Name, AsName,
macros::MacroCallLoc,
module::{Module, ModuleId, ModuleTree},
};
@ -71,7 +70,7 @@ pub struct InputModuleItems {
#[derive(Debug, PartialEq, Eq)]
struct ModuleItem {
id: SourceFileItemId,
id: SourceItemId,
name: Name,
kind: SyntaxKind,
vis: Vis,
@ -210,24 +209,28 @@ pub fn map<U>(self, f: impl Fn(T) -> U) -> PerNs<U> {
}
impl InputModuleItems {
pub(crate) fn new<'a>(
pub(crate) fn add_item(
&mut self,
mfile_id: MFileId,
file_items: &SourceFileItems,
items: impl Iterator<Item = ast::ModuleItem<'a>>,
) -> InputModuleItems {
let mut res = InputModuleItems::default();
for item in items {
res.add_item(file_items, item);
}
res
}
fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> {
item: ast::ModuleItem,
) -> Option<()> {
match item {
ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::StructDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::EnumDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::FnDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::TraitDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::TypeDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::ImplItem(_) => {
// impls don't define items
}
@ -235,9 +238,15 @@ fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> O
ast::ModuleItem::ExternCrateItem(_) => {
// TODO
}
ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?),
ast::ModuleItem::ConstDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::StaticDef(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
ast::ModuleItem::Module(it) => {
self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
}
}
Some(())
}
@ -259,11 +268,16 @@ fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
}
impl ModuleItem {
fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> {
fn new<'a>(
mfile_id: MFileId,
file_items: &SourceFileItems,
item: impl ast::NameOwner<'a>,
) -> Option<ModuleItem> {
let name = item.name()?.as_name();
let kind = item.syntax().kind();
let vis = Vis::Other;
let id = file_items.id_of_unchecked(item.syntax());
let item_id = Some(file_items.id_of_unchecked(item.syntax()));
let id = SourceItemId { mfile_id, item_id };
let res = ModuleItem {
id,
name,
@ -303,7 +317,7 @@ pub(crate) fn new(
pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> {
for (&module_id, items) in self.input.iter() {
self.populate_module(module_id, items)?;
self.populate_module(module_id, Arc::clone(items))?;
}
for &module_id in self.input.keys() {
@ -313,9 +327,11 @@ pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> {
Ok(self.result)
}
fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) -> Cancelable<()> {
let file_id = module_id.source(&self.module_tree).file_id();
fn populate_module(
&mut self,
module_id: ModuleId,
input: Arc<InputModuleItems>,
) -> Cancelable<()> {
let mut module_items = ModuleScope::default();
// Populate extern crates prelude
@ -355,18 +371,6 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) ->
if item.kind == MODULE {
continue;
}
if item.kind == MACRO_CALL {
let loc = MacroCallLoc {
source_root_id: self.source_root,
module_id,
source_item_id: SourceItemId {
mfile_id: file_id.into(),
item_id: Some(item.id),
},
};
let id = loc.id(self.db);
continue;
}
// depending on the item kind, the location can define something in
// the values namespace, the types namespace, or both
let kind = DefKind::for_syntax_kind(item.kind);
@ -375,10 +379,7 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) ->
kind: k,
source_root_id: self.source_root,
module_id,
source_item_id: SourceItemId {
mfile_id: file_id.into(),
item_id: Some(item.id),
},
source_item_id: item.id,
};
def_loc.id(self.db)
});

View File

@ -128,11 +128,12 @@ pub(super) fn input_module_items(
) -> Cancelable<Arc<InputModuleItems>> {
let module_tree = db.module_tree(source_root)?;
let source = module_id.source(&module_tree);
let file_items = db.file_items(source.file_id().into());
let mfile_id = source.file_id().into();
let file_items = db.file_items(mfile_id);
let res = match source.resolve(db) {
ModuleSourceNode::SourceFile(it) => {
let items = it.borrowed().items();
InputModuleItems::new(&file_items, items)
InputModuleItems::new(mfile_id, &file_items, items)
}
ModuleSourceNode::Module(it) => {
let items = it
@ -140,7 +141,7 @@ pub(super) fn input_module_items(
.item_list()
.into_iter()
.flat_map(|it| it.items());
InputModuleItems::new(&file_items, items)
InputModuleItems::new(mfile_id, &file_items, items)
}
};
Ok(Arc::new(res))