Suggest postfix templates on lambda expressions

This commit is contained in:
Denis Zharkov
2016-09-29 11:35:54 +03:00
parent 54d9b4bee6
commit ba2d2a036e
7 changed files with 40 additions and 4 deletions
@@ -138,10 +138,15 @@ private class KtExpressionPostfixTemplateSelector(
.filterIsInstance<KtExpression>() .filterIsInstance<KtExpression>()
.takeWhile { .takeWhile {
it !is KtBlockExpression && it !is KtBlockExpression &&
it !is KtDeclarationWithBody &&
!it.isEffectivelyDeclaration() !it.isEffectivelyDeclaration()
}.filter { !it.isSelector && it.parent !is KtUserType && !it.isOperationReference && !KtPsiUtil.isAssignment(it) } }.filter {
.toList() !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 private val KtExpression.isOperationReference: Boolean
+3
View File
@@ -0,0 +1,3 @@
fun foo() {
{ y: Int -> "abc" }.val<caret>
}
+3
View File
@@ -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" }
}
@@ -18,7 +18,9 @@ package org.jetbrains.kotlin.idea.codeInsight.postfix
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtureTestCase() { abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtureTestCase() {
@@ -27,11 +29,12 @@ abstract class AbstractPostfixTemplateProviderTest : KotlinLightCodeInsightFixtu
override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory() override fun getTestDataPath() = KotlinTestUtils.getHomeDirectory()
protected fun doTest(fileName: String) { protected fun doTest(fileName: String) {
myFixture.configureByFile(fileName) myFixture.configureByFile(fileName)
myFixture.type("\t") myFixture.type("\t")
val previouslySuggestedExpressions = KtPostfixTemplateProvider.previouslySuggestedExpressions 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") fail("Only one expression should be suggested, but $previouslySuggestedExpressions were found")
} }
@@ -155,6 +155,18 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
doTest(fileName); 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") @TestMetadata("var.kt")
public void testVar() throws Exception { public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/postfix/var.kt");