From 7de7869b4212b06cb76298ccf6f25adb94f29a1f Mon Sep 17 00:00:00 2001 From: Martin Verzilli Date: Fri, 7 Feb 2025 16:42:03 +0100 Subject: [PATCH 1/4] Format assign right hand side if block when left hand side includes type declaration --- spec/compiler/formatter/formatter_spec.cr | 1 + src/compiler/crystal/tools/formatter.cr | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 0a7695f4ead6..1d37b2a3e1fd 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -1116,6 +1116,7 @@ describe Crystal::Formatter do assert_format "a = # foo\n bar(1)" assert_format "a = \\\n # foo\n bar(1)" assert_format "a = \\\n # foo\n nil" + assert_format "a : String = if 1\n\"One\"\nelse\"Zero\"\nend", "a : String = if 1\n \"One\"\n else\n \"Zero\"\n end" assert_format %(require "foo"), %(require "foo") diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 7ea32627078e..68a103f7a1fe 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -3650,8 +3650,8 @@ module Crystal skip_space check :OP_EQ next_token_skip_space_or_newline - write " = " - accept value + write " =" + accept_assign_value_after_equals value end false end From bd49efcebf38594d527e6337adda3def88aac1ac Mon Sep 17 00:00:00 2001 From: Martin Verzilli Date: Fri, 7 Feb 2025 16:47:38 +0100 Subject: [PATCH 2/4] Add formatter case for case as right hand side of assign with type declaration --- spec/compiler/formatter/formatter_spec.cr | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 1d37b2a3e1fd..ed638285a421 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -1117,6 +1117,7 @@ describe Crystal::Formatter do assert_format "a = \\\n # foo\n bar(1)" assert_format "a = \\\n # foo\n nil" assert_format "a : String = if 1\n\"One\"\nelse\"Zero\"\nend", "a : String = if 1\n \"One\"\n else\n \"Zero\"\n end" + assert_format "a : String = case 1\nwhen 2\n3\nend", "a : String = case 1\n when 2\n 3\n end" assert_format %(require "foo"), %(require "foo") From f89564925225dcae61f35f555775e9e393bb54c1 Mon Sep 17 00:00:00 2001 From: Martin Verzilli Date: Mon, 10 Feb 2025 11:39:46 +0100 Subject: [PATCH 3/4] Preserve newline --- spec/compiler/formatter/formatter_spec.cr | 1 + src/compiler/crystal/tools/formatter.cr | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index ed638285a421..92c568f088b7 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -1118,6 +1118,7 @@ describe Crystal::Formatter do assert_format "a = \\\n # foo\n nil" assert_format "a : String = if 1\n\"One\"\nelse\"Zero\"\nend", "a : String = if 1\n \"One\"\n else\n \"Zero\"\n end" assert_format "a : String = case 1\nwhen 2\n3\nend", "a : String = case 1\n when 2\n 3\n end" + assert_format "a : String = \nif 1\n1\nelse\n2\nend", "a : String =\n if 1\n 1\n else\n 2\n end" assert_format %(require "foo"), %(require "foo") diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 68a103f7a1fe..d6955c43e6e5 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -3649,7 +3649,7 @@ module Crystal if value = node.value skip_space check :OP_EQ - next_token_skip_space_or_newline + next_token_skip_space write " =" accept_assign_value_after_equals value end From 997f40b66981e7eeb4ca360e71c83e74f0e2c9b5 Mon Sep 17 00:00:00 2001 From: Martin Verzilli Date: Mon, 10 Feb 2025 15:04:21 +0100 Subject: [PATCH 4/4] Apply format --- lib/markd/src/markd/options.cr | 2 +- lib/markd/src/markd/renderers/html_renderer.cr | 2 +- lib/markd/src/markd/utils.cr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/markd/src/markd/options.cr b/lib/markd/src/markd/options.cr index 7605a261e939..10e87c29d031 100644 --- a/lib/markd/src/markd/options.cr +++ b/lib/markd/src/markd/options.cr @@ -50,7 +50,7 @@ module Markd @source_pos = false, @safe = false, @prettyprint = false, - @base_url = nil + @base_url = nil, ) end end diff --git a/lib/markd/src/markd/renderers/html_renderer.cr b/lib/markd/src/markd/renderers/html_renderer.cr index f1149fe58217..e91b44bad54b 100644 --- a/lib/markd/src/markd/renderers/html_renderer.cr +++ b/lib/markd/src/markd/renderers/html_renderer.cr @@ -222,7 +222,7 @@ module Markd @last_output = ">" end - private def tag(name : String, attrs = nil) + private def tag(name : String, attrs = nil, &) tag(name, attrs) yield tag(name, end_tag: true) diff --git a/lib/markd/src/markd/utils.cr b/lib/markd/src/markd/utils.cr index deb40668610e..b1871e08d8d7 100644 --- a/lib/markd/src/markd/utils.cr +++ b/lib/markd/src/markd/utils.cr @@ -2,7 +2,7 @@ require "json" module Markd module Utils - def self.timer(label : String, measure_time? : Bool) + def self.timer(label : String, measure_time? : Bool, &) return yield unless measure_time? start_time = Time.utc