aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r--crates/ide_db/src/helpers.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs
index 3c95d3cff..9992a92bd 100644
--- a/crates/ide_db/src/helpers.rs
+++ b/crates/ide_db/src/helpers.rs
@@ -2,6 +2,10 @@
2pub mod insert_use; 2pub mod insert_use;
3pub mod import_assets; 3pub mod import_assets;
4 4
5use std::collections::VecDeque;
6
7use base_db::FileId;
8use either::Either;
5use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; 9use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
6use syntax::ast::{self, make}; 10use syntax::ast::{self, make};
7 11
@@ -39,6 +43,30 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
39 make::path_from_segments(segments, is_abs) 43 make::path_from_segments(segments, is_abs)
40} 44}
41 45
46/// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
47pub fn visit_file_defs(
48 sema: &Semantics<RootDatabase>,
49 file_id: FileId,
50 cb: &mut dyn FnMut(Either<hir::ModuleDef, hir::Impl>),
51) {
52 let db = sema.db;
53 let module = match sema.to_module_def(file_id) {
54 Some(it) => it,
55 None => return,
56 };
57 let mut defs: VecDeque<_> = module.declarations(db).into();
58 while let Some(def) = defs.pop_front() {
59 if let ModuleDef::Module(submodule) = def {
60 if let hir::ModuleSource::Module(_) = submodule.definition_source(db).value {
61 defs.extend(submodule.declarations(db));
62 submodule.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
63 }
64 }
65 cb(Either::Left(def));
66 }
67 module.impl_defs(db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
68}
69
42/// Helps with finding well-know things inside the standard library. This is 70/// Helps with finding well-know things inside the standard library. This is
43/// somewhat similar to the known paths infra inside hir, but it different; We 71/// somewhat similar to the known paths infra inside hir, but it different; We
44/// want to make sure that IDE specific paths don't become interesting inside 72/// want to make sure that IDE specific paths don't become interesting inside