Code beautifier macro for Komodo Edit 9.2


One of the thing in a software project is the source code layout, code formatting/code beautifiers and readability. This is very helpful and important aspect so that the source files do not appear strange and new, if every developer follows his/her own coding style, layout and formatting.

Well, most of the open source IDEs (integrated development environment) do not support code formatting for all the type of files - Java, C/C++, C#, PHP, Python, jQuery/Javascript, HTML, etc
If formatting for one language is supported by the IDE, the other one may not be supported.

Recently I ended up using Komodo IDE on Windows 7 (64-bit) for editing PHP, jQuery/CSS/HTML source files in a project; but soon understood that the open source version did not support formatting of all these types of files.

There is formatting support using macros and looking at various resources in the Komodo forums and other places, compiled a somewhat complete macro as detailed below.

Install and set up the following libraries (dependencies) in order to use the formatting tools,
Once the above tools/libraries are installed successfully, you are ready to add the following JavaScript macro to Komodo Edit 9.2
       
    
 if (komodo.view.scintilla) { 
  komodo.view.scintilla.focus(); 
 }

        var language = komodo.view.language;
        var file = komodo.interpolate('%F')
        var formatter;
        var script;
        var cannot_tidy_selection = false;

        switch (language) {
            case 'C':
                formatter = 'astyle --style=linux';
                break;

            case 'CSS':
                formatter = 'csstidy - --preserve_css=true --lowercase_s=true --case_properties=true --sort_properties=true --template=low --remove_bslash=false';
                break;       

            case 'C++':
                formatter = 'astyle --style=kr';
                break;

            case 'C#':
                formatter = 'astyle --style=ansi';
                break;

            case 'HTML':
                cannot_tidy_selection = true;
                formatter = 'tidy -q -utf8 -asxhtml -i -w 80';
                break;

            case 'Java':
                formatter = 'astyle --style=java';
                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: 4}));
                return null;

            case 'XLST':
            case 'XML':
            case 'XUL':
                cannot_tidy_selection = true;
                formatter = 'tidy -q -utf8 -xml -i -w 80';
                break;

            case 'Perl':
                formatter = '/usr/local/bin/perltidy -l=200 -et=4 -nolq -b';
                break;

            case 'PHP':
                var php = komodo.interpolate('%(php)');
                script = "E:/winapps/KomodoEdit/beautifier/phpbeautifier.php"
                formatter = php+" -f "+script+" "+file; // add options dialog?
                break;

            default:
                alert("I don't know how to tidy " + language);
                return null
        }
        try {
            var currentPos = komodo.editor.currentPos;
            var ke = komodo.editor;
            StatusBar_AddMessage("Beautifying "+file,"editor",5000,true);
            komodo.doCommand('cmd_save');
            ke.beginUndoAction();

            var text_not_selected = cannot_tidy_selection
                     || komodo.editor.selText == "";

            if (text_not_selected) {
                komodo.doCommand('cmd_selectAll');
            }

            ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True}");
            ke.gotoPos(currentPos);
            komodo.doCommand('cmd_cleanLineEndings');
            StatusBar_AddMessage("Finished Beautification","editor",5000,true);
        }
        catch(e) {
            alert(e);
        }
        finally {
            // Must end undo action or may corrupt edit buffer
            ke.endUndoAction();
        }
       
 

To Add / Edit new macro in Komodo Edit:
  1. Go to - View > Tabs & Sidebars > Toolbox
  2. Right click on the Toolbox window and select 'New macro'
  3. Give the macro a nice name 'Code Beautifier'
  4. Copy /paste the following code as JavaScript and save
  5. Open a PHP or C or other source file and double click on the 'Code Beautifier' macro to run beautifier
  6. The source files opened will be re-formatted in K&R/ C-style


Links to other pages/ references, which were helpful while compiling this version of the macro script:
  1. Jeff's JavaScript script macro on which this is based
  2. Installing .xpi files in Komodo Edit IDE
  3. Activestate community forum

Comments