From 58e535656b2939ae4418124da40783fc2132db55 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 24 Oct 2024 08:21:01 -0300 Subject: [PATCH] fix: (formatter) indent after infix lhs --- tooling/nargo_fmt/src/formatter/expression.rs | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tooling/nargo_fmt/src/formatter/expression.rs b/tooling/nargo_fmt/src/formatter/expression.rs index 448481047df..6f7ebf56348 100644 --- a/tooling/nargo_fmt/src/formatter/expression.rs +++ b/tooling/nargo_fmt/src/formatter/expression.rs @@ -705,6 +705,12 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> { } }; + // Indent right after the lhs so that if there's a trailing comment, + // the next line is indented correctly. + if increase_indentation { + group.increase_indentation(); + } + let mut comment_chunk_after_lhs = self.skip_comments_and_whitespace_chunk(); // If the comment is not empty but doesn't have newlines, it's surely `/* comment */`. @@ -719,10 +725,6 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> { group.trailing_comment(comment_chunk_after_lhs); } - if increase_indentation { - group.increase_indentation(); - } - group.space_or_line(); group.text(self.chunk(|formatter| { let tokens_count = @@ -1421,6 +1423,23 @@ global y = 1; assert_format_with_max_width(src, expected, "one + two + three + four".len() - 1); } + #[test] + fn format_infix_with_trailing_comments() { + let src = "fn foo() { + let x = 1 // one ++ 2 // two ++ 3; // three +} +"; + let expected = "fn foo() { + let x = 1 // one + + 2 // two + + 3; // three +} +"; + assert_format(src, expected); + } + #[test] fn format_empty_block() { let src = "global x = { } ;";