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 to place only a newline between call-comment-newline and def #4690

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -859,6 +859,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
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 @@ -4228,6 +4230,7 @@ module Crystal

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

skip_space_or_newline
Expand Down Expand Up @@ -4305,6 +4308,7 @@ module Crystal
end

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

Expand Down