typstyle_core/pretty/
util.rs1use typst_syntax::{SyntaxKind, SyntaxNode, ast::*};
2
3pub fn is_only_one_and<T>(
4 mut iterator: impl Iterator<Item = T>,
5 f: impl FnOnce(&T) -> bool,
6) -> bool {
7 iterator
8 .next()
9 .is_some_and(|first| iterator.next().is_none() && f(&first))
10}
11
12pub fn is_empty_or_one_if<T>(
13 mut iterator: impl Iterator<Item = T>,
14 f: impl FnOnce(&T) -> bool,
15) -> bool {
16 iterator
17 .next()
18 .is_none_or(|it| iterator.next().is_none() && f(&it))
19}
20
21pub fn is_comment_node(node: &SyntaxNode) -> bool {
22 matches!(
23 node.kind(),
24 SyntaxKind::LineComment | SyntaxKind::BlockComment
25 )
26}
27
28pub fn has_comment_children(node: &SyntaxNode) -> bool {
29 node.children().any(is_comment_node)
30}
31
32pub(super) fn func_name(node: FuncCall<'_>) -> Option<&str> {
33 match node.callee() {
34 Expr::Ident(ident) => Some(ident.as_str()),
35 Expr::FieldAccess(field_access) => Some(field_access.field().as_str()),
36 _ => None,
37 }
38}