Skip to content

Commit

Permalink
Formatter: don't format code fence when language is not crystal (#6424)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and RX14 committed Jul 23, 2018
1 parent 3817c7a commit d1ac793
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 16 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Expand Up @@ -1174,4 +1174,20 @@ describe Crystal::Formatter do
assert_format "begin\n 0[1] rescue 2 end", "begin 0[1] rescue 2 end"

assert_format "{%\n if 1\n 2\n end\n%}"

assert_format <<-CODE
# ```text
# 1 + 2
# ```
CODE

assert_format <<-CODE
# ```text
# 1 + 2
# ```
#
# ```
# 3 + 4
# ```
CODE
end
15 changes: 11 additions & 4 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -33,8 +33,9 @@ module Crystal
property start_line : Int32
property end_line : Int32
property needs_newline : Bool
property needs_format : Bool

def initialize(@start_line : Int32, @kind : Symbol)
def initialize(@start_line : Int32, @kind : Symbol, @needs_format : Bool)
@end_line = @start_line
@needs_newline = true
end
Expand Down Expand Up @@ -4378,12 +4379,18 @@ module Crystal
current_doc_comment = @current_doc_comment

if after_comment_value.starts_with?("```")
if current_doc_comment
# Determine code fence language (what comes after "```")
language = after_comment_value.lchop("```").strip

if current_doc_comment && language.empty?
# A code fence ends when nothing comes after "```"
current_doc_comment.end_line = @line - 1
@doc_comments << current_doc_comment
@doc_comments << current_doc_comment if current_doc_comment.needs_format
@current_doc_comment = nil
else
@current_doc_comment = CommentInfo.new(@line + 1, :backticks)
# We only format crystal code (empty by default means crystal)
needs_format = language.empty? || language == "crystal"
@current_doc_comment = CommentInfo.new(@line + 1, :backticks, needs_format)
end
end
end
Expand Down

0 comments on commit d1ac793

Please sign in to comment.