Deleting block comments with keeping the number of lines constant

Help with writing and running scripts

Deleting block comments with keeping the number of lines constant

Postby stonkin » Tue Mar 02, 2010 8:59 am

I'm using UE v15.20.0.1022.

Being an old-fashioned mainframe techie I know nothing about JS but using the UE documentation and info gleaned from these fora I've managed to create a script to extract certain data from PL/I source code. As part of my data extract I need to preserve the line number on which code is situated. The first part of my script clears down all the comments in the program...

I initially used...

UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.perlReOn();

UltraEdit.activeDocument.top();

var searchRegexp = "/\\*(.|[\\r\\n])*?\\*/";
UltraEdit.activeDocument.findReplace.replace(searchRegexp,"");

This is fine for comments wholly on a single line, but if a comment spans several lines I need the comment to be replaced with the same number of blank lines so as to preserve subsequent line numbers.

I did some reading on findreplace.replace and it seems to indicate that the 2nd argument can be a function. Is this true in the implementation of JavaScript in UE? I've tried all sorts but can't seem to get .replace to invoke the function. All I need is for the replace string to contain the same number of CRLF as were in the original comment.

Can anybody please help.

Cheers
User avatar
stonkin
Newbie
 
Posts: 3
Joined: Fri Jan 05, 2007 12:00 am

Re: Deleting block comments with keeping the number of lines constant

Postby jorrasdk » Tue Mar 02, 2010 9:42 am

Try this :

Code: Select all
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.replaceAll=false;
UltraEdit.perlReOn();

UltraEdit.activeDocument.top();

var searchRegexp = "/\\*(.|[\\r\\n])*?\\*/";

/* Loop through all block of comments with a UE find */
while (UltraEdit.activeDocument.findReplace.find(searchRegexp)) {
   
   /* retrieve currently selected comment block */
   var selBlock = UltraEdit.activeDocument.selection;
   
   /* Remove everything but CRLF thus preserving the number of lines */
   selBlock = selBlock.replace(/[^\r\n]/g,"");

   /* Now write "empty" block back */
   UltraEdit.activeDocument.write(selBlock);
   
}

I do not use a function in the replace but a mix of a UE find and a javascript replace. The UE find finds the block of comments using your regExp and in the javascript replace everything but newline (\r\n) is removed and written back into the document as an empty block preserving the number of lines.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Deleting block comments with keeping the number of lines constant

Postby stonkin » Tue Mar 02, 2010 10:00 am

Thanks jorrasdk...works brilliantly.
User avatar
stonkin
Newbie
 
Posts: 3
Joined: Fri Jan 05, 2007 12:00 am


Return to Scripts