ConvertToAnonymousObjectFix: do not suggest when there is no lambda
#KT-31912 Fixed
This commit is contained in:
committed by
Roman Golyshev
parent
d3c54f664b
commit
09304bbd54
+2
-7
@@ -78,12 +78,7 @@ class SamConversionToAnonymousObjectIntention : SelfTargetingRangeIntention<KtCa
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun convertToAnonymousObject(call: KtCallExpression, functionDescriptor: FunctionDescriptor, functionName: String) {
|
fun convertToAnonymousObject(
|
||||||
val lambda = getLambdaExpression(call) ?: return
|
|
||||||
convertToAnonymousObject(call, lambda, functionDescriptor, functionName)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun convertToAnonymousObject(
|
|
||||||
call: KtCallExpression,
|
call: KtCallExpression,
|
||||||
lambda: KtLambdaExpression,
|
lambda: KtLambdaExpression,
|
||||||
functionDescriptor: FunctionDescriptor,
|
functionDescriptor: FunctionDescriptor,
|
||||||
@@ -108,7 +103,7 @@ class SamConversionToAnonymousObjectIntention : SelfTargetingRangeIntention<KtCa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getLambdaExpression(element: KtCallExpression): KtLambdaExpression? {
|
fun getLambdaExpression(element: KtCallExpression): KtLambdaExpression? {
|
||||||
return element.lambdaArguments.firstOrNull()?.getLambdaExpression()
|
return element.lambdaArguments.firstOrNull()?.getLambdaExpression()
|
||||||
?: element.valueArguments.firstOrNull()?.getArgumentExpression() as? KtLambdaExpression
|
?: element.valueArguments.firstOrNull()?.getArgumentExpression() as? KtLambdaExpression
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,12 @@ class ConvertToAnonymousObjectFix(element: KtNameReferenceExpression) : KotlinQu
|
|||||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
val nameReference = element ?: return
|
val nameReference = element ?: return
|
||||||
val call = nameReference.parent as? KtCallExpression ?: return
|
val call = nameReference.parent as? KtCallExpression ?: return
|
||||||
|
val lambda = SamConversionToAnonymousObjectIntention.getLambdaExpression(call) ?: return
|
||||||
val functionDescriptor = nameReference.analyze().diagnostics.forElement(nameReference).firstNotNullResult {
|
val functionDescriptor = nameReference.analyze().diagnostics.forElement(nameReference).firstNotNullResult {
|
||||||
if (it.factory == Errors.RESOLUTION_TO_CLASSIFIER) getFunctionDescriptor(Errors.RESOLUTION_TO_CLASSIFIER.cast(it)) else null
|
if (it.factory == Errors.RESOLUTION_TO_CLASSIFIER) getFunctionDescriptor(Errors.RESOLUTION_TO_CLASSIFIER.cast(it)) else null
|
||||||
} ?: return
|
} ?: return
|
||||||
val functionName = functionDescriptor.name.asString()
|
val functionName = functionDescriptor.name.asString()
|
||||||
SamConversionToAnonymousObjectIntention.convertToAnonymousObject(call, functionDescriptor, functionName)
|
SamConversionToAnonymousObjectIntention.convertToAnonymousObject(call, lambda, functionDescriptor, functionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
@@ -43,7 +44,8 @@ class ConvertToAnonymousObjectFix(element: KtNameReferenceExpression) : KotlinQu
|
|||||||
val casted = Errors.RESOLUTION_TO_CLASSIFIER.cast(diagnostic)
|
val casted = Errors.RESOLUTION_TO_CLASSIFIER.cast(diagnostic)
|
||||||
if (casted.b != WrongResolutionToClassifier.INTERFACE_AS_FUNCTION) return null
|
if (casted.b != WrongResolutionToClassifier.INTERFACE_AS_FUNCTION) return null
|
||||||
val nameReference = casted.psiElement as? KtNameReferenceExpression ?: return null
|
val nameReference = casted.psiElement as? KtNameReferenceExpression ?: return null
|
||||||
if (nameReference.parent as? KtCallExpression == null) return null
|
val call = nameReference.parent as? KtCallExpression ?: return null
|
||||||
|
if (SamConversionToAnonymousObjectIntention.getLambdaExpression(call) == null) return null
|
||||||
if (getFunctionDescriptor(casted) == null) return null
|
if (getFunctionDescriptor(casted) == null) return null
|
||||||
return ConvertToAnonymousObjectFix(nameReference)
|
return ConvertToAnonymousObjectFix(nameReference)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Convert to anonymous object" "true"
|
||||||
|
interface I {
|
||||||
|
fun foo(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = <caret>I({ "" })
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// "Convert to anonymous object" "true"
|
||||||
|
interface I {
|
||||||
|
fun foo(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = object : I {
|
||||||
|
override fun foo(): String {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// "Convert to anonymous object" "false"
|
||||||
|
// ACTION: Introduce import alias
|
||||||
|
// ACTION: Split property declaration
|
||||||
|
// ERROR: Interface I does not have constructors
|
||||||
|
interface I {
|
||||||
|
fun foo(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = <caret>I()
|
||||||
|
}
|
||||||
@@ -2838,11 +2838,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
runTest("idea/testData/quickfix/convertToAnonymousObject/labeledReturn.kt");
|
runTest("idea/testData/quickfix/convertToAnonymousObject/labeledReturn.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("lambdaInParentheses.kt")
|
||||||
|
public void testLambdaInParentheses() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/convertToAnonymousObject/lambdaInParentheses.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("multiMethod.kt")
|
@TestMetadata("multiMethod.kt")
|
||||||
public void testMultiMethod() throws Exception {
|
public void testMultiMethod() throws Exception {
|
||||||
runTest("idea/testData/quickfix/convertToAnonymousObject/multiMethod.kt");
|
runTest("idea/testData/quickfix/convertToAnonymousObject/multiMethod.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("noLambda.kt")
|
||||||
|
public void testNoLambda() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/convertToAnonymousObject/noLambda.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("parameter.kt")
|
@TestMetadata("parameter.kt")
|
||||||
public void testParameter() throws Exception {
|
public void testParameter() throws Exception {
|
||||||
runTest("idea/testData/quickfix/convertToAnonymousObject/parameter.kt");
|
runTest("idea/testData/quickfix/convertToAnonymousObject/parameter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user