diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ptr.rs | 15 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 2 |
2 files changed, 9 insertions, 8 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 | }; |
5 | use crate::{ | 5 | use 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] |
75 | fn test_local_syntax_ptr() { | 76 | fn 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 | } |
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 92c15234e..80054f529 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -392,7 +392,7 @@ impl SyntaxNode { | |||
392 | // `range` private afterwards | 392 | // `range` private afterwards |
393 | let mut ptr = SyntaxNodePtr::new(self); | 393 | let mut ptr = SyntaxNodePtr::new(self); |
394 | ptr.range = TextRange::offset_len(ptr.range().start(), len); | 394 | ptr.range = TextRange::offset_len(ptr.range().start(), len); |
395 | return ptr.to_node(&file).to_owned(); | 395 | return ptr.to_node(file.syntax()).to_owned(); |
396 | } | 396 | } |
397 | 397 | ||
398 | fn position_of_child(&self, child: SyntaxElement) -> usize { | 398 | fn position_of_child(&self, child: SyntaxElement) -> usize { |