Thursday, April 07, 2005

JEdit javadoc comment macro

Quick and dirty, but it only works on one line functions. I'll get around to handling multiple lines another time:
//author RR, 7 Apr 2005
//create Javadoc comments for one-liner functions
//parses method signature
//for each param adds @param
//if returns value, prints @return


int funcLine=textArea.getCaretLine();
int cntr=0;
String function = textArea.getLineText(funcLine);
StringTokenizer st = new StringTokenizer(function," ");
String returnType =null;

textArea.goToStartOfLine(false);
textArea.insertEnterAndIndent();
textArea.setSelectedText("/**");
textArea.insertEnterAndIndent();
textArea.setSelectedText("*");
textArea.insertEnterAndIndent();
//parse for return type
while(st.hasMoreTokens()){
String element = st.nextToken();
if(!element.equals("void")&&cntr==1){
returnType = "* @return " +element;
break;
}
cntr++;
}
//parse for params
String params = function.substring(function.indexOf("(")+1,function.length()-1);
StringTokenizer stParams = new StringTokenizer(params," ");
int cntr2=0;
while(stParams.hasMoreTokens()){
String element = stParams.nextToken();
if(cntr2==0||(cntr2 %2==0)){
textArea.setSelectedText("* @param "+element);
textArea.insertEnterAndIndent();
}
cntr2++;
}

if(returnType!=null){
textArea.setSelectedText(returnType);
textArea.insertEnterAndIndent();
}

textArea.setSelectedText("*/");
textArea.insertEnterAndIndent();
textArea.shiftIndentRight();

No comments: