From 6964a88e8c90f06220498d3e9194b7e2073c1e32 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 9 Feb 2019 18:27:11 +0100 Subject: Add an ra_cli command that analyses all crates in the current workspace ... and prints various stats about how many expressions have a type etc. --- crates/ra_hir/src/code_model_api.rs | 34 ++++++++++++++++++++++++++++++++-- crates/ra_hir/src/expr.rs | 8 ++++++++ crates/ra_hir/src/ty.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index cafc5279d..19f103855 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use relative_path::RelativePathBuf; -use ra_db::{CrateId, FileId}; +use ra_db::{CrateId, FileId, SourceRootId}; use ra_syntax::{ast::self, TreeArc, SyntaxNode}; use crate::{ @@ -16,7 +16,7 @@ use crate::{ docs::{Documentation, Docs, docs_from_ast}, module_tree::ModuleId, ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, - impl_block::ImplId, + impl_block::{ImplId, ImplBlock}, resolve::Resolver, }; @@ -44,6 +44,15 @@ impl Crate { pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option { self.root_module_impl(db) } + + // TODO: should this be in source_binder? + pub fn source_root_crates( + db: &impl PersistentHirDatabase, + source_root: SourceRootId, + ) -> Vec { + let crate_ids = db.source_root_crates(source_root); + crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect() + } } #[derive(Debug)] @@ -168,6 +177,27 @@ impl Module { let item_map = db.item_map(self.krate); Resolver::default().push_module_scope(item_map, *self) } + + pub fn declarations(self, db: &impl HirDatabase) -> Vec { + let (lowered_module, _) = db.lower_module(self); + lowered_module + .declarations + .values() + .cloned() + .flat_map(|per_ns| { + per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) + }) + .collect() + } + + pub fn impl_blocks(self, db: &impl HirDatabase) -> Vec { + let module_impl_blocks = db.impls_in_module(self); + module_impl_blocks + .impls + .iter() + .map(|(impl_id, _)| ImplBlock::from_id(module_impl_blocks.clone(), impl_id)) + .collect() + } } impl Docs for Module { diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index b30e11abb..4e73590d0 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -70,6 +70,14 @@ impl Body { self.owner } + pub fn exprs(&self) -> impl Iterator { + self.exprs.iter() + } + + pub fn pats(&self) -> impl Iterator { + self.pats.iter() + } + pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc { db.body_syntax_mapping(self.owner) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 7203a8a10..08561573b 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -449,6 +449,41 @@ impl Ty { Ty::Tuple(Arc::new([])) } + pub fn walk(&self, f: &mut impl FnMut(&Ty)) { + f(self); + match self { + Ty::Slice(t) | Ty::Array(t) => t.walk(f), + Ty::RawPtr(t, _) => t.walk(f), + Ty::Ref(t, _) => t.walk(f), + Ty::Tuple(ts) => { + for t in ts.iter() { + t.walk(f); + } + } + Ty::FnPtr(sig) => { + for input in &sig.input { + input.walk(f); + } + sig.output.walk(f); + } + Ty::FnDef { substs, sig, .. } => { + for input in &sig.input { + input.walk(f); + } + sig.output.walk(f); + for t in substs.0.iter() { + t.walk(f); + } + } + Ty::Adt { substs, .. } => { + for t in substs.0.iter() { + t.walk(f); + } + } + _ => {} + } + } + fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { f(self); match self { -- cgit v1.2.3