J2K: generating of elvis operators instead of if's
This commit is contained in:
+6
-2
@@ -60,6 +60,11 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetIfExpression, editor: Editor) {
|
||||
val elvis = applyTo(element)
|
||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||
}
|
||||
|
||||
public fun applyTo(element: JetIfExpression): JetBinaryExpression {
|
||||
val condition = element.getCondition() as JetBinaryExpression
|
||||
|
||||
val thenClause = checkNotNull(element.getThen(), "The then clause cannot be null")
|
||||
@@ -80,7 +85,6 @@ public class IfThenToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
|
||||
assert(resultingExpression is JetBinaryExpression,
|
||||
"Unexpected expression type: ${resultingExpression?.javaClass}, expected JetBinaryExpression, element = '${element.getText()}'")
|
||||
|
||||
val elvis = resultingExpression as JetBinaryExpression
|
||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||
return resultingExpression as JetBinaryExpression
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArguments
|
||||
import org.jetbrains.kotlin.idea.intentions.SimplifyNegatedBinaryExpressionIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveRightPartOfBinaryExpressionFix
|
||||
import org.jetbrains.kotlin.j2k.PostProcessor
|
||||
@@ -69,9 +70,20 @@ public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : Pos
|
||||
override fun visitIfExpression(expression: JetIfExpression) {
|
||||
super.visitIfExpression(expression)
|
||||
|
||||
val intention = IfThenToSafeAccessIntention()
|
||||
if (intention.isApplicableTo(expression)) {
|
||||
intention.applyTo(expression)
|
||||
run {
|
||||
val intention = IfThenToSafeAccessIntention()
|
||||
if (intention.isApplicableTo(expression)) {
|
||||
intention.applyTo(expression)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
run {
|
||||
val intention = IfThenToElvisIntention()
|
||||
if (intention.isApplicableTo(expression)) {
|
||||
intention.applyTo(expression)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class C {
|
||||
String foo(String s) {
|
||||
return s != null ? s : "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class C {
|
||||
fun foo(s: String?): String {
|
||||
return s ?: ""
|
||||
}
|
||||
}
|
||||
@@ -982,6 +982,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/codeSimplifications"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("IfToElvis.java")
|
||||
public void testIfToElvis() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/IfToElvis.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("IfToSafeCall.java")
|
||||
public void testIfToSafeCall() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/IfToSafeCall.java");
|
||||
|
||||
@@ -982,6 +982,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/codeSimplifications"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("IfToElvis.java")
|
||||
public void testIfToElvis() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/IfToElvis.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("IfToSafeCall.java")
|
||||
public void testIfToSafeCall() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/IfToSafeCall.java");
|
||||
|
||||
Reference in New Issue
Block a user