From fa6c8cc6b3c150e76013460af96c0793b5931878 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Thu, 11 Jan 2024 01:14:29 +0800 Subject: [PATCH] Formatter: Add more whitespace around `ProcLiteral`s --- spec/compiler/formatter/formatter_spec.cr | 8 ++++++++ src/compiler/crystal/tools/formatter.cr | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 4567638f0fd8..ce2c7e72364c 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -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) {}" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index fc3be7a4cf66..a18634fe8232 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -4256,7 +4256,7 @@ 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) @@ -4264,6 +4264,7 @@ module Crystal is_do = true else write_token :OP_LCURLY + write " " if a_def.body.is_a?(Nop) && flag?("proc_literal_whitespace") end skip_space