Skip to content

Commit

Permalink
Handle single node MacroExpressions
Browse files Browse the repository at this point in the history
some minor cleanup
  • Loading branch information
Blacksmoke16 committed Dec 23, 2024
1 parent 79641c7 commit 86e8959
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
58 changes: 56 additions & 2 deletions spec/compiler/parser/to_s_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
{%
Expand All @@ -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 %}
{%
Expand All @@ -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 %}
Expand All @@ -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) ::))
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ? "%}" : " %}")
Expand Down

0 comments on commit 86e8959

Please sign in to comment.