ConvertLambdaToReference refactoring

Intention is not registered now; inspection is enabled by default now,
 but has highlight level of "No highlighting"
Inspection does not depend on text length now

So #KT-14335 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-05-03 20:07:00 +03:00
parent 1d722e01f0
commit 759e6643c6
4 changed files with 13 additions and 19 deletions
+2 -7
View File
@@ -1385,11 +1385,6 @@
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertLambdaToReferenceIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertReferenceToLambdaIntention</className> <className>org.jetbrains.kotlin.idea.intentions.ConvertReferenceToLambdaIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
@@ -1953,8 +1948,8 @@
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ConvertLambdaToReferenceInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ConvertLambdaToReferenceInspection"
displayName="Can be replaced with function reference" displayName="Can be replaced with function reference"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="false" enabledByDefault="true"
level="INFO" level="INFORMATION"
language="kotlin" language="kotlin"
/> />
@@ -40,12 +40,7 @@ import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.isUnit
class ConvertLambdaToReferenceInspection : IntentionBasedInspection<KtLambdaExpression>( class ConvertLambdaToReferenceInspection : IntentionBasedInspection<KtLambdaExpression>(ConvertLambdaToReferenceIntention::class)
ConvertLambdaToReferenceIntention::class,
{ it -> ConvertLambdaToReferenceIntention.shouldSuggestToConvert(it) }
) {
override fun inspectionTarget(element: KtLambdaExpression) = element.bodyExpression?.statements?.singleOrNull()
}
open class ConvertLambdaToReferenceIntention(text: String) : open class ConvertLambdaToReferenceIntention(text: String) :
SelfTargetingOffsetIndependentIntention<KtLambdaExpression>(KtLambdaExpression::class.java, text) { SelfTargetingOffsetIndependentIntention<KtLambdaExpression>(KtLambdaExpression::class.java, text) {
@@ -219,10 +214,6 @@ open class ConvertLambdaToReferenceIntention(text: String) :
} }
companion object { companion object {
internal fun shouldSuggestToConvert(element: KtLambdaExpression): Boolean {
val referenceName = buildReferenceText(lambdaExpression = element, shortTypes = true) ?: return false
return referenceName.length < element.text.length
}
private fun buildReferenceText(lambdaExpression: KtLambdaExpression, shortTypes: Boolean): String? { private fun buildReferenceText(lambdaExpression: KtLambdaExpression, shortTypes: Boolean): String? {
val singleStatement = lambdaExpression.singleStatementOrNull() val singleStatement = lambdaExpression.singleStatementOrNull()
@@ -9,7 +9,15 @@
</problem> </problem>
<problem> <problem>
<file>test.kt</file> <file>test.kt</file>
<line>14</line> <line>9</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>
<problem>
<file>test.kt</file>
<line>13</line>
<module>light_idea_test_case</module> <module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" /> <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> <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Can be replaced with function reference</problem_class>
+1 -1
View File
@@ -4,7 +4,7 @@
class TheirWrapper(val x: Int) class TheirWrapper(val x: Int)
val x = { y: Int -> TheirWrapper(y) } val x = { y: Int -> TheirWrapper(y) }
// Should not suggest to convert (too long reference) // Should suggest to convert despite of too long reference
fun foo(arg: TheirWrapper, convert: (TheirWrapper) -> String) = convert(arg) fun foo(arg: TheirWrapper, convert: (TheirWrapper) -> String) = convert(arg)
val y = foo(TheirWrapper(42)) { it.toString() } val y = foo(TheirWrapper(42)) { it.toString() }