more crate boilerplate

This commit is contained in:
Aleksey Kladov 2018-12-09 00:51:06 +03:00
parent 9c6c7ec2da
commit ca7e5905c1
5 changed files with 54 additions and 13 deletions

View file

@ -1,6 +1,5 @@
[workspace]
members = [ "crates/*" ]
exclude = [ "crates/rowan"]
[profile.release]
debug = true

View file

@ -20,25 +20,31 @@ pub struct CrateGraph {
#[derive(Debug, Clone, PartialEq, Eq)]
struct CrateData {
file_id: FileId,
deps: Vec<Dependency>,
dependencies: Vec<Dependency>,
}
impl CrateData {
fn new(file_id: FileId) -> CrateData {
CrateData {
file_id,
deps: Vec::new(),
dependencies: Vec::new(),
}
}
fn add_dep(&mut self, dep: CrateId) {
self.deps.push(Dependency { crate_: dep })
self.dependencies.push(Dependency { crate_id: dep })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency {
crate_: CrateId,
crate_id: CrateId,
}
impl Dependency {
pub fn crate_id(&self) -> CrateId {
self.crate_id
}
}
impl CrateGraph {
@ -64,6 +70,12 @@ impl CrateGraph {
.find(|(_crate_id, data)| data.file_id == file_id)?;
Some(crate_id)
}
pub fn dependencies<'a>(
&'a self,
crate_id: CrateId,
) -> impl Iterator<Item = &'a Dependency> + 'a {
self.arena[&crate_id].dependencies.iter()
}
}
salsa::query_group! {

View file

@ -1,15 +1,37 @@
use crate::FileId;
use crate::{HirDatabase, Module, Cancelable};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CrateId(u32);
pub use ra_db::CrateId;
/// 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
/// root module.
#[derive(Debug)]
pub struct Crate {
root: FileId,
crate_id: CrateId,
}
impl Crate {
pub fn dependencies(&self) -> Vec<CrateId> {
Vec::new()
pub(crate) fn new(crate_id: CrateId) -> Crate {
Crate { crate_id }
}
pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<Crate> {
let crate_graph = db.crate_graph();
crate_graph
.dependencies(self.crate_id)
.map(|dep| Crate::new(dep.crate_id()))
.collect()
}
pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
let crate_graph = db.crate_graph();
let file_id = crate_graph.crate_root(self.crate_id);
let source_root_id = db.file_source_root(file_id);
let module_tree = db.module_tree(source_root_id)?;
// FIXME: teach module tree about crate roots instead of guessing
let (module_id, _) = ctry!(module_tree
.modules_with_sources()
.find(|(_, src)| src.file_id() == file_id));
let module = Module::new(db, source_root_id, module_id)?;
Ok(Some(module))
}
}

View file

@ -26,7 +26,6 @@ mod krate;
mod module;
mod function;
use std::ops::Index;
use ra_syntax::{SyntaxNodeRef, SyntaxNode};

View file

@ -12,7 +12,7 @@ use ra_db::{SourceRootId, FileId, Cancelable};
use relative_path::RelativePathBuf;
use crate::{
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
arena::{Arena, Id},
};
@ -64,6 +64,15 @@ impl Module {
})
}
/// Returns the crate this module is part of.
pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
let root_id = self.module_id.crate_root(&self.tree);
let file_id = root_id.source(&self.tree).file_id();
let crate_graph = db.crate_graph();
let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
Some(Crate::new(crate_id))
}
/// The root of the tree this module is part of
pub fn crate_root(&self) -> Module {
let root_id = self.module_id.crate_root(&self.tree);