Force PsiElement reparsing

Hi

Is there an OpenAPI way to force IDEA to reparse a PsiElement, e.g. a PsiMethod? My search efforts to answer this question have been unsuccessful.

The reason I'm asking is that after adding a PsiDocComment to a PsiMethod, method.getDocComment() still returns null until the file gets modified through typing a character or something like that.

Looking forward to any hints on this!

etienne

6 comments

method.getManager().getCodeStyleManager().reformat(method) will cause the reparsing you need (as well as reformatting the method).

--Dave Griffith

0

Thanks, Dave. I'm already doing the reformat on the method but it's not having any effect since the method does not know of the javadoc, yet. Even after applying the reformat, the method does not know of the javadoc, as can be reproduced with the code below:


Only after manually "touching" the class does the method learn about its javadoc.

Any help on how I can force the method to learn about the javadoc would be much appreciated. Or maybe there is a better way to add the method javadoc without running into the problem described above.

etienne

0

This sounds like a bug. You should file a request on it. The only reason
you might want to reparse psi is a document change.

0

Hello etienne,

I think the way you're adding the javadoc is not quite correct. I've experienced
this myself already as well. You should not add the javadoc to the class, but to
the method instead. This is a little simplified example that works for me:

public void createMethodWithDoc(PsiClass clazz, String name, String type, String
doc) throws IncorrectOperationException
{
final PsiManager mgr = clazz.getManager();
final PsiElementFactory ef = mgr.getElementFactory();

final PsiMethod psiMethod = ef.createMethodFromText(
"public " + type + " get" + name + "() {\n" + "// ... \n" +
"}", null);

PsiElement element = clazz.add(psiMethod);
element.addBefore(ef.createDocCommentFromText(
"/** " + doc + "\n"
+ "*/\n", element), element.getFirstChild());

mgr.getCodeStyleManager().reformat(element);
}

HTH,
Sascha

Thanks, Dave. I'm already doing the reformat on the method but it's not having any effect since the method does not know of the javadoc, yet. Even after applying the reformat, the method does not know of the javadoc, as can be reproduced with the code below:

 docComment = elementFactory.createDocCommentFromText(docCommentString, null);
> inMethod.getParent().addBefore(docComment, inMethod);
> System.out.println("inMethod.getDocComment() = " + inMethod.getDocComment()); // prints null
> CodeStyleManager.getInstance(inMethod.getManager()).reformat(inMethod);  // has no effect on javadoc layout
> System.out.println("inMethod.getDocComment() = " + inMethod.getDocComment()); // prints null
> ]]>


Only after manually "touching" the class does the method learn about its javadoc.

Any help on how I can force the method to learn about the javadoc would be much appreciated. Or maybe there is a better way to add the method javadoc without running into the problem described above.

etienne

0

Thanks for the hint, Sascha.
It now works fine!

etienne

0

Please sign in to leave a comment.