[script] Add smart block comments
Posted: 27 Jun 2012 19:00
Recently I've learnt a neat trick, which I dubbed "smart block comments". The idea is that you can comment block of code out or "in" by adding or deleting just one space (this of course works only for languages that support block comments in the form of /* */). This is normal code
and here it is commented out:
which puts appropriate marks (not the best word, I'm afraid) "around" selected lines. I hope you'll find it helpful (both technique and the script.)
(I put the code on-line, as addition to the zipfile, since attachments on the forum get lost after some time and you can't download them anymore
)
/* */
line of code
line of code
/* */
and here it is commented out:
Thanks to Rickard and crafty GetSelection() function we can now learn selection coordinates, which allowed me to write this little script:/* * /
line of code
line of code
/* */
Code: Select all
// turn off screen updating
Document.BeginUpdate();
// turn word-wrap off
if (Document.Wordwrap) {
Document.Wordwrap = false;
}
// make sure variables are integers
var startX = 0, startY = 0, endX = 0, endY = 0;
var temp = 0, docLastY = 0; last = false;
// find selection's coordinates
Document.GetSelection(startX, startY, endX, endY);
// if selection was made upwards, switch startY and endY
if (startY > endY) {
temp = startY;
startY = endY;
endY = temp;
}
// find if selection ends at the last line of the document
Document.CursorDocEnd(false);
docLastY = Document.CursorY();
if (endY == docLastY) {
last = true;
Document.InsertText("\n");
}
// add upper marks
Document.CursorY() = startY;
Document.CursorX() = 0;
Document.InsertText("/* */\n");
// add lower marks
Document.CursorY() = endY + 2;
Document.CursorX() = 0;
if (last) {
Document.InsertText("/* */");
} else {
Document.InsertText("/* */\n");
}
// move cursor to the upper marks
Document.CursorY() = startY;
Document.CursorX() = 4;
// turn on screen updating
Document.EndUpdate();
(I put the code on-line, as addition to the zipfile, since attachments on the forum get lost after some time and you can't download them anymore
