Skip to content

Commit

Permalink
Format: fix to use right alignment only if all whens are number liter…
Browse files Browse the repository at this point in the history
…al (crystal-lang#6392)

* Format: fix to use right alignment only if all whens are number literal

Fix crystal-lang#6366

* Fix test case
  • Loading branch information
makenowjust authored and RX14 committed Jul 16, 2018
1 parent 3b3bf3f commit 210d8ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ describe Crystal::Formatter do
assert_format "case 1\nwhen \"foo\" ; 3\nwhen \"lalalala\"; 4\nelse 5\nend"
assert_format "case 1\nwhen \"foo\" then 3\nwhen \"lalalala\" then 4\nelse 5\nend"
assert_format "case 1 # foo\nwhen 2 then 3 # bar\nwhen 4 then 5 # baz\nelse 6 # zzz\nend"
assert_format "case 1\nwhen 8 then 1\nwhen 16 then 2\nwhen 256 then 3\nwhen 'a' then 5\nwhen \"foo\" then 6\nelse 4\nend"
assert_format "case 1\nwhen 8 then 1\nwhen 16 then 2\nwhen 256 then 3\nwhen 'a' then 5\nwhen \"foo\" then 6\nelse 4\nend"
assert_format "case 1\nwhen 1 then 1\nwhen 123 then 2\nwhen 1..123 then 3\nelse 4\nend"
assert_format "macro bar\n 1\nend\n\ncase 1\nwhen 2 then 3\nwhen 45 then 6\nend"
assert_format "{\n 1 => 2,\n 10 => 30,\n 30 => 40,\n \"foobar\" => 50,\n \"coco\" => 60,\n}"
assert_format "{1 => 2, 3 => 4}\n{5234234 => 234098234, 7 => 8}"
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3355,8 +3355,10 @@ module Crystal

skip_space_write_line

align_number = node.whens.all? { |a_when| a_when.conds.size === 1 && a_when.conds.first.is_a?(NumberLiteral) }

node.whens.each_with_index do |a_when, i|
format_when(node, a_when, last?(i, node.whens))
format_when(node, a_when, last?(i, node.whens), align_number)
skip_space_or_newline(@indent + 2)
end

Expand Down Expand Up @@ -3391,7 +3393,7 @@ module Crystal
false
end

def format_when(case_node, node, is_last)
def format_when(case_node, node, is_last, align_number)
skip_space_or_newline

slash_is_regex!
Expand Down Expand Up @@ -3441,8 +3443,7 @@ module Crystal
when_column_end = @column
accept node.body
if @line == when_start_line
number = node.conds.size == 1 && node.conds.first.is_a?(NumberLiteral)
@when_infos << AlignInfo.new(case_node.object_id, @line, when_start_column, when_column_middle, when_column_end, number)
@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
Expand Down

0 comments on commit 210d8ef

Please sign in to comment.