From 549728bba87ed8f4375f27bb9a77223bf8f65452 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 May 2019 19:39:06 +0300 Subject: make AstId untyped --- crates/ra_syntax/src/ptr.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'crates/ra_syntax/src/ptr.rs') 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::{ iter::successors, }; use crate::{ - AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, + AstNode, SyntaxKind, SyntaxNode, TextRange, }; /// A pointer to a syntax node inside a file. It can be used to remember a @@ -19,8 +19,9 @@ impl SyntaxNodePtr { SyntaxNodePtr { range: node.range(), kind: node.kind() } } - pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { - successors(Some(source_file.syntax()), |&node| { + pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode { + assert!(root.parent().is_none()); + successors(Some(root), |&node| { node.children().find(|it| self.range.is_subrange(&it.range())) }) .find(|it| it.range() == self.range && it.kind() == self.kind) @@ -55,8 +56,8 @@ impl AstPtr { AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } } - pub fn to_node(self, source_file: &SourceFile) -> &N { - let syntax_node = self.raw.to_node(source_file); + pub fn to_node(self, root: &SyntaxNode) -> &N { + let syntax_node = self.raw.to_node(root); N::cast(syntax_node).unwrap() } @@ -73,11 +74,11 @@ impl From> for SyntaxNodePtr { #[test] fn test_local_syntax_ptr() { - use crate::{ast, AstNode}; + use crate::{ast, AstNode, SourceFile}; let file = SourceFile::parse("struct Foo { f: u32, }"); let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); let ptr = SyntaxNodePtr::new(field.syntax()); - let field_syntax = ptr.to_node(&file); + let field_syntax = ptr.to_node(file.syntax()); assert_eq!(field.syntax(), &*field_syntax); } -- cgit v1.2.3