diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 185f8ec83e4e..a5b03dfe1f34 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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}" @@ -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" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 872a48fdb2a0..569f2594908d 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -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