diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/WrapWithSafeLetCallFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/WrapWithSafeLetCallFix.kt index f5339c078d7..5401379c442 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/WrapWithSafeLetCallFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/WrapWithSafeLetCallFix.kt @@ -28,11 +28,12 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow import org.jetbrains.kotlin.psi.psiUtil.getParentOfType +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.types.typeUtil.isNullabilityMismatch class WrapWithSafeLetCallFix( - expression: KtExpression, - nullableExpression: KtExpression + expression: KtExpression, + nullableExpression: KtExpression ) : KotlinQuickFixAction(expression) { private val nullableExpressionPointer = nullableExpression.createSmartPointer() @@ -43,16 +44,19 @@ class WrapWithSafeLetCallFix( override fun invoke(project: Project, editor: Editor?, file: KtFile) { val element = element ?: return val nullableExpression = nullableExpressionPointer.element ?: return + val qualifiedExpression = element.getQualifiedExpressionForSelector() + val receiverExpression = qualifiedExpression?.receiverExpression val factory = KtPsiFactory(element) val nullableText = nullableExpression.text val validator = NewDeclarationNameValidator(element, nullableExpression, NewDeclarationNameValidator.Target.VARIABLES) val name = KotlinNameSuggester.suggestNameByName("it", validator) nullableExpression.replace(factory.createExpression(name)) + val newExpression: Any = if (receiverExpression != null) "${receiverExpression.text}.${element.text}" else element val wrapped = when (name) { - "it" -> factory.createExpressionByPattern("$0?.let { $1 }", nullableText, element) - else -> factory.createExpressionByPattern("$0?.let { $1 -> $2 }", nullableText, name, element) + "it" -> factory.createExpressionByPattern("$0?.let { $1 }", nullableText, newExpression) + else -> factory.createExpressionByPattern("$0?.let { $1 -> $2 }", nullableText, name, newExpression) } - element.replace(wrapped) + (qualifiedExpression ?: element).replace(wrapped) } object UnsafeFactory : KotlinSingleIntentionActionFactory() { diff --git a/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt new file mode 100644 index 00000000000..ecfe92eb63c --- /dev/null +++ b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt @@ -0,0 +1,6 @@ +// "Wrap with '?.let { ... }' call" "true" +// WITH_RUNTIME + +fun f(s: String, action: (String.() -> Unit)?) { + s.action() +} \ No newline at end of file diff --git a/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt.after b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt.after new file mode 100644 index 00000000000..470884d554f --- /dev/null +++ b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt.after @@ -0,0 +1,6 @@ +// "Wrap with '?.let { ... }' call" "true" +// WITH_RUNTIME + +fun f(s: String, action: (String.() -> Unit)?) { + action?.let { s.it() } +} \ No newline at end of file diff --git a/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt new file mode 100644 index 00000000000..b021c5e584f --- /dev/null +++ b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt @@ -0,0 +1,10 @@ +// "Wrap with '?.let { ... }' call" "true" +// WITH_RUNTIME + +fun f(s: String, action: (String.() -> Unit)?) { + s.foo().bar().action() +} + +fun String.foo() = "" + +fun String.bar() = "" \ No newline at end of file diff --git a/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt.after b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt.after new file mode 100644 index 00000000000..25487a8da95 --- /dev/null +++ b/idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt.after @@ -0,0 +1,10 @@ +// "Wrap with '?.let { ... }' call" "true" +// WITH_RUNTIME + +fun f(s: String, action: (String.() -> Unit)?) { + action?.let { s.foo().bar().it() } +} + +fun String.foo() = "" + +fun String.bar() = "" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 96ae6da89cd..5bb0d6c2af0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -12059,6 +12059,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/testData/quickfix/wrapWithSafeLetCall/extensionMethod.kt"); } + @TestMetadata("extentionFunctionCall.kt") + public void testExtentionFunctionCall() throws Exception { + runTest("idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall.kt"); + } + + @TestMetadata("extentionFunctionCall2.kt") + public void testExtentionFunctionCall2() throws Exception { + runTest("idea/testData/quickfix/wrapWithSafeLetCall/extentionFunctionCall2.kt"); + } + @TestMetadata("insideLet.kt") public void testInsideLet() throws Exception { runTest("idea/testData/quickfix/wrapWithSafeLetCall/insideLet.kt");