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

Fix formatting nested multiline array and tuple #11153

Merged
merged 1 commit into from
Sep 7, 2021
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
13 changes: 13 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1859,4 +1859,17 @@ describe Crystal::Formatter do
2
}
CODE

# #11079
assert_format <<-CODE
foo = [1, [2,
3],
4]
CODE

assert_format <<-CODE
foo = {1, {2,
3},
4}
CODE
end
9 changes: 4 additions & 5 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -857,13 +857,12 @@ module Crystal
indent(offset, element)
end
element_lines = @line - start_line
next_offset = element_lines == 0 ? start_column : offset

has_heredoc_in_line = [email protected]?

last = last?(i, elements)

found_comment = skip_space(next_offset, write_comma: (last || has_heredoc_in_line) && has_newlines)
found_comment = skip_space(offset, write_comma: (last || has_heredoc_in_line) && has_newlines)

if @token.type == :","
if !found_comment && (!last || has_heredoc_in_line)
Expand All @@ -873,7 +872,7 @@ module Crystal

slash_is_regex!
next_token
found_comment = skip_space(element_lines == 0 ? start_column : offset, write_comma: last && has_newlines)
found_comment = skip_space(offset, write_comma: last && has_newlines)
if @token.type == :NEWLINE
if last && !found_comment && !wrote_comma
write ","
Expand All @@ -883,14 +882,14 @@ module Crystal
skip_space_or_newline
next_needs_indent = true
has_newlines = true
offset = next_offset if element_lines == 0
offset = start_column
else
if !last && !found_comment
write " "
next_needs_indent = false
elsif found_comment
next_needs_indent = true
offset = next_offset if element_lines == 0
offset = start_column
end
end
end
Expand Down