Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: fix for begin ... rescue ... end #6274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1147,4 +1147,7 @@ describe Crystal::Formatter do
assert_format "Hash{\n foo => <<-EOF,\n foo\n EOF\n bar => <<-BAR,\n bar\n BAR\n}"
assert_format "Hash{\n foo => <<-EOF\n foo\n EOF\n}"
assert_format "{\n <<-KEY => 1,\n key\n KEY\n}"

assert_format "begin 0[1] rescue 2 end"
assert_format "begin\n 0[1] rescue 2 end", "begin 0[1] rescue 2 end"
end
26 changes: 26 additions & 0 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,25 @@ module Crystal
column = @implicit_exception_handler_indent
else
if node.suffix
inline = false

# This is the case of:
#
# begin exp rescue exp end
#
# It's parsed as:
#
# begin (exp rescue exp) end
#
# So it's a suffix rescue inside a begin/end, but the Parser
# returns it as an ExceptionHandler node.
if @token.keyword?(:begin)
inline = true
write_keyword :begin
skip_space_or_newline
write " "
end

accept node.body
passed_backslash_newline = @token.passed_backslash_newline
skip_space
Expand All @@ -3708,6 +3727,13 @@ module Crystal
else
raise "expected 'rescue' or 'ensure'"
end

if inline
skip_space_or_newline
write " "
write_keyword :end
end

return false
end
end
Expand Down