home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / mwjpp516.exe / %MAINDIR% / Tools / Java / comment.script < prev    next >
Encoding:
Text File  |  2001-06-19  |  2.2 KB  |  93 lines

  1. !!Script
  2. // Copyright ⌐ 1997-1998 - Modelworks Software
  3. // @Modified build 270 cm19990226 - changed to add and remove '//' at the indent level
  4. // @Modified build 272 cm19990301 - fixed case where the caret is not at the beginning of the line
  5.  
  6. /**
  7. @Tool: comment~adds or removes // comment in a selection. 
  8. To use select one or more lines and then run this script. 
  9. @EndTool: 
  10. @Summary: comment~adds or removes // comment in a selection 
  11. */
  12.  
  13. function DoCommand()
  14. {
  15.     var editor = getActiveEditor();
  16.     if (editor)
  17.     {
  18.         var range = editor.getSelection();
  19.         
  20.         // Clear selection
  21.         editor.select(range.startLineIndex, range.startCharIndex, 
  22.             range.startLineIndex, range.startCharIndex);
  23.         
  24.         // Select at lease one line
  25.         if (range.empty())
  26.         {
  27.             range.startCharIndex = 0;
  28.             range.endCharIndex = 0;
  29.             range.endLineIndex = range.startLineIndex + 1;
  30.         }
  31.         
  32.         // Don't include the last line if no characters are selected on the line
  33.         if (range.endCharIndex != 0)
  34.         {
  35.             range.endLineIndex += 1;
  36.         }
  37.  
  38.         var indentLevel = -1;
  39.         var findData = editor.findFirst("[ |\t]*(//)", range.startLineIndex, 0 , true, true, true);
  40.             
  41.         if (findData)
  42.         {
  43.             var foundRange = findData.getSubRange(0);
  44.             if (foundRange.startLineIndex != range.startLineIndex || foundRange.startCharIndex != 0)
  45.             {
  46.                 findData = null;
  47.             }
  48.         }
  49.  
  50.         if (findData == null)
  51.         {
  52.              indentLevel = editor.getIndentLevel(range.startLineIndex);
  53.         }
  54.         
  55.         for (var i = range.startLineIndex; i < range.endLineIndex; i++)
  56.         {
  57.             if (indentLevel == -1)
  58.             {
  59.                 if (findData)
  60.                 {
  61.                     var topRange = findData.getSubRange(0);
  62.                     if (topRange.startLineIndex == i && topRange.startCharIndex == 0)
  63.                     {
  64.                         var slashslashRange = findData.getSubRange(1);
  65.                         if (slashslashRange)
  66.                         {
  67.                             editor.remove(slashslashRange);
  68.                         }
  69.                     }
  70.                 }
  71.                 findData = editor.findFirst("[ |\t]*(//)", i + 1, 0 , true, true, true);    
  72.             }
  73.             else
  74.             {
  75.                 var level = editor.getIndentLevel(i);
  76.                 if (i < indentLevel)
  77.                 {
  78.                     editor.setIndentLevel(i, indentLevel);
  79.                 }
  80.                 editor.insert(i, indentLevel, "//");
  81.             }
  82.         }
  83.         
  84.         range.startCharIndex = 0;
  85.         range.endCharIndex = 0;
  86.         
  87.         editor.select(range);
  88.         editor.setActive(); 
  89.     } 
  90. }
  91.  
  92. !!/Script 
  93.