Skip to content

Commit 1b675b0

Browse files
giacomocavalierilpil
authored andcommitted
fix formatting of panic/todo block messages
1 parent 718c9ad commit 1b675b0

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@
451451
in a different function.
452452
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
453453

454+
- Fixed a bug where the formatter would format a panic message adding more
455+
nesting than necessary.
456+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
457+
454458
- Fixed a bug where the compiler allowed to write a guard with an empty clause.
455459
([Tristan-Mihai Radulescu](https://github.com/Courtcircuits))
456460

compiler-core/src/format.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,12 +2912,27 @@ impl<'comments> Formatter<'comments> {
29122912
docvec![line(), comments, line(), self.expr(message).group()].nest(INDENT)
29132913
],
29142914

2915-
None => docvec![
2916-
doc.group(),
2917-
as_,
2918-
" ",
2919-
self.expr(message).group().nest(INDENT),
2920-
],
2915+
None => {
2916+
let message = match (preceding_as, message) {
2917+
// If we have `as` preceded by a keyword (like with `panic` and `todo`)
2918+
// and the message is a block, we don't want to nest it any further. That is,
2919+
// we want it to look like this:
2920+
// ```gleam
2921+
// panic as {
2922+
// wibble wobble
2923+
// }
2924+
// ```
2925+
// instead of this:
2926+
// ```gleam
2927+
// panic as {
2928+
// wibble wobble
2929+
// }
2930+
// ```
2931+
(PrecedingAs::Keyword, UntypedExpr::Block { .. }) => self.expr(message).group(),
2932+
_ => self.expr(message).group().nest(INDENT),
2933+
};
2934+
docvec![doc.group(), as_, " ", message]
2935+
}
29212936
};
29222937

29232938
doc.group()

compiler-core/src/format/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6719,3 +6719,17 @@ fn pattern_unused_discard() {
67196719
"#
67206720
);
67216721
}
6722+
6723+
// https://github.com/gleam-lang/gleam/issues/4929
6724+
#[test]
6725+
fn format_panic_as_with_block_message() {
6726+
assert_format!(
6727+
r#"pub fn main() {
6728+
panic as {
6729+
// b
6730+
a
6731+
}
6732+
}
6733+
"#
6734+
);
6735+
}

0 commit comments

Comments
 (0)