J2K: generate accessor bodies in expression form if possible

This commit is contained in:
Valentin Kipyatkov
2015-09-23 15:11:22 +03:00
parent ee5a853faa
commit c05232bbb7
16 changed files with 58 additions and 94 deletions
@@ -114,7 +114,7 @@ public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependen
is JetDeclaration, is JetLoopExpression -> return null // is JetExpression but does not have value
else -> {
if (statement is JetBinaryExpression && statement.getOperationToken() == JetTokens.EQ) return null // assignment does not have value
if (statement is JetBinaryExpression && statement.operationToken in JetTokens.ALL_ASSIGNMENTS) return null // assignment does not have value
val expressionType = statement.analyze().getType(statement) ?: return null
if (!KotlinBuiltIns.isUnit(expressionType) && !KotlinBuiltIns.isNothing(expressionType)) return null
return statement
@@ -52,6 +52,7 @@ object J2KPostProcessingRegistrar {
_processings.add(ConvertToStringTemplateProcessing())
_processings.add(UsePropertyAccessSyntaxProcessing())
_processings.add(RemoveRedundantSamAdaptersProcessing())
_processings.add(AccessorBodyToExpression())
registerIntentionBasedProcessing(IfThenToSafeAccessIntention()) { applyTo(it) }
registerIntentionBasedProcessing(IfThenToElvisIntention()) { applyTo(it) }
@@ -192,4 +193,14 @@ object J2KPostProcessingRegistrar {
}
}
}
private class AccessorBodyToExpression : J2kPostProcessing {
private val intention = ConvertToExpressionBodyIntention()
override fun createAction(element: JetElement, diagnostics: Diagnostics): (() -> Unit)? {
if (element !is JetPropertyAccessor) return null
if (!intention.isApplicableTo(element)) return null
return { intention.applyTo(element, true) }
}
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
var x = 0
fun <caret>foo() {
x += 1
}
@@ -4027,6 +4027,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("assignPlus.kt")
public void testAssignPlus() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/assignPlus.kt");
doTest(fileName);
}
@TestMetadata("assignment.kt")
public void testAssignment() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/assignment.kt");