diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt index bf66509f216..8c34822f563 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/postfix/KtPostfixTemplateProvider.kt @@ -138,10 +138,15 @@ private class KtExpressionPostfixTemplateSelector( .filterIsInstance() .takeWhile { it !is KtBlockExpression && - it !is KtDeclarationWithBody && !it.isEffectivelyDeclaration() - }.filter { !it.isSelector && it.parent !is KtUserType && !it.isOperationReference && !KtPsiUtil.isAssignment(it) } - .toList() + }.filter { + !it.isSelector && + it.parent !is KtUserType && + !it.isOperationReference && + !KtPsiUtil.isAssignment(it) && + // Both KtLambdaExpression and KtFunctionLiteral have the same offset, so we add only one of them -> KtLambdaExpression + it !is KtFunctionLiteral + }.toList() } private val KtExpression.isOperationReference: Boolean diff --git a/idea/testData/codeInsight/postfix/valAtLamba.kt b/idea/testData/codeInsight/postfix/valAtLamba.kt new file mode 100644 index 00000000000..d313bd8b526 --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAtLamba.kt @@ -0,0 +1,3 @@ +fun foo() { + { y: Int -> "abc" }.val +} diff --git a/idea/testData/codeInsight/postfix/valAtLamba.kt.after b/idea/testData/codeInsight/postfix/valAtLamba.kt.after new file mode 100644 index 00000000000..e97de1876f3 --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAtLamba.kt.after @@ -0,0 +1,3 @@ +fun foo() { + val function = { y: Int -> "abc" } +} diff --git a/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt b/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt new file mode 100644 index 00000000000..29e320f9912 --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt @@ -0,0 +1,5 @@ +// ALLOW_MULTIPLE_EXPRESSIONS +fun bar(x: (Int) -> String) = x(1) +fun foo() { + bar() { y: Int -> "abc" }.val +} diff --git a/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt.after b/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt.after new file mode 100644 index 00000000000..65f9497f7bb --- /dev/null +++ b/idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt.after @@ -0,0 +1,5 @@ +// ALLOW_MULTIPLE_EXPRESSIONS +fun bar(x: (Int) -> String) = x(1) +fun foo() { + val bar = bar() { y: Int -> "abc" } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/AbstractPostfixTemplateProviderTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/AbstractPostfixTemplateProviderTest.kt index 918a29fa45b..8d318d5dedb 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/AbstractPostfixTemplateProviderTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/AbstractPostfixTemplateProviderTest.kt @@ -18,7 +18,9 @@ package org.jetbrains.kotlin.idea.codeInsight.postfix import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.KotlinTestUtils +import java.io.File abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtureTestCase() { @@ -27,11 +29,12 @@ abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtu override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory() protected fun doTest(fileName: String) { + myFixture.configureByFile(fileName) myFixture.type("\t") val previouslySuggestedExpressions = KtPostfixTemplateProvider.previouslySuggestedExpressions - if (previouslySuggestedExpressions.size > 1) { + if (previouslySuggestedExpressions.size > 1 && !InTextDirectivesUtils.isDirectiveDefined(File(fileName).readText(), "ALLOW_MULTIPLE_EXPRESSIONS")) { fail("Only one expression should be suggested, but $previouslySuggestedExpressions were found") } diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java index dc45ac4131c..46066e3dd22 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/postfix/PostfixTemplateProviderTestGenerated.java @@ -155,6 +155,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat doTest(fileName); } + @TestMetadata("valAtLamba.kt") + public void testValAtLamba() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/valAtLamba.kt"); + doTest(fileName); + } + + @TestMetadata("valAtLambaArgumentInCall.kt") + public void testValAtLambaArgumentInCall() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/valAtLambaArgumentInCall.kt"); + doTest(fileName); + } + @TestMetadata("var.kt") public void testVar() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt");