use std::borrow::Cow;
use rowan::{GreenNodeData, GreenTokenData, NodeOrToken};
use crate::ast;
use crate::ast::AstNode;
use crate::{SyntaxNode, TokenText};
use super::support;
impl ast::Constraint {
#[inline]
pub fn name(&self) -> Option<ast::Name> {
support::child(self.syntax())
}
}
impl ast::BinExpr {
pub fn lhs(&self) -> Option<ast::Expr> {
support::children(self.syntax()).next()
}
pub fn rhs(&self) -> Option<ast::Expr> {
support::children(self.syntax()).nth(1)
}
}
impl ast::NameRef {
#[inline]
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
}
impl ast::Name {
#[inline]
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
}
impl ast::CharType {
#[inline]
pub fn text(&self) -> TokenText<'_> {
text_of_first_token(self.syntax())
}
}
pub(crate) fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
green_ref
.children()
.next()
.and_then(NodeOrToken::into_token)
.unwrap()
}
match node.green() {
Cow::Borrowed(green_ref) => TokenText::borrowed(first_token(green_ref).text()),
Cow::Owned(green) => TokenText::owned(first_token(&green).to_owned()),
}
}