home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- // Object Scripting
- // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
- //
- // COMMENT.SPP: Commenter. "Comments-out" the selected block, or
- // uncomments the lines if they are already commented.
- //
- // USE: Select a block and run script.
- //--------------------------------------------------------------------------
- print typeid(module());
-
- //
- // IDE imports.
- //
- import IDE;
- import editor;
-
- comment()
- {
- // Bail if no block.
- //
- if (!editor.TopView.IsValid) return;
-
- //
- // Determine if the block is commented (i.e., "//" are first two
- // non-whitespace characters of the first line.
- //
- declare curView = editor.TopView;
- declare curBlk = curView.Block;
- declare curPos = curView.Position;
- curBlk.Save();
- curPos.GotoLine(curBlk.StartingRow);
- declare sTmp = new String(curPos.Read());
- sTmp = sTmp.Trim(TRUE); // Trim leading whitespace.
- declare bComment = (sTmp.Index("//") == 1 ? TRUE : FALSE);
- curBlk.Restore();
-
- // Call routine to comment or uncomment the block. Stash and restore cursor.
- //
- curPos.Save();
- if (bComment) {
- RemoveCommentChars();
- }
- else {
- AddCommentChars();
- }
- curPos.Restore();
- }
-
- //
- // Add comment characters to all the lines in the block.
- //
- AddCommentChars()
- {
- declare curView = editor.TopView;
- declare curBlk = curView.Block;
- declare curPos = curView.Position;
- declare lastRow = curBlk.EndingRow;
-
- // Walk each line and add comment characters.
- //
- for (declare i = curBlk.StartingRow; i <= lastRow; i++) {
- curPos.Move(i, 1);
- curPos.InsertText("//");
- }
- }
-
- //
- // Remove comment characters from all the lines in the block.
- //
- RemoveCommentChars()
- {
- declare curView = editor.TopView;
- declare curBlk = curView.Block;
- declare curPos = curView.Position;
- declare lastRow = curBlk.EndingRow;
- declare sTmp = new String();
-
- declare commentPosition;
-
- // Walk each line.
- //
- for (declare i = curBlk.StartingRow; i <= lastRow; i++) {
- curPos.GotoLine(i);
- sTmp.Text = curPos.Read();
- commentPosition = sTmp.Index("//");
-
- // Be sure the entire line is a comment.
- //
- if (commentPosition) {
- sTmp = sTmp.Trim(TRUE);
- if (sTmp.Index("//") == 1) {
-
- // Delete comment characters.
- //
- curPos.Move(i, commentPosition);
- curPos.Delete(2);
- }
- }
- }
- }
-
-