2019-01-23 23:14:13 +03:00
|
|
|
use ra_db::FileId;
|
|
|
|
|
use ra_syntax::{ast, SyntaxNode, TreeArc};
|
2019-01-06 17:33:27 +03:00
|
|
|
|
|
|
|
|
use crate::{
|
2019-01-25 10:29:00 +03:00
|
|
|
Module, ModuleSource, Problem,
|
2019-01-30 22:23:14 +03:00
|
|
|
Name,
|
2019-01-08 15:57:45 +03:00
|
|
|
module_tree::ModuleId,
|
2019-01-28 09:26:32 -05:00
|
|
|
impl_block::ImplId,
|
2019-01-25 10:29:00 +03:00
|
|
|
nameres::{lower::ImportId},
|
2019-02-01 18:33:41 +08:00
|
|
|
HirDatabase, PersistentHirDatabase,
|
2019-01-06 17:33:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
impl Module {
|
2019-01-23 23:14:13 +03:00
|
|
|
fn with_module_id(&self, module_id: ModuleId) -> Module {
|
2019-02-08 14:49:43 +03:00
|
|
|
Module { 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> {
|
2019-01-23 23:14:13 +03:00
|
|
|
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-02-01 18:33:41 +08:00
|
|
|
pub(crate) fn definition_source_impl(
|
|
|
|
|
&self,
|
|
|
|
|
db: &impl PersistentHirDatabase,
|
|
|
|
|
) -> (FileId, ModuleSource) {
|
2019-01-23 23:14:13 +03:00
|
|
|
let module_tree = db.module_tree(self.krate);
|
2019-01-26 23:25:18 +03:00
|
|
|
let file_id = self.module_id.file_id(&module_tree);
|
|
|
|
|
let decl_id = self.module_id.decl_id(&module_tree);
|
|
|
|
|
let module_source = ModuleSource::new(db, file_id, decl_id);
|
|
|
|
|
let file_id = 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>)> {
|
2019-01-23 23:14:13 +03:00
|
|
|
let module_tree = db.module_tree(self.krate);
|
|
|
|
|
let link = self.module_id.parent_link(&module_tree)?;
|
2019-02-08 14:49:43 +03:00
|
|
|
let file_id = link.owner(&module_tree).file_id(&module_tree).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> {
|
2019-01-28 09:26:32 -05:00
|
|
|
let source_map = db.lower_module_source_map(*self);
|
2019-01-18 16:36:56 +03:00
|
|
|
let (_, source) = self.definition_source(db);
|
|
|
|
|
source_map.get(&source, import)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 09:26:32 -05:00
|
|
|
pub(crate) fn impl_source_impl(
|
|
|
|
|
&self,
|
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
|
impl_id: ImplId,
|
|
|
|
|
) -> TreeArc<ast::ImplBlock> {
|
|
|
|
|
let source_map = db.impls_in_module_source_map(*self);
|
|
|
|
|
let (_, source) = self.definition_source(db);
|
|
|
|
|
source_map.get(&source, impl_id)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 18:33:41 +08:00
|
|
|
pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module {
|
2019-01-23 23:14:13 +03:00
|
|
|
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-08 16:01:19 +01:00
|
|
|
|
2019-01-06 17:33:27 +03:00
|
|
|
/// Finds a child module with the specified name.
|
2019-01-13 11:58:41 +01:00
|
|
|
pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
|
2019-01-23 23:14:13 +03:00
|
|
|
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
|
|
|
}
|
2019-01-08 16:01:19 +01:00
|
|
|
|
2019-01-07 13:44:54 +01:00
|
|
|
/// Iterates over all child modules.
|
2019-02-01 18:33:41 +08:00
|
|
|
pub(crate) fn children_impl(
|
|
|
|
|
&self,
|
|
|
|
|
db: &impl PersistentHirDatabase,
|
|
|
|
|
) -> impl Iterator<Item = Module> {
|
2019-01-23 23:14:13 +03:00
|
|
|
let module_tree = db.module_tree(self.krate);
|
|
|
|
|
let children = self
|
2019-01-07 13:44:54 +01:00
|
|
|
.module_id
|
|
|
|
|
.children(&module_tree)
|
2019-01-23 23:14:13 +03:00
|
|
|
.map(|(_, module_id)| self.with_module_id(module_id))
|
2019-01-15 18:13:11 +03:00
|
|
|
.collect::<Vec<_>>();
|
2019-01-15 18:30:58 +03:00
|
|
|
children.into_iter()
|
2019-01-07 13:44:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-01 18:33:41 +08:00
|
|
|
pub(crate) fn parent_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
|
2019-01-23 23:14:13 +03:00
|
|
|
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-08 16:01:19 +01:00
|
|
|
|
2019-01-13 11:58:41 +01:00
|
|
|
pub(crate) fn problems_impl(
|
|
|
|
|
&self,
|
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
|
) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
|
2019-01-23 23:14:13 +03:00
|
|
|
let module_tree = db.module_tree(self.krate);
|
|
|
|
|
self.module_id.problems(&module_tree, db)
|
2019-01-06 17:33:27 +03:00
|
|
|
}
|
|
|
|
|
}
|