Skip to content

Commit

Permalink
Fix few formatter annoyances.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Sep 13, 2024
1 parent 799546b commit a9a7a4f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
- **aiken-lang**: Aiken IR now interns variables while building up to ensure uniqueness for local vars. @Microproofs
- **aiken-lang**: Fix reification of `Data` (failing to reify) & `PRNG` (missing variants' arguments). @KtorZ
- **aiken-lang**: Adjust reification of `String` to be shown as plain UTF-8 text strings (instead of hex-encoded byte array). @KtorZ

- **aiken-lang**: Fix formatting of long if-condition over multiline. @KtorZ & @Microproofs
- **aiken-lang**: Fix formatting of standalone logical binary chains (`and` & `or`) in functions. @KtorZ

### Removed

Expand Down
10 changes: 5 additions & 5 deletions crates/aiken-lang/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,9 @@ impl<'comments> Formatter<'comments> {
) -> Document<'a> {
let args = wrap_args(args.iter().map(|e| (self.fn_arg(e), false))).group();
let body = match body {
UntypedExpr::Trace { .. } | UntypedExpr::When { .. } => {
self.expr(body, true).force_break()
}
UntypedExpr::Trace { .. }
| UntypedExpr::When { .. }
| UntypedExpr::LogicalOpChain { .. } => self.expr(body, true).force_break(),
_ => self.expr(body, true),
};

Expand Down Expand Up @@ -1268,7 +1268,7 @@ impl<'comments> Formatter<'comments> {
final_else: &'a UntypedExpr,
) -> Document<'a> {
let if_branches = self
.if_branch(break_("if", "if "), branches.first())
.if_branch(Document::Str("if "), branches.first())
.append(join(
branches[1..].iter().map(|branch| {
self.if_branch(line().append(break_("} else if", "} else if ")), branch)
Expand Down Expand Up @@ -1330,7 +1330,7 @@ impl<'comments> Formatter<'comments> {
}
None => nil(),
})
.append(break_("{", " {"))
.append(Document::Str(" {"))
.group();

let if_body = line().append(self.expr(&branch.body, true)).nest(INDENT);
Expand Down
35 changes: 35 additions & 0 deletions crates/aiken-lang/src/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,3 +1352,38 @@ fn multiline_constant() {
"#
);
}

#[test]
fn multiline_if_condition() {
assert_format!(
r#"
fn foo() {
if
list.is_empty(outputs) && (
!list.is_empty(mint_redeemers) || !list.is_empty(cert_redeemers)
){
True
} else {
False
}
}
"#
);
}

#[test]
fn callback_and_op() {
assert_format!(
r#"
fn foo() {
let labels = list.filter(labels, fn(lbl) {
and {
lbl != sc_missing_admin_approval_for_foreign_assets,
lbl != sc_missing_admin_approval_for_certificate_publish,
}
})
labels
}
"#
);
}
17 changes: 17 additions & 0 deletions crates/aiken-lang/src/tests/snapshots/callback_and_op.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n let labels = list.filter(labels, fn(lbl) {\n and {\n lbl != sc_missing_admin_approval_for_foreign_assets,\n lbl != sc_missing_admin_approval_for_certificate_publish,\n }\n })\n labels\n}\n"
---
fn foo() {
let labels =
list.filter(
labels,
fn(lbl) {
and {
lbl != sc_missing_admin_approval_for_foreign_assets,
lbl != sc_missing_admin_approval_for_certificate_publish,
}
},
)
labels
}
13 changes: 13 additions & 0 deletions crates/aiken-lang/src/tests/snapshots/multiline_if_condition.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n if\n list.is_empty(outputs) && (\n !list.is_empty(mint_redeemers) || !list.is_empty(cert_redeemers)\n ){\n True\n } else {\n False\n }\n}\n"
---
fn foo() {
if list.is_empty(outputs) && (
!list.is_empty(mint_redeemers) || !list.is_empty(cert_redeemers)
) {
True
} else {
False
}
}

0 comments on commit a9a7a4f

Please sign in to comment.