Skip to content

Commit

Permalink
Fix to work formatting foo.[bar] = baz
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and asterite committed Jan 10, 2018
1 parent 157eca0 commit 972f2b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,31 @@ module Crystal
return false
end

# 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_multiline_call_indent = @multiline_call_indent

assignment = node.name.ends_with?('=') && node.name.chars.any?(&.ascii_letter?)
Expand Down

0 comments on commit 972f2b3

Please sign in to comment.