The variable definition lines
var code = ''; and
var re = ''; should be modified to just
var code; and
var re; to define just the two variables without a type instead of defining them as string variables with an empty string assigned. The variable
code becomes a
String object on next non-empty line and variable
re a
RegExp object on next but one non-empty line.
The line
var re = /^/g
; should be modified to just
re = /^/g
; as the variable
re is already defined on line 7 above.
The line
UltraEdit.clipboardContent should be removed from the script.
The semicolon at end of the script file should be also removed.
There is no function
exit(). For that reason the script execution is terminated with an error if there is nothing selecting on running this script. One solution is modifying the first two IF conditions to:
Code: Select all
if (!bColumnMode && UltraEdit.activeDocument.isSel()) {
So the script with those small improvements would be:
Code: Select all
// basic framework code is from https://www.ultraedit.com/resources/scripts/jsDeminifyReformat.js
var bColumnMode = false;
if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
if (!bColumnMode && UltraEdit.activeDocument.isSel()) {
var code;
var re;
code = UltraEdit.activeDocument.selection;
re = /^# /g;
code = code.replace(re, '');
re = /\r\n# /g;
code = code.replace(re, ' ');
// wrap at 78 to allow for '# ' prefix
code = wordWrap(code, 78);
re = /^/g;
code = code.replace(re, '# ');
re = /\r\n/g;
code = code.replace(re, '\r\n# ');
var nActiveClipboard = UltraEdit.clipboardIdx;
UltraEdit.selectClipboard(9);
UltraEdit.clipboardContent = code;
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(nActiveClipboard);
}
// from https://stackoverflow.com/questions/14484787/wrap-text-in-javascript
function wordWrap(str, maxWidth) {
var newLineStr = "\r\n"; done = false; res = '';
while (str.length > maxWidth) {
found = false;
// Inserts new line at first whitespace of the line
for (i = maxWidth - 1; i >= 0; i--) {
if (testWhite(str.charAt(i))) {
res = res + [str.slice(0, i), newLineStr].join('');
str = str.slice(i + 1);
found = true;
break;
}
}
// Inserts new line at maxWidth position, the word is too long to wrap
if (!found) {
res += [str.slice(0, maxWidth), newLineStr].join('');
str = str.slice(maxWidth);
}
}
return res + str;
}
function testWhite(x) {
var white = new RegExp(/^\s$/);
return white.test(x.charAt(0));
}
A further improved version of the script above is:
Code: Select all
// basic framework code is from https://www.ultraedit.com/resources/scripts/jsDeminifyReformat.js
var bColumnMode = false;
if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
if (!bColumnMode && UltraEdit.activeDocument.isSel()) {
var comment = UltraEdit.activeDocument.selection;
comment = comment.replace(/^[\t ]*# ?/,'');
comment = comment.replace(/\r\n[\t ]*# ?/g,' ');
// wrap at 78 to allow for '# ' prefix
comment = wordWrap(comment, 78);
var nActiveClipboard = UltraEdit.clipboardIdx;
UltraEdit.selectClipboard(9);
UltraEdit.clipboardContent = "# " + comment.replace(/\r\n(?!$)/g,'\r\n# ');
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(nActiveClipboard);
}
// from https://stackoverflow.com/questions/14484787/wrap-text-in-javascript
function wordWrap(str, maxWidth) {
var newLineStr = "\r\n"; done = false; res = '';
while (str.length > maxWidth) {
found = false;
// Inserts new line at first whitespace of the line
for (i = maxWidth - 1; i >= 0; i--) {
if (testWhite(str.charAt(i))) {
res = res + [str.slice(0, i), newLineStr].join('');
str = str.slice(i + 1);
found = true;
break;
}
}
// Inserts new line at maxWidth position, the word is too long to wrap
if (!found) {
res += [str.slice(0, maxWidth), newLineStr].join('');
str = str.slice(maxWidth);
}
}
return res + str;
}
function testWhite(x) {
var white = new RegExp(/^\s$/);
return white.test(x.charAt(0));
}
The improvements are:
- It removes also leading horizontal tabs and normal spaces from all lines with # at beginning.
- # at beginning of a line is also removed if the next character is not a space before word-wrap.
- There is no # and space appended to end of selected comment block if the selected block ends with carriage return + line-feed to avoid commenting out the next line with code.
Please let me know if you would like to have a further improved script on which the indentation on first comment line is used for all other comment lines, too.