Files
rust/crates/ra_hir/src/code_model_impl/module.rs

200 lines
7.5 KiB
Rust
Raw Normal View History

2019-01-15 20:56:06 +03:00
use ra_db::{SourceRootId, FileId};
use ra_syntax::{ast, SyntaxNode, AstNode, TreeArc};
2019-01-06 17:33:27 +03:00
use crate::{
Module, ModuleSource, Problem,
2019-01-08 15:57:45 +03:00
Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def,
module_tree::ModuleId,
2019-01-18 16:56:02 +03:00
nameres::{ModuleScope, lower::ImportId},
2019-01-06 17:33:27 +03:00
db::HirDatabase,
};
impl Module {
pub(crate) fn new(def_id: DefId) -> Self {
crate::code_model_api::Module { def_id }
}
2019-01-06 17:33:27 +03:00
pub(crate) fn from_module_id(
db: &impl HirDatabase,
source_root_id: SourceRootId,
module_id: ModuleId,
2019-01-15 18:13:11 +03:00
) -> Self {
let module_tree = db.module_tree(source_root_id);
2019-01-06 17:33:27 +03:00
let def_loc = DefLoc {
kind: DefKind::Module,
source_root_id,
module_id,
2019-01-06 19:58:10 +03:00
source_item_id: module_id.source(&module_tree),
2019-01-06 17:33:27 +03:00
};
let def_id = def_loc.id(db);
2019-01-15 18:13:11 +03:00
Module::new(def_id)
2019-01-06 17:33:27 +03:00
}
2019-01-15 18:26:29 +03:00
pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
2019-01-15 18:26:29 +03:00
let link = loc.module_id.parent_link(&module_tree)?;
Some(link.name(&module_tree).clone())
2019-01-06 17:33:27 +03:00
}
2019-01-18 16:36:56 +03:00
pub(crate) fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
2019-01-06 17:33:27 +03:00
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 11:28:42 +03:00
let module_source = if let Some(source_file) = ast::SourceFile::cast(&syntax_node) {
ModuleSource::SourceFile(source_file.to_owned())
2019-01-06 17:33:27 +03:00
} else {
2019-01-08 11:28:42 +03:00
let module = ast::Module::cast(&syntax_node).unwrap();
ModuleSource::Module(module.to_owned())
2019-01-06 17:33:27 +03:00
};
2019-01-15 18:26:29 +03:00
(file_id, module_source)
2019-01-06 17:33:27 +03:00
}
2019-01-18 16:36:56 +03:00
pub(crate) fn declaration_source_impl(
2019-01-06 17:33:27 +03:00
&self,
db: &impl HirDatabase,
2019-01-15 18:26:29 +03:00
) -> Option<(FileId, TreeArc<ast::Module>)> {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
2019-01-15 18:26:29 +03:00
let link = loc.module_id.parent_link(&module_tree)?;
2019-01-06 17:33:27 +03:00
let file_id = link
.owner(&module_tree)
.source(&module_tree)
2019-01-06 19:58:10 +03:00
.file_id
2019-01-06 17:33:27 +03:00
.as_original_file();
2019-01-06 19:58:10 +03:00
let src = link.source(&module_tree, db);
2019-01-15 18:26:29 +03:00
Some((file_id, src))
2019-01-06 17:33:27 +03:00
}
2019-01-18 16:36:56 +03:00
pub(crate) fn import_source_impl(
&self,
db: &impl HirDatabase,
2019-01-18 16:56:02 +03:00
import: ImportId,
2019-01-18 16:36:56 +03:00
) -> TreeArc<ast::PathSegment> {
let loc = self.def_id.loc(db);
let source_map = db.lower_module_source_map(loc.source_root_id, loc.module_id);
let (_, source) = self.definition_source(db);
source_map.get(&source, import)
}
pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> {
let root = self.crate_root(db);
2019-01-06 17:33:27 +03:00
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 = crate_graph.crate_id_for_crate_root(file_id)?;
Some(Crate::new(crate_id))
2019-01-06 17:33:27 +03:00
}
pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
2019-01-06 17:33:27 +03:00
let module_id = loc.module_id.crate_root(&module_tree);
Module::from_module_id(db, loc.source_root_id, module_id)
2019-01-06 17:33:27 +03:00
}
2019-01-06 17:33:27 +03:00
/// Finds a child module with the specified name.
2019-01-15 18:13:11 +03:00
pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
2019-01-15 18:13:11 +03: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 17:33:27 +03:00
}
/// Iterates over all child modules.
pub fn children_impl(&self, db: &impl HirDatabase) -> 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);
let module_tree = db.module_tree(loc.source_root_id);
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 18:13:11 +03:00
.collect::<Vec<_>>();
children.into_iter()
}
pub fn parent_impl(&self, db: &impl HirDatabase) -> Option<Module> {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
let parent_id = loc.module_id.parent(&module_tree)?;
Some(Module::from_module_id(db, loc.source_root_id, parent_id))
2019-01-06 17:33:27 +03:00
}
2019-01-06 17:33:27 +03:00
/// Returns a `ModuleScope`: a set of items, visible in this module.
2019-01-15 19:15:01 +03:00
pub fn scope_impl(&self, db: &impl HirDatabase) -> ModuleScope {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
2019-01-15 19:15:01 +03:00
let item_map = db.item_map(loc.source_root_id);
item_map.per_module[&loc.module_id].clone()
2019-01-06 17:33:27 +03:00
}
2019-01-15 19:15:01 +03:00
pub fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs<DefId> {
2019-01-06 17:33:27 +03:00
let mut curr_per_ns = PerNs::types(
match path.kind {
PathKind::Crate => self.crate_root(db),
2019-01-06 17:33:27 +03:00
PathKind::Self_ | PathKind::Plain => self.clone(),
PathKind::Super => {
if let Some(p) = self.parent(db) {
2019-01-06 17:33:27 +03:00
p
} else {
2019-01-15 19:15:01 +03:00
return PerNs::none();
2019-01-06 17:33:27 +03:00
}
}
}
.def_id,
);
for segment in path.segments.iter() {
2019-01-13 20:26:14 +01:00
let curr = match curr_per_ns.as_ref().take_types() {
Some(r) => r,
None => {
// we still have path segments left, but the path so far
// didn't resolve in the types namespace => no resolution
// (don't break here because curr_per_ns might contain
// something in the value namespace, and it would be wrong
// to return that)
return PerNs::none();
}
2019-01-06 17:33:27 +03:00
};
2019-01-13 20:26:14 +01:00
// resolve segment in curr
curr_per_ns = match curr.resolve(db) {
Def::Module(m) => {
let scope = m.scope(db);
match scope.get(&segment.name) {
2019-01-13 20:26:14 +01:00
Some(r) => r.def_id,
None => PerNs::none(),
}
}
Def::Enum(e) => {
2019-01-13 20:26:14 +01:00
// enum variant
let matching_variant = e
.variants(db)
.into_iter()
.find(|(n, _variant)| n == &segment.name);
2019-01-13 20:26:14 +01:00
match matching_variant {
Some((_n, variant)) => PerNs::both(variant.def_id(), e.def_id()),
None => PerNs::none(),
}
}
2019-01-13 20:26:14 +01:00
_ => {
// could be an inherent method call in UFCS form
// (`Struct::method`), or some other kind of associated
// item... Which we currently don't handle (TODO)
PerNs::none()
}
2019-01-06 17:33:27 +03:00
};
}
2019-01-15 19:15:01 +03:00
curr_per_ns
2019-01-06 17:33:27 +03:00
}
2019-01-15 20:56:06 +03:00
pub fn problems_impl(&self, db: &impl HirDatabase) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
2019-01-06 17:33:27 +03:00
let loc = self.def_id.loc(db);
let module_tree = db.module_tree(loc.source_root_id);
2019-01-15 20:56:06 +03:00
loc.module_id.problems(&module_tree, db)
2019-01-06 17:33:27 +03:00
}
}