2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-12-05 16:34:12 -06:00
|
|
|
use std::{
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
iter::successors,
|
|
|
|
marker::PhantomData,
|
|
|
|
};
|
2019-01-23 08:37:10 -06:00
|
|
|
|
2019-07-18 11:23:05 -05:00
|
|
|
use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange};
|
|
|
|
|
2019-01-23 08:37:10 -06:00
|
|
|
/// A pointer to a syntax node inside a file. It can be used to remember a
|
|
|
|
/// specific node across reparses of the same file.
|
2020-04-10 17:27:00 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-01-23 08:37:10 -06:00
|
|
|
pub struct SyntaxNodePtr {
|
2020-10-06 13:06:14 -05:00
|
|
|
// Don't expose this field further. At some point, we might want to replace
|
|
|
|
// range with node id.
|
2019-04-21 09:47:55 -05:00
|
|
|
pub(crate) range: TextRange,
|
2019-01-23 08:37:10 -06:00
|
|
|
kind: SyntaxKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxNodePtr {
|
|
|
|
pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
|
2019-07-20 04:58:27 -05:00
|
|
|
SyntaxNodePtr { range: node.text_range(), kind: node.kind() }
|
2019-01-23 08:37:10 -06:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:27:00 -05:00
|
|
|
pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode {
|
2019-09-03 03:04:38 -05:00
|
|
|
assert!(root.parent().is_none());
|
|
|
|
successors(Some(root.clone()), |node| {
|
2020-04-24 16:40:41 -05:00
|
|
|
node.children().find(|it| it.text_range().contains_range(self.range))
|
2019-01-23 08:37:10 -06:00
|
|
|
})
|
2019-07-20 04:58:27 -05:00
|
|
|
.find(|it| it.text_range() == self.range && it.kind() == self.kind)
|
2019-01-23 08:37:10 -06:00
|
|
|
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:39:11 -05:00
|
|
|
pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> {
|
2020-04-16 12:21:18 -05:00
|
|
|
if !N::can_cast(self.kind) {
|
2019-08-12 14:39:11 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(AstPtr { raw: self, _ty: PhantomData })
|
|
|
|
}
|
2019-01-23 08:37:10 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 09:26:02 -06:00
|
|
|
/// Like `SyntaxNodePtr`, but remembers the type of node
|
2019-12-05 16:34:12 -06:00
|
|
|
#[derive(Debug)]
|
2019-01-23 09:26:02 -06:00
|
|
|
pub struct AstPtr<N: AstNode> {
|
2019-01-24 04:40:36 -06:00
|
|
|
raw: SyntaxNodePtr,
|
2019-07-18 10:13:32 -05:00
|
|
|
_ty: PhantomData<fn() -> N>,
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: AstNode> Clone for AstPtr<N> {
|
|
|
|
fn clone(&self) -> AstPtr<N> {
|
2020-04-10 17:27:00 -05:00
|
|
|
AstPtr { raw: self.raw.clone(), _ty: PhantomData }
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 07:03:59 -06:00
|
|
|
impl<N: AstNode> Eq for AstPtr<N> {}
|
|
|
|
|
|
|
|
impl<N: AstNode> PartialEq for AstPtr<N> {
|
|
|
|
fn eq(&self, other: &AstPtr<N>) -> bool {
|
|
|
|
self.raw == other.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 16:34:12 -06:00
|
|
|
impl<N: AstNode> Hash for AstPtr<N> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.raw.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:26:02 -06:00
|
|
|
impl<N: AstNode> AstPtr<N> {
|
|
|
|
pub fn new(node: &N) -> AstPtr<N> {
|
2019-02-08 05:49:43 -06:00
|
|
|
AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:27:00 -05:00
|
|
|
pub fn to_node(&self, root: &SyntaxNode) -> N {
|
2019-05-13 11:39:06 -05:00
|
|
|
let syntax_node = self.raw.to_node(root);
|
2019-01-23 09:26:02 -06:00
|
|
|
N::cast(syntax_node).unwrap()
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:27:00 -05:00
|
|
|
pub fn syntax_node_ptr(&self) -> SyntaxNodePtr {
|
|
|
|
self.raw.clone()
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
2019-07-19 07:53:16 -05:00
|
|
|
|
2019-07-19 10:22:00 -05:00
|
|
|
pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> {
|
2020-04-16 12:21:18 -05:00
|
|
|
if !U::can_cast(self.raw.kind) {
|
2019-07-19 07:53:16 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(AstPtr { raw: self.raw, _ty: PhantomData })
|
|
|
|
}
|
2019-01-23 09:26:02 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 08:28:47 -05:00
|
|
|
impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
|
|
|
|
fn from(ptr: AstPtr<N>) -> SyntaxNodePtr {
|
|
|
|
ptr.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:37:10 -06:00
|
|
|
#[test]
|
|
|
|
fn test_local_syntax_ptr() {
|
2019-05-13 11:39:06 -05:00
|
|
|
use crate::{ast, AstNode, SourceFile};
|
2019-01-23 08:37:10 -06:00
|
|
|
|
2019-05-28 09:34:28 -05:00
|
|
|
let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap();
|
2020-07-30 09:49:13 -05:00
|
|
|
let field = file.syntax().descendants().find_map(ast::RecordField::cast).unwrap();
|
2019-01-23 08:37:10 -06:00
|
|
|
let ptr = SyntaxNodePtr::new(field.syntax());
|
2019-05-13 11:39:06 -05:00
|
|
|
let field_syntax = ptr.to_node(file.syntax());
|
2019-07-18 11:23:05 -05:00
|
|
|
assert_eq!(field.syntax(), &field_syntax);
|
2019-01-23 08:37:10 -06:00
|
|
|
}
|