Rails and .html.erb files indentation

Bug #579581 reported by lborgman
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
nXhtml
Fix Committed
Medium
Unassigned

Bug Description

Thanks Perry. This looks like a bug to me so I am converting your question to a bug report. This makes some things more easy. Here is what you wrote.

This is probably more of a mumamo question. In Rails' view files, there are things like:

<p style='width: 40em;'>
  Enter a defect, APAR, PTF, Lpp name (e.g. bos.rte) or Fileset
  (e.g. bos.rte 5.3.0.30 -- with the space. The VRMF can be
  incomplete such as 5.3.8)
  <% form_tag swinfos_form_path do -%>
    <%= text_field_tag "item" %>
    <%= submit_tag nil, :class => "concat-fields" %>
  <% end -%>
</p>

The <% ruby code... do %> starts a new indent level. With my current set up, the ruby code is recognized but if I hit tab on the two lines that start with <%=, the first one is indented by 4 and the second is indented by 2. In other places in this file, the same basic thing indents the first by 2 and the second by 0. So, it isn't really understanding things.

I plan to play around with this but thought I'd post here to see if anyone has solved this yet. Surfing the net didn't find any real solutions.

Thanks,
Perry

Changed in nxhtml:
importance: Undecided → Medium
status: New → In Progress
Revision history for this message
Perry Smith (pedz) wrote :

I just noticed that just before the text I pasted I have an <h3>foo</h3> tag which is not being recognized. So, I may not have my stuff set up right. The <p> tag is also brighter so it is not recognized either.

Revision history for this message
lborgman (lennart-borgman) wrote :

The multi major mode you are supposed to use here is `eruby-html-mumamo'.

I do not think I have added a file extension for it. What file extension do you believe normally is used for this kind of files?

Revision history for this message
Perry Smith (pedz) wrote :

Multiple updates:

1) I've added (add-to-list 'auto-mode-alist '("\\.html\\.erb\\'" . eruby-nxhtml-mumamo-mode))

(which I got from the web). The .html.erb is very common for Rails people. There appears to be a historical extension of .rhtml but I think it is not used much any more.

2) From your email:

It works like this:

- The code between <% and %> is extracted into a new buffer.
- That buffer is indented by ruby-mode.
- The relative indentations are then used in the original buffer.

You can find the new buffer. It has a name ending with
"-template-indent-buffer".

Can you please look in that buffer for your example and tell me what
is wrong? Because there might be other readers (now or later) I think
the best is if you enter your answer in the bug report.
<EOM>

If I have the -%>, then the buffer contains the - and that is wrong. That might be part of the problem.

A side issue (which might disappear), if I add the - and hit tab, I get an error that the overlay is not found:

  goto-char(nil)
  mumamo-indent-indentor(#<overlay in no buffer>)
  (let ((i-str ...) (i-beg ...) (c-str ...) (p-str ...) (c-beg ...) (p-beg ...)) (when indentor (unless ... ...)) (when prev-indentor (unless ... ...)) (unless indentor (setq i-beg ...) (goto-char i-beg) (setq indentor ...)) (unless prev-indentor (when prev-indentor-chunk ... ... ...)) (when prev-indentor (mumamo-indent-indentor prev-indentor)) (mumamo-indent-indentor indentor) (let (prev-ind this-ind next-ind shift-in shift-out) (when prev-indentor ... ...) (goto-char ...) (setq this-ind ...) (goto-char ...) (setq next-ind ...) (when prev-ind ...) (setq shift-out ...) (msgtrc "template-indent-get-shunk-shift => (%s . %s)" shift-in shift-out) (cons shift-in shift-out)))
 ...

The same also happens when I first remove the -. (I hit q from the debugger and hit tab again and the problem is gone. The template buffer changes to match.

Part of my current finding is the same code acts differently. I can't figure out why. I'm wondering if maybe its the fact that the <h3> and <p> tags are not being recognized.

The top of the file does not start off with a doctype, it just starts with:

<!-- app/views/welcome/index.html.erb -->
<h1>Welcome to Condor</h1>
<h3>Swinfo</h3>

This is common for erb files. They are just small snippets of html + ruby code.

Revision history for this message
lborgman (lennart-borgman) wrote :

Thanks. I have just added the extension ".erb.html" as you suggested.

I think I have taken care of the - in -%> too.

However the bug you see might still be there, I am not sure. If you see it again, please report it (better as a new bug).

Can you please try the new nXhtml beta 2.09 I just uploaded?

Revision history for this message
lborgman (lennart-borgman) wrote :

Since the indentation problem seems taken care of I am starting to close this bug.

Changed in nxhtml:
status: In Progress → Fix Committed
Revision history for this message
lborgman (lennart-borgman) wrote :

Ah, forgot one thing.

Isn't there chunks beginning with <%- too? How should they be handled?

Revision history for this message
Perry Smith (pedz) wrote :

From the erb documentation:

       <% Ruby code -- inline with output %>
       <%= Ruby expression -- replace with result %>
       <%# comment -- ignored -- useful in testing %>
       % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
       %% replaced with % if first thing on a line and % processing is used
       <%% or %%> -- replace with <% or %> respectively

The 4th through 6th items I've never used.

From the Rails source code:

  # <%- and -%> suppress leading and trailing whitespace, including the trailing newline, and can be used interchangeably with <% and %>.

I would treat <%- the same as <% but do not put the - in the template buffer.

Revision history for this message
lborgman (lennart-borgman) wrote :

Thanks Perry,

I will put in the <%- thing.

How about the double %%? Is that something that is used? And can it include "-" (<%%- ... -%%>)?

Revision history for this message
Perry Smith (pedz) wrote :

The way that ERB.new is called changes how it works. I searched the Rails code to see if I could find how it was called and it seemed fairly open as if a user could cause Rails (and ERB) to either accept the % or not. Plus, ERB can be used from other packages.

So, to really do this right, you would need a flag (like a buffer local variable) to change which patterns are accepted or not. And then add a customized variable so the default of the buffer local variable could be customized. But I think that is more work than it is worth. I don't think anyone uses the 4th through 6th items.

Perhaps add a comment in your code for the %, %%, and <%% constructs and then see if anyone asks for it. I really doubt it will come up.

Changed in nxhtml:
status: Fix Committed → In Progress
Revision history for this message
lborgman (lennart-borgman) wrote :

Hi Perry,

I have added a comment to `eruy-html-mumamo-mode' about which chunk dividers are support and a link to this bug report.

I think the original bug you reported here is fixed so I am changing the status here to "Fix Committed".

If you see more problems please submit a new bug report. Thanks for your help.

Changed in nxhtml:
status: In Progress → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

Bug watches keep track of this bug in other bug trackers.