From 4702bd807bf9693181cc9d48ecc7f9ce0fc5e7e0 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Thu, 21 Dec 2017 20:16:08 +0900 Subject: [PATCH] Fix to work formatting `foo.[bar] = baz` Fixed #5416 --- spec/compiler/formatter/formatter_spec.cr | 2 ++ src/compiler/crystal/tools/formatter.cr | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index aa9b7451f022..23bfb9612a76 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -901,6 +901,8 @@ describe Crystal::Formatter do assert_format "foo.[]" assert_format "foo.[1]" + assert_format "foo.[] = 1" + assert_format "foo.[1, 2] = 3" assert_format "@foo : Int32 # comment\n\ndef foo\nend" assert_format "getter foo # comment\n\ndef foo\nend" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index c8ca96269a24..28bc019e229d 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -2230,6 +2230,33 @@ module Crystal current_multiline_call_indent = @multiline_call_indent + # This is for foo.[bar] = baz + if node.name == "[]=" && @token.type == :"[" + write "[" + next_token_skip_space_or_newline + args = node.args + last_arg = args.pop + format_args args, true, node.named_args + write_token :"]" + skip_space_or_newline + write " =" + next_token_skip_space + accept_assign_value_after_equals last_arg + return false + end + + # This is for foo.[] = bar + if node.name == "[]=" && @token.type == :"[]" + write_token :"[]" + next_token_skip_space_or_newline + write " =" + next_token_skip_space + accept_assign_value_after_equals node.args.last + return false + end + + current_dot_column = @dot_column + assignment = node.name.ends_with?('=') && node.name.chars.any?(&.ascii_letter?) if assignment