2019-01-06 14:33:27 +00:00
|
|
|
use ra_db::{Cancelable, SourceRootId, FileId};
|
2019-01-11 16:59:06 +00:00
|
|
|
use ra_syntax::{ast, SyntaxNode, AstNode, TreeArc};
|
2019-01-06 14:33:27 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
Module, ModuleSource, Problem,
|
2019-01-08 12:57:45 +00:00
|
|
|
Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def,
|
|
|
|
module_tree::ModuleId,
|
2019-01-06 14:33:27 +00:00
|
|
|
nameres::ModuleScope,
|
|
|
|
db::HirDatabase,
|
|
|
|
};
|
|
|
|
|
|
|
|
impl Module {
|
|
|
|
pub(crate) fn new(def_id: DefId) -> Self {
|
|
|
|
crate::code_model_api::Module { def_id }
|
|
|
|
}
|
2019-01-08 15:01:19 +00:00
|
|
|
|
2019-01-06 14:33:27 +00:00
|
|
|
pub(crate) fn from_module_id(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
source_root_id: SourceRootId,
|
|
|
|
module_id: ModuleId,
|
2019-01-15 15:13:11 +00:00
|
|
|
) -> Self {
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id,
|
|
|
|
module_id,
|
2019-01-06 16:58:10 +00:00
|
|
|
source_item_id: module_id.source(&module_tree),
|
2019-01-06 14:33:27 +00:00
|
|
|
};
|
|
|
|
let def_id = def_loc.id(db);
|
2019-01-15 15:13:11 +00:00
|
|
|
Module::new(def_id)
|
2019-01-06 14:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
let link = ctry!(loc.module_id.parent_link(&module_tree));
|
|
|
|
Ok(Some(link.name(&module_tree).clone()))
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:38:51 +00:00
|
|
|
pub fn definition_source_impl(
|
2019-01-06 14:33:27 +00:00
|
|
|
&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);
|
2019-01-08 08:28:42 +00:00
|
|
|
let module_source = if let Some(source_file) = ast::SourceFile::cast(&syntax_node) {
|
|
|
|
ModuleSource::SourceFile(source_file.to_owned())
|
2019-01-06 14:33:27 +00:00
|
|
|
} else {
|
2019-01-08 08:28:42 +00:00
|
|
|
let module = ast::Module::cast(&syntax_node).unwrap();
|
|
|
|
ModuleSource::Module(module.to_owned())
|
2019-01-06 14:33:27 +00:00
|
|
|
};
|
|
|
|
Ok((file_id, module_source))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn declaration_source_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
2019-01-11 16:59:06 +00:00
|
|
|
) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> {
|
2019-01-06 14:33:27 +00:00
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
let link = ctry!(loc.module_id.parent_link(&module_tree));
|
|
|
|
let file_id = link
|
|
|
|
.owner(&module_tree)
|
|
|
|
.source(&module_tree)
|
2019-01-06 16:58:10 +00:00
|
|
|
.file_id
|
2019-01-06 14:33:27 +00:00
|
|
|
.as_original_file();
|
2019-01-06 16:58:10 +00:00
|
|
|
let src = link.source(&module_tree, db);
|
2019-01-06 14:33:27 +00:00
|
|
|
Ok(Some((file_id, src)))
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
let module_id = loc.module_id.crate_root(&module_tree);
|
2019-01-15 15:13:11 +00:00
|
|
|
Ok(Module::from_module_id(db, loc.source_root_id, module_id))
|
2019-01-06 14:33:27 +00:00
|
|
|
}
|
2019-01-08 15:01:19 +00:00
|
|
|
|
2019-01-06 14:33:27 +00:00
|
|
|
/// Finds a child module with the specified name.
|
2019-01-15 15:13:11 +00:00
|
|
|
pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
|
2019-01-06 14:33:27 +00:00
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-15 15:13:11 +00:00
|
|
|
let child_id = loc.module_id.child(&module_tree, name)?;
|
|
|
|
Some(Module::from_module_id(db, loc.source_root_id, child_id))
|
2019-01-06 14:33:27 +00:00
|
|
|
}
|
2019-01-08 15:01:19 +00:00
|
|
|
|
2019-01-07 12:44:54 +00:00
|
|
|
/// Iterates over all child modules.
|
|
|
|
pub fn children_impl(&self, db: &impl HirDatabase) -> Cancelable<impl Iterator<Item = Module>> {
|
|
|
|
// FIXME this should be implementable without collecting into a vec, but
|
|
|
|
// it's kind of hard since the iterator needs to keep a reference to the
|
|
|
|
// module tree.
|
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-07 12:44:54 +00:00
|
|
|
let children = loc
|
|
|
|
.module_id
|
|
|
|
.children(&module_tree)
|
|
|
|
.map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id))
|
2019-01-15 15:13:11 +00:00
|
|
|
.collect::<Vec<_>>();
|
2019-01-07 12:44:54 +00:00
|
|
|
Ok(children.into_iter())
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:33:27 +00:00
|
|
|
pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
|
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
let parent_id = ctry!(loc.module_id.parent(&module_tree));
|
2019-01-15 15:13:11 +00:00
|
|
|
Ok(Some(Module::from_module_id(
|
|
|
|
db,
|
|
|
|
loc.source_root_id,
|
|
|
|
parent_id,
|
|
|
|
)))
|
2019-01-06 14:33:27 +00:00
|
|
|
}
|
2019-01-08 15:01:19 +00:00
|
|
|
|
2019-01-06 14:33:27 +00:00
|
|
|
/// 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-08 15:01:19 +00:00
|
|
|
|
2019-01-06 14:33:27 +00: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;
|
2019-01-08 15:01:19 +00:00
|
|
|
for (idx, name) in segments.iter().enumerate() {
|
2019-01-06 14:33:27 +00:00
|
|
|
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,
|
2019-01-08 15:01:19 +00:00
|
|
|
Def::Enum(e) => {
|
|
|
|
if segments.len() == idx + 1 {
|
|
|
|
// enum variant
|
2019-01-10 01:07:42 +00:00
|
|
|
let matching_variant =
|
|
|
|
e.variants(db)?.into_iter().find(|(n, _variant)| n == name);
|
2019-01-08 15:01:19 +00:00
|
|
|
|
2019-01-10 01:07:42 +00:00
|
|
|
if let Some((_n, variant)) = matching_variant {
|
2019-01-08 15:01:19 +00:00
|
|
|
return Ok(PerNs::both(variant.def_id(), e.def_id()));
|
|
|
|
} else {
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
}
|
|
|
|
} else if segments.len() == idx {
|
|
|
|
// enum
|
|
|
|
return Ok(PerNs::types(e.def_id()));
|
|
|
|
} else {
|
|
|
|
// malformed enum?
|
|
|
|
return Ok(PerNs::none());
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 14:33:27 +00:00
|
|
|
_ => 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-08 15:01:19 +00:00
|
|
|
|
2019-01-08 08:28:42 +00:00
|
|
|
pub fn problems_impl(
|
|
|
|
&self,
|
|
|
|
db: &impl HirDatabase,
|
2019-01-11 16:59:06 +00:00
|
|
|
) -> Cancelable<Vec<(TreeArc<SyntaxNode>, Problem)>> {
|
2019-01-06 14:33:27 +00:00
|
|
|
let loc = self.def_id.loc(db);
|
2019-01-15 14:55:15 +00:00
|
|
|
let module_tree = db.module_tree(loc.source_root_id);
|
2019-01-06 14:33:27 +00:00
|
|
|
Ok(loc.module_id.problems(&module_tree, db))
|
|
|
|
}
|
|
|
|
}
|