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] [workspace]
members = [ "crates/*" ] members = [ "crates/*" ]
exclude = [ "crates/rowan"]
[profile.release] [profile.release]
debug = true debug = true

View file

@ -20,25 +20,31 @@ pub struct CrateGraph {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
struct CrateData { struct CrateData {
file_id: FileId, file_id: FileId,
deps: Vec<Dependency>, dependencies: Vec<Dependency>,
} }
impl CrateData { impl CrateData {
fn new(file_id: FileId) -> CrateData { fn new(file_id: FileId) -> CrateData {
CrateData { CrateData {
file_id, file_id,
deps: Vec::new(), dependencies: Vec::new(),
} }
} }
fn add_dep(&mut self, dep: CrateId) { 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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency { pub struct Dependency {
crate_: CrateId, crate_id: CrateId,
}
impl Dependency {
pub fn crate_id(&self) -> CrateId {
self.crate_id
}
} }
impl CrateGraph { impl CrateGraph {
@ -64,6 +70,12 @@ impl CrateGraph {
.find(|(_crate_id, data)| data.file_id == file_id)?; .find(|(_crate_id, data)| data.file_id == file_id)?;
Some(crate_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! { 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 use ra_db::CrateId;
pub struct CrateId(u32);
/// 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)] #[derive(Debug)]
pub struct Crate { pub struct Crate {
root: FileId, crate_id: CrateId,
} }
impl Crate { impl Crate {
pub fn dependencies(&self) -> Vec<CrateId> { pub(crate) fn new(crate_id: CrateId) -> Crate {
Vec::new() 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 module;
mod function; mod function;
use std::ops::Index; use std::ops::Index;
use ra_syntax::{SyntaxNodeRef, SyntaxNode}; use ra_syntax::{SyntaxNodeRef, SyntaxNode};

View file

@ -12,7 +12,7 @@ use ra_db::{SourceRootId, FileId, Cancelable};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{ use crate::{
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
arena::{Arena, Id}, 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 /// The root of the tree this module is part of
pub fn crate_root(&self) -> Module { pub fn crate_root(&self) -> Module {
let root_id = self.module_id.crate_root(&self.tree); let root_id = self.module_id.crate_root(&self.tree);