From 41f5a3e8646a14a10a3f91a088f066055910f896 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 12 Jan 2017 21:06:35 +0300 Subject: [PATCH] Support "Change parameter type" for parameters with type-mismatched default value #KT-15627 Fixed --- ChangeLog.md | 1 + .../idea/quickfix/QuickFixFactoryForTypeMismatchError.kt | 7 +++++++ .../quickfix/typeMismatch/parameterDefaultValue.kt | 4 ++++ .../quickfix/typeMismatch/parameterDefaultValue.kt.after | 4 ++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 5 files changed, 22 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt create mode 100644 idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt.after diff --git a/ChangeLog.md b/ChangeLog.md index 90682e54f40..70a465f2d77 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -721,6 +721,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - Implement quickfix which enables/disables coroutine support in module or project - [`KT-15056`](https://youtrack.jetbrains.com/issue/KT-15056) Implement intention which converts object literal to class - [`KT-8855`](https://youtrack.jetbrains.com/issue/KT-8855) Implement "Create label" quick fix +- [`KT-15627`](https://youtrack.jetbrains.com/issue/KT-15627) Support "Change parameter type" for parameters with type-mismatched default value ## 1.0.6 diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt index 31aab833727..41abd385d79 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil import org.jetbrains.kotlin.idea.util.approximateWithResolvableType import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunction import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument @@ -182,6 +183,12 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() { } } + diagnosticElement.getStrictParentOfType()?.let { + if (it.defaultValue == diagnosticElement) { + actions.add(ChangeParameterTypeFix(it, expressionType)) + } + } + val resolvedCall = diagnosticElement.getParentResolvedCall(context, true) if (resolvedCall != null) { // to fix 'type mismatch' on 'if' branches diff --git a/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt b/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt new file mode 100644 index 00000000000..5c03a630e36 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt @@ -0,0 +1,4 @@ +// "Change parameter 's' type of function 'foo' to 'Int'" "true" +val ONE = 1 + +fun foo(s: String = ONE + 1) {} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt.after b/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt.after new file mode 100644 index 00000000000..9a0d1468da8 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt.after @@ -0,0 +1,4 @@ +// "Change parameter 's' type of function 'foo' to 'Int'" "true" +val ONE = 1 + +fun foo(s: Int = ONE + 1) {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 83746fda7ed..c514b72fdd2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -9555,6 +9555,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("parameterDefaultValue.kt") + public void testParameterDefaultValue() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/parameterDefaultValue.kt"); + doTest(fileName); + } + @TestMetadata("propertyTypeMismatch.kt") public void testPropertyTypeMismatch() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/propertyTypeMismatch.kt");