KT-15846 'Change lambda expression return type' quick fix does nothing (#3182)

* Change parameter type quickfix: fix it works correctly on property delegate

#KT-15846 Fixed

* Change parameter type quickfix: fix it works correctly when function literal has trailing comments

#KT-15846 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-06-11 18:07:44 +09:00
committed by GitHub
parent 5ab05e6e47
commit 957a927790
7 changed files with 74 additions and 2 deletions
@@ -167,7 +167,14 @@ public class QuickFixUtil {
public static boolean canFunctionOrGetterReturnExpression(@NotNull KtDeclaration functionOrGetter, @NotNull KtExpression expression) {
if (functionOrGetter instanceof KtFunctionLiteral) {
KtBlockExpression functionLiteralBody = ((KtFunctionLiteral) functionOrGetter).getBodyExpression();
PsiElement returnedElement = functionLiteralBody == null ? null : functionLiteralBody.getLastChild();
PsiElement returnedElement = null;
if (functionLiteralBody != null) {
PsiElement[] children = functionLiteralBody.getChildren();
int length = children.length;
if (length > 0) {
returnedElement = children[length - 1];
}
}
return returnedElement instanceof KtExpression && canEvaluateTo((KtExpression) returnedElement, expression);
}
else {
@@ -53,7 +53,8 @@ class ChangeFunctionLiteralReturnTypeFix(
val correspondingProperty = PsiTreeUtil.getParentOfType(functionLiteralExpression, KtProperty::class.java)
if (correspondingProperty != null &&
correspondingProperty.initializer?.let { QuickFixUtil.canEvaluateTo(it, functionLiteralExpression) } ?: true
correspondingProperty.delegate == null &&
correspondingProperty.initializer?.let { QuickFixUtil.canEvaluateTo(it, functionLiteralExpression) } != false
) {
val correspondingPropertyTypeRef = correspondingProperty.typeReference
val propertyType = context.get(BindingContext.TYPE, correspondingPropertyTypeRef)
@@ -0,0 +1,8 @@
// "Change parameter 'f' type of function 'foo' to '() -> Int'" "true"
fun foo(f: () -> String) {}
fun test() {
foo {
<caret>1 // comment
}
}
@@ -0,0 +1,8 @@
// "Change parameter 'f' type of function 'foo' to '() -> Int'" "true"
fun foo(f: () -> Int) {}
fun test() {
foo {
1 // comment
}
}
@@ -0,0 +1,19 @@
// "Change parameter 'code' type of primary constructor of class 'TestDelegate' to '() -> Logger'" "true"
// WITH_RUNTIME
import kotlin.reflect.KProperty
object Test {
val logger by TestDelegate {
<caret>Logger(LoggerConfig("From delegate"))
}
}
class TestDelegate(val code: () -> LoggerConfig) {
operator fun getValue(kalGlobal: Test, property: KProperty<*>): Any {
return code.invoke()
}
}
data class LoggerConfig(val name: String)
data class Logger(val loggerConfig: LoggerConfig)
@@ -0,0 +1,19 @@
// "Change parameter 'code' type of primary constructor of class 'TestDelegate' to '() -> Logger'" "true"
// WITH_RUNTIME
import kotlin.reflect.KProperty
object Test {
val logger by TestDelegate {
Logger(LoggerConfig("From delegate"))
}
}
class TestDelegate(val code: () -> Logger) {
operator fun getValue(kalGlobal: Test, property: KProperty<*>): Any {
return code.invoke()
}
}
data class LoggerConfig(val name: String)
data class Logger(val loggerConfig: LoggerConfig)
@@ -14331,6 +14331,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterType4.kt");
}
@TestMetadata("changeFunctionParameterTypeWithComment.kt")
public void testChangeFunctionParameterTypeWithComment() throws Exception {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeFunctionParameterTypeWithComment.kt");
}
@TestMetadata("changeParameterTypeLongNameRuntime.kt")
public void testChangeParameterTypeLongNameRuntime() throws Exception {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changeParameterTypeLongNameRuntime.kt");
@@ -14341,6 +14346,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterType.kt");
}
@TestMetadata("changePrimaryConstructorParameterTypeOnPropertyDelegate.kt")
public void testChangePrimaryConstructorParameterTypeOnPropertyDelegate() throws Exception {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/changePrimaryConstructorParameterTypeOnPropertyDelegate.kt");
}
@TestMetadata("multiFakeOverride.kt")
public void testMultiFakeOverride() throws Exception {
runTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/multiFakeOverride.kt");