Skip to content

Commit 41745f4

Browse files
committed
move Semantics::visit_file_defs to ide_db::helpers
1 parent a1c96e0 commit 41745f4

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
33
mod source_to_def;
44

5-
use std::{cell::RefCell, collections::VecDeque, fmt, iter::successors};
5+
use std::{cell::RefCell, fmt, iter::successors};
66

77
use base_db::{FileId, FileRange};
8-
use either::Either;
98
use hir_def::{
10-
nameres::ModuleSource,
119
resolver::{self, HasResolver, Resolver, TypeNs},
1210
AsMacroCall, FunctionId, TraitId, VariantId,
1311
};
@@ -157,28 +155,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
157155
self.imp.ancestors_at_offset_with_macros(node, offset)
158156
}
159157

160-
/// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
161-
pub fn visit_file_defs(&self, file_id: FileId, cb: &mut dyn FnMut(Either<ModuleDef, Impl>)) {
162-
let module = match self.to_module_def(file_id) {
163-
Some(it) => it,
164-
None => return,
165-
};
166-
let mut defs: VecDeque<_> = module.declarations(self.db).into();
167-
while let Some(def) = defs.pop_front() {
168-
if let ModuleDef::Module(submodule) = def {
169-
if let ModuleSource::Module(_) = submodule.definition_source(self.db).value {
170-
defs.extend(submodule.declarations(self.db));
171-
submodule
172-
.impl_defs(self.db)
173-
.into_iter()
174-
.for_each(|impl_| cb(Either::Right(impl_)));
175-
}
176-
}
177-
cb(Either::Left(def));
178-
}
179-
module.impl_defs(self.db).into_iter().for_each(|impl_| cb(Either::Right(impl_)));
180-
}
181-
182158
/// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*,
183159
/// search up until it is of the target AstNode type
184160
pub fn find_node_at_offset_with_macros<N: AstNode>(

crates/ide/src/annotations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use either::Either;
22
use hir::{HasSource, Semantics};
33
use ide_db::{
44
base_db::{FileId, FilePosition, FileRange},
5+
helpers::visit_file_defs,
56
RootDatabase,
67
};
78
use syntax::{ast::NameOwner, AstNode, TextRange, TextSize};
@@ -75,7 +76,7 @@ pub(crate) fn annotations(
7576
}
7677
}
7778

78-
Semantics::new(db).visit_file_defs(file_id, &mut |def| match def {
79+
visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def {
7980
Either::Left(def) => {
8081
let node = match def {
8182
hir::ModuleDef::Const(konst) => {

crates/ide/src/runnables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ide_assists::utils::test_related_attribute;
88
use ide_db::{
99
base_db::{FilePosition, FileRange},
1010
defs::Definition,
11+
helpers::visit_file_defs,
1112
search::SearchScope,
1213
RootDatabase, SymbolKind,
1314
};
@@ -105,7 +106,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
105106
let sema = Semantics::new(db);
106107

107108
let mut res = Vec::new();
108-
sema.visit_file_defs(file_id, &mut |def| match def {
109+
visit_file_defs(&sema, file_id, &mut |def| match def {
109110
Either::Left(def) => {
110111
let runnable = match def {
111112
hir::ModuleDef::Module(it) => runnable_mod(&sema, it),

crates/ide_db/src/helpers.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
pub mod insert_use;
33
pub mod import_assets;
44

5+
use std::collections::VecDeque;
6+
7+
use base_db::FileId;
8+
use either::Either;
59
use hir::{Crate, Enum, ItemInNs, MacroDef, Module, ModuleDef, Name, ScopeDef, Semantics, Trait};
610
use syntax::ast::{self, make};
711

@@ -39,6 +43,30 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
3943
make::path_from_segments(segments, is_abs)
4044
}
4145

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+
4270
/// Helps with finding well-know things inside the standard library. This is
4371
/// somewhat similar to the known paths infra inside hir, but it different; We
4472
/// want to make sure that IDE specific paths don't become interesting inside

0 commit comments

Comments
 (0)