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.rs73
1 files changed, 0 insertions, 73 deletions
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs
deleted file mode 100644
index 260b79661..000000000
--- a/crates/ra_hir/src/source_id.rs
+++ /dev/null
@@ -1,73 +0,0 @@
1//! FIXME: write short doc here
2
3use std::{
4 hash::{Hash, Hasher},
5 sync::Arc,
6};
7
8pub use hir_def::ast_id_map::{AstIdMap, ErasedFileAstId, FileAstId};
9use ra_syntax::{AstNode, SyntaxNode};
10
11use crate::{db::AstDatabase, HirFileId};
12
13/// `AstId` points to an AST node in any file.
14///
15/// It is stable across reparses, and can be used as salsa key/value.
16// FIXME: isn't this just a `Source<FileAstId<N>>` ?
17#[derive(Debug)]
18pub(crate) struct AstId<N: AstNode> {
19 file_id: HirFileId,
20 file_ast_id: FileAstId<N>,
21}
22
23impl<N: AstNode> Clone for AstId<N> {
24 fn clone(&self) -> AstId<N> {
25 *self
26 }
27}
28impl<N: AstNode> Copy for AstId<N> {}
29
30impl<N: AstNode> PartialEq for AstId<N> {
31 fn eq(&self, other: &Self) -> bool {
32 (self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id)
33 }
34}
35impl<N: AstNode> Eq for AstId<N> {}
36impl<N: AstNode> Hash for AstId<N> {
37 fn hash<H: Hasher>(&self, hasher: &mut H) {
38 (self.file_id, self.file_ast_id).hash(hasher);
39 }
40}
41
42impl<N: AstNode> AstId<N> {
43 pub fn new(file_id: HirFileId, file_ast_id: FileAstId<N>) -> AstId<N> {
44 AstId { file_id, file_ast_id }
45 }
46
47 pub(crate) fn file_id(&self) -> HirFileId {
48 self.file_id
49 }
50
51 pub(crate) fn to_node(&self, db: &impl AstDatabase) -> N {
52 let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.into());
53 N::cast(syntax_node).unwrap()
54 }
55}
56
57pub(crate) fn ast_id_map_query(db: &impl AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
58 let map = if let Some(node) = db.parse_or_expand(file_id) {
59 AstIdMap::from_source(&node)
60 } else {
61 AstIdMap::default()
62 };
63 Arc::new(map)
64}
65
66pub(crate) fn file_item_query(
67 db: &impl AstDatabase,
68 file_id: HirFileId,
69 ast_id: ErasedFileAstId,
70) -> SyntaxNode {
71 let node = db.parse_or_expand(file_id).unwrap();
72 db.ast_id_map(file_id)[ast_id].to_node(&node)
73}