davidwessman / syntax_tree-erb

Syntax Tree support for ERB
MIT License
21 stars 3 forks source link

Support for more syntax and better multiline ERB formatting #92

Closed spect88 closed 1 week ago

spect88 commented 2 months ago

Hi,

First of all, thanks for building syntax_tree-erb! It was the closest to what I was looking for, so I forked it and made a few changes. I think you may agree with most (or all?) of them, so I'm letting you know what I did here.

My code is slightly hacky in places and I don't fully know what I'm doing, so do let me know if you'd like me to clean something up.

Multiline ERB formatting

Before my change multiline ERB would be formatted more or less like this:

<%= with_brackets(
  argument1,
  argument2,
  argument3
) %>
<%= without_brackets argument1,
                 argument2,
                 argument3 %>

I think whether the version with brackets looks good is subjective, but I have to assume the version without brackets wasn't intended - the alignment is off by the length of <%= prefix.

One way to improve it may be to use the erb plugin together with the no_alignment plugin, which will give something like this:

<%= without_brackets argument1,
  argument2,
  argument3 %>

That still doesn't look great though.

What my branch (this PR) does is the following:

<%=
  with_brackets(
    argument1,
    argument2,
    argument3
  )
%>
<%=
  without_brackets argument1,
                   argument2,
                   argument3
%>

It adds more linebreaks, but I much prefer it. I think it could be a configuration option if you (or other users) prefer the more condensed version.

ERB inside HTML tags

Previous state:

<%# supported %>
<tag <% something %> />
<tag attr="<%= something %>" />
<tag attr="<% if a %>b<% end %>" />

<%# unsupported %>
<tag <%= something %> />
<tag <% if a %>b="c"<% end %> />

In my branch all of these cases are supported.

when 1, 2

Previous state:

<%# supported %>
<% case foo %>
<% when 1 %>foo
<% end %>

<%# unsupported %>
<% case foo %>
<% when 1, 2 %>foo
<% end %>

In my branch comma-separated arguments are also supported.

Formatting of keywords in ERB blocks

Previously code like below was possible

<%
if
  some_method(argument1, argument2, argument3)
%>
foo
<% end %>

In my branch keywords are formatted together with the rest of the Ruby code, so it looks better:

<%
  if some_method(argument1, argument2, argument3)
%>
foo
<% end %>

And that's it for now.

Let me know what you think.

davidwessman commented 2 months ago

Could you see why the tests are failing?

davidwessman commented 2 months ago

I am on vacation, so I am not by my computer as much.