Start code_model::Module
This commit is contained in:
parent
9a820dc0ee
commit
147b0f94e6
@ -1,6 +1,6 @@
|
||||
use ra_db::{CrateId, Cancelable};
|
||||
|
||||
use crate::{Module, Name, db::HirDatabase};
|
||||
use crate::{Name, db::HirDatabase, DefId};
|
||||
|
||||
/// hir::Crate describes a single crate. It's the main inteface with which
|
||||
/// crate's dependencies interact. Mostly, it should be just a proxy for the
|
||||
@ -24,3 +24,24 @@ pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
|
||||
self.root_module_impl(db)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Module {
|
||||
pub(crate) def_id: DefId,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
/// Returns the crate this module is part of.
|
||||
pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> {
|
||||
self.krate_impl(db)
|
||||
}
|
||||
|
||||
pub fn crate_root(&self, db: &impl HirDatabase) -> Cancelable<Module> {
|
||||
self.crate_root_impl(db)
|
||||
}
|
||||
|
||||
/// Finds a child module with the specified name.
|
||||
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
||||
self.child_impl(db, name)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
use ra_db::{CrateId, Cancelable};
|
||||
|
||||
use crate::{Module, HirFileId, db::HirDatabase, Crate, CrateDependency, AsName};
|
||||
use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name};
|
||||
|
||||
use crate::code_model_api::Module;
|
||||
|
||||
|
||||
impl Crate {
|
||||
pub(crate) fn new(crate_id: CrateId) -> Crate {
|
||||
@ -28,7 +31,59 @@ pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Optio
|
||||
.modules_with_sources()
|
||||
.find(|(_, src)| src.file_id() == file_id));
|
||||
|
||||
let module = Module::new(db, source_root_id, module_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(Some(module))
|
||||
}
|
||||
}
|
||||
|
||||
impl Module {
|
||||
fn new(def_id: DefId) -> Self {
|
||||
crate::code_model_api::Module { def_id }
|
||||
}
|
||||
|
||||
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);
|
||||
let def_loc = DefLoc {
|
||||
module_id,
|
||||
source_item_id: module_id.source(&module_tree).0,
|
||||
..loc
|
||||
};
|
||||
let def_id = def_loc.id(db);
|
||||
let module = Module::new(def_id);
|
||||
Ok(module)
|
||||
}
|
||||
/// 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));
|
||||
let def_loc = DefLoc {
|
||||
module_id: child_id,
|
||||
source_item_id: child_id.source(&module_tree).0,
|
||||
..loc
|
||||
};
|
||||
let def_id = def_loc.id(db);
|
||||
let module = Module::new(def_id);
|
||||
Ok(Some(module))
|
||||
}
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ pub(crate) fn modules_with_sources<'a>(
|
||||
/// `ModuleSource` is the syntax tree element that produced this module:
|
||||
/// either a file, or an inlinde module.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ModuleSource(SourceItemId);
|
||||
pub struct ModuleSource(pub(crate) SourceItemId);
|
||||
|
||||
/// An owned syntax node for a module. Unlike `ModuleSource`,
|
||||
/// this holds onto the AST for the whole file.
|
||||
@ -255,12 +255,12 @@ fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
|
||||
let link = self.parent_link(tree)?;
|
||||
Some(tree.links[link].owner)
|
||||
}
|
||||
fn crate_root(self, tree: &ModuleTree) -> ModuleId {
|
||||
pub(crate) fn crate_root(self, tree: &ModuleTree) -> ModuleId {
|
||||
generate(Some(self), move |it| it.parent(tree))
|
||||
.last()
|
||||
.unwrap()
|
||||
}
|
||||
fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> {
|
||||
pub(crate) fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> {
|
||||
let link = tree.mods[self]
|
||||
.children
|
||||
.iter()
|
||||
|
@ -346,7 +346,7 @@ fn populate_module(
|
||||
let krate = Crate::new(crate_id);
|
||||
for dep in krate.dependencies(self.db) {
|
||||
if let Some(module) = dep.krate.root_module(self.db)? {
|
||||
let def_id = module.def_id(self.db);
|
||||
let def_id = module.def_id;
|
||||
self.add_module_item(
|
||||
&mut module_items,
|
||||
dep.name.clone(),
|
||||
|
Loading…
Reference in New Issue
Block a user