diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f40d245..221cf27b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,9 @@ ### Changed -- **aiken-lang**: Fix underflow in error message reported by the validator arity. See [#1013](https://github.com/aiken-lang/aiken/issues/1013) @KtorZ. +- **aiken-lang**: Fix underflow in error message reported by the validator arity. See [#1013](https://github.com/aiken-lang/aiken/issues/1013) @KtorZ - **aiken-lang**: Fix list-pattern needlessly formatting over multiple lines. @KtorZ +- **aiken-lang**: Fix formatter on long alternative patterns spanning over multiple lines. @KtorZ ### Removed diff --git a/crates/aiken-lang/src/format.rs b/crates/aiken-lang/src/format.rs index 472f9925..7aadd6e6 100644 --- a/crates/aiken-lang/src/format.rs +++ b/crates/aiken-lang/src/format.rs @@ -1906,8 +1906,9 @@ impl<'comments> Formatter<'comments> { let space_before = self.pop_empty_lines(clause.location.start); let clause_doc = join( clause.patterns.iter().map(|p| self.pattern(p)), - " | ".to_doc(), - ); + break_(" | ", " | "), + ) + .group(); if index == 0 { clause_doc diff --git a/crates/aiken-lang/src/tests/format.rs b/crates/aiken-lang/src/tests/format.rs index 0ea0f1c0..ee095e4d 100644 --- a/crates/aiken-lang/src/tests/format.rs +++ b/crates/aiken-lang/src/tests/format.rs @@ -1249,17 +1249,48 @@ fn format_validator_exhaustive_handlers_extra_non_default_fallback() { } #[test] -fn list_pattern() { +fn single_line_alternative_patterns() { assert_format!( r#" fn foo() { - when xs is { - [_] -> True - [1, 2] -> False - [_, x] -> { - let y = x - y == 42 + when bar is { + a | b | c -> True + d | e -> { + let x = e + d + x > 10 + } + _ -> False } + } + "# + ); +} + +#[test] +fn multiline_alternative_patterns() { + assert_format!( + r#" + validator direct_proxy { + mint(_redeemer: Void, policy_id: PolicyId, self: Transaction) { + list.any( + self.certificates, + fn(certificate) { + when certificate is { + RegisterDelegateRepresentative { + delegate_representative: credential, + .. + } | UnregisterDelegateRepresentative { + delegate_representative: credential, + .. + } | RegisterCredential { credential, .. } | UnregisterCredential { + credential, + .. + } | RegisterAndDelegateCredential { credential, .. } -> + credential == Script(policy_id) + _ -> False + } + }, + ) } } "# diff --git a/crates/aiken-lang/src/tests/snapshots/multiline_alternative_patterns.snap b/crates/aiken-lang/src/tests/snapshots/multiline_alternative_patterns.snap new file mode 100644 index 00000000..ef36ad87 --- /dev/null +++ b/crates/aiken-lang/src/tests/snapshots/multiline_alternative_patterns.snap @@ -0,0 +1,32 @@ +--- +source: crates/aiken-lang/src/tests/format.rs +description: "Code:\n\nvalidator direct_proxy {\n mint(_redeemer: Void, policy_id: PolicyId, self: Transaction) {\n list.any(\n self.certificates,\n fn(certificate) {\n when certificate is {\n RegisterDelegateRepresentative {\n delegate_representative: credential,\n ..\n } | UnregisterDelegateRepresentative {\n delegate_representative: credential,\n ..\n } | RegisterCredential { credential, .. } | UnregisterCredential {\n credential,\n ..\n } | RegisterAndDelegateCredential { credential, .. } ->\n credential == Script(policy_id)\n _ -> False\n }\n },\n )\n }\n}\n" +--- +validator direct_proxy { + mint(_redeemer: Void, policy_id: PolicyId, self: Transaction) { + list.any( + self.certificates, + fn(certificate) { + when certificate is { + RegisterDelegateRepresentative { + delegate_representative: credential, + .. + } | + UnregisterDelegateRepresentative { + delegate_representative: credential, + .. + } | + RegisterCredential { credential, .. } | + UnregisterCredential { credential, .. } | + RegisterAndDelegateCredential { credential, .. } -> + credential == Script(policy_id) + _ -> False + } + }, + ) + } + + else(_) { + fail + } +} diff --git a/crates/aiken-lang/src/tests/snapshots/single_line_alternative_patterns.snap b/crates/aiken-lang/src/tests/snapshots/single_line_alternative_patterns.snap new file mode 100644 index 00000000..d1d1dca5 --- /dev/null +++ b/crates/aiken-lang/src/tests/snapshots/single_line_alternative_patterns.snap @@ -0,0 +1,14 @@ +--- +source: crates/aiken-lang/src/tests/format.rs +description: "Code:\n\nfn foo() {\n when bar is {\n a | b | c -> True\n d | e -> {\n let x = e + d\n x > 10\n }\n _ -> False\n }\n}\n" +--- +fn foo() { + when bar is { + a | b | c -> True + d | e -> { + let x = e + d + x > 10 + } + _ -> False + } +}