Indenting ERB templates as HTML

Why does RubyMine apply Ruby indentation rules to HTML tags in an ERB template?

Is there a way to change this?

It's very annoying.

13 comments
Comment actions Permalink

Could you please provide the situation where the indentation is incorrectly used?

Regards, Oleg.

0
Comment actions Permalink

Here's an example:

<%= form_for(@user) do |f| %>
    <% if @user.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@user.errors.count, "error") %>
            prohibited this user from being saved:</h2>
          <ul>
            <% @user.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>


    <div >
      <%= f.label :email %>
      <br/>
      <%= f.text_field :email %>
    </div>
    <div >
      <%= f.label :password %>
      <br/>
      <%= f.password_field :password %>
    </div>
    <div >
      <%= f.label :password_confirmation %>
      <br/>
      <%= f.password_field :password_confirmation %>
    </div>
    <div >
      <%= f.submit %>
    </div>
<% end %>



Notice how RubyMine formats the code with 2 and 4 spaces of indentation. This is not consistent and very annoying. I tried changing the indentation in Settings, but there isn't a separate tab for ERB files.

I am using RubyMine 5.4.3.2.1 on Mac OS X.

Joseph

0
Comment actions Permalink

You can try to change xml code style settings for ERB formatting

Oleg.

0
Comment actions Permalink

Can you show me how to get to that option? I searched for erb, and the only option that showed up is:

Colors & Fonts.

0
Comment actions Permalink

Hi,

I meant that ERB uses XML code style settings, so you can change them.

Regards, Oleg.

0
Comment actions Permalink

Nope. That didn't work. See screenshot below.

Could you test out your solution before you suggest it? It's getting annoying.

Screen Shot 2013-10-01 at 10.48.41 PM.png

0
Comment actions Permalink

Hmm, it looks like you do not like indentation of <li> in
<ul>
  <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
  <% end %>
</ul>

Am I right?
If so, there is no way to fix it :( And it looks like a bug for me.  So, please file it in our tracker (http://youtrack.jetbrains.com/issues/RUBY).

Thanks, Oleg.
0
Comment actions Permalink

Actually, the indentation is inconsistent. It's 4 spaces, then 2 spaces. Sometimes, the HTML elements are indented 2 spaces. Other times, they are indented 4 spaces. Some ERB script elements are indented 2 spaces. Some are indented 4 spaces. There is no logic to the indentation. I think it's a bug.

Here's the original file:

<%= form_for(@user) do |f| %>
    <% if @user.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@user.errors.count, "error") %>
            prohibited this user from being saved:</h2>
          <ul>
            <% @user.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>


    <div >
      <%= f.label :email %>
      <br/>
      <%= f.text_field :email %>
    </div>
    <div >
      <%= f.label :password %>
      <br/>
      <%= f.password_field :password %>
    </div>
    <div >
      <%= f.label :password_confirmation %>
      <br/>
      <%= f.password_field :password_confirmation %>
    </div>
    <div >
      <%= f.submit %>
    </div>
<% end %>


0
Comment actions Permalink

I can confirm I have the same issue. My team style is 2 spaces everywhere, yet CMD-SHIFT-L or TAB gets me 4 (even with the XML suggestion). I have to manually indent with the space bar and try to avoid formatting code when I work with ERB. It's not just annoying, it attracts criticism from the team (who already think less of me for using an IDE rather than a text editor!). When working with views I switch to Sublime Text or vim - Rubymine is not up to it right now, sadly.

0
Comment actions Permalink

I am also unable to use 2 space indentation in ERB (.erb) files.

I tried changing the XML indentation settings, but it has no effect on ERB files for me.

Is there another work-around for this? Wouldn't it make more sense to have a dedicated settings section in Editor > Code Style > ERB?

I'm not the only person with this problem it seems:

https://youtrack.jetbrains.com/issue/RUBY-16183

0
Comment actions Permalink

Setting tab size and indent in Editor > Code Style > Other File Types fixed the issue for me.

6
Comment actions Permalink

@James Reidsmith, thanks for your replying. 

It's very helpful!!!

0

Please sign in to leave a comment.