/*
 * Copyright 2003-2005 Dave Griffith
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ipp.increment;

import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.IntentionPowerPackBundle;
import org.jetbrains.annotations.NotNull;

public class ExtractIncrementIntention extends MutablyNamedIntention {

    public String getTextForElement(PsiElement element) {
        final PsiJavaToken sign;
        if (element instanceof PsiPostfixExpression) {
            sign = ((PsiPostfixExpression)element).getOperationSign();
        } else {
            sign = ((PsiPrefixExpression)element).getOperationSign();
        }
        final String operator = sign.getText();
        return IntentionPowerPackBundle.message(
                "extract.increment.intention.name", operator);
    }

    @NotNull
    public PsiElementPredicate getElementPredicate() {
        return new ExtractIncrementPredicate();
    }

    public void processIntention(PsiElement element)
            throws IncorrectOperationException {
        final PsiExpression operand;
        if (element instanceof PsiPostfixExpression) {
            operand = ((PsiPostfixExpression)element).getOperand();
        } else {
            operand = ((PsiPrefixExpression)element).getOperand();
        }
        if (operand == null) {
            return;
        }
        final PsiStatement statement =
                PsiTreeUtil.getParentOfType(element, PsiStatement.class);
        assert statement != null;
        final PsiElement parent = statement.getParent();
        if (parent == null) {
            return;
        }
        final PsiManager manager = element.getManager();
        final PsiElementFactory factory = manager.getElementFactory();
        final String newStatementText = element.getText() + ';';
        final PsiStatement newCall =
                factory.createStatementFromText(newStatementText, null);
        final PsiElement insertedElement;
        if (element instanceof PsiPostfixExpression) {
            insertedElement = parent.addAfter(newCall, statement);
        } else {
            insertedElement = parent.addBefore(newCall, statement);
        }
        final CodeStyleManager codeStyleManager = manager.getCodeStyleManager();
        codeStyleManager.reformat(insertedElement);
        replaceExpression(operand.getText(), (PsiExpression)element);
    }
}
@@ -796,24 +796,67 @@
 nException;%0A
+import com.siyeh.IntentionPowerPackBundle;%0A
 import com.s
@@ -943,51 +943,8 @@
 te;%0A
-import com.siyeh.IntentionPowerPackBundle;%0A
 impo
@@ -2075,38 +2075,32 @@
       %7D%0A        
-final 
 PsiStatement sta
@@ -2107,32 +2107,16 @@
 tement =
-%0A               
  PsiTree
@@ -2200,38 +2200,32 @@
 = null;%0A        
-final 
 PsiElement paren
@@ -2303,32 +2303,33 @@
 turn;%0A        %7D%0A
+%0A
         final Ps
@@ -2439,16 +2439,988 @@
 ctory();
+%0A%0A        if (parent instanceof PsiIfStatement %7C%7C parent instanceof PsiLoopStatement) %7B%0A            int elementOffsetInStatement = element.getTextRange().getStartOffset() - statement.getTextRange().getStartOffset();%0A            int elementLength = element.getTextLength();%0A%0A            assert elementOffsetInStatement %3E= 0;%0A            assert elementLength %3E 0;%0A%0A            PsiCodeBlock codeBlock = factory.createCodeBlockFromText(%22%7B %22 + statement.getText() + %22%7D%22, parent);%0A            codeBlock = (PsiCodeBlock) statement.replace(codeBlock);%0A            statement = codeBlock.getStatements()%5B0%5D;%0A            parent = statement.getParent();%0A%0A            element = statement.getContainingFile().findElementAt(statement.getTextRange().getStartOffset() + elementOffsetInStatement);%0A%0A            while (element != null && element.getTextLength() %3C elementLength) element = element.getParent();%0A            if (element == null) return; // Shall not happen BTW%0A        %7D%0A      
 %0A       

Fix for the problem reported at JavaLobby.