diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index a71fc103be3..2debddd0a68 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -567,5 +567,8 @@ class QuickFixRegistrar : QuickFixContributor { CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.registerFactory(MoveMemberToCompanionObjectIntention) NO_COMPANION_OBJECT.registerFactory(AddIsToWhenConditionFix) + + DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE.registerFactory(RemoveDefaultParameterValueFix) + ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS.registerFactory(RemoveDefaultParameterValueFix) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveDefaultParameterValueFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveDefaultParameterValueFix.kt new file mode 100644 index 00000000000..54b982e236f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveDefaultParameterValueFix.kt @@ -0,0 +1,30 @@ +package org.jetbrains.kotlin.idea.quickfix + +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.util.CommentSaver +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtParameter + +class RemoveDefaultParameterValueFix(parameter: KtParameter) : KotlinQuickFixAction(parameter) { + + override fun getText() = "Remove default parameter value" + + override fun getFamilyName() = text + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val parameter = element ?: return + val typeReference = parameter.typeReference ?: return + val defaultValue = parameter.defaultValue ?: return + val commentSaver = CommentSaver(parameter) + parameter.deleteChildRange(typeReference.nextSibling, defaultValue) + commentSaver.restore(parameter) + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? = + (diagnostic.psiElement as? KtParameter)?.let { RemoveDefaultParameterValueFix(it) } + } + +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt b/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt new file mode 100644 index 00000000000..a9f5ed6bb6f --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt @@ -0,0 +1,13 @@ +// "Remove default parameter value" "true" +// DISABLE-ERRORS +interface Foo { + fun test(x: Int, y: Int) +} + +expect class Bar : Foo { + override fun test(x: Int, y: Int) +} + +actual class Bar : Foo { + actual override fun test(x: Int, y: Int = 1) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt.after b/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt.after new file mode 100644 index 00000000000..666e15df9c2 --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt.after @@ -0,0 +1,13 @@ +// "Remove default parameter value" "true" +// DISABLE-ERRORS +interface Foo { + fun test(x: Int, y: Int) +} + +expect class Bar : Foo { + override fun test(x: Int, y: Int) +} + +actual class Bar : Foo { + actual override fun test(x: Int, y: Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt b/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt new file mode 100644 index 00000000000..f2dd18e655e --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt @@ -0,0 +1,8 @@ +// "Remove default parameter value" "true" +open class A { + open fun foo(x: Int, y: Int) {} +} + +class B : A() { + override fun foo(x : Int = 1, y: Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt.after b/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt.after new file mode 100644 index 00000000000..061f7a24bb4 --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt.after @@ -0,0 +1,8 @@ +// "Remove default parameter value" "true" +open class A { + open fun foo(x: Int, y: Int) {} +} + +class B : A() { + override fun foo(x : Int, y: Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt b/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt new file mode 100644 index 00000000000..2b64c4a6a98 --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt @@ -0,0 +1,8 @@ +// "Remove default parameter value" "true" +open class A { + open fun foo(x: Int, y: Int) {} +} + +class B : A() { + override fun foo(x : Int /* comment1 */ = /* comment2 */ 1, y: Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt.after b/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt.after new file mode 100644 index 00000000000..e9b20670561 --- /dev/null +++ b/idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt.after @@ -0,0 +1,8 @@ +// "Remove default parameter value" "true" +open class A { + open fun foo(x: Int, y: Int) {} +} + +class B : A() { + override fun foo(x : Int /* comment1 */ /* comment2 */, y: Int) {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 126a5373b96..5c32647d552 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -3298,6 +3298,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes } } + @TestMetadata("idea/testData/quickfix/removeDefaultParameterValue") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveDefaultParameterValue extends AbstractQuickFixMultiFileTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInRemoveDefaultParameterValue() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeDefaultParameterValue"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true); + } + } + @TestMetadata("idea/testData/quickfix/removeFinalUpperBound") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 6cd2aa7a0d0..cff47e7942d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -8963,6 +8963,34 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/removeDefaultParameterValue") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveDefaultParameterValue extends AbstractQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + @TestMetadata("actualFunctionWithDefaultArguments.kt") + public void testActualFunctionWithDefaultArguments() throws Exception { + runTest("idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt"); + } + + public void testAllFilesPresentInRemoveDefaultParameterValue() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeDefaultParameterValue"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("defaultValueNotAllowedInOverride.kt") + public void testDefaultValueNotAllowedInOverride() throws Exception { + runTest("idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt"); + } + + @TestMetadata("hasComment.kt") + public void testHasComment() throws Exception { + runTest("idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt"); + } + } + @TestMetadata("idea/testData/quickfix/removeFinalUpperBound") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)