From c0f5cafbd87f37c6a6e03d0edb03563ab99cb8fe Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 26 Jan 2017 15:23:26 +0300 Subject: [PATCH] Set type reference: destructuring support #KT-14994 Fixed --- .../frontend/src/org/jetbrains/kotlin/psi/TypeRefHelpers.kt | 4 +++- .../specifyTypeExplicitly/destructuringInLambda.kt | 3 +++ .../specifyTypeExplicitly/destructuringInLambda.kt.after | 3 +++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt create mode 100644 idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt.after diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/TypeRefHelpers.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/TypeRefHelpers.kt index 5ea806741a3..3397d95db40 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/TypeRefHelpers.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/TypeRefHelpers.kt @@ -38,7 +38,9 @@ fun setTypeReference(declaration: KtCallableDeclaration, addAfter: PsiElement?, return oldTypeRef.replace(typeRef) as KtTypeReference } else { - val anchor = addAfter ?: declaration.nameIdentifier?.siblings(forward = true)?.firstOrNull { it is PsiErrorElement } + val anchor = addAfter + ?: declaration.nameIdentifier?.siblings(forward = true)?.firstOrNull { it is PsiErrorElement } + ?: (declaration as? KtParameter)?.destructuringDeclaration val newTypeRef = declaration.addAfter(typeRef, anchor) as KtTypeReference declaration.addAfter(KtPsiFactory(declaration.project).createColon(), anchor) return newTypeRef diff --git a/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt b/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt new file mode 100644 index 00000000000..ee4f49cb088 --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt @@ -0,0 +1,3 @@ +data class A(val x: String, val y: String) +fun foo(a: A, block: (Int, A, String) -> String): String = block(1, a, "#") +fun bb() = foo(A("O", "K")) { i: Int, (x, y), v: String -> i.toString() + x + y + v } \ No newline at end of file diff --git a/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt.after b/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt.after new file mode 100644 index 00000000000..21a2dc2ed24 --- /dev/null +++ b/idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt.after @@ -0,0 +1,3 @@ +data class A(val x: String, val y: String) +fun foo(a: A, block: (Int, A, String) -> String): String = block(1, a, "#") +fun bb() = foo(A("O", "K")) { i: Int, (x, y): A, v: String -> i.toString() + x + y + v } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 41a3d12e0aa..5a32d2004e5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13867,6 +13867,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("destructuringInLambda.kt") + public void testDestructuringInLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/destructuringInLambda.kt"); + doTest(fileName); + } + @TestMetadata("enumType.kt") public void testEnumType() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyTypeExplicitly/enumType.kt");