From ca7e5905c1daffbed6a08fe674ae821f99bd274d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 Dec 2018 00:51:06 +0300 Subject: more crate boilerplate --- Cargo.toml | 1 - crates/ra_db/src/input.rs | 20 ++++++++++++++++---- crates/ra_hir/src/krate.rs | 34 ++++++++++++++++++++++++++++------ crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/module.rs | 11 ++++++++++- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8e4e84729..5cfc064b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,5 @@ [workspace] members = [ "crates/*" ] -exclude = [ "crates/rowan"] [profile.release] debug = true diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index a48d05b98..37da8c549 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -20,25 +20,31 @@ pub struct CrateGraph { #[derive(Debug, Clone, PartialEq, Eq)] struct CrateData { file_id: FileId, - deps: Vec, + dependencies: Vec, } 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 + 'a { + self.arena[&crate_id].dependencies.iter() + } } salsa::query_group! { diff --git a/crates/ra_hir/src/krate.rs b/crates/ra_hir/src/krate.rs index 367ddbd21..61007cc29 100644 --- a/crates/ra_hir/src/krate.rs +++ b/crates/ra_hir/src/krate.rs @@ -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 { - Vec::new() + pub(crate) fn new(crate_id: CrateId) -> Crate { + Crate { crate_id } + } + pub fn dependencies(&self, db: &impl HirDatabase) -> Vec { + 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> { + 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)) } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 0fa2ec50f..578fde259 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -26,7 +26,6 @@ mod krate; mod module; mod function; - use std::ops::Index; use ra_syntax::{SyntaxNodeRef, SyntaxNode}; diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index e7a49f83a..c6bb76d56 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs @@ -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 { + 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); -- cgit v1.2.3