Skip to content

Commit

Permalink
Formatter: handle comment before do in a separate line (crystal-lang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Sep 29, 2020
1 parent 31be12b commit f095adc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1728,4 +1728,17 @@ describe Crystal::Formatter do
def bar
end
CODE

assert_format <<-CODE
foo 1, # comment
do
end
CODE

assert_format <<-CODE
foo 1, # comment
# bar
do
end
CODE
end
25 changes: 22 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2906,18 +2906,37 @@ module Crystal
old_inside_call_or_assign = @inside_call_or_assign
@inside_call_or_assign = 0

comma_before_comment = false

if @token.type == :","
needs_comma = true
next_token_skip_space_or_newline
next_token
next_token if @token.type == :SPACE
if @token.type == :COMMENT
write ","
needs_comma = false
comma_before_comment = true
@indent += 2
else
needs_comma = true
end
skip_space_or_newline
@indent -= 2 if comma_before_comment
end

if @token.keyword?(:do)
write " do"
if comma_before_comment
@indent += 2
write_indent
else
write " "
end
write "do"
next_token_skip_space
body = format_block_args node.args, node
old_implicit_exception_handler_indent, @implicit_exception_handler_indent = @implicit_exception_handler_indent, @indent
format_nested_with_end body
@implicit_exception_handler_indent = old_implicit_exception_handler_indent
@indent -= 2
elsif @token.type == :"{"
write "," if needs_comma
write " {"
Expand Down

0 comments on commit f095adc

Please sign in to comment.