aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_id.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_id.rs')
-rw-r--r--crates/ra_hir/src/source_id.rs50
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 @@
1use std::sync::Arc; 1use std::{marker::PhantomData, sync::Arc};
2 2
3use ra_arena::{Arena, RawId, impl_arena_id}; 3use ra_arena::{Arena, RawId, impl_arena_id};
4use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; 4use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
5 5
6use crate::{HirFileId, DefDatabase}; 6use crate::{HirFileId, DefDatabase};
7 7
8#[derive(Debug, PartialEq, Eq)]
9pub(crate) struct AstId<N: AstNode> {
10 file_id: HirFileId,
11 file_ast_id: FileAstId<N>,
12}
13
14impl<N: AstNode> Clone for AstId<N> {
15 fn clone(&self) -> AstId<N> {
16 *self
17 }
18}
19
20impl<N: AstNode> Copy for AstId<N> {}
21
22impl<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)]
34pub(crate) struct FileAstId<N: AstNode> {
35 raw: SourceFileItemId,
36 _ty: PhantomData<N>,
37}
38
39impl<N: AstNode> Clone for FileAstId<N> {
40 fn clone(&self) -> FileAstId<N> {
41 *self
42 }
43}
44
45impl<N: AstNode> Copy for FileAstId<N> {}
46
47impl<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
95impl std::ops::Index<SourceFileItemId> for SourceFileItems { 143impl std::ops::Index<SourceFileItemId> for SourceFileItems {