diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 15:31:59 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-23 15:31:59 +0000 |
commit | 118e11e50b5f39aca88e2ed342e5d355d4f32c00 (patch) | |
tree | 8b6f174292aee253fa6db15184a2ed7b440ca919 /crates/ra_syntax/src | |
parent | a583070b7d47852094b498c3191b4b5d87520fc3 (diff) | |
parent | d4ed25d86fdec0ce47199c262af62213b62e4863 (diff) |
Merge #612
612: Some random refactorings r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ptr.rs | 34 |
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 | ||
49 | use ra_text_edit::AtomTextEdit; | 49 | use 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 @@ | |||
1 | use std::marker::PhantomData; | ||
2 | |||
1 | use crate::{ | 3 | use 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)] | ||
44 | pub struct AstPtr<N: AstNode> { | ||
45 | ptr: SyntaxNodePtr, | ||
46 | _ty: PhantomData<N>, | ||
47 | } | ||
48 | |||
49 | impl<N: AstNode> Copy for AstPtr<N> {} | ||
50 | impl<N: AstNode> Clone for AstPtr<N> { | ||
51 | fn clone(&self) -> AstPtr<N> { | ||
52 | *self | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl<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] |
41 | fn test_local_syntax_ptr() { | 75 | fn test_local_syntax_ptr() { |
42 | use crate::{ast, AstNode}; | 76 | use crate::{ast, AstNode}; |