How to add a PsiComment

Yikes, overlooking something simple I suppose. Trying to add a comment followed by a field. The field adds OK but the comment refuses. Tried several variations on the last line to no effect.

field = factory.createField(CLASSES, classType.createArrayType());
field.getModifierList().setModifierProperty("static", true);
field.getModifierList().setModifierProperty("final", true);
pdb.add(field);
PsiComment comment = factory.createCommentFromText("// yada yada ", null);
pdb.addBefore(comment, field);

3 comments
Comment actions Permalink

The only idea that comes to my mind is that you probably add the comment to the wrong parent. Use PsiViewer to see the actual structure of PSI.

0
Comment actions Permalink

The only idea that comes to my mind is that you probably add the comment to the wrong parent.


Yep ;)

This should work:

PsiElement anchor = pdb.add(field);
PsiComment comment = factory.createCommentFromText("// yada yada ", null);
pdb.addBefore(comment, anchor);

0
Comment actions Permalink

That worked! Thanks.

0

Please sign in to leave a comment.