aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/lib.rs2
-rw-r--r--crates/ra_syntax/src/ptr.rs34
2 files changed, 35 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 97b196118..104f32851 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -43,7 +43,7 @@ pub use crate::{
43 lexer::{tokenize, Token}, 43 lexer::{tokenize, Token},
44 syntax_kinds::SyntaxKind, 44 syntax_kinds::SyntaxKind,
45 yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc}, 45 yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc},
46 ptr::SyntaxNodePtr, 46 ptr::{SyntaxNodePtr, AstPtr},
47}; 47};
48 48
49use ra_text_edit::AtomTextEdit; 49use ra_text_edit::AtomTextEdit;
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index e8c40e5d3..b50cd8a52 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -1,3 +1,5 @@
1use std::marker::PhantomData;
2
1use crate::{ 3use crate::{
2 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, 4 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
3 algo::generate, 5 algo::generate,
@@ -37,6 +39,38 @@ impl SyntaxNodePtr {
37 } 39 }
38} 40}
39 41
42/// Like `SyntaxNodePtr`, but remembers the type of node
43#[derive(Debug, PartialEq, Eq, Hash)]
44pub struct AstPtr<N: AstNode> {
45 ptr: SyntaxNodePtr,
46 _ty: PhantomData<N>,
47}
48
49impl<N: AstNode> Copy for AstPtr<N> {}
50impl<N: AstNode> Clone for AstPtr<N> {
51 fn clone(&self) -> AstPtr<N> {
52 *self
53 }
54}
55
56impl<N: AstNode> AstPtr<N> {
57 pub fn new(node: &N) -> AstPtr<N> {
58 AstPtr {
59 ptr: SyntaxNodePtr::new(node.syntax()),
60 _ty: PhantomData,
61 }
62 }
63
64 pub fn to_node(self, source_file: &SourceFile) -> &N {
65 let syntax_node = self.ptr.to_node(source_file);
66 N::cast(syntax_node).unwrap()
67 }
68
69 pub fn syntax_node_ptr(self) -> SyntaxNodePtr {
70 self.ptr
71 }
72}
73
40#[test] 74#[test]
41fn test_local_syntax_ptr() { 75fn test_local_syntax_ptr() {
42 use crate::{ast, AstNode}; 76 use crate::{ast, AstNode};