diff options
Diffstat (limited to 'crates/syntax/src/ptr.rs')
-rw-r--r-- | crates/syntax/src/ptr.rs | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/crates/syntax/src/ptr.rs b/crates/syntax/src/ptr.rs new file mode 100644 index 000000000..ca7957747 --- /dev/null +++ b/crates/syntax/src/ptr.rs | |||
@@ -0,0 +1,105 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::{ | ||
4 | hash::{Hash, Hasher}, | ||
5 | iter::successors, | ||
6 | marker::PhantomData, | ||
7 | }; | ||
8 | |||
9 | use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange}; | ||
10 | |||
11 | /// A pointer to a syntax node inside a file. It can be used to remember a | ||
12 | /// specific node across reparses of the same file. | ||
13 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
14 | pub struct SyntaxNodePtr { | ||
15 | pub(crate) range: TextRange, | ||
16 | kind: SyntaxKind, | ||
17 | } | ||
18 | |||
19 | impl SyntaxNodePtr { | ||
20 | pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { | ||
21 | SyntaxNodePtr { range: node.text_range(), kind: node.kind() } | ||
22 | } | ||
23 | |||
24 | pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode { | ||
25 | assert!(root.parent().is_none()); | ||
26 | successors(Some(root.clone()), |node| { | ||
27 | node.children().find(|it| it.text_range().contains_range(self.range)) | ||
28 | }) | ||
29 | .find(|it| it.text_range() == self.range && it.kind() == self.kind) | ||
30 | .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) | ||
31 | } | ||
32 | |||
33 | pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> { | ||
34 | if !N::can_cast(self.kind) { | ||
35 | return None; | ||
36 | } | ||
37 | Some(AstPtr { raw: self, _ty: PhantomData }) | ||
38 | } | ||
39 | } | ||
40 | |||
41 | /// Like `SyntaxNodePtr`, but remembers the type of node | ||
42 | #[derive(Debug)] | ||
43 | pub struct AstPtr<N: AstNode> { | ||
44 | raw: SyntaxNodePtr, | ||
45 | _ty: PhantomData<fn() -> N>, | ||
46 | } | ||
47 | |||
48 | impl<N: AstNode> Clone for AstPtr<N> { | ||
49 | fn clone(&self) -> AstPtr<N> { | ||
50 | AstPtr { raw: self.raw.clone(), _ty: PhantomData } | ||
51 | } | ||
52 | } | ||
53 | |||
54 | impl<N: AstNode> Eq for AstPtr<N> {} | ||
55 | |||
56 | impl<N: AstNode> PartialEq for AstPtr<N> { | ||
57 | fn eq(&self, other: &AstPtr<N>) -> bool { | ||
58 | self.raw == other.raw | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl<N: AstNode> Hash for AstPtr<N> { | ||
63 | fn hash<H: Hasher>(&self, state: &mut H) { | ||
64 | self.raw.hash(state) | ||
65 | } | ||
66 | } | ||
67 | |||
68 | impl<N: AstNode> AstPtr<N> { | ||
69 | pub fn new(node: &N) -> AstPtr<N> { | ||
70 | AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } | ||
71 | } | ||
72 | |||
73 | pub fn to_node(&self, root: &SyntaxNode) -> N { | ||
74 | let syntax_node = self.raw.to_node(root); | ||
75 | N::cast(syntax_node).unwrap() | ||
76 | } | ||
77 | |||
78 | pub fn syntax_node_ptr(&self) -> SyntaxNodePtr { | ||
79 | self.raw.clone() | ||
80 | } | ||
81 | |||
82 | pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> { | ||
83 | if !U::can_cast(self.raw.kind) { | ||
84 | return None; | ||
85 | } | ||
86 | Some(AstPtr { raw: self.raw, _ty: PhantomData }) | ||
87 | } | ||
88 | } | ||
89 | |||
90 | impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr { | ||
91 | fn from(ptr: AstPtr<N>) -> SyntaxNodePtr { | ||
92 | ptr.raw | ||
93 | } | ||
94 | } | ||
95 | |||
96 | #[test] | ||
97 | fn test_local_syntax_ptr() { | ||
98 | use crate::{ast, AstNode, SourceFile}; | ||
99 | |||
100 | let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap(); | ||
101 | let field = file.syntax().descendants().find_map(ast::RecordField::cast).unwrap(); | ||
102 | let ptr = SyntaxNodePtr::new(field.syntax()); | ||
103 | let field_syntax = ptr.to_node(file.syntax()); | ||
104 | assert_eq!(field.syntax(), &field_syntax); | ||
105 | } | ||