2019-01-06 15:58:45 +03:00
|
|
|
use ra_db::{CrateId, Cancelable, SourceRootId, FileId};
|
|
|
|
use ra_syntax::{ast, SyntaxNode, AstNode};
|
2018-12-08 23:40:55 +03:00
|
|
|
|
2019-01-06 15:16:21 +03:00
|
|
|
use crate::{
|
|
|
|
HirFileId, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId,
|
2019-01-06 15:58:45 +03:00
|
|
|
module::{ModuleScope, Problem},
|
2019-01-06 15:16:21 +03:00
|
|
|
db::HirDatabase,
|
|
|
|
};
|
2019-01-05 01:37:40 +03:00
|
|
|
|
2019-01-06 15:58:45 +03:00
|
|
|
use crate::code_model_api::{Module, ModuleSource};
|
2019-01-05 01:37:40 +03:00
|
|
|
|
2018-12-08 23:40:55 +03:00
|
|
|
impl Crate {
|
2018-12-09 00:51:06 +03:00
|
|
|
pub(crate) fn new(crate_id: CrateId) -> Crate {
|
|
|
|
Crate { crate_id }
|
|
|
|
}
|
2019-01-05 00:02:05 +03:00
|
|
|
pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
|
2018-12-09 00:51:06 +03:00
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
crate_graph
|
|
|
|
.dependencies(self.crate_id)
|
2018-12-09 01:05:49 +03:00
|
|
|
.map(|dep| {
|
|
|
|
let krate = Crate::new(dep.crate_id());
|
2018-12-27 20:07:21 +03:00
|
|
|
let name = dep.as_name();
|
2018-12-09 01:05:49 +03:00
|
|
|
CrateDependency { krate, name }
|
|
|
|
})
|
2018-12-09 00:51:06 +03:00
|
|
|
.collect()
|
|
|
|
}
|
2019-01-05 00:02:05 +03:00
|
|
|
pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
|
2018-12-09 00:51:06 +03:00
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let file_id = crate_graph.crate_root(self.crate_id);
|
|
|
|
let source_root_id = db.file_source_root(file_id);
|
2019-01-01 23:21:16 +03:00
|
|
|
let file_id = HirFileId::from(file_id);
|
2018-12-09 00:51:06 +03:00
|
|
|
let module_tree = db.module_tree(source_root_id)?;
|
|
|
|
// FIXME: teach module tree about crate roots instead of guessing
|
|
|
|
let (module_id, _) = ctry!(module_tree
|
|
|
|
.modules_with_sources()
|
|
|
|
.find(|(_, src)| src.file_id() == file_id));
|
|
|
|
|
2019-01-05 01:37:40 +03:00
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
|
|
|
source_item_id: module_id.source(&module_tree).0,
|
|
|
|
};
|
|
|
|
let def_id = def_loc.id(db);
|
|
|
|
|
|
|
|
let module = Module::new(def_id);
|
|
|
|
Ok(Some(module))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
2019-01-05 12:13:16 +03:00
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
2019-01-05 01:37:40 +03:00
|
|
|
crate::code_model_api::Module { def_id }
|
|
|
|
}
|
2019-01-06 15:16:21 +03:00
|
|
|
pub(crate) fn from_module_id(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
source_root_id: SourceRootId,
|
|
|
|
module_id: ModuleId,
|
|
|
|
) -> Cancelable<Self> {
|
|
|
|
let module_tree = db.module_tree(source_root_id)?;
|
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
|
|
|
source_item_id: module_id.source(&module_tree).0,
|
|
|
|
};
|
|
|
|
let def_id = def_loc.id(db);
|
|
|
|
let module = Module::new(def_id);
|
|
|
|
Ok(module)
|
|
|
|
}
|
2019-01-05 01:37:40 +03:00
|
|
|
|
2019-01-06 15:58:45 +03:00
|
|
|
pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
2019-01-06 13:41:12 +03:00
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-06 15:58:45 +03:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
let link = ctry!(loc.module_id.parent_link(&module_tree));
|
|
|
|
Ok(Some(link.name(&module_tree).clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn defenition_source_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
) -> Cancelable<(FileId, ModuleSource)> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let file_id = loc.source_item_id.file_id.as_original_file();
|
|
|
|
let syntax_node = db.file_item(loc.source_item_id);
|
|
|
|
let syntax_node = syntax_node.borrowed();
|
|
|
|
let module_source = if let Some(source_file) = ast::SourceFile::cast(syntax_node) {
|
|
|
|
ModuleSource::SourceFile(source_file.owned())
|
|
|
|
} else {
|
|
|
|
let module = ast::Module::cast(syntax_node).unwrap();
|
|
|
|
ModuleSource::Module(module.owned())
|
|
|
|
};
|
|
|
|
Ok((file_id, module_source))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn declaration_source_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
) -> Cancelable<Option<(FileId, ast::ModuleNode)>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
let link = ctry!(loc.module_id.parent_link(&module_tree));
|
|
|
|
let file_id = link
|
|
|
|
.owner(&module_tree)
|
|
|
|
.source(&module_tree)
|
|
|
|
.file_id()
|
|
|
|
.as_original_file();
|
|
|
|
let src = link.bind_source(&module_tree, db);
|
|
|
|
Ok(Some((file_id, src)))
|
2019-01-06 13:41:12 +03:00
|
|
|
}
|
|
|
|
|
2019-01-05 01:37:40 +03:00
|
|
|
pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
|
|
|
|
let root = self.crate_root(db)?;
|
|
|
|
let loc = root.def_id.loc(db);
|
|
|
|
let file_id = loc.source_item_id.file_id.as_original_file();
|
|
|
|
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id));
|
|
|
|
Ok(Some(Crate::new(crate_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
let module_id = loc.module_id.crate_root(&module_tree);
|
2019-01-06 15:16:21 +03:00
|
|
|
Module::from_module_id(db, loc.source_root_id, module_id)
|
2019-01-05 01:37:40 +03:00
|
|
|
}
|
|
|
|
/// Finds a child module with the specified name.
|
|
|
|
pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
let child_id = ctry!(loc.module_id.child(&module_tree, name));
|
2019-01-06 15:16:21 +03:00
|
|
|
Module::from_module_id(db, loc.source_root_id, child_id).map(Some)
|
2018-12-08 23:40:55 +03:00
|
|
|
}
|
2019-01-06 14:05:03 +03:00
|
|
|
pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
let parent_id = ctry!(loc.module_id.parent(&module_tree));
|
2019-01-06 15:16:21 +03:00
|
|
|
Module::from_module_id(db, loc.source_root_id, parent_id).map(Some)
|
|
|
|
}
|
|
|
|
/// Returns a `ModuleScope`: a set of items, visible in this module.
|
|
|
|
pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let item_map = db.item_map(loc.source_root_id)?;
|
|
|
|
let res = item_map.per_module[&loc.module_id].clone();
|
|
|
|
Ok(res)
|
2019-01-06 14:05:03 +03:00
|
|
|
}
|
|
|
|
pub fn resolve_path_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
path: &Path,
|
|
|
|
) -> Cancelable<PerNs<DefId>> {
|
|
|
|
let mut curr_per_ns = PerNs::types(
|
|
|
|
match path.kind {
|
|
|
|
PathKind::Crate => self.crate_root(db)?,
|
|
|
|
PathKind::Self_ | PathKind::Plain => self.clone(),
|
|
|
|
PathKind::Super => {
|
|
|
|
if let Some(p) = self.parent(db)? {
|
|
|
|
p
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.def_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
let segments = &path.segments;
|
|
|
|
for name in segments.iter() {
|
|
|
|
let curr = if let Some(r) = curr_per_ns.as_ref().take_types() {
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
};
|
|
|
|
let module = match curr.resolve(db)? {
|
|
|
|
Def::Module(it) => it,
|
|
|
|
// TODO here would be the place to handle enum variants...
|
|
|
|
_ => return Ok(PerNs::none()),
|
|
|
|
};
|
|
|
|
let scope = module.scope(db)?;
|
|
|
|
curr_per_ns = if let Some(r) = scope.get(&name) {
|
|
|
|
r.def_id
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(curr_per_ns)
|
|
|
|
}
|
2019-01-06 15:58:45 +03:00
|
|
|
pub fn problems_impl(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SyntaxNode, Problem)>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
|
|
|
let module_tree = db.module_tree(loc.source_root_id)?;
|
|
|
|
Ok(loc.module_id.problems(&module_tree, db))
|
|
|
|
}
|
2018-12-08 23:40:55 +03:00
|
|
|
}
|