PsiElement : AddBefore adds one empty line between the two elements


I have an action in my plugin that gets input from the user and eventually adds annotation (with the user input as the value) to the element under the caret (only if this element is instance of PsiMethod or PsiClass).
I'm doing it this way:

final PsiAnnotation annotationFromText = JavaPsiFacade.getElementFactory(getProject()).createAnnotationFromText(sb.toString(), parent);
annotatedElement.getParent().addBefore(annotationFromText, annotatedElement);

It works but it adds one line gap between the annotated method (or class) to the annotation which is a little weird...
Am I doing something wrong? How can i avoid this?

1 comment

The PSI structure you are creating does not match the correct Java PSI structure generated by the parser, so the formatter does not handle it correctly. The correct way to add an annotation is to use PsiMember.getModifierList.add() or PsiMember.getModifierList().addAnnotation().


Please sign in to leave a comment.