From 5fe3b650b574192a4a59e2ce81fb800578cf5bd8 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 26 Jun 2018 08:35:43 -0300 Subject: [PATCH] Formatter: fix for begin ... rescue ... end --- spec/compiler/formatter/formatter_spec.cr | 3 +++ src/compiler/crystal/tools/formatter.cr | 26 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 9ec3bc72df85..48d147405111 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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 diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index fa631ba54a52..87f9766322be 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -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 @@ -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