Skip to content

Commit

Permalink
Fix formatting of long multiline if/is expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Sep 15, 2024
1 parent 12c0d0b commit 47a15cf
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- **aiken-project**: Fix documentation link-tree generation messing up with modules when re-inserting the same module. @KtorZ
- **aiken-lang**: Fix formatter adding extra unnecessary newlines after literal lists clause values or assignments. @KtorZ
- **aiken0lang**: Fix formatting of long multi-line if/is expressions. @KtorZ

### Removed

Expand Down
5 changes: 1 addition & 4 deletions crates/aiken-lang/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,7 @@ impl<'comments> Formatter<'comments> {
.group()
};

break_("", " ")
.append("is")
.append(break_("", " "))
.append(is)
break_("", " ").append("is ").append(is)
}
None => nil(),
})
Expand Down
12 changes: 12 additions & 0 deletions crates/aiken-lang/src/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3264,3 +3264,15 @@ fn constant_usage() {
}] if name == "some_string_constant"
));
}

#[test]
fn wrong_arity_on_known_builtin() {
let source_code = r#"
const foo: Option<Int> = Some()
"#;

assert!(matches!(
check_validator(parse(source_code)),
Err((_, Error::IncorrectFunctionCallArity { .. }))
))
}
32 changes: 32 additions & 0 deletions crates/aiken-lang/src/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,3 +1388,35 @@ fn callback_and_op() {
"#
);
}

#[test]
fn multiline_if_is() {
assert_format!(
r#"
fn foo() {
if first_asset
is
Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {
True
} else {
False }
}
"#
);
}

#[test]
fn multiline_if_is_2() {
assert_format!(
r#"
fn foo() {
if [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
is
Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {
True
} else {
False }
}
"#
);
}
12 changes: 12 additions & 0 deletions crates/aiken-lang/src/tests/snapshots/multiline_if_is.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n if first_asset\n is\n Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {\n True\n } else {\n False }\n}\n"
---
fn foo() {
if first_asset
is Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {
True
} else {
False
}
}
15 changes: 15 additions & 0 deletions crates/aiken-lang/src/tests/snapshots/multiline_if_is_2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n if [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\n is\n Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {\n True\n } else {\n False }\n}\n"
---
fn foo() {
if [
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 0,
]
is Pair(first_asset_policy, first_asset_tokens): Pair<PolicyId, Data> {
True
} else {
False
}
}

0 comments on commit 47a15cf

Please sign in to comment.