Suggest postfix templates on lambda expressions
This commit is contained in:
@@ -138,10 +138,15 @@ private class KtExpressionPostfixTemplateSelector(
|
||||
.filterIsInstance<KtExpression>()
|
||||
.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
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
{ y: Int -> "abc" }.val<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
val function = { y: Int -> "abc" }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// ALLOW_MULTIPLE_EXPRESSIONS
|
||||
fun bar(x: (Int) -> String) = x(1)
|
||||
fun foo() {
|
||||
bar() { y: Int -> "abc" }.val<caret>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// ALLOW_MULTIPLE_EXPRESSIONS
|
||||
fun bar(x: (Int) -> String) = x(1)
|
||||
fun foo() {
|
||||
val bar = bar() { y: Int -> "abc" }
|
||||
}
|
||||
+4
-1
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
+12
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user