diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-13 15:31:49 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-13 15:31:49 +0100 |
commit | e9926948ca267932ccc1341388bfd1b3fa88a001 (patch) | |
tree | cc4b797cb39a40b59e9e3d37178e8a1907f12358 /crates/hir_def/src/src.rs | |
parent | 902f74c2697cc2a50de9067845814a2a852fccfd (diff) | |
parent | 50f8c1ebf23f634b68529603a917e3feeda457fa (diff) |
Merge #5747
5747: Rename crate r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
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 | } | ||