switch source-binders to Module

This commit is contained in:
Aleksey Kladov 2019-01-05 12:13:16 +03:00
parent 147b0f94e6
commit 8c4d277036
2 changed files with 20 additions and 9 deletions

View file

@ -4,7 +4,6 @@ use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, D
use crate::code_model_api::Module; use crate::code_model_api::Module;
impl Crate { impl Crate {
pub(crate) fn new(crate_id: CrateId) -> Crate { pub(crate) fn new(crate_id: CrateId) -> Crate {
Crate { crate_id } Crate { crate_id }
@ -45,7 +44,7 @@ impl Crate {
} }
impl Module { impl Module {
fn new(def_id: DefId) -> Self { pub(crate) fn new(def_id: DefId) -> Self {
crate::code_model_api::Module { def_id } crate::code_model_api::Module { def_id }
} }

View file

@ -13,11 +13,13 @@ use ra_syntax::{
}; };
use crate::{ use crate::{
HirDatabase, Module, Function, SourceItemId, HirDatabase, Function, SourceItemId,
module::ModuleSource, module::ModuleSource,
DefKind, DefLoc, AsName, DefKind, DefLoc, AsName,
}; };
use crate::code_model_api::Module;
/// Locates the module by `FileId`. Picks topmost module in the file. /// Locates the module by `FileId`. Picks topmost module in the file.
pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> { pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> {
let module_source = ModuleSource::new_file(file_id.into()); let module_source = ModuleSource::new_file(file_id.into());
@ -34,7 +36,7 @@ pub fn module_from_declaration(
let child_name = decl.name(); let child_name = decl.name();
match (parent_module, child_name) { match (parent_module, child_name) {
(Some(parent_module), Some(child_name)) => { (Some(parent_module), Some(child_name)) => {
if let Some(child) = parent_module.child(&child_name.as_name()) { if let Some(child) = parent_module.child(db, &child_name.as_name())? {
return Ok(Some(child)); return Ok(Some(child));
} }
} }
@ -84,7 +86,15 @@ fn module_from_source(
.modules_with_sources() .modules_with_sources()
.find(|(_id, src)| src == &module_source); .find(|(_id, src)| src == &module_source);
let module_id = ctry!(m).0; let module_id = ctry!(m).0;
Ok(Some(Module::new(db, source_root_id, module_id)?)) let def_loc = DefLoc {
kind: DefKind::Module,
source_root_id,
module_id,
source_item_id: module_source.0,
};
let def_id = def_loc.id(db);
Ok(Some(Module::new(def_id)))
} }
pub fn function_from_position( pub fn function_from_position(
@ -114,7 +124,8 @@ pub fn function_from_module(
module: &Module, module: &Module,
fn_def: ast::FnDef, fn_def: ast::FnDef,
) -> Function { ) -> Function {
let file_id = module.source().file_id(); let loc = module.def_id.loc(db);
let file_id = loc.source_item_id.file_id;
let file_items = db.file_items(file_id); let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, fn_def.syntax()); let item_id = file_items.id_of(file_id, fn_def.syntax());
let source_item_id = SourceItemId { let source_item_id = SourceItemId {
@ -123,8 +134,8 @@ pub fn function_from_module(
}; };
let def_loc = DefLoc { let def_loc = DefLoc {
kind: DefKind::Function, kind: DefKind::Function,
source_root_id: module.source_root_id, source_root_id: loc.source_root_id,
module_id: module.module_id, module_id: loc.module_id,
source_item_id, source_item_id,
}; };
Function::new(def_loc.id(db)) Function::new(def_loc.id(db))
@ -147,7 +158,8 @@ pub fn macro_symbols(
Some(it) => it, Some(it) => it,
None => return Ok(Vec::new()), None => return Ok(Vec::new()),
}; };
let items = db.input_module_items(module.source_root_id, module.module_id)?; let loc = module.def_id.loc(db);
let items = db.input_module_items(loc.source_root_id, loc.module_id)?;
let mut res = Vec::new(); let mut res = Vec::new();
for macro_call_id in items for macro_call_id in items