#![cfg(test)]
use ra_ap_syntax::{ast, Edition, NodeOrToken, SyntaxKind, SyntaxNode};
use ra_ap_syntax::{AstNode, SourceFile, SyntaxToken};
use crate::{InkAttribute, InkEntity};
pub fn parse_first_syntax_token(code: &str) -> SyntaxToken {
parse_source(code)
.syntax()
.descendants_with_tokens()
.find_map(NodeOrToken::into_token)
.unwrap()
}
pub fn parse_first_syntax_node(code: &str) -> SyntaxNode {
parse_source(code)
.syntax()
.descendants()
.find(|node| node.kind() != SyntaxKind::SOURCE_FILE)
.unwrap()
}
pub fn parse_first_ast_node_of_type<T>(code: &str) -> T
where
T: AstNode,
{
parse_source(code)
.syntax()
.descendants()
.find_map(T::cast)
.unwrap()
}
pub fn parse_first_attribute(code: &str) -> ast::Attr {
parse_first_ast_node_of_type(code)
}
pub fn parse_first_ink_attribute(code: &str) -> InkAttribute {
parse_source(code)
.syntax()
.descendants()
.find_map(|node| InkAttribute::cast(ast::Attr::cast(node)?))
.unwrap()
}
pub fn first_ink_entity_of_type<T>(code: &str) -> T
where
T: InkEntity,
{
parse_source(code)
.syntax()
.descendants()
.find_map(T::cast)
.unwrap()
}
pub fn parse_source(code: &str) -> SourceFile {
SourceFile::parse(code, Edition::Edition2021).tree()
}