Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion tooling/nargo_fmt/src/formatter/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,38 @@ impl ChunkFormatter<'_, '_> {
}));

if let Some(value) = value {
// If there's a line comment right before the value we'll put
// the comment and the value in the next line, both indented.
let mut has_comment_before_value = false;

group.text(self.chunk(|formatter| {
formatter.write_space();
formatter.write_token(Token::Assign);
formatter.write_space();
formatter.skip_whitespace();
if matches!(formatter.token, Token::LineComment(..)) {
has_comment_before_value = true;
} else {
formatter.write_space();
}
}));

if has_comment_before_value {
group.increase_indentation();
group.line();
group.trailing_comment(self.chunk(|formatter| {
formatter.skip_comments_and_whitespace();
}));
}

let mut value_group = ChunkGroup::new();
value_group.kind = GroupKind::AssignValue;
self.format_expression(value, &mut value_group);
value_group.semicolon(self);
group.group(value_group);

if has_comment_before_value {
group.decrease_indentation();
}
} else {
group.semicolon(self);
}
Expand Down Expand Up @@ -433,6 +454,21 @@ mod tests {
assert_format(src, expected);
}

#[test]
fn format_let_statement_with_unsafe_comment_right_before_unsafe() {
let src = " fn foo() {

let x = // Safety: some comment
unsafe { 1 } ; } ";
let expected = "fn foo() {
let x =
// Safety: some comment
unsafe { 1 };
}
";
assert_format(src, expected);
}

#[test]
fn format_assign() {
let src = " fn foo() { x = 2 ; } ";
Expand Down