Code lines are not indented correctly after reformating action

Hi,

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

    @Test
    public void testXYZ() {
        Assert...
    }

plugin applies following work:

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

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

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

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.

Spasibo.

3 comments
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
java.lang.Throwable
    at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:92)
    at com.intellij.psi.impl.source.tree.CompositeElement.addChild(CompositeElement.java:505)
    at com.intellij.psi.impl.source.tree.CompositeElement.addChildren(CompositeElement.java:609)
    at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.addChildren(CodeEditUtil.java:84)
    at com.intellij.psi.impl.source.tree.CompositeElement.addInternal(CompositeElement.java:367)
    at com.intellij.psi.impl.source.tree.java.MethodElement.addInternal(MethodElement.java:53)
    at com.intellij.psi.impl.source.JavaStubPsiElement.addBefore(JavaStubPsiElement.java:82)
    at com.gs.apg.conductor.plugin.intellij.atc.actions.GenerateAnnotationsAction.insertAnnotation(GenerateAnnotationsAction.java:183)
    at com.gs.apg.conductor.plugin.intellij.atc.actions.GenerateAnnotationsAction.access$000(GenerateAnnotationsAction.java:36)
    at com.gs.apg.conductor.plugin.intellij.atc.actions.GenerateAnnotationsAction$1.run(GenerateAnnotationsAction.java:83)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:844)
    at com.gs.apg.conductor.plugin.intellij.atc.actions.GenerateAnnotationsAction.actionPerformed(GenerateAnnotationsAction.java:79)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$3.performAction(IdeKeyEventDispatcher.java:524)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:564)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(IdeKeyEventDispatcher.java:444)
    at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:197)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:491)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:404)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:368)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
[  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: com.gs.apg.conductor.plugin.intellij.atc.actions.GenerateAnnotationsAction

Something wrong in my code :)
Thank you.

0
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()
modifierList.add(psiAnnotation)

(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.

0
Comment actions Permalink

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

0

Please sign in to leave a comment.