Skip to content

Commit

Permalink
Fix formatting {% verbatim do %} ... {% end %} outside macro (#6667)
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and sdogruyol committed Sep 6, 2018
1 parent a350db8 commit 14f411e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,8 @@ describe Crystal::Formatter do
assert_format "class X\n annotation FooAnnotation \n end \n end", "class X\n annotation FooAnnotation\n end\nend"

assert_format "macro foo\n{% verbatim do %}1 + 2{% end %}\nend"
assert_format "{% verbatim do %}{{1}} + {{2}}{% end %}"
assert_format "foo({% verbatim do %}{{1}} + {{2}}{% end %})"

assert_format "{% foo <<-X\nbar\nX\n%}"
assert_format "foo do\n {% foo <<-X\n bar\n X\n %}\nend"
Expand Down
26 changes: 23 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,14 @@ module Crystal
end

def visit(node : MacroVerbatim)
check :MACRO_CONTROL_START
reset_macro_state

# `{% verbatim %}`
if inside_macro?
check :MACRO_CONTROL_START
else
check :"{%"
end
write "{%"
next_token_skip_space_or_newline
check_keyword :verbatim
Expand All @@ -1676,8 +1683,14 @@ module Crystal
next_token_skip_space
check :"%}"
write " %}"

@macro_state.control_nest += 1
check_macro_whitespace
next_macro_token
node.exp.accept self
inside_macro { no_indent node.exp }
@macro_state.control_nest -= 1

# `{% end %}`
check :MACRO_CONTROL_START
write "{%"
next_token_skip_space_or_newline
Expand All @@ -1686,7 +1699,14 @@ module Crystal
next_token_skip_space
check :"%}"
write " %}"
next_macro_token

if inside_macro?
check_macro_whitespace
next_macro_token
else
next_token
end

false
end

Expand Down

0 comments on commit 14f411e

Please sign in to comment.