From 2489e0fdd0bec5e6e1f50f9b3512ccceef9012a0 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Tue, 29 Oct 2024 14:15:31 +0100 Subject: [PATCH] Fix aiken docs constant generation Fixes #1048. --- CHANGELOG.md | 2 ++ crates/aiken-lang/src/expr.rs | 16 ++++++++++++++++ crates/aiken-lang/src/format.rs | 13 ++++++++++++- crates/aiken-project/src/docs.rs | 2 +- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 160936692..2a72b0779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ ### Changed - **aiken**: Rename `--filter_traces` to `--trace_filter` for more consistency with `--trace_level`. An alias for `--filter_traces` still exists for backward compatibility. @KtorZ +- **aiken-project**: Fix `aiken docs` wrongly formatting list constants as tuples. See [#1048](https://github.com/aiken-lang/aiken/issues/1048). @KtorZ - **aiken-project**: Fix `aiken docs` source linking crashing when generating docs for config modules. See [#1044](https://github.com/aiken-lang/aiken/issues/1044). @KtorZ +- **aiken-project**: Fix `aiken docs` generating very long lines for constants. @KtorZ ### Removed diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index 0622783cf..58407df43 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -203,6 +203,22 @@ impl From> for Vec1 { } 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, diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 814436090..aa0e35c5b 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -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(""), diff --git a/crates/aiken-project/src/docs.rs b/crates/aiken-project/src/docs.rs index 573e41e63..fa5ebe1cf 100644 --- a/crates/aiken-project/src/docs.rs +++ b/crates/aiken-project/src/docs.rs @@ -22,7 +22,7 @@ use std::{ time::{Duration, SystemTime}, }; -const MAX_COLUMNS: isize = 999; +const MAX_COLUMNS: isize = 80; const VERSION: &str = env!("CARGO_PKG_VERSION"); pub mod link_tree;