Lambda to callable reference: inspection is optional depending on text length
(cherry picked from commit ce0c43c)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
220141ab2b
commit
a3b52fd8a6
@@ -33,7 +33,12 @@ import org.jetbrains.kotlin.types.isDynamic
|
||||
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||
|
||||
class ConvertLambdaToReferenceInspection : IntentionBasedInspection<KtLambdaExpression>(ConvertLambdaToReferenceIntention())
|
||||
class ConvertLambdaToReferenceInspection(
|
||||
val intention: ConvertLambdaToReferenceIntention = ConvertLambdaToReferenceIntention()
|
||||
) : IntentionBasedInspection<KtLambdaExpression>(
|
||||
intention,
|
||||
{ intention.shouldSuggestToConvert(it) }
|
||||
)
|
||||
|
||||
class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntention<KtLambdaExpression>(
|
||||
KtLambdaExpression::class.java, "Convert lambda to reference"
|
||||
@@ -141,6 +146,12 @@ class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntentio
|
||||
}
|
||||
}
|
||||
|
||||
internal fun shouldSuggestToConvert(element: KtLambdaExpression): Boolean {
|
||||
val body = element.bodyExpression ?: return false
|
||||
val referenceName = buildReferenceText(body.statements.singleOrNull() ?: return false) ?: return false
|
||||
return referenceName.length < element.text.length
|
||||
}
|
||||
|
||||
private fun KtCallExpression.getCallReferencedName() = (calleeExpression as? KtNameReferenceExpression)?.getReferencedName()
|
||||
|
||||
private fun buildReferenceText(expression: KtExpression): String? {
|
||||
@@ -166,7 +177,7 @@ class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntentio
|
||||
override fun applyTo(element: KtLambdaExpression, editor: Editor?) {
|
||||
val body = element.bodyExpression ?: return
|
||||
val referenceName = buildReferenceText(body.statements.singleOrNull() ?: return) ?: return
|
||||
val factory = KtPsiFactory(editor?.project)
|
||||
val factory = KtPsiFactory(element)
|
||||
val lambdaArgument = element.parent as? KtLambdaArgument
|
||||
if (lambdaArgument == null) {
|
||||
// Without lambda argument syntax, just replace lambda with reference
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with function reference</problem_class>
|
||||
<description>Convert lambda to reference</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+1
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.ConvertLambdaToReferenceInspection
|
||||
@@ -0,0 +1,9 @@
|
||||
// Mostly tested as intention. Here we just test shouldSuggestToConvert()
|
||||
|
||||
// Should suggest to convert
|
||||
class TheirWrapper(val x: Int)
|
||||
val x = { y: Int -> TheirWrapper(y) }
|
||||
|
||||
// Should not suggest to convert (too long reference)
|
||||
fun foo(arg: TheirWrapper, convert: (TheirWrapper) -> String) = convert(arg)
|
||||
val y = foo(TheirWrapper(42)) { it.toString() }
|
||||
@@ -148,6 +148,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("convertLambdaToReference/inspectionData/inspections.test")
|
||||
public void testConvertLambdaToReference_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/convertLambdaToReference/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("equalsAndHashCode/inspectionData/inspections.test")
|
||||
public void testEqualsAndHashCode_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/equalsAndHashCode/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user