up vote 1 down vote favorite
share [g+] share [fb]

this should be a simple one for some who using komodo edit for a while. I've a rails html.erb file in the editor and the indentation has gone a bit wild. Is there a function to automatically indent my code so it's easier to read?

link|improve this question

50% accept rate
feedback

3 Answers

I use this slightly edited version of the that code. Variations have been floating around the Komodo Forums for some time. I use this on Komodo Edit 6.1, works fairly well. I might come back and update this post, if/when I add new syntax support. I changed some of the tidy and csstidy options, added the XML support, and Changed undefined syntax alert message. You might want to check out rbeautify, i'm sure adding a new case statement wouldn't be too difficult. (I would integrate it for you, but i'm lost on Ruby)

Format_Syntax.js

   komodo.assertMacroVersion(3);
if (komodo.view.scintilla) { komodo.view.scintilla.focus(); } // bug 67103
var formatter;
var language = komodo.document.language;
var cannot_tidy_selection = false;
switch (language) {
    case 'C':
        formatter = 'astyle --style=linux';
        break;
    case 'C++':
        formatter = 'astyle --style=kr';
        break;
    case 'C#':
        formatter = 'astyle --style=ansi';
        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':
        formatter = 'astyle --style=java';
        break;
    case 'Perl':
        formatter = 'perltidy';
        break;
    case 'PHP':
        formatter = 'php_beautifier -s4 -l"Pear()"';
        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 '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;
}
link|improve this answer
feedback

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.

link|improve this answer
feedback

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();
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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