aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ptr.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-05-13 17:39:06 +0100
committerAleksey Kladov <[email protected]>2019-05-13 17:39:06 +0100
commit549728bba87ed8f4375f27bb9a77223bf8f65452 (patch)
tree7d2711f243047a5e7ee74d7181905a36ae2c033d /crates/ra_syntax/src/ptr.rs
parent033a32f34944d7e07facd900a78db59b35e6698c (diff)
make AstId untyped
Diffstat (limited to 'crates/ra_syntax/src/ptr.rs')
-rw-r--r--crates/ra_syntax/src/ptr.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index b0816b135..cee9503ca 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -3,7 +3,7 @@ use std::{
3 iter::successors, 3 iter::successors,
4}; 4};
5use crate::{ 5use crate::{
6 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, 6 AstNode, SyntaxKind, SyntaxNode, TextRange,
7}; 7};
8 8
9/// A pointer to a syntax node inside a file. It can be used to remember a 9/// A pointer to a syntax node inside a file. It can be used to remember a
@@ -19,8 +19,9 @@ impl SyntaxNodePtr {
19 SyntaxNodePtr { range: node.range(), kind: node.kind() } 19 SyntaxNodePtr { range: node.range(), kind: node.kind() }
20 } 20 }
21 21
22 pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { 22 pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode {
23 successors(Some(source_file.syntax()), |&node| { 23 assert!(root.parent().is_none());
24 successors(Some(root), |&node| {
24 node.children().find(|it| self.range.is_subrange(&it.range())) 25 node.children().find(|it| self.range.is_subrange(&it.range()))
25 }) 26 })
26 .find(|it| it.range() == self.range && it.kind() == self.kind) 27 .find(|it| it.range() == self.range && it.kind() == self.kind)
@@ -55,8 +56,8 @@ impl<N: AstNode> AstPtr<N> {
55 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } 56 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
56 } 57 }
57 58
58 pub fn to_node(self, source_file: &SourceFile) -> &N { 59 pub fn to_node(self, root: &SyntaxNode) -> &N {
59 let syntax_node = self.raw.to_node(source_file); 60 let syntax_node = self.raw.to_node(root);
60 N::cast(syntax_node).unwrap() 61 N::cast(syntax_node).unwrap()
61 } 62 }
62 63
@@ -73,11 +74,11 @@ impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
73 74
74#[test] 75#[test]
75fn test_local_syntax_ptr() { 76fn test_local_syntax_ptr() {
76 use crate::{ast, AstNode}; 77 use crate::{ast, AstNode, SourceFile};
77 78
78 let file = SourceFile::parse("struct Foo { f: u32, }"); 79 let file = SourceFile::parse("struct Foo { f: u32, }");
79 let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); 80 let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
80 let ptr = SyntaxNodePtr::new(field.syntax()); 81 let ptr = SyntaxNodePtr::new(field.syntax());
81 let field_syntax = ptr.to_node(&file); 82 let field_syntax = ptr.to_node(file.syntax());
82 assert_eq!(field.syntax(), &*field_syntax); 83 assert_eq!(field.syntax(), &*field_syntax);
83} 84}