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

Format: fix indentation in collection with comment after beginning delimiter #5893

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
4 changes: 4 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ describe Crystal::Formatter do
assert_format " [ 1, \n 2 , \n 3 ] ", "[1,\n 2,\n 3]"
assert_format "Set { 1 , 2 }", "Set{1, 2}"
assert_format "[\n1,\n\n2]", "[\n 1,\n\n 2,\n]"
assert_format "[ # foo\n 1,\n]"
assert_format "Set{ # foo\n 1,\n}"

assert_format "{1, 2, 3}"
assert_format "{ {1, 2, 3} }"
assert_format "{ {1 => 2} }"
assert_format "{ {1, 2, 3} => 4 }"
assert_format "{ {foo: 2} }"
assert_format "{ # foo\n 1,\n}"

assert_format "{ } of A => B", "{} of A => B"
assert_format "{ 1 => 2 }", "{1 => 2}"
Expand All @@ -91,6 +94,7 @@ describe Crystal::Formatter do
assert_format "{ \"foo\" => 1 }", "{\"foo\" => 1}"
assert_format "{ 1 => 2 ,\n\n 3 => 4 }", "{1 => 2,\n\n 3 => 4}"
assert_format "foo({\nbar: 1,\n})", "foo({\n bar: 1,\n})"
assert_format "{ # foo\n 1 => 2,\n}"

assert_format "Foo"
assert_format "Foo:: Bar", "Foo::Bar"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,8 @@ module Crystal
found_comment = false
found_first_newline = false

skip_space
if @token.type == :NEWLINE
found_comment = skip_space
if found_comment || @token.type == :NEWLINE
# add one level of indentation for contents if a newline is present
offset = @indent + 2

Expand Down