diff options
author | Lukas Wirth <[email protected]> | 2021-03-15 11:18:52 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-15 11:18:52 +0000 |
commit | 41745f48d5f867ff0896ce7906b5b4c04e72a767 (patch) | |
tree | 8a70342d903be16b20c62f8800a59af368ffb012 /crates/ide_db | |
parent | a1c96e04be55b3412e5510fc8d09cd82675dd4cd (diff) |
move Semantics::visit_file_defs to ide_db::helpers
Diffstat (limited to 'crates/ide_db')
-rw-r--r-- | crates/ide_db/src/helpers.rs | 28 |
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 @@ | |||
2 | pub mod insert_use; | 2 | pub mod insert_use; |
3 | pub mod import_assets; | 3 | pub mod import_assets; |
4 | 4 | ||
5 | use std::collections::VecDeque; | ||
6 | |||
7 | use base_db::FileId; | ||
8 | use either::Either; | ||
5 | use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; | 9 | use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait}; |
6 | use syntax::ast::{self, make}; | 10 | use 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. | ||
47 | pub 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 |