views:

37

answers:

1

I'm wondering if there's an existent solution to the following problem:

I have the following code:

<div>
  <div>
<div><%= {:something => 'abc',
          :else => 'abc',
          :nice => 'ok'} %>
</div>
</div>
</div>

As you can see it's unested and hard to read. I was wondering if there's an existent tool preferably in ruby which could reindent the document (no modifications or validations to the source). Just reindent.

The expected result would be:

<div>
  <div>
    <div>
      <%= {:something => 'abc',
           :else => 'abc',
           :nice => 'ok'} %>
    </div>
  </div>
</div>

Also, on a side not I want to implement this as a command on Textmate.

A: 

Implementing in Textmate? You do know about ⌘⌥[ i.e. Cmd+Alt+[ (or choose it from the application menu with Text > Indent Selection)?

It's not exactly what you try to achieve in your example, but perhaps it's close enough. This is the output when applying it on your code in Textmate:

<div>
  <div>
    <div><%= {:something => 'abc',
      :else => 'abc',
      :nice => 'ok'} %>
    </div>
  </div>
</div>

It works in any Textmate bundle that supports indentation.

molf
Doesn't ⌘⌥[ just let you indent the selected text? to that result you just selected the whole chunk and executed that command? or did it manually?
kuroir
Textmate did the indentation automatically, I started with the non-indented version that you posted. Just select all (⌘a) and press ⌘⌥[ to auto-indent it. Make sure you have the correct bundle selected, i.e. "HTML (Rails)" in this case.
molf
I don't seem to be able to achieve this. Since ⌘⌥[ does not that for me. Is there any chance you can tell me the name of the command? ⌘⌃T and then on the search icon select key equivalent and use the combo you mentioned.
kuroir
From the menu: *Text* > *Indent Selection* (make sure you have something selected).
molf