mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-87069 Intentions: Convert ternary operator to if-else
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
if (a) {
|
||||
return b
|
||||
} else {
|
||||
return c
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
return a ? b : c
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<body>
|
||||
<span style="font-family: verdana,serif; font-size: smaller;">
|
||||
This intention replaces <b><font color="#000080">ternary</font></b> statement with
|
||||
an <b><font color="#000080">if else</font></b> statement.
|
||||
</span>
|
||||
</body>
|
||||
</html>
|
||||
@@ -764,6 +764,11 @@
|
||||
<categoryKey>intention.category.groovy/intention.category.control.flow</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.control.InvertIfIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.control.flow</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.control.ReplaceTernaryWithIfElseIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.control.flow</categoryKey>
|
||||
|
||||
+2
@@ -45,6 +45,8 @@ split.if.intention.name=Split into 2 if's
|
||||
split.if.intention.family.name=Split into 2 if's
|
||||
invert.if.intention.name=Invert If Condition
|
||||
invert.if.intention.family.name=Invert If Condition
|
||||
replace.ternary.with.if.else.intention.name=Replace ''?:'' with if else
|
||||
replace.ternary.with.if.else.intention.family.name=Replace ''?:'' with if else
|
||||
flip.conditional.intention.name=Flip ''?:''
|
||||
flip.conditional.intention.family.name=Flip Conditional
|
||||
conditional.to.elvis.intention.name=Convert Conditional to Elvis
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.intentions.control;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
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.branch.GrReturnStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression;
|
||||
|
||||
/**
|
||||
* @author Andreas Arledal
|
||||
*/
|
||||
public class ReplaceTernaryWithIfElseIntention extends Intention {
|
||||
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException {
|
||||
|
||||
GrConditionalExpression parentTernary = findTernary(element);
|
||||
GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
|
||||
|
||||
GrReturnStatement parentReturn = (GrReturnStatement)parentTernary.getParent();
|
||||
|
||||
GrIfStatement ifStatement = (GrIfStatement)groovyPsiElementFactory.createStatementFromText("if (" +
|
||||
parentTernary.getCondition().getText() +
|
||||
") { \nreturn " +
|
||||
parentTernary.getThenBranch().getText() +
|
||||
"\n} else {\n return " +
|
||||
parentTernary.getElseBranch().getText() +
|
||||
"\n}");
|
||||
ifStatement = parentReturn.replaceWithStatement(ifStatement);
|
||||
editor.getCaretModel().moveToOffset(ifStatement.getRParenth().getTextRange().getEndOffset());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new PsiElementPredicate() {
|
||||
@Override
|
||||
public boolean satisfiedBy(PsiElement element) {
|
||||
GrConditionalExpression ternary = findTernary(element);
|
||||
if (ternary == null || element == null || ternary.getThenBranch() == null || ternary.getElseBranch() == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(ternary.getParent() instanceof GrReturnStatement)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static GrConditionalExpression findTernary(PsiElement element) {
|
||||
GrConditionalExpression ternary = PsiTreeUtil.getParentOfType(element, GrConditionalExpression.class);
|
||||
if (ternary == null) {
|
||||
GrReturnStatement ret = PsiTreeUtil.getParentOfType(element, GrReturnStatement.class);
|
||||
if (ret != null && ret.getReturnValue() instanceof GrConditionalExpression) {
|
||||
ternary = (GrConditionalExpression)ret.getReturnValue();
|
||||
}
|
||||
}
|
||||
return ternary;
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.intentions
|
||||
|
||||
/**
|
||||
* @author Andreas Arledal
|
||||
*/
|
||||
class ReplaceTernaryWithIfElseTest extends GrIntentionTestCase {
|
||||
|
||||
String intentionName = GroovyIntentionsBundle.message("replace.ternary.with.if.else.intention.name")
|
||||
|
||||
// public void testDoNotTriggerOnIncompleteIf() throws Exception {
|
||||
// doAntiTest '''
|
||||
//i<caret>f () {
|
||||
// succes
|
||||
//} else {
|
||||
// no_succes
|
||||
//}
|
||||
//''', intentionName
|
||||
//
|
||||
// }
|
||||
|
||||
public void testDoNotTriggerOnIncompleteTernary() throws Exception {
|
||||
doAntiTest '''
|
||||
return aaa ? <caret>bbb
|
||||
''', intentionName
|
||||
}
|
||||
|
||||
private void doTest(String before, String after) {
|
||||
|
||||
doTextTest before, intentionName, after
|
||||
}
|
||||
|
||||
public void testSimpleCondition() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return aaa <caret>? bbb : ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretAfterQuestionMark() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return aaa ?<caret> bbb : ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretInfrontOfConditional() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return <caret>aaa ? bbb : ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretInfrontOfElse() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return aaa ? bbb <caret>: ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretAfterElse() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return aaa ? bbb :<caret> ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretBeforeElseReturn() throws Exception {
|
||||
|
||||
doTest '''
|
||||
return aaa ? bbb : <caret>ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public void testCaretBeforeReturnStatement() throws Exception {
|
||||
|
||||
doTest '''
|
||||
<caret>return aaa ? bbb : ccc
|
||||
''', '''\
|
||||
if (aaa)<caret> {
|
||||
return bbb
|
||||
} else {
|
||||
return ccc
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user