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

Formatter: Add more whitespace around ProcLiterals #14209

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
8 changes: 8 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,14 @@ describe Crystal::Formatter do
assert_format "->( x : Int32 , y ) { x }", "->(x : Int32, y) { x }"
assert_format "->{}"

# #13232
assert_format "->{}", "-> { }", flags: %w[proc_literal_whitespace]
assert_format "->(){}", "-> { }", flags: %w[proc_literal_whitespace]
assert_format "->{1}", "-> { 1 }", flags: %w[proc_literal_whitespace]
assert_format "->(x : Int32) {}", "->(x : Int32) { }", flags: %w[proc_literal_whitespace]
assert_format "-> : Int32 {}", "-> : Int32 { }", flags: %w[proc_literal_whitespace]
assert_format "->do\nend", "-> do\nend", flags: %w[proc_literal_whitespace]

assert_format "-> : Int32 {}"
assert_format "-> : Int32 | String { 1 }"
assert_format "-> : Array(Int32) {}"
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4256,14 +4256,15 @@ module Crystal
skip_space_or_newline
end

write " " unless a_def.args.empty? && !return_type
write " " if a_def.args.present? || return_type || flag?("proc_literal_whitespace")

is_do = false
if @token.keyword?(:do)
write_keyword :do
is_do = true
else
write_token :OP_LCURLY
write " " if a_def.body.is_a?(Nop) && flag?("proc_literal_whitespace")
end
skip_space

Expand Down