make AstId untyped

This commit is contained in:
Aleksey Kladov
2019-05-13 19:39:06 +03:00
parent 033a32f349
commit 549728bba8
11 changed files with 29 additions and 24 deletions

View File

@@ -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<N: AstNode> AstPtr<N> {
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<N: AstNode> From<AstPtr<N>> 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);
}