diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ptr.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/crates/syntax/src/ptr.rs b/crates/syntax/src/ptr.rs index 195d2251b..282470bae 100644 --- a/crates/syntax/src/ptr.rs +++ b/crates/syntax/src/ptr.rs | |||
@@ -32,10 +32,19 @@ impl SyntaxNodePtr { | |||
32 | SyntaxNodePtr { range: node.text_range(), kind: node.kind() } | 32 | SyntaxNodePtr { range: node.text_range(), kind: node.kind() } |
33 | } | 33 | } |
34 | 34 | ||
35 | /// "Dereference" the pointer to get the node it points to. | ||
36 | /// | ||
37 | /// Panics if node is not found, so make sure that `root` syntax tree is | ||
38 | /// equivalent (is build from the same text) to the tree which was | ||
39 | /// originally used to get this [`SyntaxNodePtr`]. | ||
40 | /// | ||
41 | /// The complexity is linear in the depth of the tree and logarithmic in | ||
42 | /// tree width. As most trees are shallow, thinking about this as | ||
43 | /// `O(log(N))` in the size of the tree is not too wrong! | ||
35 | pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode { | 44 | pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode { |
36 | assert!(root.parent().is_none()); | 45 | assert!(root.parent().is_none()); |
37 | successors(Some(root.clone()), |node| { | 46 | successors(Some(root.clone()), |node| { |
38 | node.children().find(|it| it.text_range().contains_range(self.range)) | 47 | node.child_or_token_at_range(self.range).and_then(|it| it.into_node()) |
39 | }) | 48 | }) |
40 | .find(|it| it.text_range() == self.range && it.kind() == self.kind) | 49 | .find(|it| it.text_range() == self.range && it.kind() == self.kind) |
41 | .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) | 50 | .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) |