use std::{
fmt,
hash::{Hash, Hasher},
iter::successors,
marker::PhantomData,
};
use crate::{Language, SyntaxNode, SyntaxNodeChildren, TextRange};
pub trait AstNode {
type Language: Language;
fn can_cast(kind: <Self::Language as Language>::Kind) -> bool
where
Self: Sized;
fn cast(node: SyntaxNode<Self::Language>) -> Option<Self>
where
Self: Sized;
fn syntax(&self) -> &SyntaxNode<Self::Language>;
fn clone_for_update(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_for_update()).unwrap()
}
fn clone_subtree(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_subtree()).unwrap()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct SyntaxNodePtr<L: Language> {
kind: L::Kind,
range: TextRange,
}
impl<L: Language> SyntaxNodePtr<L> {
pub fn new(node: &SyntaxNode<L>) -> Self {
Self { kind: node.kind(), range: node.text_range() }
}
pub fn to_node(&self, root: &SyntaxNode<L>) -> SyntaxNode<L> {
self.try_to_node(root).unwrap_or_else(|| panic!("can't resolve {self:?} with {root:?}"))
}
pub fn try_to_node(&self, root: &SyntaxNode<L>) -> Option<SyntaxNode<L>> {
if root.parent().is_some() {
return None;
}
successors(Some(root.clone()), |node| node.child_or_token_at_range(self.range)?.into_node())
.find(|it| it.text_range() == self.range && it.kind() == self.kind)
}
pub fn cast<N: AstNode<Language = L>>(self) -> Option<AstPtr<N>> {
if !N::can_cast(self.kind) {
return None;
}
Some(AstPtr { raw: self })
}
pub fn kind(&self) -> L::Kind {
self.kind
}
pub fn text_range(&self) -> TextRange {
self.range
}
}
pub struct AstPtr<N: AstNode> {
raw: SyntaxNodePtr<N::Language>,
}
impl<N: AstNode> AstPtr<N> {
pub fn new(node: &N) -> Self {
Self { raw: SyntaxNodePtr::new(node.syntax()) }
}
pub fn to_node(&self, root: &SyntaxNode<N::Language>) -> N {
self.try_to_node(root).unwrap_or_else(|| panic!("can't resolve {self:?} with {root:?}"))
}
pub fn try_to_node(&self, root: &SyntaxNode<N::Language>) -> Option<N> {
N::cast(self.raw.try_to_node(root)?)
}
pub fn syntax_node_ptr(&self) -> SyntaxNodePtr<N::Language> {
self.raw.clone()
}
pub fn cast<U: AstNode<Language = N::Language>>(self) -> Option<AstPtr<U>> {
if !U::can_cast(self.raw.kind) {
return None;
}
Some(AstPtr { raw: self.raw })
}
}
impl<N: AstNode> fmt::Debug for AstPtr<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AstPtr").field("raw", &self.raw).finish()
}
}
impl<N: AstNode> Clone for AstPtr<N> {
fn clone(&self) -> Self {
Self { raw: self.raw.clone() }
}
}
impl<N: AstNode> PartialEq for AstPtr<N> {
fn eq(&self, other: &AstPtr<N>) -> bool {
self.raw == other.raw
}
}
impl<N: AstNode> Eq for AstPtr<N> {}
impl<N: AstNode> Hash for AstPtr<N> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.raw.hash(state)
}
}
impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr<N::Language> {
fn from(ptr: AstPtr<N>) -> SyntaxNodePtr<N::Language> {
ptr.raw
}
}
#[derive(Debug, Clone)]
pub struct AstChildren<N: AstNode> {
inner: SyntaxNodeChildren<N::Language>,
ph: PhantomData<N>,
}
impl<N: AstNode> AstChildren<N> {
fn new(parent: &SyntaxNode<N::Language>) -> Self {
AstChildren { inner: parent.children(), ph: PhantomData }
}
}
impl<N: AstNode> Iterator for AstChildren<N> {
type Item = N;
fn next(&mut self) -> Option<N> {
self.inner.find_map(N::cast)
}
}
pub mod support {
use super::{AstChildren, AstNode};
use crate::{Language, SyntaxNode, SyntaxToken};
pub fn child<N: AstNode>(parent: &SyntaxNode<N::Language>) -> Option<N> {
parent.children().find_map(N::cast)
}
pub fn children<N: AstNode>(parent: &SyntaxNode<N::Language>) -> AstChildren<N> {
AstChildren::new(parent)
}
pub fn token<L: Language>(parent: &SyntaxNode<L>, kind: L::Kind) -> Option<SyntaxToken<L>> {
parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
}
}