diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/AddContextParameterToExistingContextFix.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/AddContextParameterToExistingContextFix.kt index 12350b56c7ff..d1ae139f3b9f 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/AddContextParameterToExistingContextFix.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/AddContextParameterToExistingContextFix.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtNameReferenceExpression import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.KtUserType +import org.jetbrains.kotlin.psi.KtValueArgument internal class AddContextParameterToExistingContextFix( surroundingCall: KtCallExpression, @@ -41,9 +42,16 @@ internal class AddContextParameterToExistingContextFix( } else { val typeReference = psiFactory.createType(parameterTypeFqNameText) val userType = shortenReferences(typeReference) as? KtUserType - psiFactory.createExpression("TODO(\"Provide $parameterTypeText\")${userType?.let { " as ${it.text}" } ?: ""}") + psiFactory.createArgument( + psiFactory.createExpression("TODO(\"Provide $parameterTypeText\")${userType?.let { " as ${it.text}" } ?: ""}") + ) } - argList.addBefore(newElement, rightParen) + val insertedElement = argList.addBefore(newElement, rightParen) as? KtValueArgument ?: return + if (candidateName == null) { + val insertedExpression = insertedElement.getArgumentExpression() ?: return + updater.moveCaretTo(insertedExpression) + updater.templateBuilder().field(insertedExpression, insertedExpression.text) + } } override fun getActionPresentation(context: ActionContext, element: KtCallExpression): Presentation = diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/SurroundCallWithContextFix.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/SurroundCallWithContextFix.kt index e9502083f4e6..fd050a8e5c44 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/SurroundCallWithContextFix.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/SurroundCallWithContextFix.kt @@ -7,6 +7,7 @@ import com.intellij.modcommand.Presentation import org.jetbrains.kotlin.idea.base.analysis.api.utils.shortenReferences import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinPsiUpdateModCommandAction +import org.jetbrains.kotlin.psi.KtCallExpression import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtPsiFactory @@ -37,7 +38,14 @@ internal class SurroundCallWithContextFix( } val newExpression = psiFactory.createExpression(expressionText) shortenReferences(newExpression) - element.replace(newExpression) + val replace = element.replace(newExpression) as? KtCallExpression + + if (candidateName == null && replace != null) { + val valueArgument = replace.valueArguments.firstOrNull() + val insertedExpression = valueArgument?.getArgumentExpression() ?: return + updater.moveCaretTo(insertedExpression) + updater.templateBuilder().field(insertedExpression, insertedExpression.text) + } } override fun getActionPresentation(context: ActionContext, element: KtExpression): Presentation = diff --git a/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/existingContext/functionWithNoSuitableType.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/existingContext/functionWithNoSuitableType.kt.after index f8e0ce97f99a..df92b79517a3 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/existingContext/functionWithNoSuitableType.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/existingContext/functionWithNoSuitableType.kt.after @@ -4,8 +4,8 @@ context(s: String, l: Long) fun bar() {} fun foo(i: Int) { - context("a", TODO("Provide Long") as Long) { - bar() + context("a", TODO("Provide Long") as Long) { + bar() } } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/surroundWithContext/functionWithNoSuitableType.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/surroundWithContext/functionWithNoSuitableType.kt.after index 7613ebc86017..dc8d11103a58 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/surroundWithContext/functionWithNoSuitableType.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/contextParameter/surroundWithContext/functionWithNoSuitableType.kt.after @@ -4,7 +4,7 @@ context(s: String) fun bar() {} fun foo(i: Int) { - context(TODO("Provide String") as String) { bar() } + context(TODO("Provide String") as String) { bar() } } // FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.SurroundCallWithContextFix \ No newline at end of file