Skip to content

Commit

Permalink
Fix formatting call arguments with trailing comments
Browse files Browse the repository at this point in the history
Fixed #5489
  • Loading branch information
makenowjust authored and asterite committed Jan 1, 2018
1 parent 24fedf2 commit 15a357c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -242,6 +242,8 @@ describe Crystal::Formatter do
assert_format "foo(\n 1, 2)", "foo(\n 1, 2)"
assert_format "foo(\n1,\n 2 \n)", "foo(\n 1,\n 2\n)"
assert_format "foo(\n1,\n\n 2 \n)", "foo(\n 1,\n\n 2\n)"
assert_format "foo(\n 1,\n # 2,\n 3,\n)"
assert_format "foo(\n 1,\n # 2,\n # 3,\n)"
assert_format "foo 1,\n2", "foo 1,\n 2"
assert_format "foo 1, a: 1,\nb: 2,\nc: 3", "foo 1, a: 1,\n b: 2,\n c: 3"
assert_format "foo 1,\na: 1,\nb: 2,\nc: 3", "foo 1,\n a: 1,\n b: 2,\n c: 3"
Expand Down
19 changes: 13 additions & 6 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -2457,13 +2457,20 @@ module Crystal
if @token.type == :","
next_token
found_comment |= skip_space(column + 2, write_comma: true)
found_comment |= skip_space_or_newline(column + 2)
if has_newlines
unless found_comment
write ","
write_line
end
if @token.type == :NEWLINE && has_newlines
write ","
write_line
write_indent(column)
skip_space_or_newline(column + 2)
else
found_comment |= skip_space_or_newline(column + 2)
if has_newlines
unless found_comment
write ","
write_line
end
write_indent(column)
end
end
elsif found_comment
write_indent(column)
Expand Down

0 comments on commit 15a357c

Please sign in to comment.