diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 00b88e628bd6..1577f6e40105 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index a8dfa12fb5a6..6f9164479a20 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -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)