Script to block comment/uncomment selected text

Ask questions about how to create a script or swap scripts with other users.
Post Reply
kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

Script to block comment/uncomment selected text

Post by kbieb86 »

The block commenter built into RJ TextEd actually does line comments for each line. I prefer using actual block comments (/* */ or <!-- -->).

This script will check to see if the selected block is already commented (select the delimiters too), if so it will uncomment the block, otherwise it will comment it. Currently, it only checks for HTML and XML files to apply the <!----> block comments, every other type of file gets /**/. I use the file extension to determine the comment type, so it won't work on embedded scripts or styles in an HTML doc or on unsaved documents. If I can figure out how to determine the current highlighter, I will change it.

This is my first RJ TextEd script, so comments are appreciated.

Code: Select all

var selectedText = Document.SelText;
var firstTwoSelChars = Copy(selectedText, 1, 2);
var selectionLength = Length(selectedText);
if (firstTwoSelChars == "/*") {
	Document.SelText = Copy(selectedText, 3, (selectionLength - 4));
} else if (firstTwoSelChars == "<!") {
	Document.SelText = Copy(selectedText, 5, (selectionLength - 7));
} else {
	var fileName = Document.FileName;
	var dotPos = -1;
	var i = Length(fileName);
	while ((dotPos == -1) && (i > 0)) {
		if (fileName[i] == ".") {
			dotPos = i;
		} else {
			i--;
		}
	}
	var fileExtension = Copy(fileName, dotPos + 1, Length(fileName));
	if ((fileExtension == "html") || (fileExtension == "xml")) {
		Document.SelText = "<!--" + Document.SelText + "-->";
	} else {
		Document.SelText = "/*" + Document.SelText + "*/";
	}
}

kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

Re: Script to block comment/uncomment selected text

Post by kbieb86 »

If you're running 11.20+ you can use this script which makes use of the new Document.HighlighterAtCursor property:

Code: Select all

var selectedText = Document.SelText;
var firstTwoSelChars = Copy(selectedText, 1, 2);
var selectionLength = Length(selectedText);
if (firstTwoSelChars == "/*") {
	Document.SelText = Copy(selectedText, 3, (selectionLength - 4));
} else if (firstTwoSelChars == "<!") {
	Document.SelText = Copy(selectedText, 5, (selectionLength - 7));
} else {
	var currentHighlighter = Document.HighlighterAtCursor;
	if ((currentHighlighter == "html") || (currentHighlighter == "xml")) {
		Document.SelText = "<!--" + Document.SelText + "-->";
	} else {
		Document.SelText = "/*" + Document.SelText + "*/";
	}
}

Post Reply