From 9a820dc0ee23051137e4909d4698fe71930c04bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 5 Jan 2019 00:02:05 +0300 Subject: move crate to code_model_api --- crates/ra_hir/src/code_model_api.rs | 26 +++++++++++++++++++ crates/ra_hir/src/code_model_impl.rs | 34 +++++++++++++++++++++++++ crates/ra_hir/src/krate.rs | 48 ------------------------------------ crates/ra_hir/src/lib.rs | 7 +++--- 4 files changed, 64 insertions(+), 51 deletions(-) create mode 100644 crates/ra_hir/src/code_model_api.rs create mode 100644 crates/ra_hir/src/code_model_impl.rs delete mode 100644 crates/ra_hir/src/krate.rs diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs new file mode 100644 index 000000000..e8b3a1fb6 --- /dev/null +++ b/crates/ra_hir/src/code_model_api.rs @@ -0,0 +1,26 @@ +use ra_db::{CrateId, Cancelable}; + +use crate::{Module, Name, db::HirDatabase}; + +/// 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, Clone, PartialEq, Eq, Hash)] +pub struct Crate { + pub(crate) crate_id: CrateId, +} + +#[derive(Debug)] +pub struct CrateDependency { + pub krate: Crate, + pub name: Name, +} + +impl Crate { + pub fn dependencies(&self, db: &impl HirDatabase) -> Vec { + self.dependencies_impl(db) + } + pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable> { + self.root_module_impl(db) + } +} diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs new file mode 100644 index 000000000..75d4e04c1 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl.rs @@ -0,0 +1,34 @@ +use ra_db::{CrateId, Cancelable}; + +use crate::{Module, HirFileId, db::HirDatabase, Crate, CrateDependency, AsName}; + +impl Crate { + pub(crate) fn new(crate_id: CrateId) -> Crate { + Crate { crate_id } + } + pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec { + let crate_graph = db.crate_graph(); + crate_graph + .dependencies(self.crate_id) + .map(|dep| { + let krate = Crate::new(dep.crate_id()); + let name = dep.as_name(); + CrateDependency { krate, name } + }) + .collect() + } + pub(crate) fn root_module_impl(&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 file_id = HirFileId::from(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/krate.rs b/crates/ra_hir/src/krate.rs deleted file mode 100644 index 5194e280b..000000000 --- a/crates/ra_hir/src/krate.rs +++ /dev/null @@ -1,48 +0,0 @@ -pub use ra_db::{CrateId, Cancelable}; - -use crate::{HirDatabase, Module, Name, AsName, HirFileId}; - -/// 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, Clone, PartialEq, Eq, Hash)] -pub struct Crate { - crate_id: CrateId, -} - -#[derive(Debug)] -pub struct CrateDependency { - pub krate: Crate, - pub name: Name, -} - -impl Crate { - 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| { - let krate = Crate::new(dep.crate_id()); - let name = dep.as_name(); - CrateDependency { krate, name } - }) - .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 file_id = HirFileId::from(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 d600b91df..0fe80deb5 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -24,8 +24,6 @@ pub mod source_binder; mod ids; mod macros; mod name; -// can't use `crate` or `r#crate` here :( -mod krate; mod module; mod function; mod adt; @@ -34,16 +32,19 @@ mod ty; mod impl_block; mod expr; +pub mod code_model_api; +mod code_model_impl; + use crate::{ db::HirDatabase, name::{AsName, KnownName}, ids::{DefKind, SourceItemId, SourceFileItemId, SourceFileItems}, + code_model_api::{Crate, CrateDependency} }; pub use self::{ path::{Path, PathKind}, name::Name, - krate::Crate, ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc}, macros::{MacroDef, MacroInput, MacroExpansion}, module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution}, -- cgit v1.2.3