aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ptr.rs
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-08-24 10:19:53 +0100
committerZac Pullar-Strecker <[email protected]>2020-08-24 10:20:13 +0100
commit7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch)
treebdb47765991cb973b2cd5481a088fac636bd326c /crates/syntax/src/ptr.rs
parentca464650eeaca6195891199a93f4f76cf3e7e697 (diff)
parente65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff)
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/syntax/src/ptr.rs')
-rw-r--r--crates/syntax/src/ptr.rs105
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
3use std::{
4 hash::{Hash, Hasher},
5 iter::successors,
6 marker::PhantomData,
7};
8
9use 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)]
14pub struct SyntaxNodePtr {
15 pub(crate) range: TextRange,
16 kind: SyntaxKind,
17}
18
19impl 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)]
43pub struct AstPtr<N: AstNode> {
44 raw: SyntaxNodePtr,
45 _ty: PhantomData<fn() -> N>,
46}
47
48impl<N: AstNode> Clone for AstPtr<N> {
49 fn clone(&self) -> AstPtr<N> {
50 AstPtr { raw: self.raw.clone(), _ty: PhantomData }
51 }
52}
53
54impl<N: AstNode> Eq for AstPtr<N> {}
55
56impl<N: AstNode> PartialEq for AstPtr<N> {
57 fn eq(&self, other: &AstPtr<N>) -> bool {
58 self.raw == other.raw
59 }
60}
61
62impl<N: AstNode> Hash for AstPtr<N> {
63 fn hash<H: Hasher>(&self, state: &mut H) {
64 self.raw.hash(state)
65 }
66}
67
68impl<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
90impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
91 fn from(ptr: AstPtr<N>) -> SyntaxNodePtr {
92 ptr.raw
93 }
94}
95
96#[test]
97fn 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}