diff options
author | Florian Diebold <[email protected]> | 2019-02-09 17:27:11 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-02-10 09:56:58 +0000 |
commit | 6964a88e8c90f06220498d3e9194b7e2073c1e32 (patch) | |
tree | a3f8e225118c810d5c2784d64b559e3e05e6b6db /crates/ra_hir | |
parent | 43e52ac9e2b26ec287b1778823bad10851cfd44e (diff) |
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.
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 34 | ||||
-rw-r--r-- | crates/ra_hir/src/expr.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 35 |
3 files changed, 75 insertions, 2 deletions
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 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use relative_path::RelativePathBuf; | 3 | use relative_path::RelativePathBuf; |
4 | use ra_db::{CrateId, FileId}; | 4 | use ra_db::{CrateId, FileId, SourceRootId}; |
5 | use ra_syntax::{ast::self, TreeArc, SyntaxNode}; | 5 | use ra_syntax::{ast::self, TreeArc, SyntaxNode}; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
@@ -16,7 +16,7 @@ use crate::{ | |||
16 | docs::{Documentation, Docs, docs_from_ast}, | 16 | docs::{Documentation, Docs, docs_from_ast}, |
17 | module_tree::ModuleId, | 17 | module_tree::ModuleId, |
18 | ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, | 18 | ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeId}, |
19 | impl_block::ImplId, | 19 | impl_block::{ImplId, ImplBlock}, |
20 | resolve::Resolver, | 20 | resolve::Resolver, |
21 | }; | 21 | }; |
22 | 22 | ||
@@ -44,6 +44,15 @@ impl Crate { | |||
44 | pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option<Module> { | 44 | pub fn root_module(&self, db: &impl PersistentHirDatabase) -> Option<Module> { |
45 | self.root_module_impl(db) | 45 | self.root_module_impl(db) |
46 | } | 46 | } |
47 | |||
48 | // TODO: should this be in source_binder? | ||
49 | pub fn source_root_crates( | ||
50 | db: &impl PersistentHirDatabase, | ||
51 | source_root: SourceRootId, | ||
52 | ) -> Vec<Crate> { | ||
53 | let crate_ids = db.source_root_crates(source_root); | ||
54 | crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect() | ||
55 | } | ||
47 | } | 56 | } |
48 | 57 | ||
49 | #[derive(Debug)] | 58 | #[derive(Debug)] |
@@ -168,6 +177,27 @@ impl Module { | |||
168 | let item_map = db.item_map(self.krate); | 177 | let item_map = db.item_map(self.krate); |
169 | Resolver::default().push_module_scope(item_map, *self) | 178 | Resolver::default().push_module_scope(item_map, *self) |
170 | } | 179 | } |
180 | |||
181 | pub fn declarations(self, db: &impl HirDatabase) -> Vec<ModuleDef> { | ||
182 | let (lowered_module, _) = db.lower_module(self); | ||
183 | lowered_module | ||
184 | .declarations | ||
185 | .values() | ||
186 | .cloned() | ||
187 | .flat_map(|per_ns| { | ||
188 | per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) | ||
189 | }) | ||
190 | .collect() | ||
191 | } | ||
192 | |||
193 | pub fn impl_blocks(self, db: &impl HirDatabase) -> Vec<ImplBlock> { | ||
194 | let module_impl_blocks = db.impls_in_module(self); | ||
195 | module_impl_blocks | ||
196 | .impls | ||
197 | .iter() | ||
198 | .map(|(impl_id, _)| ImplBlock::from_id(module_impl_blocks.clone(), impl_id)) | ||
199 | .collect() | ||
200 | } | ||
171 | } | 201 | } |
172 | 202 | ||
173 | impl Docs for Module { | 203 | 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 { | |||
70 | self.owner | 70 | self.owner |
71 | } | 71 | } |
72 | 72 | ||
73 | pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | ||
74 | self.exprs.iter() | ||
75 | } | ||
76 | |||
77 | pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> { | ||
78 | self.pats.iter() | ||
79 | } | ||
80 | |||
73 | pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> { | 81 | pub fn syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> { |
74 | db.body_syntax_mapping(self.owner) | 82 | db.body_syntax_mapping(self.owner) |
75 | } | 83 | } |
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 { | |||
449 | Ty::Tuple(Arc::new([])) | 449 | Ty::Tuple(Arc::new([])) |
450 | } | 450 | } |
451 | 451 | ||
452 | pub fn walk(&self, f: &mut impl FnMut(&Ty)) { | ||
453 | f(self); | ||
454 | match self { | ||
455 | Ty::Slice(t) | Ty::Array(t) => t.walk(f), | ||
456 | Ty::RawPtr(t, _) => t.walk(f), | ||
457 | Ty::Ref(t, _) => t.walk(f), | ||
458 | Ty::Tuple(ts) => { | ||
459 | for t in ts.iter() { | ||
460 | t.walk(f); | ||
461 | } | ||
462 | } | ||
463 | Ty::FnPtr(sig) => { | ||
464 | for input in &sig.input { | ||
465 | input.walk(f); | ||
466 | } | ||
467 | sig.output.walk(f); | ||
468 | } | ||
469 | Ty::FnDef { substs, sig, .. } => { | ||
470 | for input in &sig.input { | ||
471 | input.walk(f); | ||
472 | } | ||
473 | sig.output.walk(f); | ||
474 | for t in substs.0.iter() { | ||
475 | t.walk(f); | ||
476 | } | ||
477 | } | ||
478 | Ty::Adt { substs, .. } => { | ||
479 | for t in substs.0.iter() { | ||
480 | t.walk(f); | ||
481 | } | ||
482 | } | ||
483 | _ => {} | ||
484 | } | ||
485 | } | ||
486 | |||
452 | fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { | 487 | fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { |
453 | f(self); | 488 | f(self); |
454 | match self { | 489 | match self { |