Skip to content

Commit

Permalink
Formatter: fixed case with block arg decomposition and parenthesized …
Browse files Browse the repository at this point in the history
…exp. Fixes #3407
Ary Borenszweig committed Oct 10, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 2da5008 commit 2be9f09
Showing 2 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -968,4 +968,5 @@ describe Crystal::Formatter do

assert_format "foo(A |\nB |\nC)", "foo(A |\n B |\n C)"
assert_format "def foo\n case x\n # z\n when 1\n end\nend"
assert_format "foo { |x| (x).a }"
end
45 changes: 22 additions & 23 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
@@ -1323,10 +1323,10 @@ module Crystal
write " "
end

remove_to_skip node, to_skip
body = remove_to_skip node, to_skip

unless node.abstract?
format_nested_with_end node.body
format_nested_with_end body
end

@inside_def -= 1
@@ -2442,21 +2442,21 @@ module Crystal
if @token.keyword?(:do)
write " do"
next_token_skip_space
format_block_args node.args, node
format_nested_with_end node.body
body = format_block_args node.args, node
format_nested_with_end body
elsif @token.type == :"{"
write "," if needs_comma
write " {"
next_token_skip_space
format_block_args node.args, node
body = format_block_args node.args, node
if @token.type == :NEWLINE
format_nested node.body
format_nested body
skip_space_or_newline
write_indent
else
unless node.body.is_a?(Nop)
unless body.is_a?(Nop)
write " "
accept node.body
accept body
end
skip_space_or_newline
write " "
@@ -2606,7 +2606,7 @@ module Crystal
end

def format_block_args(args, node)
return 0 if args.empty?
return node.body if args.empty?

to_skip = 0

@@ -2673,27 +2673,26 @@ module Crystal
if to_skip > 0
body = node.body
if body.is_a?(ExceptionHandler) && body.implicit
node = body
body = body.body
sub_body = remove_to_skip(body, to_skip)
body.body = sub_body
return body
end

if body.is_a?(Expressions)
body.expressions = body.expressions[to_skip..-1]
if body.expressions.empty?
set_body(node, Nop.new)
case body.expressions.size
when 0
Nop.new
when 1
body.expressions.first
else
body
end
else
set_body(node, Nop.new)
Nop.new
end
end
end

def set_body(node, body)
case node
when Def
node.body = body
when ExceptionHandler
node.body = body
else
node.body
end
end

0 comments on commit 2be9f09

Please sign in to comment.