diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 628d3d2c8781..5791644f9359 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -1329,4 +1329,8 @@ describe Crystal::Formatter do # #7608 assert_format "enum E\n A # hello\n B # hello; C # hello\nend" + + # #7631 + assert_format "x.try &.[] 123" + assert_format "x.try &.[]= 123, 456" end diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index f876a29ead74..8d5c9cb514bf 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -2768,58 +2768,7 @@ module Crystal when Call call = body clear_object call - - if !call.obj && (call.name == "[]" || call.name == "[]?") - case @token.type - when :"[" - write_token :"[" - skip_space_or_newline - format_call_args(call, false) - skip_space_or_newline - write_token :"]" - write_token :"?" if call.name == "[]?" - when :"[]", :"[]?" - write_token @token.type - skip_space_or_newline - if @token.type == :"(" - write "(" - next_token_skip_space_or_newline - format_call_args(call, true) - skip_space_or_newline - write_token :")" - end - else - raise "BUG: expected `[`, `[]` or `[]?`" - end - elsif !call.obj && call.name == "[]=" - case @token.type - when :"[" - last_arg = call.args.pop - write_token :"[" - skip_space_or_newline - format_call_args(call, false) - skip_space_or_newline - write_token :"]" - skip_space - write_token " ", :"=", " " - skip_space_or_newline - accept last_arg - when :"[]=" - write_token @token.type - skip_space_or_newline - if @token.type == :"(" - write "(" - next_token_skip_space_or_newline - format_call_args(call, true) - skip_space_or_newline - write_token :")" - end - else - raise "BUG: expected `[` or `[]=`" - end - else - indent(@indent, call) - end + indent(@indent, call) when IsA if body.obj.is_a?(Var) if body.nil_check?