Skip to content

Commit c07aa93

Browse files
giacomocavalierilpil
authored andcommitted
fix formatting
1 parent e8eb002 commit c07aa93

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@
520520
writing a constructor with a lowercase name.
521521
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
522522

523+
- Fixed a bug where guards with comments wouldn't be formatted properly.
524+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
525+
523526
- A `gleam@@compile.erl` is no longer left in the build output of
524527
`gleam compile-package`.
525528
([Louis Pilfold](https://github.com/lpil))

format/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,14 +3093,17 @@ impl<'a, 'doc> Formatter<'a> {
30933093
left: &'a UntypedClauseGuard,
30943094
right: &'a UntypedClauseGuard,
30953095
) -> Document<'a, 'doc> {
3096-
self.clause_guard_bin_op_side(arena, name, left, left.precedence())
3097-
.append(arena, BREAKABLE_SPACE_DOCUMENT)
3098-
.append(arena, binop(*name))
3099-
.append(arena, SPACE_DOCUMENT)
3100-
.append(
3101-
arena,
3102-
self.clause_guard_bin_op_side(arena, name, right, right.precedence() - 1),
3103-
)
3096+
let left_comments = self.pop_comments(left.location().start);
3097+
let left = self.clause_guard_bin_op_side(arena, name, left, left.precedence());
3098+
let left = commented(arena, left, left_comments);
3099+
3100+
let right_comments = self.pop_comments(right.location().start);
3101+
let right = self.clause_guard_bin_op_side(arena, name, right, right.precedence() - 1);
3102+
let right = docvec![arena, binop(*name), SPACE_DOCUMENT, right];
3103+
let right = commented(arena, right, right_comments);
3104+
3105+
left.append(arena, BREAKABLE_SPACE_DOCUMENT)
3106+
.append(arena, right)
31043107
}
31053108

31063109
fn clause_guard_bin_op_side(

format/src/tests/guards.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ fn operators_in_guard() {
5151
);
5252
}
5353

54+
#[test]
55+
fn commented_operators_in_guard() {
56+
assert_format!(
57+
r#"pub fn main() {
58+
case list.map(codepoints, string.utf_codepoint_to_int) {
59+
[drive, colon, slash]
60+
if { slash == 47 || slash == 92 }
61+
&& colon == 58
62+
// Hello
63+
&& drive >= 65
64+
&& drive <= 90
65+
// This is a comment
66+
|| drive >= 97
67+
// And another one
68+
&& drive <= 122
69+
-> {
70+
1
71+
|> 2
72+
}
73+
}
74+
}
75+
"#
76+
);
77+
}
78+
5479
#[test]
5580
fn a_comment_before_a_guard_doesnt_force_it_to_break() {
5681
assert_format!(

0 commit comments

Comments
 (0)