diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 2cf3e9ae468..acb1731cf6e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -640,5 +640,7 @@ class QuickFixRegistrar : QuickFixContributor { ACCIDENTAL_OVERRIDE.registerFactory(MakePrivateAndOverrideMemberFix.AccidentalOverrideFactory) MUST_BE_INITIALIZED.registerFactory(ChangeVariableMutabilityFix.MUST_BE_INITIALIZED_FACTORY) + + TOO_MANY_ARGUMENTS.registerFactory(RemoveArgumentFix) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveArgumentFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveArgumentFix.kt new file mode 100644 index 00000000000..303fcb26af3 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveArgumentFix.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +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.psi.KtFile +import org.jetbrains.kotlin.psi.KtLambdaArgument +import org.jetbrains.kotlin.psi.KtValueArgument +import org.jetbrains.kotlin.psi.KtValueArgumentList + +class RemoveArgumentFix(argument: KtValueArgument) : KotlinQuickFixAction(argument) { + override fun getText(): String = "Remove argument" + + override fun getFamilyName(): String = text + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val argument = element ?: return + if (argument is KtLambdaArgument) { + argument.delete() + } else { + (argument.parent as? KtValueArgumentList)?.removeArgument(argument) + } + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val argument = diagnostic.psiElement.parent as? KtValueArgument ?: return null + return RemoveArgumentFix(argument) + } + } +} diff --git a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callWithExtraArgs.kt b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callWithExtraArgs.kt index 4b2a7eb468a..d07872555ed 100644 --- a/idea/testData/quickfix/createFromUsage/createClass/callExpression/callWithExtraArgs.kt +++ b/idea/testData/quickfix/createFromUsage/createClass/callExpression/callWithExtraArgs.kt @@ -4,6 +4,7 @@ // ACTION: Create secondary constructor // ERROR: Too many arguments for public constructor Foo(a: Int) defined in Foo // ACTION: Put arguments on separate lines +// ACTION: Remove argument // ACTION: To raw string literal class Foo(a: Int) diff --git a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/callWithTypeArguments.kt b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/callWithTypeArguments.kt index 5707d29f55d..cf8a81dc497 100644 --- a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/callWithTypeArguments.kt +++ b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/callWithTypeArguments.kt @@ -1,6 +1,7 @@ // "Create secondary constructor" "false" // ACTION: Add parameter to constructor 'A' // ACTION: Create function 'A' +// ACTION: Remove argument // ERROR: No type arguments expected for constructor A() // ERROR: Too many arguments for public constructor A() defined in A diff --git a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/superCallNoClass.kt b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/superCallNoClass.kt index e82afcef257..116e75c9409 100644 --- a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/superCallNoClass.kt +++ b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/superCallNoClass.kt @@ -1,6 +1,7 @@ // "Create secondary constructor" "false" // ERROR: Too many arguments for public constructor Any() defined in kotlin.Any // WITH_RUNTIME +// ACTION: Remove argument interface T { diff --git a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/wrongExpectedType.kt b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/wrongExpectedType.kt index c611030b85d..106a2acff68 100644 --- a/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/wrongExpectedType.kt +++ b/idea/testData/quickfix/createFromUsage/createSecondaryConstructor/wrongExpectedType.kt @@ -2,6 +2,7 @@ // ACTION: Add parameter to constructor 'A' // ACTION: Change type of 'b' to 'A' // ACTION: Create function 'A' +// ACTION: Remove argument // ERROR: Type mismatch: inferred type is A but B was expected // ERROR: Too many arguments for public constructor A() defined in A diff --git a/idea/testData/quickfix/removeArgument/constructor.kt b/idea/testData/quickfix/removeArgument/constructor.kt new file mode 100644 index 00000000000..6d93c104feb --- /dev/null +++ b/idea/testData/quickfix/removeArgument/constructor.kt @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + + } +} + +fun main() { + val b = Bar("2", 1, "2") +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/constructor.kt.after b/idea/testData/quickfix/removeArgument/constructor.kt.after new file mode 100644 index 00000000000..f39bc56e554 --- /dev/null +++ b/idea/testData/quickfix/removeArgument/constructor.kt.after @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + + } +} + +fun main() { + val b = Bar("2", 1) +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/function.kt b/idea/testData/quickfix/removeArgument/function.kt new file mode 100644 index 00000000000..41179717fde --- /dev/null +++ b/idea/testData/quickfix/removeArgument/function.kt @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + } +} + +fun main() { + val b = Bar("2", 1) + b.foo("a", 1) +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/function.kt.after b/idea/testData/quickfix/removeArgument/function.kt.after new file mode 100644 index 00000000000..e6b9f21a7a1 --- /dev/null +++ b/idea/testData/quickfix/removeArgument/function.kt.after @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + } +} + +fun main() { + val b = Bar("2", 1) + b.foo("a") +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/lambdaArgument.kt b/idea/testData/quickfix/removeArgument/lambdaArgument.kt new file mode 100644 index 00000000000..da234e708df --- /dev/null +++ b/idea/testData/quickfix/removeArgument/lambdaArgument.kt @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + } +} + +fun main() { + val b = Bar("2", 1) + b.foo("a") {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/lambdaArgument.kt.after b/idea/testData/quickfix/removeArgument/lambdaArgument.kt.after new file mode 100644 index 00000000000..853e2ea3504 --- /dev/null +++ b/idea/testData/quickfix/removeArgument/lambdaArgument.kt.after @@ -0,0 +1,10 @@ +// "Remove argument" "true" +class Bar(s: String, i: Int) { + fun foo(s: String) { + } +} + +fun main() { + val b = Bar("2", 1) + b.foo("a") +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/matchedArguments.kt b/idea/testData/quickfix/removeArgument/matchedArguments.kt new file mode 100644 index 00000000000..fe38f12cb6a --- /dev/null +++ b/idea/testData/quickfix/removeArgument/matchedArguments.kt @@ -0,0 +1,16 @@ +// "Remove argument" "false" +// ACTION: Add 'i =' to argument +// ACTION: Convert to also +// ACTION: Convert to apply +// ACTION: Convert to run +// ACTION: Convert to with +// ACTION: Put arguments on separate lines +class Bar() { + fun foo(s: String, i: Int) { + } +} + +fun main() { + val b = Bar() + b.foo("a", 1) +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeArgument/typeMismatch.kt b/idea/testData/quickfix/removeArgument/typeMismatch.kt new file mode 100644 index 00000000000..f9547c90ca2 --- /dev/null +++ b/idea/testData/quickfix/removeArgument/typeMismatch.kt @@ -0,0 +1,18 @@ +// "Remove argument" "false" +// ACTION: Add 'toString()' call +// ACTION: Change parameter 't' type of function 'foo' to 'Int' +// ACTION: Convert to also +// ACTION: Convert to apply +// ACTION: Convert to run +// ACTION: Convert to with +// ACTION: Put arguments on separate lines +// ERROR: The integer literal does not conform to the expected type String +class Bar() { + fun foo(s: String, vararg t: String) { + } +} + +fun main() { + val b = Bar() + b.foo("a", "b", 1) +} \ 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 73d100a0272..ca5f545e735 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -3723,6 +3723,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes } } + @TestMetadata("idea/testData/quickfix/removeArgument") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveArgument extends AbstractQuickFixMultiFileTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath); + } + + public void testAllFilesPresentInRemoveArgument() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/removeArgument"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true); + } + } + @TestMetadata("idea/testData/quickfix/removeAtFromAnnotationArgument") @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 742fafc6cac..be62564f1fc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -10358,6 +10358,44 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/removeArgument") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveArgument extends AbstractQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInRemoveArgument() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/removeArgument"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true); + } + + @TestMetadata("constructor.kt") + public void testConstructor() throws Exception { + runTest("idea/testData/quickfix/removeArgument/constructor.kt"); + } + + @TestMetadata("function.kt") + public void testFunction() throws Exception { + runTest("idea/testData/quickfix/removeArgument/function.kt"); + } + + @TestMetadata("lambdaArgument.kt") + public void testLambdaArgument() throws Exception { + runTest("idea/testData/quickfix/removeArgument/lambdaArgument.kt"); + } + + @TestMetadata("matchedArguments.kt") + public void testMatchedArguments() throws Exception { + runTest("idea/testData/quickfix/removeArgument/matchedArguments.kt"); + } + + @TestMetadata("typeMismatch.kt") + public void testTypeMismatch() throws Exception { + runTest("idea/testData/quickfix/removeArgument/typeMismatch.kt"); + } + } + @TestMetadata("idea/testData/quickfix/removeAtFromAnnotationArgument") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)