use crate::alloc::borrow::ToOwned;
use crate::ast::node_utils::{children, AstNodeAndId};
use crate::ast::{AstGraph, AstNode};
use crate::syntax::_node_builders::core::SyntaxNodeBuildError;
use crate::syntax::core_nodes::{CoreSyntaxNode, CoreSyntaxNodeWithId};
use alloc::vec::Vec;
pub fn filter_comments(items: Vec<CoreSyntaxNodeWithId>) -> Vec<CoreSyntaxNodeWithId> {
items
.into_iter()
.filter(|(n, _)| !matches!(n, CoreSyntaxNode::Comment(_)))
.collect()
}
pub fn ensure_one_expression(
element_group: &[CoreSyntaxNodeWithId],
) -> Result<CoreSyntaxNodeWithId, SyntaxNodeBuildError> {
match element_group {
[unique] => Ok(unique.clone()),
[_, ..] => Err(SyntaxNodeBuildError::Error(
"There is more than one expression/node for an element of an ArrayExpression"
.to_owned(),
)),
[] => Err(SyntaxNodeBuildError::Error(
"An element group of an ArrayExpression is empty".to_owned(),
)),
}
}
pub struct FilteredChildren<'a> {
items: Vec<AstNodeAndId<'a>>,
}
impl<'a> FilteredChildren<'a> {
pub fn removed_delimiters(
graph: &'a AstGraph,
node: &AstNode,
) -> Result<Self, SyntaxNodeBuildError> {
let all_children = children(graph, node).map_err(SyntaxNodeBuildError::ResolutionError);
all_children
.map(|children| {
children
.into_iter()
.filter(|(child, _)| !matches!(child.kind.as_str(), "[" | "]"))
.collect::<Vec<_>>()
})
.map(|items| Self { items })
}
pub fn filtered_children_items(&self) -> &Vec<AstNodeAndId<'a>> {
&self.items
}
}