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

  1. !!Script
  2. // Copyright ⌐ 1997-1998 - Modelworks Software
  3. // @Created build 213 (9 September 1998)
  4. // @Modified build 215 cm19980911 - changed order to match JavaDoc
  5. // @Modified build 265 cm19990213 - fixed abstract method problem
  6.  
  7. /**
  8. @Tool: insertJavadocMethod~inserts a Javadoc method comment. 
  9. To use put the caret at the begining of the method declaration 
  10. then run this script.
  11. @EndTool: 
  12. @Summary: insertJavadocMethod~inserts a Javadoc method comment
  13. */
  14.  
  15. var gOutput = getOutput();
  16.  
  17. var localCommentHeader = 
  18.     "\n/**" +
  19.     "\n* @CaretShort concise description." + 
  20.     "\n* Additional verbose description.";
  21.  
  22. var commentReturns = 
  23.     "\n* @return description.";
  24.  
  25. var commentTrailer =
  26.     "\n* @see package.class" +
  27.     "\n*/\n";
  28.  
  29. function DoCommand()  
  30. {
  31.     var editor = getActiveEditor();
  32.     if (editor)
  33.     {
  34.         Insert(editor, localCommentHeader, 1); 
  35.         editor.setActive("Insert Javadoc Method");
  36.     }
  37. }
  38.  
  39. function Insert(editor, commentHeader,  adjust)  
  40. {
  41.     var selection = editor.getSelection();
  42.     var indentLevel = editor.getIndentLevel(selection.startLineIndex);
  43.     var delimiterList = editor.findNextDelimiters("(", ")", selection.startLineIndex, selection.startCharIndex);
  44.     if (delimiterList && delimiterList.count > 0)
  45.     {
  46.         var parametersRange = delimiterList.removeHead();
  47.         if (parametersRange)
  48.         {
  49.             var throwsList = GetThrowsList(editor, parametersRange.endLineIndex, parametersRange.endCharIndex);
  50.             
  51.             parametersRange.startCharIndex += 1;
  52.             parametersRange.endCharIndex -= 1;
  53.             var parameters = editor.copy(parametersRange);
  54.             
  55.             var comment = commentHeader;
  56.             if (parameters != null)
  57.             {
  58.                 comment += ProcessParameters(parameters);
  59.             }
  60.             comment += commentReturns;
  61.             if (throwsList != null)
  62.             {
  63.                 comment += ProcessThrows(throwsList);
  64.             }
  65.             comment += commentTrailer;
  66.             
  67.             var range = editor.insert(selection.startLineIndex, selection.startCharIndex, comment);
  68.             
  69.             CleanupMultilineComment(editor, indentLevel, range.startLineIndex + adjust); // The comment starts in the next line
  70.             
  71.             // Now replace @Caret with the real caret
  72.             var findData = editor.findFirst("@Caret", selection.startLineIndex, 0);
  73.             if (findData && findData.found)
  74.             {
  75.                 editor.replace("", findData.range); 
  76.                 editor.select(findData.range.startLineIndex, findData.range.startCharIndex);
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. function GetThrowsList(editor, startLineIndex, startCharIndex)
  83. {    
  84.     var throwsList = "";
  85.     var bodyRange = null;
  86.     var delimiterList = editor.findNextDelimiters("{", "}", startLineIndex, startCharIndex);
  87.     if (delimiterList && delimiterList.count > 0)
  88.     {
  89.         bodyRange = delimiterList.removeHead();
  90.     }
  91.     
  92.     var semicolonFindData = editor.findFirst(";", startLineIndex, startCharIndex);
  93.     var throwsFindData = editor.findFirst("throws", startLineIndex, startCharIndex);
  94.     
  95.     if (throwsFindData && throwsFindData.found)
  96.     {
  97.         // cm19990213
  98.         if (semicolonFindData && bodyRange)
  99.         {
  100.             if (semicolonFindData.range.compare(bodyRange) == -1)
  101.             {
  102.                 throwsList = editor.copy(throwsFindData.range.endLineIndex,throwsFindData.range.endCharIndex,
  103.                     semicolonFindData.range.startLineIndex, semicolonFindData.range.endCharIndex-1);
  104.             }
  105.             else
  106.             {                
  107.                 throwsList = editor.copy(throwsFindData.range.endLineIndex,throwsFindData.range.endCharIndex,
  108.                     bodyRange.startLineIndex, bodyRange.startCharIndex-1);
  109.             }
  110.         }
  111.         else if (semicolonFindData && bodyRange == null)
  112.         {
  113.             throwsList = editor.copy(throwsFindData.range.endLineIndex,throwsFindData.range.endCharIndex,
  114.                 semicolonFindData.range.startLineIndex, semicolonFindData.range.endCharIndex-1);
  115.         }
  116.         else if (semicolonFindData == null && bodyRange)
  117.         {
  118.             throwsList = editor.copy(throwsFindData.range.endLineIndex,throwsFindData.range.endCharIndex,
  119.                 bodyRange.startLineIndex, bodyRange.startCharIndex-1);
  120.         }
  121.     }
  122.     return throwsList;
  123. }
  124.  
  125. function ProcessThrows(throwsList)
  126. {
  127.     // add "\n* @exception name"  for each throw
  128.     
  129.     var insert = "";
  130.     var start = 0;
  131.     var length = throwsList.length;
  132.     var nextWord;
  133.     while (start < length)
  134.     {
  135.         start = skipDelimiters(throwsList, start);
  136.         
  137.         var typeString = searchInString(throwsList, "[^\t\r\n\, ]*", start);
  138.         
  139.         if (typeString == null || typeString.key.length == 0)
  140.         {
  141.             break;
  142.         }
  143.         
  144.         insert += "\n* @exception " + typeString.key + " description.";
  145.         start += typeString.key.length        
  146.         }
  147.     return insert;
  148. }
  149.  
  150. function skipDelimiters(string, start)
  151. {
  152.     var nextWord = searchInString(string, "[\t\r\n\, ]*", start);
  153.     if (nextWord)
  154.     {        
  155.         return start + nextWord.key.length;
  156.     }
  157.     return start;
  158. }
  159.  
  160. var gSplitParam = /\t|\n| /;
  161. var gTrim = /[\t|\n|\r| ]*([^\t|\n|\r| ]*)/;
  162.  
  163. function Trim(s)
  164. {
  165.     if (gTrim.exec(s))
  166.     {
  167.         return RegExp.$1;
  168.     }
  169.     return s;
  170. }
  171.  
  172. function ProcessParameters(parameters)
  173. {
  174.     var insert = "";
  175.     var paramarray = parameters.split(',');
  176.     if (paramarray)
  177.     {
  178.         var count = paramarray.length;
  179.         for (var i = 0; i < count; i++)
  180.         {
  181.             if (paramarray[i].length > 0)
  182.             {
  183.                 var entry = paramarray[i];
  184.                 gOutput.writeLine(entry);
  185.                 // Get last word
  186.                 var array = entry.split(gSplitParam);
  187.                 if (array)
  188.                 {
  189.                     var index = array.length - 1;
  190.                     while (array[index].length == 0 && index > 0)
  191.                     {
  192.                         index -= 1;
  193.                     }
  194.                     insert += "\n* @param " + Trim(array[index]) + " description.";
  195.                 }
  196.             }
  197.         }
  198.     }
  199.     return insert;
  200. }
  201.  
  202. var gFirstRegExp = /\s*\/\*/;
  203. var gInnerRegExp = /(\s*\*)/;
  204.  
  205. function CleanupMultilineComment(editor, indentLevel, startLine)
  206. {
  207.     var smartReformat = getMapFileValue("Preferences", "Smart reformat multiline comments", true);
  208.     editor.setIndentLevel(startLine, indentLevel);
  209.     var lineData = editor.copy(startLine);
  210.     if (gFirstRegExp.exec(lineData))
  211.     {
  212.         startLine += 1;
  213.         editor.setIndentLevel(startLine, indentLevel);
  214.         lineData = editor.copy(startLine);        
  215.         while (gInnerRegExp.exec(lineData))
  216.         {
  217.             if (smartReformat)
  218.             {
  219.                 var index = RegExp.$1.length; 
  220.                 editor.replace(" ", startLine, index-1, startLine, index-1);
  221.             }
  222.             startLine += 1;
  223.             editor.setIndentLevel(startLine, indentLevel);
  224.             lineData = editor.copy(startLine);     
  225.         }
  226.     }
  227. }
  228.  
  229. !!/Script
  230.  
  231.