From eed5b3f1d1d3b963b13060c3a18bc09016744f65 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 17 Dec 2019 13:35:44 +0300 Subject: [PATCH] Add quickfix for removing redundant spread operator #KT-25306 Fixed --- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 1 + .../RemoveRedundantSpreadOperatorFix.kt | 34 +++++++++++++++++++ .../removeRedundantSpreadOperator/simple.kt | 8 +++++ .../simple.kt.after | 8 +++++ .../QuickFixMultiFileTestGenerated.java | 13 +++++++ .../idea/quickfix/QuickFixTestGenerated.java | 18 ++++++++++ 6 files changed, 82 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantSpreadOperatorFix.kt create mode 100644 idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt create mode 100644 idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 76f6a28fc3a..d978daaa6fd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -579,6 +579,7 @@ class QuickFixRegistrar : QuickFixContributor { ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR.registerFactory(SurroundWithArrayOfWithSpreadOperatorInFunctionFix) REDUNDANT_SPREAD_OPERATOR_IN_NAMED_FORM_IN_ANNOTATION.registerFactory(ReplaceWithArrayCallInAnnotationFix) + REDUNDANT_SPREAD_OPERATOR_IN_NAMED_FORM_IN_FUNCTION.registerFactory(RemoveRedundantSpreadOperatorFix) JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE.registerFactory(KotlinAddRequiredModuleFix) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantSpreadOperatorFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantSpreadOperatorFix.kt new file mode 100644 index 00000000000..8848c35aced --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantSpreadOperatorFix.kt @@ -0,0 +1,34 @@ +/* + * 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.KtExpression +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtValueArgument +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +class RemoveRedundantSpreadOperatorFix(argument: KtExpression) : KotlinQuickFixAction(argument) { + override fun getText(): String = "Remove redundant *" + + override fun getFamilyName(): String = text + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val argument = element?.getParentOfType(false) ?: return + val spreadElement = argument.getSpreadElement() ?: return + spreadElement.delete() + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val element = diagnostic.psiElement.safeAs() ?: return null + return RemoveRedundantSpreadOperatorFix(element) + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt b/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt new file mode 100644 index 00000000000..5bef7dfcf3a --- /dev/null +++ b/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt @@ -0,0 +1,8 @@ +// "Remove redundant *" "true" +// LANGUAGE_VERSION: 1.4 + +fun takeVararg(vararg s: String) {} + +fun test(strings: Array) { + takeVararg(s = *strings) +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt.after b/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt.after new file mode 100644 index 00000000000..c4a6d1452cd --- /dev/null +++ b/idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt.after @@ -0,0 +1,8 @@ +// "Remove redundant *" "true" +// LANGUAGE_VERSION: 1.4 + +fun takeVararg(vararg s: String) {} + +fun test(strings: Array) { + takeVararg(s = strings) +} \ 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 09866d720e0..51e71fb32e3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -3809,6 +3809,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes } } + @TestMetadata("idea/testData/quickfix/removeRedundantSpreadOperator") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveRedundantSpreadOperator extends AbstractQuickFixMultiFileTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath); + } + + public void testAllFilesPresentInRemoveRedundantSpreadOperator() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/removeRedundantSpreadOperator"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true); + } + } + @TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter") @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 143492f81f8..fa2b1717e71 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -10509,6 +10509,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/removeRedundantSpreadOperator") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveRedundantSpreadOperator extends AbstractQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInRemoveRedundantSpreadOperator() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/removeRedundantSpreadOperator"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + runTest("idea/testData/quickfix/removeRedundantSpreadOperator/simple.kt"); + } + } + @TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)