
package com.intellij.codeInsight.generation.surroundWith;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.jsp.JspFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;

class JavaWithIfExpressionSurrounder extends JavaExpressionSurrounder{
  public boolean isApplicable(PsiExpression expr) {
    PsiType type = expr.getType();
    if (PsiType.BOOLEAN != type) return false;
    PsiElement parent = expr.getParent();
    if (!(parent instanceof PsiExpressionStatement)) return false;
    final PsiElement element = parent.getParent();
    if (!(element instanceof PsiCodeBlock) && !(PsiUtil.isInJspFile(element)  && element instanceof PsiFile)) return false;
    return true;
  }

  public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
    PsiManager manager = expr.getManager();
    PsiElementFactory factory = manager.getElementFactory();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

    @NonNls String text = "if(a){\nst;\n}";
    PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null);
    ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement);

    ifStatement.getCondition().replace(expr);

    PsiExpressionStatement statement = (PsiExpressionStatement)expr.getParent();
    ifStatement = (PsiIfStatement)statement.replace(ifStatement);

    PsiCodeBlock block = ((PsiBlockStatement)ifStatement.getThenBranch()).getCodeBlock();
    TextRange range = block.getStatements()[0].getTextRange();
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    return new TextRange(range.getStartOffset(), range.getStartOffset());
  }

  public String getTemplateDescription() {
    return CodeInsightBundle.message("surround.with.if.expression.template");
  }
}
@@ -100,24 +100,73 @@
 ightBundle;%0A
+import com.intellij.codeInsight.CodeInsightUtil;%0A
 import com.i
@@ -1852,24 +1852,97 @@
 odeBlock();%0A
+    block = CodeInsightUtil.forcePsiPostprocessAndRestoreElement(block);%0A
     TextRang

document locked by PSI