diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/hir_def/src/src.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/hir_def/src/src.rs')
-rw-r--r-- | crates/hir_def/src/src.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/hir_def/src/src.rs b/crates/hir_def/src/src.rs new file mode 100644 index 000000000..7a79b0314 --- /dev/null +++ b/crates/hir_def/src/src.rs | |||
@@ -0,0 +1,43 @@ | |||
1 | //! Utilities for mapping between hir IDs and the surface syntax. | ||
2 | |||
3 | use arena::map::ArenaMap; | ||
4 | use hir_expand::InFile; | ||
5 | |||
6 | use crate::{db::DefDatabase, item_tree::ItemTreeNode, AssocItemLoc, ItemLoc}; | ||
7 | |||
8 | pub trait HasSource { | ||
9 | type Value; | ||
10 | fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>; | ||
11 | } | ||
12 | |||
13 | impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> { | ||
14 | type Value = N::Source; | ||
15 | |||
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]; | ||
21 | |||
22 | InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root)) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | impl<N: ItemTreeNode> HasSource for ItemLoc<N> { | ||
27 | type Value = N::Source; | ||
28 | |||
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]; | ||
34 | |||
35 | InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root)) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | pub trait HasChildSource { | ||
40 | type ChildId; | ||
41 | type Value; | ||
42 | fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>; | ||
43 | } | ||