Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/ast_id_map.rs')
-rw-r--r--crates/hir-expand/src/ast_id_map.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/crates/hir-expand/src/ast_id_map.rs b/crates/hir-expand/src/ast_id_map.rs
index 2abeaaeec4..d0d229e131 100644
--- a/crates/hir-expand/src/ast_id_map.rs
+++ b/crates/hir-expand/src/ast_id_map.rs
@@ -199,6 +199,19 @@ impl AstIdMap {
FileAstId { raw, covariant: PhantomData }
}
+ pub fn ast_id_for_ptr<N: AstIdNode>(&self, ptr: AstPtr<N>) -> FileAstId<N> {
+ let ptr = ptr.syntax_node_ptr();
+ let hash = hash_ptr(&ptr);
+ match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
+ Some((&raw, &())) => FileAstId { raw, covariant: PhantomData },
+ None => panic!(
+ "Can't find {:?} in AstIdMap:\n{:?}",
+ ptr,
+ self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
+ ),
+ }
+ }
+
pub fn get<N: AstIdNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
AstPtr::try_from_raw(self.arena[id.raw].clone()).unwrap()
}