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 ++++++++------- crates/ra_syntax/src/syntax_node.rs | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax') 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); } 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 { // `range` private afterwards let mut ptr = SyntaxNodePtr::new(self); ptr.range = TextRange::offset_len(ptr.range().start(), len); - return ptr.to_node(&file).to_owned(); + return ptr.to_node(file.syntax()).to_owned(); } fn position_of_child(&self, child: SyntaxElement) -> usize { -- cgit v1.2.3