Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: fix extra newline after comment in case when #6595

Merged
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ describe Crystal::Formatter do
assert_format "case\nend"
assert_format "case\nelse\n 1\nend"

assert_format <<-CODE
case 0
when 0 then 1; 2
# Comments
end
CODE

assert_format "select \n when foo \n 2 \n end", "select\nwhen foo\n 2\nend"
assert_format "select \n when foo \n 2 \n when bar \n 3 \n end", "select\nwhen foo\n 2\nwhen bar\n 3\nend"
assert_format "select \n when foo then 2 \n end", "select\nwhen foo then 2\nend"
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3455,11 +3455,12 @@ module Crystal
write " "
when_column_end = @column
accept node.body
wrote_newline = @wrote_newline
if @line == when_start_line
@when_infos << AlignInfo.new(case_node.object_id, @line, when_start_column, when_column_middle, when_column_end, align_number)
end
found_comment = skip_space
write_line unless found_comment
write_line unless found_comment || wrote_newline
end
else
format_nested(node.body, @indent)
Expand Down