2019-01-23 09:26:02 -06:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2019-01-23 08:37:10 -06:00
|
|
|
use crate::{
|
|
|
|
AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
|
|
|
|
algo::generate,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A pointer to a syntax node inside a file. It can be used to remember a
|
|
|
|
/// specific node across reparses of the same file.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct SyntaxNodePtr {
|
|
|
|
range: TextRange,
|
|
|
|
kind: SyntaxKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxNodePtr {
|
|
|
|
pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
|
|
|
|
SyntaxNodePtr {
|
|
|
|
range: node.range(),
|
|
|
|
kind: node.kind(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode {
|
|
|
|
generate(Some(source_file.syntax()), |&node| {
|
|
|
|
node.children()
|
|
|
|
.find(|it| self.range.is_subrange(&it.range()))
|
|
|
|
})
|
|
|
|
.find(|it| it.range() == self.range && it.kind() == self.kind)
|
|
|
|
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range(self) -> TextRange {
|
|
|
|
self.range
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn kind(self) -> SyntaxKind {
|
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:26:02 -06:00
|
|
|
/// Like `SyntaxNodePtr`, but remembers the type of node
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct AstPtr<N: AstNode> {
|
2019-01-24 04:40:36 -06:00
|
|
|
raw: SyntaxNodePtr,
|
2019-01-23 09:26:02 -06:00
|
|
|
_ty: PhantomData<N>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: AstNode> Copy for AstPtr<N> {}
|
|
|
|
impl<N: AstNode> Clone for AstPtr<N> {
|
|
|
|
fn clone(&self) -> AstPtr<N> {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: AstNode> AstPtr<N> {
|
|
|
|
pub fn new(node: &N) -> AstPtr<N> {
|
|
|
|
AstPtr {
|
2019-01-24 04:40:36 -06:00
|
|
|
raw: SyntaxNodePtr::new(node.syntax()),
|
2019-01-23 09:26:02 -06:00
|
|
|
_ty: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_node(self, source_file: &SourceFile) -> &N {
|
2019-01-24 04:40:36 -06:00
|
|
|
let syntax_node = self.raw.to_node(source_file);
|
2019-01-23 09:26:02 -06:00
|
|
|
N::cast(syntax_node).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn syntax_node_ptr(self) -> SyntaxNodePtr {
|
2019-01-24 04:40:36 -06:00
|
|
|
self.raw
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:37:10 -06:00
|
|
|
#[test]
|
|
|
|
fn test_local_syntax_ptr() {
|
|
|
|
use crate::{ast, AstNode};
|
|
|
|
|
|
|
|
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);
|
|
|
|
assert_eq!(field.syntax(), &*field_syntax);
|
|
|
|
}
|