Integrate Perltidy in UE

Help with setting up and configuring custom user tools in UltraEdit (based on command line input)

Integrate Perltidy in UE

Postby jens_g » Wed Oct 25, 2006 6:45 pm

Hi,

How is it possible to integrate Perltidy in the UE IDE, to use Perltidy directly out of the editor?

Has anyone experience with Perltidy and Ultraedit?

Ciao - Jens
User avatar
jens_g
Newbie
 
Posts: 1
Joined: Mon Oct 23, 2006 11:00 pm

Re: Integrate Perltidy in UE

Postby mjcarman » Wed Nov 01, 2006 6:25 pm

jens_g wrote:How is it possible to integrate Perltidy in the UE IDE, to use Perltidy directly out of the editor?


You can create a user tool to run perltidy on the active file. See Advanced->Tool Configuration. Create a new tool with a command line of:

Code: Select all
perltidy "%f"


You'll have to open up the resulting *.tdy file manually, although it's probably possible to create a macro to execute the tool and then open the file for you. Alternately, you could use perltidy's -b option but you'd have to be careful about overwriting your backup (and thus losing your original).

I haven't had much luck getting PerlTidy to work on the selection, though. I think this is more of a UE problem as the selection is passed as an argument; there's no way to make it appear on STDIO. I've sent a feature request to IDM about this.
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: Integrate Perltidy in UE

Postby mjcarman » Tue Jun 26, 2007 8:37 pm

With the addition of scripting support in version 13 I was able to write a script to run perltidy on a selection. To use it, configure a tool named "perltidy file" with a command line of perltidy "%f" and a working directory of %P. Save the code below as perltidy.js. Add it to your list of scripts and configure your favorite hotkey. (I use Ctrl+Shift+T.)

By itself, the user tool will run perltidy on the active file. Run through the script it will update only the current selection. If you need to undo the changes it's only a single step.

Note: You may want to change the name/path of the tempfile to something more appropriate for your system.
Code: Select all
/*
 * Run perltidy on the current selection
 */

if (UltraEdit.activeDocument.isSel()) {
   var tidyin  = "C:\\Temp\\perltidy.txt";
   var tidyout = tidyin + '.tdy';

   // Hack! Thanks, jorrasdk
   // http://www.ultraedit.com/index.php?name=Forums&file=viewtopic&t=4571
   var idx = getActiveDocumentIndex();

   // Switch to a clipboard that's probably not in use
   UltraEdit.selectClipboard(9);

   // Save selection to temporary file
   UltraEdit.activeDocument.copy();
   UltraEdit.newFile();
   UltraEdit.activeDocument.paste();
   UltraEdit.saveAs(tidyin);

   // Run perltidy on code from selection
   // Tool must exist and be configured to run 'perltidy "%f"'
   UltraEdit.runTool("perltidy file");

   // Close tempfile
   UltraEdit.closeFile(tidyin, 2);

   // Get perltidy output
   UltraEdit.open(tidyout);
   UltraEdit.activeDocument.selectAll();
   UltraEdit.activeDocument.copy();
   UltraEdit.closeFile(tidyout, 2);

   // Replace selection with output of perltidy
   UltraEdit.document[idx].paste(); // why doesn't activeDocument work here?

   // Restore the clipboard we were probably using before
   UltraEdit.selectClipboard(0);
}

/* Find the tab index of the active document */
function getActiveDocumentIndex() {
   var tabindex = -1; /* start value */

   for (i = 0; i < UltraEdit.document.length; i++) {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
         tabindex = i;
         break;
      }
   }
   return tabindex;
}
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am


Return to Custom User Tools/Tool Configuration