Skip to content

Commit

Permalink
Formatter: Add more whitespace around ProcLiterals (#14209)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Jan 12, 2024
1 parent e814029 commit db8ecef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit db8ecef

Please sign in to comment.