diff options
author | Aleksey Kladov <[email protected]> | 2019-03-26 14:25:14 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-03-26 14:25:14 +0000 |
commit | fb8b354dcc837d5eb9b81fc205e4282a203df177 (patch) | |
tree | d44edc8c6bf90ff29d1b4422b69ccc3ada06ad38 /crates/ra_hir/src/source_id.rs | |
parent | 619a4c05ba098220ab0fb65d42b7f415dfcc1b6a (diff) |
add typed ids
Diffstat (limited to 'crates/ra_hir/src/source_id.rs')
-rw-r--r-- | crates/ra_hir/src/source_id.rs | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 62707ba6a..881cc590e 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs | |||
@@ -1,10 +1,55 @@ | |||
1 | use std::sync::Arc; | 1 | use std::{marker::PhantomData, sync::Arc}; |
2 | 2 | ||
3 | use ra_arena::{Arena, RawId, impl_arena_id}; | 3 | use ra_arena::{Arena, RawId, impl_arena_id}; |
4 | use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; | 4 | use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; |
5 | 5 | ||
6 | use crate::{HirFileId, DefDatabase}; | 6 | use crate::{HirFileId, DefDatabase}; |
7 | 7 | ||
8 | #[derive(Debug, PartialEq, Eq)] | ||
9 | pub(crate) struct AstId<N: AstNode> { | ||
10 | file_id: HirFileId, | ||
11 | file_ast_id: FileAstId<N>, | ||
12 | } | ||
13 | |||
14 | impl<N: AstNode> Clone for AstId<N> { | ||
15 | fn clone(&self) -> AstId<N> { | ||
16 | *self | ||
17 | } | ||
18 | } | ||
19 | |||
20 | impl<N: AstNode> Copy for AstId<N> {} | ||
21 | |||
22 | impl<N: AstNode> AstId<N> { | ||
23 | pub(crate) fn file_id(&self) -> HirFileId { | ||
24 | self.file_id | ||
25 | } | ||
26 | |||
27 | pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc<N> { | ||
28 | let syntax_node = db.file_item(self.file_ast_id.raw.with_file_id(self.file_id)); | ||
29 | N::cast(&syntax_node).unwrap().to_owned() | ||
30 | } | ||
31 | } | ||
32 | |||
33 | #[derive(Debug, PartialEq, Eq)] | ||
34 | pub(crate) struct FileAstId<N: AstNode> { | ||
35 | raw: SourceFileItemId, | ||
36 | _ty: PhantomData<N>, | ||
37 | } | ||
38 | |||
39 | impl<N: AstNode> Clone for FileAstId<N> { | ||
40 | fn clone(&self) -> FileAstId<N> { | ||
41 | *self | ||
42 | } | ||
43 | } | ||
44 | |||
45 | impl<N: AstNode> Copy for FileAstId<N> {} | ||
46 | |||
47 | impl<N: AstNode> FileAstId<N> { | ||
48 | pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId<N> { | ||
49 | AstId { file_id, file_ast_id: self } | ||
50 | } | ||
51 | } | ||
52 | |||
8 | /// Identifier of item within a specific file. This is stable over reparses, so | 53 | /// Identifier of item within a specific file. This is stable over reparses, so |
9 | /// it's OK to use it as a salsa key/value. | 54 | /// it's OK to use it as a salsa key/value. |
10 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 55 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
@@ -90,6 +135,9 @@ impl SourceFileItems { | |||
90 | self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(), | 135 | self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(), |
91 | ); | 136 | ); |
92 | } | 137 | } |
138 | pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> { | ||
139 | FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData } | ||
140 | } | ||
93 | } | 141 | } |
94 | 142 | ||
95 | impl std::ops::Index<SourceFileItemId> for SourceFileItems { | 143 | impl std::ops::Index<SourceFileItemId> for SourceFileItems { |