Skip to content

Commit

Permalink
Format: fix to place only a newline between call-comment-newline and def
Browse files Browse the repository at this point in the history
Fixed #4672
  • Loading branch information
makenowjust authored and asterite committed Sep 8, 2017
1 parent 3fbdde5 commit cd4c132
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ describe Crystal::Formatter do
assert_format "foo.[1]"

assert_format "@foo : Int32 # comment\n\ndef foo\nend"
assert_format "getter foo # comment\n\ndef foo\nend"
assert_format "getter foo : Int32 # comment\n\ndef foo\nend"

assert_format "a &.b.as C", "a &.b.as C"
assert_format "a &.b.c.as C", "a &.b.c.as C"
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module Crystal
@output : IO::Memory
@line_output : IO::Memory
@wrote_newline : Bool
@wrote_double_newlines : Bool
@wrote_comment : Bool
@macro_state : Token::MacroState
@inside_macro : Int32
Expand Down Expand Up @@ -91,6 +92,7 @@ module Crystal
@output = IO::Memory.new(source.bytesize)
@line_output = IO::Memory.new
@wrote_newline = false
@wrote_double_newlines = false
@wrote_comment = false
@macro_state = Token::MacroState.default
@inside_macro = 0
Expand Down Expand Up @@ -257,7 +259,7 @@ module Crystal
unless found_comment
skip_space_write_line
found_comment = skip_space_or_newline last: true, at_least_one: true
write_line unless found_comment
write_line unless found_comment || @wrote_double_newlines
end
else
consume_newlines
Expand Down Expand Up @@ -4275,6 +4277,7 @@ module Crystal

if @token.type == :NEWLINE
write_line
@wrote_double_newlines = true
end

skip_space_or_newline
Expand Down Expand Up @@ -4352,6 +4355,7 @@ module Crystal
end

def write_line
@wrote_double_newlines = false
@current_doc_comment = nil unless @wrote_comment
@wrote_comment = false

Expand Down

0 comments on commit cd4c132

Please sign in to comment.