Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/nameres.rs')
-rw-r--r--crates/hir-def/src/nameres.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs
index 59ca38c7c0..5f05cdb1e2 100644
--- a/crates/hir-def/src/nameres.rs
+++ b/crates/hir-def/src/nameres.rs
@@ -502,6 +502,7 @@ impl DefMap {
}
impl DefMap {
+ /// Returns all modules in the crate that are associated with the given file.
pub fn modules_for_file<'a>(
&'a self,
db: &'a dyn DefDatabase,
@@ -509,16 +510,33 @@ impl DefMap {
) -> impl Iterator<Item = ModuleId> + 'a {
self.modules
.iter()
- .filter(move |(_id, data)| {
+ .filter(move |(_, data)| {
data.origin.file_id().map(|file_id| file_id.file_id(db)) == Some(file_id)
})
- .map(|(id, _data)| id)
+ .map(|(id, _)| id)
}
pub fn modules(&self) -> impl Iterator<Item = (ModuleId, &ModuleData)> + '_ {
self.modules.iter()
}
+ /// Returns all inline modules (mod name { ... }) in the crate that are associated with the given macro expansion.
+ pub fn inline_modules_for_macro_file(
+ &self,
+ file_id: MacroCallId,
+ ) -> impl Iterator<Item = ModuleId> + '_ {
+ self.modules
+ .iter()
+ .filter(move |(_, data)| {
+ matches!(
+ data.origin,
+ ModuleOrigin::Inline { definition_tree_id, .. }
+ if definition_tree_id.file_id().macro_file() == Some(file_id)
+ )
+ })
+ .map(|(id, _)| id)
+ }
+
pub fn derive_helpers_in_scope(
&self,
id: AstId<ast::Adt>,