Home » Company » Blog » Community » Source code cleanup script

Source code cleanup script

Andy Boyd, IDM
Marketing Manager

In some of our recent posts, we have been providing answers to commonly-asked support questions - such as how to print syntax highlighted files. The post that follows is anything but common. In fact, I don't recall seeing anyone ask the question before. However, I thought the question/solution may benefit a wider group of you, so we decided to share it...

In short, the question was, can UltraEdit detect declared (but unused) variables AND can UltraEdit detect and remove variables that have been declared twice.

As you may know, there isn't a specific feature for this, but it can be done with scripting! I created a script for this person for the express purpose of sharing it with our community.

Caveat 1: I understand that in many cases you can do this type of process with a compiler, but the question was "how to do this in UltraEdit", so we answered it... :)

Caveat 2: I am not a programmer for UltraEdit, I just happen to know a little programming. If you are reading this and would like to update/improve the script, please feel free. We are always happy for our loyal users to provide content that can benefit the community as a whole.

Without further ado... You can download the script here

//---------------------------------------------------------------------------------
// 
//     The script will find all variables in the file held in the types array.
//     In this case, the types are float, integer, and string.  It will do two
//     things:  
//      
//     1) Look for multiple instantiations of the same variable and remove the duplicates
//        ie: float AAAA
//            float BBBB
//            float AAAA
//
//            After script:
//            
//        ie: float AAAA
//            float BBBB             
//            
//     2) Remove an instantiation of the variable is not used in the file.
//        ie: float AAAA
//            float BBBB
//            
//            AAAA = 1
//        
//            After script:
//
//            float AAAA
//            
//            AAAA = 1
//             
//---------------------------------------------------------------------------------
//
//    Example:  Run the script on the following sample file:
// 
//     float AAAA
//     float BBBB
//     integer CCCC
//     string DDDD
//     float BBBB
//     float EEEE
//     float FFFF
//     float GGGG
// 
//     AAAA=1
//     BBBB=2.3
//     CCCC=9
//     DDDD="TEST"
//     function1(1,2,3,GGGG)
//
//    Script returns: 
//
//     float AAAA
//     float BBBB
//     integer CCCC
//     string DDDD
//     float GGGG
// 
//     AAAA=1
//     BBBB=2.3
//     CCCC=9
//     DDDD="TEST"
//     function1(1,2,3,GGGG)
//
//---------------------------------------------------------------------------------


// Variable types 
var types = new Array("float", "string", "integer");

// Array that holds variable names, 
// ie: [0] => array(2) {
//            [0] => string "integer cccc"  <- original variable instantiation
//            [1] => string "cccc" <- variable name
var varNames = new Array();

// Split "found" string 
// ie: 'integer AAAA' -> 'AAAA'
function getVariableName(typeStr, fullStr) {
  var resultArr = fullStr.split(typeStr);
  return resultArr[1].replace(/^\s+|\s+$/g, '') ;
}

// Get all the variables in the file and populate varNames array
for ( var i in types)
{
  UltraEdit.activeDocument.top();
  do 
    {  
      UltraEdit.perlReOn();
      UltraEdit.activeDocument.findReplace.regExp=true;
      var isFound = UltraEdit.activeDocument.findReplace.find("^\\s*?"+types[i]+".*")
      if (isFound) 
      { 
        var str = UltraEdit.activeDocument.selection;
        // remove duplicate variable declarations/instantiations
        var howMany = 1; // hold number of occurrences of find string
        UltraEdit.activeDocument.findReplace.find(str);
        while (UltraEdit.activeDocument.isFound()){
          UltraEdit.activeDocument.findReplace.find(str);
          howMany = howMany + 1;
          if (howMany > 1) {
            UltraEdit.outputWindow.write("WARNING: Duplicate instantiation of: \"" + str + "\" (" + howMany + " times)");
            UltraEdit.activeDocument.deleteLine();
          };
        }
        //populate varName array with var name 
        //(ie: remove "type" text)
        var varName = getVariableName(types[i], str);
        varNames.push(new Array(str, varName)); 
      } 
      else {
        break; 
      };
    }
  while (true);
}

// look for non-used variables
// if they don't exist, remove them from the file
for ( var x in varNames)
{
      var isFound = false;
      UltraEdit.activeDocument.top();
      UltraEdit.perlReOn();
      UltraEdit.activeDocument.findReplace.regExp=true;
      isFound = UltraEdit.activeDocument.findReplace.find("^.*?"+varNames[x][1]+".*(=|[)]).*?$");
      // if not found, remove (replace) original instantiation
      if (!isFound) {
        UltraEdit.outputWindow.write("WARNING: Variable "+varNames[x][0]+" was instantiated but not used");        
        UltraEdit.activeDocument.top();
        UltraEdit.activeDocument.findReplace.replaceAll = true;
        UltraEdit.activeDocument.findReplace.replace("^.*?"+varNames[x][0]+".*?$\r\n", "");
      }
}

UltraEdit.activeDocument.top();

Summary

As a brief summary, the script will find all variables in the file held in the types array (float, integer, and string). It will then do two things:

1. Look for multiple instantiations of the same variable and remove the duplicates:

Before

float AAAA
float BBBB
float AAAA

After script

float AAAA
float BBBB

2. Remove an instantiation of the variable if it is not used in the file.

Before

float AAAA
float BBBB

AAAA = 1

After Script

float AAAA

AAAA = 1

Example

The sample file we used to create the script is as follows:

Before:

float AAAA
float BBBB
integer CCCC
string DDDD
float BBBB
float EEEE
float FFFF
float GGGG

AAAA=1
BBBB=2.3
CCCC=9
DDDD="TEST"
function1(1,2,3,GGGG)

Note that "BBBB" (float BBBB) is declared twice and "EEEE" (float EEEE) is not used in the sample file.

After:

After running the script, the file looks like the following:

float AAAA
float BBBB
integer CCCC
string DDDD
float FFFF
float GGGG

AAAA=1
BBBB=2.3
CCCC=9
DDDD="TEST"
function1(1,2,3,GGGG)

As you can see, the duplicate variable (float BBBB) has been removed. Also, the unused variable (float EEEE) has been removed.

Got a script or tip you would like to share? Let us know!


Share this:  

Submit to Twitter Submit to Facebook Submit to Technorati Submit to Delicious Submit to Digg Submit to Mixx Submit to Reddit Submit to Stumbleupon Submit to LinkedIn

Back to Top

Comment on this post

Required fields are marked with *.


Back to Top

Subscribe to our blog

Get software updates, company news, staff editorials, and power tips in your RSS reader.

IDM Highlights newsletter

Sign up now for...

  • Software Powertips
  • New Releases
  • Update Notices
  • Cool Tools, Specials
  • Company News