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

176 lines
6.4 KiB
Rust
Raw Normal View History

use ra_db::FileId;
use ra_syntax::{ast, SyntaxNode, TreeArc};
2019-01-06 17:33:27 +03:00
use crate::{
Module, ModuleSource, Problem, ModuleDef,
Crate, Name, Path, PathKind, PerNs, Def,
2019-01-08 15:57:45 +03:00
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 {
fn with_module_id(&self, module_id: ModuleId) -> Module {
Module {
2019-01-06 17:33:27 +03:00
module_id,
krate: self.krate,
}
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> {
let module_tree = db.module_tree(self.krate);
let link = self.module_id.parent_link(&module_tree)?;
2019-01-15 18:26:29 +03:00
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) {
let module_tree = db.module_tree(self.krate);
let source = self.module_id.source(&module_tree);
let module_source = ModuleSource::from_source_item_id(db, source);
let file_id = source.file_id.as_original_file();
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>)> {
let module_tree = db.module_tree(self.krate);
let link = self.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 source_map = db.lower_module_source_map(self.clone());
2019-01-18 16:36:56 +03:00
let (_, source) = self.definition_source(db);
source_map.get(&source, import)
}
pub(crate) fn krate_impl(&self, _db: &impl HirDatabase) -> Option<Crate> {
Some(Crate::new(self.krate))
2019-01-06 17:33:27 +03:00
}
pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module {
let module_tree = db.module_tree(self.krate);
let module_id = self.module_id.crate_root(&module_tree);
self.with_module_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.
pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
let module_tree = db.module_tree(self.krate);
let child_id = self.module_id.child(&module_tree, name)?;
Some(self.with_module_id(child_id))
2019-01-06 17:33:27 +03:00
}
/// Iterates over all child modules.
pub(crate) fn children_impl(&self, db: &impl HirDatabase) -> impl Iterator<Item = Module> {
let module_tree = db.module_tree(self.krate);
let children = self
.module_id
.children(&module_tree)
.map(|(_, module_id)| self.with_module_id(module_id))
2019-01-15 18:13:11 +03:00
.collect::<Vec<_>>();
children.into_iter()
}
pub(crate) fn parent_impl(&self, db: &impl HirDatabase) -> Option<Module> {
let module_tree = db.module_tree(self.krate);
let parent_id = self.module_id.parent(&module_tree)?;
Some(self.with_module_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.
pub(crate) fn scope_impl(&self, db: &impl HirDatabase) -> ModuleScope {
let item_map = db.item_map(self.krate);
item_map.per_module[&self.module_id].clone()
2019-01-06 17:33:27 +03:00
}
pub(crate) fn resolve_path_impl(&self, db: &impl HirDatabase, path: &Path) -> PerNs<ModuleDef> {
let mut curr_per_ns: PerNs<ModuleDef> = PerNs::types(match path.kind {
PathKind::Crate => self.crate_root(db).into(),
PathKind::Self_ | PathKind::Plain => self.clone().into(),
PathKind::Super => {
if let Some(p) = self.parent(db) {
p.into()
} else {
2019-01-23 13:21:29 +08:00
return PerNs::none();
}
2019-01-06 17:33:27 +03:00
}
PathKind::Abs => {
// TODO: absolute use is not supported
return PerNs::none();
}
});
2019-01-06 17:33:27 +03:00
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 {
ModuleDef::Module(m) => {
2019-01-13 20:26:14 +01:00
let scope = m.scope(db);
match scope.get(&segment.name) {
Some(r) => r.def_id.clone(),
2019-01-13 20:26:14 +01:00
None => PerNs::none(),
}
}
2019-01-24 17:54:18 +03:00
ModuleDef::Function(_) | ModuleDef::Struct(_) => PerNs::none(),
ModuleDef::Def(def) => {
match def.resolve(db) {
Def::Enum(e) => {
// enum variant
let matching_variant = e
.variants(db)
.into_iter()
.find(|(n, _variant)| n == &segment.name);
match matching_variant {
Some((_n, variant)) => {
PerNs::both(variant.def_id().into(), e.def_id().into())
}
None => PerNs::none(),
}
}
_ => {
// 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
}
pub(crate) fn problems_impl(
&self,
db: &impl HirDatabase,
) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
let module_tree = db.module_tree(self.krate);
self.module_id.problems(&module_tree, db)
2019-01-06 17:33:27 +03:00
}
}