aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ptr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ptr.rs')
-rw-r--r--crates/ra_syntax/src/ptr.rs19
1 files changed, 4 insertions, 15 deletions
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index 13ee1305f..aae590cb6 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -15,16 +15,12 @@ pub struct SyntaxNodePtr {
15 15
16impl SyntaxNodePtr { 16impl SyntaxNodePtr {
17 pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { 17 pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
18 SyntaxNodePtr { 18 SyntaxNodePtr { range: node.range(), kind: node.kind() }
19 range: node.range(),
20 kind: node.kind(),
21 }
22 } 19 }
23 20
24 pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { 21 pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode {
25 generate(Some(source_file.syntax()), |&node| { 22 generate(Some(source_file.syntax()), |&node| {
26 node.children() 23 node.children().find(|it| self.range.is_subrange(&it.range()))
27 .find(|it| self.range.is_subrange(&it.range()))
28 }) 24 })
29 .find(|it| it.range() == self.range && it.kind() == self.kind) 25 .find(|it| it.range() == self.range && it.kind() == self.kind)
30 .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) 26 .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
@@ -55,10 +51,7 @@ impl<N: AstNode> Clone for AstPtr<N> {
55 51
56impl<N: AstNode> AstPtr<N> { 52impl<N: AstNode> AstPtr<N> {
57 pub fn new(node: &N) -> AstPtr<N> { 53 pub fn new(node: &N) -> AstPtr<N> {
58 AstPtr { 54 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
59 raw: SyntaxNodePtr::new(node.syntax()),
60 _ty: PhantomData,
61 }
62 } 55 }
63 56
64 pub fn to_node(self, source_file: &SourceFile) -> &N { 57 pub fn to_node(self, source_file: &SourceFile) -> &N {
@@ -76,11 +69,7 @@ fn test_local_syntax_ptr() {
76 use crate::{ast, AstNode}; 69 use crate::{ast, AstNode};
77 70
78 let file = SourceFile::parse("struct Foo { f: u32, }"); 71 let file = SourceFile::parse("struct Foo { f: u32, }");
79 let field = file 72 let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
80 .syntax()
81 .descendants()
82 .find_map(ast::NamedFieldDef::cast)
83 .unwrap();
84 let ptr = SyntaxNodePtr::new(field.syntax()); 73 let ptr = SyntaxNodePtr::new(field.syntax());
85 let field_syntax = ptr.to_node(&file); 74 let field_syntax = ptr.to_node(&file);
86 assert_eq!(field.syntax(), &*field_syntax); 75 assert_eq!(field.syntax(), &*field_syntax);