Fix formatting of multi-line alternative patterns.

This commit is contained in:
KtorZ 2024-09-08 13:11:18 +02:00
parent 8db4a60986
commit b6d99142f9
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
5 changed files with 89 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}
},
)
}
}
"#

View File

@ -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
}
}

View File

@ -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
}
}