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 --- crates/ra_hir/src/krate.rs | 34 ++++++++++++++++++++++++++++------ crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/module.rs | 11 ++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir') 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