Page 1 of 1

Script to block comment/uncomment selected text

Posted: 26 Aug 2016 20:15
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 + "*/";
	}
}

Re: Script to block comment/uncomment selected text

Posted: 12 Sep 2016 17:42
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 + "*/";
	}
}