Skip to content

Commit

Permalink
Fix formatter comment on extra line at end of method args
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jan 6, 2023
1 parent 01aaf2d commit d5e797d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ describe Crystal::Formatter do
assert_format "foo.% bar"
assert_format "foo.bar(&.%(baz))"
assert_format "foo.bar(&.% baz)"
assert_format "if 1\n foo(\n bar\n # comment\n )\nend"
assert_format "if 1\n foo(\n bar,\n # comment\n )\nend"

assert_format "foo.bar\n.baz", "foo.bar\n .baz"
assert_format "foo.bar.baz\n.qux", "foo.bar.baz\n .qux"
Expand Down
19 changes: 9 additions & 10 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,14 @@ module Crystal
offset = @indent + 2
start_column = @indent + 2

indent(offset) do
if elements.empty?
skip_space_or_newline(last: true, at_least_one: true)
write_token suffix
return false
end

consume_newlines
if elements.empty?
skip_space_or_newline(offset, last: true, at_least_one: true)
write_token suffix
return false
end

indent(offset) { consume_newlines }

skip_space_or_newline
wrote_newline = true
next_needs_indent = true
Expand Down Expand Up @@ -2769,7 +2767,7 @@ module Crystal
if @token.type.newline?
ends_with_newline = true
end
skip_space_or_newline
indent(base_indent + 2) { skip_space_or_newline(last: true, at_least_one: ends_with_newline) }
elsif has_args || node.block_arg
write " " unless passed_backslash_newline
skip_space
Expand Down Expand Up @@ -2961,8 +2959,9 @@ module Crystal
if @token.type.newline? && has_newlines
write ","
write_line
write_indent(column)
skip_space_or_newline(column + 2)
write_indent(column)
skip_space_or_newline(column)
else
found_comment |= skip_space_or_newline(column + 2)
if has_newlines
Expand Down

0 comments on commit d5e797d

Please sign in to comment.