1 //! Utilities for mapping between hir IDs and the surface syntax.
3 use hir_expand::InFile;
4 use la_arena::ArenaMap;
8 db::DefDatabase, item_tree::ItemTreeNode, AssocItemLoc, ItemLoc, Macro2Loc, MacroRulesLoc,
14 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
17 impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> {
18 type Value = N::Source;
20 fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
21 let tree = self.id.item_tree(db);
22 let ast_id_map = db.ast_id_map(self.id.file_id());
23 let root = db.parse_or_expand(self.id.file_id()).unwrap();
24 let node = &tree[self.id.value];
26 InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
30 impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
31 type Value = N::Source;
33 fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
34 let tree = self.id.item_tree(db);
35 let ast_id_map = db.ast_id_map(self.id.file_id());
36 let root = db.parse_or_expand(self.id.file_id()).unwrap();
37 let node = &tree[self.id.value];
39 InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
43 impl HasSource for Macro2Loc {
44 type Value = ast::MacroDef;
46 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
47 let tree = self.id.item_tree(db);
48 let ast_id_map = db.ast_id_map(self.id.file_id());
49 let root = db.parse_or_expand(self.id.file_id()).unwrap();
50 let node = &tree[self.id.value];
52 InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
56 impl HasSource for MacroRulesLoc {
57 type Value = ast::MacroRules;
59 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
60 let tree = self.id.item_tree(db);
61 let ast_id_map = db.ast_id_map(self.id.file_id());
62 let root = db.parse_or_expand(self.id.file_id()).unwrap();
63 let node = &tree[self.id.value];
65 InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
69 impl HasSource for ProcMacroLoc {
72 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
73 let tree = self.id.item_tree(db);
74 let ast_id_map = db.ast_id_map(self.id.file_id());
75 let root = db.parse_or_expand(self.id.file_id()).unwrap();
76 let node = &tree[self.id.value];
78 InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
82 pub trait HasChildSource<ChildId> {
84 fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;