Skip to content

Commit

Permalink
Format: insert space between { and % to prevent writing {% (#5278)
Browse files Browse the repository at this point in the history
* Format: insert space between `{` and `%` to prevent writing `{%`

Fixed #5277

I think it is the best solution because it treats same thing against `{{` already.

* Join `{{` and `{%` conditions

Thank you @straight-shoota and @Sija
  • Loading branch information
makenowjust authored and Martin Verzilli committed Nov 24, 2017
1 parent 1b669c1 commit 24ee423
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -1007,6 +1007,8 @@ describe Crystal::Formatter do
assert_format "{ {{FOO}}, nil}", "{ {{FOO}}, nil }"
assert_format "{ {% begin %}1{% end %}, nil }"
assert_format "{ {% for x in 1..2 %}3{% end %}, nil }"
assert_format "{ %() }"
assert_format "{ %w() }"

assert_format "String?"
assert_format "String???"
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -762,19 +762,20 @@ module Crystal
end

elements.each_with_index do |element, i|
# This is to prevent writing `{{`
current_element = element
if current_element.is_a?(HashLiteral::Entry)
current_element = current_element.key
end

# This is to prevent writing `{{` and `{%`
if prefix == :"{" && i == 0 && !wrote_newline && (
current_element.is_a?(TupleLiteral) ||
current_element.is_a?(NamedTupleLiteral) ||
current_element.is_a?(HashLiteral) ||
current_element.is_a?(MacroExpression) ||
current_element.is_a?(MacroIf) ||
current_element.is_a?(MacroFor)
current_element.is_a?(MacroFor) ||
@token.raw.starts_with?('%')
)
write " "
write_space_at_end = true
Expand Down

0 comments on commit 24ee423

Please sign in to comment.