split if: assertion when no spaces present (IDEA-100389)

This commit is contained in:
Anna Kozlova
2013-02-04 14:07:10 +04:00
parent 68f7ee96f7
commit dba040b010
4 changed files with 44 additions and 33 deletions
@@ -26,6 +26,7 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -125,25 +126,28 @@ public class SplitIfAction extends PsiElementBaseIntentionAction {
}
private static PsiExpression getROperands(PsiPolyadicExpression expression, PsiJavaToken separator) throws IncorrectOperationException {
PsiElement next = PsiTreeUtil.skipSiblingsForward(separator.getNextSibling(), PsiWhiteSpace.class, PsiComment.class);
PsiElement next = PsiTreeUtil.skipSiblingsForward(separator, PsiWhiteSpace.class, PsiComment.class);
final int offsetInParent;
if (next == null) {
throw new IncorrectOperationException("Unable to split '"+expression.getText()+"' at '"+separator+"' (offset "+separator.getStartOffsetInParent()+")");
offsetInParent = separator.getStartOffsetInParent() + separator.getTextLength();
} else {
offsetInParent = next.getStartOffsetInParent();
}
PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
String rOperands = expression.getText().substring(next.getStartOffsetInParent());
String rOperands = expression.getText().substring(offsetInParent);
return factory.createExpressionFromText(rOperands, expression.getParent());
}
private static PsiExpression getLOperands(PsiPolyadicExpression expression, PsiJavaToken separator) throws IncorrectOperationException {
PsiElement next = separator;
if (next.getPrevSibling() instanceof PsiWhiteSpace) next = next.getPrevSibling();
if (next == null) {
PsiElement prev = separator;
if (prev.getPrevSibling() instanceof PsiWhiteSpace) prev = prev.getPrevSibling();
if (prev == null) {
throw new IncorrectOperationException("Unable to split '"+expression.getText()+"' left to '"+separator+"' (offset "+separator.getStartOffsetInParent()+")");
}
PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
String rOperands = expression.getText().substring(0, next.getStartOffsetInParent());
String rOperands = expression.getText().substring(0, prev.getStartOffsetInParent());
return factory.createExpressionFromText(rOperands, expression.getParent());
}
@@ -0,0 +1,8 @@
public class SplitCondition {
private static void appendString(StringBuilder builder, boolean condition) {
if (condition) {
if (builder.length() > 0) {
}
}
}
}
@@ -0,0 +1,6 @@
public class SplitCondition {
private static void appendString(StringBuilder builder, boolean condition) {
if (condition&<caret>&builder.length() > 0) {
}
}
}
@@ -10,51 +10,45 @@ import com.intellij.testFramework.LightCodeInsightTestCase;
public class SplitIfActionTest extends LightCodeInsightTestCase {
public void test1() throws Exception {
CodeStyleSettingsManager.getSettings(getProject()).ELSE_ON_NEW_LINE= true;
configureByFile("/codeInsight/splitIfAction/before1.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/after1.java");
doTest();
}
public void test2() throws Exception {
configureByFile("/codeInsight/splitIfAction/before2.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/after2.java");
doTest();
}
public void test3() throws Exception {
configureByFile("/codeInsight/splitIfAction/before3.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/after3.java");
doTest();
}
public void test4() throws Exception {
configureByFile("/codeInsight/splitIfAction/before4.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/after4.java");
doTest();
}
public void test5() throws Exception {
configureByFile("/codeInsight/splitIfAction/before5.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/after5.java");
doTest();
}
public void test6() throws Exception {
configureByFile("/codeInsight/splitIfAction/beforeParenthesis.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/afterParenthesis.java");
public void testParenthesis() throws Exception {
doTest();
}
public void test7() throws Exception {
configureByFile("/codeInsight/splitIfAction/beforeOrParenthesis.java");
perform();
checkResultByFile("/codeInsight/splitIfAction/afterOrParenthesis.java");
public void testOrParenthesis() throws Exception {
doTest();
}
public void testComment() throws Exception {
configureByFile("/codeInsight/splitIfAction/beforeComment.java");
doTest();
}
public void testWithoutSpaces() throws Exception {
doTest();
}
private void doTest() throws Exception {
configureByFile("/codeInsight/splitIfAction/before" + getTestName(false)+ ".java");
perform();
checkResultByFile("/codeInsight/splitIfAction/afterComment.java");
checkResultByFile("/codeInsight/splitIfAction/after" + getTestName(false) + ".java");
}
public void test8() throws Exception {
@@ -64,7 +58,6 @@ public class SplitIfActionTest extends LightCodeInsightTestCase {
}
private void perform() throws Exception {
SplitIfAction action = new SplitIfAction();
assertTrue(action.isAvailable(getProject(), getEditor(), getFile()));