diff options
author | Aleksey Kladov <[email protected]> | 2019-10-29 11:55:39 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-10-29 11:55:39 +0000 |
commit | 541387564483ee3a42a1969fd048f94e57599ca4 (patch) | |
tree | 65575cdef1622e15d2fea79e886951a8b38c3406 /crates/ra_hir/src/source_id.rs | |
parent | 4f22d2f3b0852f32c0ba5e4545ec8cc2d986cfcc (diff) |
move expansion-related code to a separate crate
Diffstat (limited to 'crates/ra_hir/src/source_id.rs')
-rw-r--r-- | crates/ra_hir/src/source_id.rs | 73 |
1 files changed, 3 insertions, 70 deletions
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 260b79661..c70245949 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs | |||
@@ -1,73 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::{ | 3 | pub use hir_def::{ |
4 | hash::{Hash, Hasher}, | 4 | ast_id_map::{AstIdMap, ErasedFileAstId, FileAstId}, |
5 | sync::Arc, | 5 | expand::AstId, |
6 | }; | 6 | }; |
7 | |||
8 | pub use hir_def::ast_id_map::{AstIdMap, ErasedFileAstId, FileAstId}; | ||
9 | use ra_syntax::{AstNode, SyntaxNode}; | ||
10 | |||
11 | use 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)] | ||
18 | pub(crate) struct AstId<N: AstNode> { | ||
19 | file_id: HirFileId, | ||
20 | file_ast_id: FileAstId<N>, | ||
21 | } | ||
22 | |||
23 | impl<N: AstNode> Clone for AstId<N> { | ||
24 | fn clone(&self) -> AstId<N> { | ||
25 | *self | ||
26 | } | ||
27 | } | ||
28 | impl<N: AstNode> Copy for AstId<N> {} | ||
29 | |||
30 | impl<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 | } | ||
35 | impl<N: AstNode> Eq for AstId<N> {} | ||
36 | impl<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 | |||
42 | impl<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 | |||
57 | pub(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 | |||
66 | pub(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 | } | ||