Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for more syntax and better multiline ERB formatting #92

Merged
merged 3 commits into from
Sep 14, 2024

Conversation

spect88
Copy link

@spect88 spect88 commented Jul 19, 2024

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.

- Fix alignment in multiline Ruby, at the cost of making the code less
  concise
- Don't break the line between "<% if" and the condition
- Support <% when 1, 2 %>

The most controversial change is

<%=
  long_command(
    ...
  )
%>

instead of

<%= long_command(
  ...
) %>
Copy link
Owner

@davidwessman davidwessman left a comment

Choose a reason for hiding this comment

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

15/18, looked through all the tests and I like the way it looks 🙂

Will look into the code changes as well, thanks for your contribution!

@davidwessman
Copy link
Owner

Could you see why the tests are failing?

@davidwessman
Copy link
Owner

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

Copy link
Owner

@davidwessman davidwessman left a comment

Choose a reason for hiding this comment

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

Sorry that it took ages, have not been having any time for open-source.
I think I like it, and the code looks nice and readable.

Thank you! 🙂

@davidwessman davidwessman merged commit e87e074 into davidwessman:main Sep 14, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants