Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a Rails html.erb file in Komodo Edit 5 and the indentation has gone a bit wild.

Is there a plugin or function that automatically indents my code so it's easier to read?

share|improve this question
    
Code Formatting support, is actually built-in Komodo IDE. I also use plain Komodo Edit, and get kludge support from the macro. I updated the macro with Ruby Support, consider accepting my answer. –  TechZilla Mar 31 '12 at 0:30

4 Answers 4

I use this slightly edited version of other posted code. Variations have been floating around the Komodo Forums for some time. I've updated the macro for both Komodo Edit 7.0 and 6.X, it usually works well enough. I changed some of the tidy and csstidy options, added the XML support, and altered the undefined syntax alert. I also had to create a very ugly kludge to get astyle working, as astyle does not accept stdin. At this point the entire macro needs to be completely redone, as its limitations have become obvious.

As for Ruby Support, check out rbeautify, I have finally integrated support for Ruby, You must have rbeautify installed in your PATH. I must warn you, I don't have Ruby installed so I'm unable to fully test. I should also mention my JS is dreadful, but I did verify what I could and the macro worked. This should finally answer this question, it might be time to accept my answer.

Format_Syntax.js

komodo.assertMacroVersion(3);
if (komodo.view.scintilla) {
    komodo.view.scintilla.focus();
} // bug 67103
var koDoc = (komodo.koDoc === undefined ? komodo.document : komodo.koDoc);
var formatter;
var language = koDoc.language;
var cannot_tidy_selection = false;

switch (language) {
case 'C#':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=ansi --mode=cs --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'C++':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=linux --mode=c --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'CSS':
    formatter = 'csstidy - --preserve_css=true --lowercase_s=true --case_properties=true --sort_properties=true --remove_bslash=false --silent=true --template=medium';
    break;
case 'HTML':
    cannot_tidy_selection = true;
    formatter = 'tidy -q -asxhtml -i -b -c -w 120 --show-warnings no --show-errors 0 --tidy-mark no --css-prefix block --drop-proprietary-attributes yes --anchor-as-name no --enclose-text yes';
    break;
case 'Java':
    cannot_tidy_selection = true;
    formatter = 'astyle --style=java --mode=java --convert-tabs --indent=spaces=4 %F > /dev/null 2>&1; cat %F';
    break;
case 'Perl':
    formatter = 'perltidy';
    break;
case 'PHP':
    formatter = 'php_beautifier -s4 -l"Pear()"';
    break;
case 'Ruby':
    formatter = 'rbeautify.rb -';
    break;
case 'XSLT':
    cannot_tidy_selection = true;
    formatter = 'tidy -q -xml -i -w 120 --show-warnings no --show-errors 0 --tidy-mark no';
    break;
case 'XML':
    cannot_tidy_selection = true;
    formatter = 'xmllint --format --recover -';
    break;
default:
    alert("Syntax Undefined, Add Case to Macro " + language);
    return null;
}

// Save Curser Position
var currentPos = komodo.editor.currentPos;
try {
    // Save the file, Check Changes with "File -> Show Unsaved Changes"
    //komodo.doCommand('cmd_save');
    // Group operations in a single undo
    komodo.editor.beginUndoAction();
    // Select Buffer, pipe it into formatter.
    var text_not_selected = cannot_tidy_selection || komodo.editor.selText == "";
    if (text_not_selected) {
        komodo.doCommand('cmd_selectAll');
    }
    Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
    if (text_not_selected) {
        komodo.editor.gotoPos(currentPos);
    }
    // Restore Cursor Position
    komodo.editor.gotoPos(currentPos);
    // Clean Potential EOL Mismatches
    komodo.doCommand('cmd_cleanLineEndings');
}
catch (e) {
    alert(e);
}
finally {
    // End Undo Action to Avoid Edit Buffer Corruption
    // komodo.editor.endUndoAction();
    return true;
}
share|improve this answer

Not directly. However, the "Run Commands" system (and possibly the use of macros) can be used to help run an external script that will massage the contents of the current buffer. So, if you have a script that can do good .html.erb formatting then you should be able to integrate that.

Aside: Komodo IDE (the commercial relative of Komodo Edit) has a framework for integration code formatters into Komodo. It ships with an "HTML Tidy" formatter that might do an okay job of .html.erb formatting.

share|improve this answer

I found this formatting script (macro) and adapted it for my personal use with the latest Komodo Edit (v6.1.0). It works well (assuming you've got HTML Tidy available on your system) and I included the JavaScript formatting code provided by a commentator but I think it may only work with Komodo IDE. It's unimportant for my purposes. Perhaps someone out there can find a universal improvement (using something like html tidy).

komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }

var formatter;
var language = komodo.document.language;
switch (language) {
    case 'Perl':
        formatter = 'perltidy -i=2 -pt=2 -l=0';
        break;
    case 'XML':
    case 'XUL':
    case 'XLST':
        formatter = 'tidy -q -xml -i -w 80';
        break;
    case 'HTML':
        formatter = 'tidy -q -asxhtml -i -w 120';
        break;
  //case 'JavaScript':
  //    ko.views.manager.currentView.scimoz.selectAll();
  //    ko.views.manager.currentView.scimoz.replaceSel(js_beautify(ko.views.manager.currentView.scimoz.text, {indent_size: 2}));
  //    return null;
  default:
        alert("I don't know how to tidy " + language);
        return null;
}

//save current cursor position
var currentPos = komodo.editor.currentPos;

try {
    // Save the file.  After the operation you can check what changes where made by
    // File -> Show Unsaved Changes
    komodo.doCommand('cmd_save');

    // Group operations into a single undo
    komodo.editor.beginUndoAction();

    // Select entire buffer & pipe it into formatter.
    komodo.doCommand('cmd_selectAll');
    Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");

     // Restore cursor.  It will be close to the where it started depending on how the text was modified.
     komodo.editor.gotoPos(currentPos);

    // On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
    komodo.doCommand('cmd_cleanLineEndings');
}
catch (e) {
    alert(e);
}
finally {
    // Must end undo action or may corrupt edit buffer
    komodo.editor.endUndoAction();
}
share|improve this answer

To reformat code to your liking try astyle

You might be able to find this as a package, e.g. ap

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.