JS Var Statement Manager - CS, CS2

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

Andrew

JS Var Statement Manager - CS, CS2

Post by Andrew »

Another crazy project but useful now it's done. This helps keep track of vars in large scripts.

ah_var_manager.js manages var statements within functions. The script identifies all vars within the target functions and inserts a var statement in the right place (hopefully) at the front of the function. It then helps you to remove (and edit) any now redundant var statements further in the functions. I know this is not necessarily the way everyone wants to organise var statements but it does work and for large functions I find it very useful (and it can be managed within a script, the 'vars' as you go approach would be almost impossible).

The script initially browses for the target script to be edited (output is to a new file 'scriptname_newvars.js') and then lists out all the functions within that script.



You can work on individual functions, groups of functions (separated by spaces) or all functions.

You can also tick the checkbox 'insert var statement and close'. This will insert a var statement which contains all the variables within that function (in order of appearance) at the beginning of all selected functions. It is a fast and easy way of using the script if you do not want to also remove redundant var statements.

If you do not tick that checkbox, you get taken to the 'insert var statement dialog'.



This provides the new var statement to be inserted for the function (function name is given in the title bar and just below as well). You can edit the var statement and insert it or you can decide not to insert it.

View Function: reveals the full function you are working on in an alert screen (even if the screen goes off your PC screen, Enter will close it.).



Back: takes you to the previous function.

Edit No Insert: moves on to the edit vars screens but does NOT insert the var statement (not likely that you will want to do this).

Insert - No Edit: inserts the var statement and moves on to the next function.

Insert - Edit: inserts the statement and opens the next dialog which allows you to work with any var statements that have now been made redundant within the function.

Quit- Save: quits the script but saves changes so far.

If you opted to edit redundant var statements you move to the next screen.



Here you can edit any line in your function that starts with 'var' (by using the 'extend' button you can edit any line of the function at all). The most common use is to remove the 'var' (and sometimes the whole line) since it has been inserted up front in the function.

Pad: [rarely required] with very long var statements you cannot add anything to the text because of limitations on edit-text length within the dialog window. This adds a PAD to the text which you can then delete and you will find you can now add text.

Rem All Var: this removes the var upfront of all var statements in the current function. I use this almost every time (you add the var back for individual lines if necessary).

Rem Var: removes the var for the current line.

Clear: removes the whole current line.

Extend: extends the editable statement to the next statement.

Edit: having got the text how you want it, this does the edit and moves on to the next var statement.

-----------------------------

I have tested this script on itself (given it is full of text strings, RE's and comments a tough test) and on a 1000+ line script so far. It dealt with both. It will not be full-proof but it will cover most situations. The way I work with it is to do a full edit and then do a diff comparison in my text editor to make sure I have everything how I want it.

Watch out for string.search(re) - sometimes these need to have their var statement inline rather than up top.

Here is how the script rendered my find-functions function:

Code: Select allfunction findFunctions (jsStr)
{
   var iMax,jMax,i,functionAr,fpos,mpos,oStr,n1,n2,npos,startpos,fName;
   // safety net, iMax = max # functions, jMax = max # brackets in one function
   iMax = 100; jMax = 1000; i = 0; functionAr = new Array(); fpos = mpos = 0;
   oStr = jsStr;
   while(i < iMax)
   {
      var re1 = /^[ \t]*function\s+([\w\-]+)\s*\([^)]*)\s*{/im; // change the + to * on the function name and catch internal unnamed functions
      n1 = n2 = npos = 0;
      startpos = jsStr.search(re1);
      if (startpos == -1) break;
      fName = jsStr.substr(startpos).match(re1)[1];
      while ((n1 == 0 || n2 != n1) && (n1+n2) < jMax && npos != -1) {
         var re2 = /\{|\}|'|"|\//;
         npos = jsStr.search(re2);
         if (jsStr.charAt(npos) == '{') {n1++;}
         else if (jsStr.charAt(npos) == '}') {n2++;}
         else if (jsStr.charAt(npos) == '"' || jsStr.charAt(npos) == "'") {
            npos = jsStringLength (jsStr.substr(npos))+npos;}
         else if (jsStr.charAt(npos) == '/')  {
            npos = jsRELitLength (jsStr.substr(npos))+npos; }
         fpos = fpos + npos + 1;
         jsStr = jsStr.substr(npos + 1);
      } // if (!confirm(oStr.substring(startpos+mpos,fpos)))throw('b');
      functionAr = [fName,startpos+mpos,fpos];
      mpos = fpos;
      i++;   
   }
   return functionAr;   
}


Anndrew