Configuring PerlTidy as a Komodo 10 Formatter

#Configuring PerlTidy as a Komodo 10 Formatter

Komodo IDE is a great IDE for Perl development, but unfortunately,PerlTidy is not configured out of the box as a default formatter for Perl.This is especially surprising given ActiveState’s deep background in Perl.

I have seen several forum posts in the past in this regard, so I thought making a quick tutorial for Komodo 10 might help. The same process can be followed for just about any command-line formatter.

Have no fear, this tutorial will show that it is easy to configure PerlTidy as an external formatter in Komodo 10.

Prerequisite:

Install PerlTidy

Perltidyrc setup:

Although it is possible to pass your PerlTidy options in as command-line options in the Formatter configuration, I do not recommend it. Using PerlTidy’s default configuration file .perltidyrc allows me to document the purpose of each switch better, and ensures that I get the same results whether formatting from the command-line or in Komodo.

PerlTidy automatically looks for the file .perltidyrc to load your default preferences upon execution. It first looks for this file in the directory from which it is executed, and if it doesn’t find it, it looks in your home directory.I prefer to put the .pertidyrc in my home directory, as that ensures I get the same formatting everywhere.

My ~/.perltidyrc:
-pbp # Start with Perl Best Practices
-w # Show all warnings
-iob # Ignore old breakpoints
-l=80 # 80 characters per line
-mbl=2 # No more than 2 blank lines
-i=2 # Indentation is 2 columns
-ci=2 # Continuation indentation is 2 columns
-vt=0 # Less vertical tightness
-pt=2 # High parenthesis tightness
-bt=2 # High brace tightness
-sbt=2 # High square bracket tightness
-isbc # Don’t indent comments without leading space
-ce # cuddle elses so they arent lonely

#####Note: I’ve included my file only as an example. I have zero interest in debating the merits or demerits of a particular coding style.

While PerlTidy provides some flags for I/O control, they are not necessary for Komodo. Komodo will pass the current file or text selection to a formatter on STDIN, and accept the reformatted text on STDOUT.

Komodo Configuration:

Go to Edit -> Preferences -> Formatters. Click the + sign to create a new formatter.

You will see a dialog for creating the new formatter. Add PerlTidy as the name of the formatter, choose Perl as the language, and under Formatter click the drop-down menu and choose Other -> Generic command-line formatter. Click Ok.

A Configurable Options dialog will appear. Just enter perltidy in the ‘Executable to use’ field. Komodo will use your shell $PATH to find perltidy. This works especially well with tools like plenv or Perlbrew. However, if perltidy is not in your $PATH, you can add the whole path to PerlTidy (e.g. /usr/bin/perltidy ).

Click Ok to dimiss the Configurable Options dialog, then click Ok again to dismiss the Preferences dialog.
PerlTidy is now configured for easy use.

Using PerlTidy:

Document Formatting:

Open a Perl file and click Code -> Format -> Document using PerlTidy. The code will be reformatted, and then you can save the file in Komodo.

Selection Formatting:

Highlight some Perl code in an open file. Right-click and hover over Format You should see the option to format the selection using PerlTidy, as well as the Document using PerlTidy options.

Command-line PerlTidy formatting

When running PerlTidy at the command line, I pass in the options -nst (don’t send to standard out - necessary because I use -pbp, which includes -se), -b (backup and modify in place), and -b=’/’ (don’t create a backup file if perltidy encounters no errors).

I have aliased this to pt for convenience, but typed out, it would look like:
perltidy -nst -b -bext=’/’ somefile.pl

Note: the -bext=’/’ option only works on PerlTidy verion 20120619 or higher. Prior to that, you have to just use -nst -b and manually delete the .bak files.

May all your code be tidy.

2 Likes

Cool tutorial!