typstyle_core/pretty/style.rs
1use typst_syntax::{SyntaxKind, SyntaxNode, ast::*};
2
3use crate::ext::StrExt;
4
5/// A style for formatting items
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FoldStyle {
8 /// Fold items if them can fit in a single line
9 Fit,
10 /// Never fold items
11 Never,
12 /// Always fold items
13 Always,
14 /// Try to fold items except the last one in a single line
15 Compact,
16}
17
18/// A syntax node is multiline flavored, if a line break appears before the first item.
19pub fn is_multiline_flavored(node: &SyntaxNode) -> bool {
20 for child in node.children() {
21 if child.kind() == SyntaxKind::Space {
22 return child.text().has_linebreak();
23 }
24 // To cover most cases, we use `len` to skip trivias (e.g., commas)
25 if child.is::<Expr>() || child.children().len() > 0 {
26 break;
27 }
28 }
29 false
30}