Skip to content

Commit cd4c132

Browse files
makenowjustasterite
authored andcommittedSep 8, 2017
Format: fix to place only a newline between call-comment-newline and def
Fixed #4672
1 parent 3fbdde5 commit cd4c132

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
 

‎spec/compiler/formatter/formatter_spec.cr

+2
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ describe Crystal::Formatter do
883883
assert_format "foo.[1]"
884884

885885
assert_format "@foo : Int32 # comment\n\ndef foo\nend"
886+
assert_format "getter foo # comment\n\ndef foo\nend"
887+
assert_format "getter foo : Int32 # comment\n\ndef foo\nend"
886888

887889
assert_format "a &.b.as C", "a &.b.as C"
888890
assert_format "a &.b.c.as C", "a &.b.c.as C"

‎src/compiler/crystal/tools/formatter.cr

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module Crystal
5454
@output : IO::Memory
5555
@line_output : IO::Memory
5656
@wrote_newline : Bool
57+
@wrote_double_newlines : Bool
5758
@wrote_comment : Bool
5859
@macro_state : Token::MacroState
5960
@inside_macro : Int32
@@ -91,6 +92,7 @@ module Crystal
9192
@output = IO::Memory.new(source.bytesize)
9293
@line_output = IO::Memory.new
9394
@wrote_newline = false
95+
@wrote_double_newlines = false
9496
@wrote_comment = false
9597
@macro_state = Token::MacroState.default
9698
@inside_macro = 0
@@ -257,7 +259,7 @@ module Crystal
257259
unless found_comment
258260
skip_space_write_line
259261
found_comment = skip_space_or_newline last: true, at_least_one: true
260-
write_line unless found_comment
262+
write_line unless found_comment || @wrote_double_newlines
261263
end
262264
else
263265
consume_newlines
@@ -4275,6 +4277,7 @@ module Crystal
42754277

42764278
if @token.type == :NEWLINE
42774279
write_line
4280+
@wrote_double_newlines = true
42784281
end
42794282

42804283
skip_space_or_newline
@@ -4352,6 +4355,7 @@ module Crystal
43524355
end
43534356

43544357
def write_line
4358+
@wrote_double_newlines = false
43554359
@current_doc_comment = nil unless @wrote_comment
43564360
@wrote_comment = false
43574361

0 commit comments

Comments
 (0)
Please sign in to comment.