mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[niels] groovy if inversion should drop empty else(cherry picked from commit 389a0cd)
This commit is contained in:
+22
-10
@@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.Intention;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
@@ -31,8 +32,6 @@ public class InvertIfIntention extends Intention {
|
||||
GrIfStatement parentIf = (GrIfStatement)parent;
|
||||
GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
|
||||
|
||||
GrStatement thenBranch = parentIf.getThenBranch();
|
||||
GrStatement elseBranch = parentIf.getElseBranch();
|
||||
|
||||
GrExpression condition = parentIf.getCondition();
|
||||
if (condition == null) {
|
||||
@@ -47,30 +46,43 @@ public class InvertIfIntention extends Intention {
|
||||
}
|
||||
}
|
||||
|
||||
if (negatedCondition==null) {
|
||||
if (negatedCondition == null) {
|
||||
// Now check whether this is a simple expression
|
||||
condition = stripParenthesis(condition);
|
||||
String negatedExpressionText;
|
||||
if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
|
||||
negatedExpressionText = "!" + condition.getText();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
negatedExpressionText = "!(" + condition.getText() + ")";
|
||||
}
|
||||
negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
|
||||
}
|
||||
|
||||
GrIfStatement newIf = (GrIfStatement)groovyPsiElementFactory.createStatementFromText(
|
||||
"if (" + negatedCondition.getText() + ") " +
|
||||
(elseBranch != null ? elseBranch.getText() : "{}") + " else " +
|
||||
(thenBranch != null ? thenBranch.getText() : "{}"), parentIf.getContext()
|
||||
);
|
||||
|
||||
GrStatement thenBranch = parentIf.getThenBranch();
|
||||
GrStatement elseBranch = parentIf.getElseBranch();
|
||||
String newIfText = "if (" + negatedCondition.getText() + ") " + (elseBranch != null ? elseBranch.getText() : "{}");
|
||||
|
||||
boolean isThenEmpty = thenBranch == null || (thenBranch instanceof GrBlockStatement) && ((GrBlockStatement)thenBranch).getBlock().getStatements().length == 0;
|
||||
if (!isThenEmpty) {
|
||||
newIfText += " else " + thenBranch.getText();
|
||||
}
|
||||
|
||||
|
||||
GrIfStatement newIf = (GrIfStatement)groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
|
||||
|
||||
parentIf.replace(newIf);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static GrExpression stripParenthesis(GrExpression operand) {
|
||||
while (operand instanceof GrParenthesizedExpression) {
|
||||
operand = ((GrParenthesizedExpression) operand).getOperand();
|
||||
GrExpression innerExpression = ((GrParenthesizedExpression)operand).getOperand();
|
||||
if (innerExpression == null) {
|
||||
break;
|
||||
}
|
||||
operand = innerExpression;
|
||||
}
|
||||
return operand;
|
||||
}
|
||||
|
||||
@@ -112,4 +112,16 @@ i<caret>f (a) {
|
||||
'''
|
||||
}
|
||||
|
||||
public void testEmptyThenBlockIsRemoved() throws Exception {
|
||||
doTest '''
|
||||
i<caret>f (a) {
|
||||
} else {
|
||||
no_succes
|
||||
}
|
||||
''', '''i<caret>f (!a) {
|
||||
no_succes
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user