Inspired by Jeff's PHP beautifier I decided to provide something similar for Ruby developers.
So I packaged together a modified version of Jeff's macro with a modified version of arachnoid's Ruby beautifier. The beautifier is pretty basic, but it handles indentation of Ruby code very well. It is aware of your indentation settings in Komodo, so as long as you're using tabs over spaces for indentation it will work perfectly for you. If I have the time I may further enhance it to handle more than just indentation.
Here is the macro portion of the beautifier:
try {
if (komodo.view.scintilla) {
komodo.view.scintilla.focus();
}
if (komodo.document.language == "Ruby") {
var ke = komodo.editor;
var currentPos = ke.currentPos;
var indent = ke.indent;
var file = komodo.interpolate('%F');
var ruby = komodo.interpolate('%(ruby)');
var script = "/home/bozhidar/rbeautify.rb";
var cmd = ruby + " " + script + " " + file + " " + indent;
StatusBar_AddMessage("Beautifying " + file + "...",
"editor", 5000, true);
komodo.doCommand('cmd_save');
ke.beginUndoAction();
komodo.doCommand('cmd_selectAll');
Run_RunEncodedCommand(window, cmd + " {'insertOutput': True}");
ke.gotoPos(currentPos);
komodo.doCommand('cmd_cleanLineEndings');
StatusBar_AddMessage("Finished beautification", "editor", 5000, true);
} else {
alert("Not a Ruby file!");
}
} catch(e) {
alert(e);
}You can simply cut and paste this code and name the newly created macros Rbeautify for example.
You have to change the line
var script = "/home/bozhidar/rbeautify.rb";
to the path to rbeautify.rb on your system.
rbeautify.rb is packaged together with Rbeautify.kpz(which you can directly import in Komodo) in the attached to this topic file Rbeautify.zip.
I hope you find the beautifier helpful. If you have any suggestions or problems, please let me know.
| Attachment | Size |
|---|---|
| Rbeautify.zip | 2.73 KB |
| CodeRed.zip | 2.58 KB |
Hey, that's cool, thank you!
So people know, you can find the original Arachnoid Ruby Beautifier at http://www.arachnoid.com/ruby/rubyBeautifier.html
It is licensed under GPL.
I had a couple of spare hours in which I wrote my very own beautifier script for Ruby code. It should be considered pre alpha quality, but it works pretty well, especially if don't have the nasty habit to span statements over multiple lines;) It has a pretty clean OO design and is easily extensible with other functionality - for example it could fix spacing between operators apart from indentation. I have named the beautifier "Code Red" and have uploaded a version of it here. It consists of two parts - a CodeRed class that hosts the logic and a Ruby Formatter that does the actual work. Take a look at it if you are interested, I'd be happy to hear some opinions.
There's a bug in this macro, for some reason, after having used the beautifier, the undo action undoes all changes done to the file since the session initiated on that file.
See http://community.activestate.com/forum-topic/undo-undoes-all
Try this mod:
try {
Run_RunEncodedCommand(window,
cmd + " {'insertOutput': True}");
ke.gotoPos(currentPos);
komodo.doCommand('cmd_cleanLineEndings');
} finally {
ke.endUndoAction();
}
ke.beginUndoAction(); try { Run_RunEncodedCommand(window, cmd + " {'insertOutput': True}"); ke.gotoPos(currentPos); komodo.doCommand('cmd_cleanLineEndings'); } finally { ke.endUndoAction(); }- Eric