1 //! Utilities for mapping between hir IDs and the surface syntax.
3 use arena::map::ArenaMap;
4 use hir_expand::InFile;
6 use crate::{db::DefDatabase, item_tree::ItemTreeNode, AssocItemLoc, ItemLoc};
10 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
13 impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> {
14 type Value = N::Source;
16 fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
17 let tree = db.item_tree(self.id.file_id);
18 let ast_id_map = db.ast_id_map(self.id.file_id);
19 let root = db.parse_or_expand(self.id.file_id).unwrap();
20 let node = &tree[self.id.value];
22 InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root))
26 impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
27 type Value = N::Source;
29 fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
30 let tree = db.item_tree(self.id.file_id);
31 let ast_id_map = db.ast_id_map(self.id.file_id);
32 let root = db.parse_or_expand(self.id.file_id).unwrap();
33 let node = &tree[self.id.value];
35 InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root))
39 pub trait HasChildSource<ChildId> {
41 fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;