Skip to content

Commit

Permalink
Improve multiline formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
i-tsvetkov authored and elia committed Jul 7, 2023
1 parent ac4fe11 commit 9645bf3
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 108 deletions.
44 changes: 25 additions & 19 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
require 'syntax_tree'

class ERB::Formatter
module SyntaxTreeCommandPatch
def format(q)
q.group do
q.format(message)
q.text(" ")
q.format(arguments) # WAS: q.nest(message.value.length + 1) { q.format(arguments) }
end
end
end

autoload :IgnoreList, 'erb/formatter/ignore_list'

class Error < StandardError; end
Expand Down Expand Up @@ -177,10 +167,14 @@ def raise(message)

def indented(string, strip: true)
string = string.strip if strip
indent = " " * tag_stack.size
indent = "\s" * indentation_width
"\n#{indent}#{string}"
end

def indentation_width
2 * tag_stack.size
end

def format_text(text)
p format_text: text if @debug
return unless text
Expand Down Expand Up @@ -217,26 +211,37 @@ def format_text(text)
end
end

def format_ruby(code, autoclose: false)
def format_ruby(code, autoclose: false, extra_indent: 0)
if autoclose
code += "\nend" unless RUBY_OPEN_BLOCK["#{code}\nend"]
code += "\n}" unless RUBY_OPEN_BLOCK["#{code}\n}"]
end
p RUBY_IN_: code if @debug

SyntaxTree::Command.prepend SyntaxTreeCommandPatch
p RUBY_IN_: code if @debug

code = begin
SyntaxTree.format(code, @line_width)
SyntaxTree.format(
code,
@line_width - (indentation_width + extra_indent)
)
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
lines = code.lines(chomp: true)
lines.delete_at(-1) if autoclose

indent = "\s" * extra_indent

code = lines.reduce(String.new) do |string, line|
string << indented("#{indent}#{line}", strip: false)
end

code.lstrip!

p RUBY_OUT: code if @debug

code
end

Expand Down Expand Up @@ -274,11 +279,12 @@ def format_erb_tags(string)
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code)
when RUBY_OPEN_BLOCK
ruby_code = format_ruby(ruby_code, autoclose: true, extra_indent: erb_open.size)
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)
else
ruby_code = format_ruby(ruby_code, autoclose: false)
ruby_code = format_ruby(ruby_code, autoclose: false, extra_indent: erb_open.size)
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
end
Expand Down
9 changes: 5 additions & 4 deletions test/erb/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def test_fixtures
Dir["#{__dir__}/../fixtures/*.html.erb"].each do |erb_path|
expected_path = erb_path.chomp('.erb') + '.expected.erb'

# File.write expected_path, ERB::Formatter.format(File.read(erb_path))
File.write(expected_path, ERB::Formatter.format(File.read(erb_path))) if ENV.key?("UPDATE_TEST_FIXTURES")

assert_equal(File.read(expected_path), ERB::Formatter.format(File.read(erb_path)), "Formatting of #{erb_path} failed")
end
end
Expand Down Expand Up @@ -127,9 +128,9 @@ def test_format_ruby
assert_equal(
"<div>\n" \
" <%= render MyComponent.new(\n" \
" foo: barbarbarbarbarbarbarbar,\n" \
" bar: bazbazbazbazbazbazbazbaz\n" \
" ) %>\n" \
" foo: barbarbarbarbarbarbarbar,\n" \
" bar: bazbazbazbazbazbazbazbaz\n" \
" ) %>\n" \
"</div>\n",
ERB::Formatter.format("<div> <%=render MyComponent.new(foo:barbarbarbarbarbarbarbar,bar:bazbazbazbazbazbazbazbaz)%> </div>"),
)
Expand Down
86 changes: 43 additions & 43 deletions test/fixtures/comments.html.expected.erb
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
<%#
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%#
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%#
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%# This fails
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%# This fails
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%# This fails
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%#This fails
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%# This fails
This fails
hey
hey
hey
hey %>
This fails
hey
hey
hey
hey %>

<%#
hey %>
hey %>

<%#
hey %>
hey %>

<%#
hey %>
hey %>
8 changes: 4 additions & 4 deletions test/fixtures/complex_case_when.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<span><%= t(".payment.stripe_invoice") %></span>
<% else %>
<% Rails.logger.error.report(
StandardError.new(
"No human readable name found for payment method #{payment_method.class}"
)
) %>
StandardError.new(
"No human readable name found for payment method #{payment_method.class}"
)
) %>
<% end %>
<% else %>
<%= t(".payment.no_payment_method_found") %>
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/formatted-2.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
<%= react_component(
"HelloWorld",
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." }
) %>
"HelloWorld",
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." }
) %>

</div>
10 changes: 5 additions & 5 deletions test/fixtures/formatted.html.expected.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% link_to "Very long string here and there",
very_very_very_long_long_long_pathhhhhh_here,
opt: "212",
options: "222sdasdasd",
class: " 322 ",
dis: diss %>
very_very_very_long_long_long_pathhhhhh_here,
opt: "212",
options: "222sdasdasd",
class: " 322 ",
dis: diss %>
16 changes: 8 additions & 8 deletions test/fixtures/if_then_else.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<% eeee ? a : c %>

<% if longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
a
else
c
end %>
a
else
c
end %>

<% if longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
"a"
else
"c"
end %>
"a"
else
"c"
end %>

<div <% if eeee then "b" else c end %>></div>
<div <% if eeee then a else c end %>></div>
Expand Down
28 changes: 16 additions & 12 deletions test/fixtures/long_deep_nested-2.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
<div>
<div>
<%= react_component(
"HelloWorld",
{
greeting:
"Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
},
{
greeting:
"Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
},
{ greeting: "Hello from react-rails." },
{ greeting: "Hello from react-rails." }
) %>
"HelloWorld",
{
greeting:
"Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
},
{
greeting:
"Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
},
{
greeting: "Hello from react-rails."
},
{
greeting: "Hello from react-rails."
}
) %>
</div>
</div>
</div>
Expand Down
19 changes: 12 additions & 7 deletions test/fixtures/long_deep_nested.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@
<div>
<div>
<% link_to "Very long long long long long long long long string here and there",
very_very_very_long_long_long_pathhhhhh_here,
opt: "212",
options: "222sdasdasd",
class: " 322 ",
dis: diss %>
very_very_very_long_long_long_pathhhhhh_here,
opt: "212",
options: "222sdasdasd",
class: " 322 ",
dis: diss %>

<% link_to "string", path, opt: "212", options: "222sdasdasd" %>
<% link_to "string",
path,
opt: "212",
options: "222sdasdasd" %>

<div>
<div>
<div>
<div>
<%= react_component({ greeting: "react-rails." }) %>
<%= react_component(
{ greeting: "react-rails." }
) %>
</div>
</div>
</div>
Expand Down

0 comments on commit 9645bf3

Please sign in to comment.