mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
IPP: handle invalid/incomplete code better (IDEA-175928)
This commit is contained in:
@@ -19,7 +19,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ipp.base.MutablyNamedIntention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
@@ -57,20 +56,24 @@ public class ExpandBooleanIntention extends MutablyNamedIntention {
|
||||
if (rhs == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = assignmentExpression.getLExpression();
|
||||
final String rhsText = rhs.getText();
|
||||
final String lhsText = lhs.getText();
|
||||
final PsiJavaToken sign = assignmentExpression.getOperationSign();
|
||||
final String signText = sign.getText();
|
||||
final String conditionText;
|
||||
if (signText.length() == 2) {
|
||||
conditionText = lhsText + signText.charAt(0) + rhsText;
|
||||
final String lhsText = assignmentExpression.getLExpression().getText();
|
||||
final String signText = assignmentExpression.getOperationSign().getText();
|
||||
@NonNls final String newStatementText = "if(" + ((signText.length() == 2) ? lhsText + signText.charAt(0) : "") + "true)" +
|
||||
lhsText + "=true; else " + lhsText + "=false;";
|
||||
final PsiIfStatement newIfStatement =
|
||||
(PsiIfStatement)JavaPsiFacade.getElementFactory(element.getProject()).createStatementFromText(newStatementText, element);
|
||||
final PsiExpression condition = newIfStatement.getCondition();
|
||||
if (condition instanceof PsiBinaryExpression) {
|
||||
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)condition;
|
||||
final PsiExpression operand = binaryExpression.getROperand();
|
||||
if (operand != null) {
|
||||
operand.replace(rhs);
|
||||
}
|
||||
}
|
||||
else {
|
||||
conditionText = rhsText;
|
||||
condition.replace(rhs);
|
||||
}
|
||||
@NonNls final String newStatementText = "if(" + conditionText + ") " + lhsText + " = true; else " + lhsText + " = false;";
|
||||
PsiReplacementUtil.replaceStatement(statement, newStatementText);
|
||||
statement.replace(newIfStatement);
|
||||
}
|
||||
else if (ExpandBooleanPredicate.isBooleanReturn(statement)) {
|
||||
final PsiReturnStatement returnStatement = (PsiReturnStatement)statement;
|
||||
@@ -78,9 +81,13 @@ public class ExpandBooleanIntention extends MutablyNamedIntention {
|
||||
if (returnValue == null) {
|
||||
return;
|
||||
}
|
||||
final String valueText = returnValue.getText();
|
||||
@NonNls final String newStatementText = "if(" + valueText + ") return true; else return false;";
|
||||
PsiReplacementUtil.replaceStatement(statement, newStatementText);
|
||||
@NonNls final String newStatementText = "if(true) return true; else return false;";
|
||||
final PsiIfStatement newIfStatement =
|
||||
(PsiIfStatement)JavaPsiFacade.getElementFactory(element.getProject()).createStatementFromText(newStatementText, element);
|
||||
final PsiExpression condition = newIfStatement.getCondition();
|
||||
assert condition != null;
|
||||
condition.replace(returnValue);
|
||||
statement.replace(newIfStatement);
|
||||
}
|
||||
else if (ExpandBooleanPredicate.isBooleanDeclaration(statement)) {
|
||||
final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)statement;
|
||||
@@ -94,10 +101,14 @@ public class ExpandBooleanIntention extends MutablyNamedIntention {
|
||||
return;
|
||||
}
|
||||
final String name = variable.getName();
|
||||
@NonNls final String newStatementText = "if(" + initializer.getText() + ") " + name +"=true; else " + name + "=false;";
|
||||
@NonNls final String newStatementText = "if(true) " + name +"=true; else " + name + "=false;";
|
||||
final Project project = statement.getProject();
|
||||
final PsiStatement newStatement = JavaPsiFacade.getElementFactory(project).createStatementFromText(newStatementText, statement);
|
||||
final PsiElement newElement = declarationStatement.getParent().addAfter(newStatement, declarationStatement);
|
||||
final PsiIfStatement newIfStatement =
|
||||
(PsiIfStatement)JavaPsiFacade.getElementFactory(project).createStatementFromText(newStatementText, statement);
|
||||
final PsiExpression condition = newIfStatement.getCondition();
|
||||
assert condition != null;
|
||||
condition.replace(initializer);
|
||||
final PsiElement newElement = declarationStatement.getParent().addAfter(newIfStatement, declarationStatement);
|
||||
CodeStyleManager.getInstance(project).reformat(newElement);
|
||||
initializer.delete();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Incomplete3 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b = true;
|
||||
b = other <caret>!= null && other.charAt(x() == 'a';
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Incomplete3 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b = true;
|
||||
if (other != null && other.charAt(x() == 'a') b = true;
|
||||
else b = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Incomplete4 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b = <caret>other != null && other.charAt(x() == 'a';
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Incomplete4 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b;
|
||||
if (other != null && other.charAt(x() == 'a') b = true;
|
||||
else b = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Incomplete5 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b = true;
|
||||
b |= other != null && other.charAt(x() == 'a'<caret>;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Incomplete5 {
|
||||
|
||||
public void foo(String other) {
|
||||
boolean b = true;
|
||||
if (b | (other != null && other.charAt(x() == 'a')) b = true;
|
||||
else b = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class Incomplete6 {
|
||||
|
||||
public boolean foo(String other) {
|
||||
return other != null <caret>&& other.charAt(x() == 'a';
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class Incomplete6 {
|
||||
|
||||
public boolean foo(String other) {
|
||||
if (other != null && other.charAt(x() == 'a') return true;
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
+4
@@ -39,6 +39,10 @@ public class ExpandBooleanIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testIncomplete1() { doTest("Expand boolean return to 'if else'"); }
|
||||
public void testIncomplete2() { doTest("Expand boolean assignment to 'if else'"); }
|
||||
public void testIncomplete3() { doTest("Expand boolean assignment to 'if else'"); }
|
||||
public void testIncomplete4() { doTest(); }
|
||||
public void testIncomplete5() { doTest("Expand boolean assignment to 'if else'"); }
|
||||
public void testIncomplete6() { doTest("Expand boolean return to 'if else'"); }
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
|
||||
Reference in New Issue
Block a user