Fix "Specify explicit lambda signature" when no parameters exists
So #KT-15075 Fixed
This commit is contained in:
@@ -345,8 +345,10 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
|
||||
|
||||
fun createTypeParameter(text: String) = createTypeParameterList("<$text>").parameters.first()!!
|
||||
|
||||
fun createLambdaParameterList(text: String) =
|
||||
createLambdaExpression(text, "0").functionLiteral.valueParameterList!!
|
||||
fun createLambdaParameterListIfAny(text: String) =
|
||||
createLambdaExpression(text, "0").functionLiteral.valueParameterList
|
||||
|
||||
fun createLambdaParameterList(text: String) = createLambdaParameterListIfAny(text)!!
|
||||
|
||||
fun createLambdaExpression(parameters: String, body: String): KtLambdaExpression =
|
||||
(if (parameters.isNotEmpty()) createExpression("{ $parameters -> $body }")
|
||||
|
||||
+3
-3
@@ -54,9 +54,9 @@ class SpecifyExplicitLambdaSignatureIntention : SelfTargetingIntention<KtLambdaE
|
||||
.map { "${it.name}: ${IdeDescriptorRenderers.SOURCE_CODE.renderType(it.type)}" }
|
||||
.joinToString(", ")
|
||||
|
||||
val newParameterList = psiFactory.createLambdaParameterList(parameterString)
|
||||
val newParameterList = psiFactory.createLambdaParameterListIfAny(parameterString)
|
||||
val oldParameterList = functionLiteral.valueParameterList
|
||||
if (oldParameterList != null) {
|
||||
if (oldParameterList != null && newParameterList != null) {
|
||||
oldParameterList.replace(newParameterList)
|
||||
}
|
||||
else {
|
||||
@@ -65,7 +65,7 @@ class SpecifyExplicitLambdaSignatureIntention : SelfTargetingIntention<KtLambdaE
|
||||
val addNewline = nextSibling is PsiWhiteSpace && nextSibling.text?.contains("\n") ?: false
|
||||
val (whitespace, arrow) = psiFactory.createWhitespaceAndArrow()
|
||||
functionLiteral.addRangeAfter(whitespace, arrow, openBraceElement)
|
||||
functionLiteral.addAfter(newParameterList, openBraceElement)
|
||||
newParameterList?.let { functionLiteral.addAfter(it, openBraceElement) }
|
||||
if (addNewline) {
|
||||
functionLiteral.addAfter(psiFactory.createNewLine(), openBraceElement)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// IS_APPLICABLE: true
|
||||
// SKIP_ERRORS_BEFORE
|
||||
// ERROR: Modifier 'override' is not applicable to 'local function'
|
||||
// ERROR: Type mismatch: inferred type is () -> Unit but Base was expected
|
||||
|
||||
// See also KT-15075
|
||||
|
||||
interface Base {
|
||||
fun foo()
|
||||
}
|
||||
|
||||
class Derived : Base by <caret>{
|
||||
override fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// IS_APPLICABLE: true
|
||||
// SKIP_ERRORS_BEFORE
|
||||
// ERROR: Modifier 'override' is not applicable to 'local function'
|
||||
// ERROR: Type mismatch: inferred type is () -> Unit but Base was expected
|
||||
|
||||
// See also KT-15075
|
||||
|
||||
interface Base {
|
||||
fun foo()
|
||||
}
|
||||
|
||||
class Derived : Base by {
|
||||
->
|
||||
override fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo() {
|
||||
run <caret>{
|
||||
bar()
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo() {
|
||||
run {
|
||||
->
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -14218,6 +14218,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("delegate.kt")
|
||||
public void testDelegate() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyExplicitLambdaSignature/delegate.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyParamListWithBrackets.kt")
|
||||
public void testEmptyParamListWithBrackets() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyExplicitLambdaSignature/emptyParamListWithBrackets.kt");
|
||||
@@ -14266,6 +14272,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("noParameters.kt")
|
||||
public void testNoParameters() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyExplicitLambdaSignature/noParameters.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("shortenReferencesForParams.kt")
|
||||
public void testShortenReferencesForParams() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/specifyExplicitLambdaSignature/shortenReferencesForParams.kt");
|
||||
|
||||
Reference in New Issue
Block a user