Code lines are not indented correctly after reformating action


I have a code in editor for which I'm invoking my plugin action to add custom Annotation:

    public void testXYZ() {

plugin applies following work:

            if (elementAt instanceof PsiMethod)
                final PsiAnnotation psiAnnotation = JavaPsiFacade.getInstance(project).getElementFactory()
                        .createAnnotationFromText(sb.toString(), elementAt);
                elementAt.addBefore(psiAnnotation, elementAt.getFirstChild());

after this 2nd line (Test annotation) and 3rd line (method declaration) are getting deeper indents then suppose to:

    @VerificationNote(ReqIds = {"Control Impl 1"}, desc = "")
            public void testXYZ() {

Do I missed anything? Do I have to do extra work with PsiWhiteSpaces?

By the way, after this even IDE's Code->Reformat Code can't do reformating of those lines.


Comment actions Permalink

But if I'm adding annotation like this :
     elementAt.addBefore(psiAnnotation, elementAt.getFirstChild().getFirstChild());
which is probably more correct way to do it, because elementAt.getFirstChild() in this case is PsiModifierList, but elementAt.getFirstChild().getFirstChild() is PsiAnnotation, but in this case I'm getting exception on addBefore() method:

[  19969]  ERROR - l.source.tree.CompositeElement - Assertion failed: anchorBefore == null || anchorBefore.getTreeParent() == parent
    at com.intellij.openapi.diagnostic.Logger.assertTrue(
    at com.intellij.psi.impl.source.tree.CompositeElement.addChild(
    at com.intellij.psi.impl.source.tree.CompositeElement.addChildren(
    at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.addChildren(
    at com.intellij.psi.impl.source.tree.CompositeElement.addInternal(
    at com.intellij.psi.impl.source.JavaStubPsiElement.addBefore(
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$3.performAction(
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(
    at com.intellij.ide.IdeEventQueue._dispatchEvent(
    at com.intellij.ide.IdeEventQueue._dispatchEvent(
    at com.intellij.ide.IdeEventQueue.dispatchEvent(
    at java.awt.EventDispatchThread.pumpOneEventForFilters(
    at java.awt.EventDispatchThread.pumpEventsForFilter(
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(
    at java.awt.EventDispatchThread.pumpEvents(
    at java.awt.EventDispatchThread.pumpEvents(
[  20016]  ERROR - l.source.tree.CompositeElement - IntelliJ IDEA 10.0.2  Build #IC-103.72
[  20016]  ERROR - l.source.tree.CompositeElement - JDK: 1.6.0_20
[  20016]  ERROR - l.source.tree.CompositeElement - VM: Java HotSpot(TM) Client VM
[  20016]  ERROR - l.source.tree.CompositeElement - Vendor: Sun Microsystems Inc.
[  20016]  ERROR - l.source.tree.CompositeElement - OS: Windows 2003
[  20016]  ERROR - l.source.tree.CompositeElement - Last Action:

Something wrong in my code :)
Thank you.

Comment actions Permalink

Actually the assertion does contain all the necessary information to find out what you're doing wrong.

The correct way to add an annotation to a method is:
PsiModifierList modifierList = psiMethod.getModifierList()

(note that a modifier list can be empty, in which case getFirstChild() will return null and an NPE will be subsequently thrown).

Also note that you almost never need to call reformat() manually after modifying the PSI if you're performing the modifications correctlly. IntelliJ IDEA takes care of the formatting for you.

Comment actions Permalink

Dima, thank you for quick response.
It's working!


Please sign in to leave a comment.