Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,17 @@ def format_ruby(code, autoclose: false)
SyntaxTree::Command.prepend SyntaxTreeCommandPatch

code = begin
SyntaxTree.format(code, @line_width)
width = @line_width - tag_stack.size * 2
SyntaxTree.format(code, width)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: this fixes part of #55

rescue SyntaxTree::Parser::ParseError => error
p RUBY_PARSE_ERROR: error if @debug
code
end

lines = code.strip.lines
lines = lines[0...-1] if autoclose
code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join
code.strip! if lines.one?
p RUBY_OUT: code if @debug
code
end
Expand Down Expand Up @@ -306,29 +308,27 @@ def format_erb_tags(string)
erb_open, ruby_code, erb_close = ERB_TAG.match(erb_code).captures
erb_open << ' ' unless ruby_code.start_with?('#')

case ruby_code
when RUBY_STANDALONE_BLOCK
block_type = case ruby_code
when RUBY_STANDALONE_BLOCK then :standalone
when RUBY_CLOSE_BLOCK then :close
when RUBY_REOPEN_BLOCK then :reopen
when RUBY_OPEN_BLOCK then :open
else :other
end

if %i[standalone other].include? block_type
ruby_code = format_ruby(ruby_code, autoclose: false)
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
when RUBY_CLOSE_BLOCK
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
tag_stack_pop('%erb%', ruby_code)
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
when RUBY_REOPEN_BLOCK
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
tag_stack_pop('%erb%', ruby_code)
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code)
when RUBY_OPEN_BLOCK
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code)
end

if ruby_code.include?("\n")
full_erb_tag = "#{erb_open}#{ruby_code.gsub(/^/, ' ')}#{indented(erb_close)}"
else
ruby_code = format_ruby(ruby_code, autoclose: false)
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
end

tag_stack_pop('%erb%', ruby_code) if %i[close reopen].include? block_type
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code) if %i[reopen open].include? block_type
else
p ERB_REST: erb_scanner.rest if @debug
rest = erb_scanner.rest.to_s
Expand Down