[script] Add smart block comments

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

[script] Add smart block comments

Post by pjj »

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
/* */
line of code
line of code
/* */

and here it is commented out:
/* * /
line of code
line of code
/*
*/
Thanks to Rickard and crafty GetSelection() function we can now learn selection coordinates, which allowed me to write this little script:

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();
add-smart-block-comments.zip
(607 Bytes) Downloaded 652 times
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 :()
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply