package com.siyeh.ipp.braces;

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

public class RemoveBracesIntention extends MutablyNamedIntention
{
	@NotNull
	protected PsiElementPredicate getElementPredicate()
	{
		return new RemoveBracesPredicate();
	}

	public String getFamilyName()
	{
		return "Remove Braces";
	}

	protected String getTextForElement(PsiElement element)
	{
		final PsiElement parent = element.getParent();
		assert parent != null;
		final String keyword;
		if (parent instanceof PsiIfStatement)
		{
			final PsiIfStatement ifStatement = (PsiIfStatement)parent;
			final PsiStatement elseBranch = ifStatement.getElseBranch();
			if (element.equals(elseBranch))
			{
				keyword = "else";
			}
			else
			{
				keyword = "if";
			}
		}
		else
		{
			final PsiElement firstChild = parent.getFirstChild();
			assert firstChild != null;
			keyword = firstChild.getText();
		}
		return "Remove braces from '" + keyword + "' statement";
	}

	protected void processIntention(@NotNull PsiElement element)
		throws IncorrectOperationException
	{
		final PsiBlockStatement blockStatement = (PsiBlockStatement)element;
		final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
		final PsiStatement[] statements = codeBlock.getStatements();
		final String text = statements[0].getText();
		replaceStatement(text, blockStatement);
	}
}
@@ -1423,24 +1423,785 @@
 ();%0A%09%09final 
+PsiStatement statement = statements%5B0%5D;%0A%0A%09%09// handle comments%0A%09%09final PsiElement parent = blockStatement.getParent();%0A%09%09assert parent != null;%0A%09%09final PsiElement grandParent = parent.getParent();%0A%09%09assert grandParent != null;%0A%09%09PsiElement sibling = codeBlock.getFirstChild();%0A%09%09assert sibling != null;%0A%09%09sibling = sibling.getNextSibling();%0A%09%09while (sibling != null && !sibling.equals(statement))%0A%09%09%7B%0A%09%09%09if (sibling instanceof PsiComment)%0A%09%09%09%7B%0A%09%09%09%09grandParent.addBefore(sibling, parent);%0A%09%09%09%7D%0A%09%09%09sibling = sibling.getNextSibling();%0A%09%09%7D%0A%09%09final PsiElement lastChild = blockStatement.getLastChild();%0A%09%09if (lastChild instanceof PsiComment)%0A%09%09%7B%0A%09%09%09final PsiElement nextSibling = parent.getNextSibling();%0A%09%09%09grandParent.addAfter(lastChild, nextSibling);%0A%09%09%7D%0A%0A%09%09final 
 String text 
@@ -2211,20 +2211,16 @@
 tatement
-s%5B0%5D
 .getText

Fix for IDEADEV-1676