From 86e895986b23c2f387e61a6c7de0f9da52a3d32a Mon Sep 17 00:00:00 2001 From: George Dietrich Date: Mon, 23 Dec 2024 12:59:00 -0500 Subject: [PATCH] Handle single node MacroExpressions some minor cleanup --- spec/compiler/parser/to_s_spec.cr | 58 ++++++++++++++++++++++++++++- src/compiler/crystal/syntax/to_s.cr | 8 ++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/spec/compiler/parser/to_s_spec.cr b/spec/compiler/parser/to_s_spec.cr index 3ea28be4e46e..d6d2ca79780d 100644 --- a/spec/compiler/parser/to_s_spec.cr +++ b/spec/compiler/parser/to_s_spec.cr @@ -262,7 +262,7 @@ describe "ASTNode#to_s" do expect_to_s "{%\n a = 1 %}", "{%\n a = 1\n%}" expect_to_s "{% a = 1\n%}", "{% a = 1 %}" - expect_to_s <<-'CR', <<-CR + expect_to_s <<-'CR', <<-'CR' macro finished {% verbatim do %} {% @@ -288,7 +288,7 @@ describe "ASTNode#to_s" do end CR - expect_to_s <<-'CR', <<-CR + expect_to_s <<-'CR', <<-'CR' macro finished {% verbatim do %} {% @@ -312,6 +312,50 @@ describe "ASTNode#to_s" do end CR + expect_to_s <<-'CR', <<-'CR' + macro finished + {% verbatim do %} + {% + 10 + + # Foo + + 20 + 30 + + # Bar + + 40 + %} + {% + 50 + 60 + %} + {% end %} + end + CR + macro finished + {% verbatim do %} + {% + 10 + + + + 20 + 30 + + + + 40 + %} + {% + 50 + 60 + %} + {% end %} + end + CR + expect_to_s <<-'CR' macro finished {% verbatim do %} @@ -323,6 +367,16 @@ describe "ASTNode#to_s" do end CR + expect_to_s <<-'CR' + macro finished + {% verbatim do %} + {% + 10 + %} + {% end %} + end + CR + expect_to_s %(asm("nop" ::::)) expect_to_s %(asm("nop" : "a"(1), "b"(2) : "c"(3), "d"(4) : "e", "f" : "volatile", "alignstack", "intel")) expect_to_s %(asm("nop" :: "c"(3), "d"(4) ::)) diff --git a/src/compiler/crystal/syntax/to_s.cr b/src/compiler/crystal/syntax/to_s.cr index 5d8d28820c68..36c34cb045bb 100644 --- a/src/compiler/crystal/syntax/to_s.cr +++ b/src/compiler/crystal/syntax/to_s.cr @@ -55,14 +55,14 @@ module Crystal true end - private def add_whitespace_around(first_node_location : Location?, second_node_location : Location?, &) : Nil + private def write_extra_newlines(first_node_location : Location?, second_node_location : Location?, &) : Nil # If any location information is missing, don't add any extra newlines. if !first_node_location || !second_node_location yield return end - # Only emit extra newlines. I.e. those more than the first handled via the Expressions visitor. + # Only write the "extra" newlines. I.e. If there are more than one. The first newline is handled directly via the Expressions visitor. ((second_node_location.line_number - 1) - first_node_location.line_number).times do newline end @@ -240,7 +240,7 @@ module Crystal node.expressions.each_with_index do |exp, i| unless exp.nop? - self.add_whitespace_around last_node.end_location, exp.location do + self.write_extra_newlines last_node.end_location, exp.location do append_indent unless node.keyword.paren? && i == 0 exp.accept self newline unless node.keyword.paren? && i == node.expressions.size - 1 @@ -774,8 +774,8 @@ module Crystal # If the opening tag has a newline after it, force trailing tag to have one as well if is_multiline @indent -= 1 - append_indent newline if !node.exp.is_a? Expressions + append_indent end @str << (node.output? ? " }}" : is_multiline ? "%}" : " %}")