Fix aiken docs constant generation

Fixes #1048.
This commit is contained in:
KtorZ
2024-10-29 14:15:31 +01:00
parent 2b7ca0e4a1
commit 2489e0fdd0
4 changed files with 31 additions and 2 deletions

View File

@@ -203,6 +203,22 @@ impl<T> From<Vec1Ref<T>> for Vec1<T> {
}
impl TypedExpr {
pub fn is_simple_expr_to_format(&self) -> bool {
match self {
Self::String { .. } | Self::UInt { .. } | Self::ByteArray { .. } | Self::Var { .. } => {
true
}
Self::Pair { fst, snd, .. } => {
fst.is_simple_expr_to_format() && snd.is_simple_expr_to_format()
}
Self::Tuple { elems, .. } => elems.iter().all(|e| e.is_simple_expr_to_format()),
Self::List { elements, .. } if elements.len() <= 3 => {
elements.iter().all(|e| e.is_simple_expr_to_format())
}
_ => false,
}
}
pub fn and_then(self, next: Self) -> Self {
if let TypedExpr::Trace {
tipo,

View File

@@ -382,7 +382,18 @@ impl<'comments> Formatter<'comments> {
.append(wrap_args(elems.iter().map(|e| (self.const_expr(e), false))).group())
}
TypedExpr::List { elements, .. } => {
wrap_args(elements.iter().map(|e| (self.const_expr(e), false))).group()
let comma: fn() -> Document<'a> =
if elements.iter().all(TypedExpr::is_simple_expr_to_format) {
|| flex_break(",", ", ")
} else {
|| break_(",", ", ")
};
list(
join(elements.iter().map(|e| self.const_expr(e)), comma()),
elements.len(),
None,
)
}
TypedExpr::Var { name, .. } => name.to_doc(),
_ => Document::Str(""),