diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt index 36701c19182..cdc801cb7d5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.config.LanguageFeature.ArrayLiteralsInAnnotations import org.jetbrains.kotlin.idea.intentions.isArrayOfMethod import org.jetbrains.kotlin.idea.project.languageVersionSettings import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType class ReplaceArrayOfWithLiteralInspection : AbstractKotlinInspection() { @@ -69,6 +70,10 @@ class ReplaceArrayOfWithLiteralInspection : AbstractKotlinInspection() { override fun applyFix(project: Project, descriptor: ProblemDescriptor) { val calleeExpression = descriptor.psiElement as KtExpression val callExpression = calleeExpression.parent as KtCallExpression + + val valueArgument = callExpression.getParentOfType(false) + valueArgument?.getSpreadElement()?.delete() + val arguments = callExpression.valueArguments val arrayLiteral = KtPsiFactory(callExpression).buildExpression { appendFixedText("[") @@ -80,6 +85,7 @@ class ReplaceArrayOfWithLiteralInspection : AbstractKotlinInspection() { } appendFixedText("]") } as KtCollectionLiteralExpression + callExpression.replace(arrayLiteral) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index e26c27d8aa5..3bf32a3ba8b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -516,5 +516,7 @@ class QuickFixRegistrar : QuickFixContributor { NO_CONSTRUCTOR.registerFactory(RemoveNoConstructorFix) ANNOTATION_USED_AS_ANNOTATION_ARGUMENT.registerFactory(RemoveAtFromAnnotationArgument) + + ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION.registerFactory(ReplaceWithArrayCallInAnnotationFix) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceWithArrayCallInAnnotationFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceWithArrayCallInAnnotationFix.kt new file mode 100644 index 00000000000..8d29c2583fe --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceWithArrayCallInAnnotationFix.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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.* +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +class ReplaceWithArrayCallInAnnotationFix(argument: KtExpression) : KotlinQuickFixAction(argument) { + override fun getText() = "Replace with array call" + + override fun getFamilyName() = text + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val argument = element?.getParentOfType(false) ?: return + val spreadElement = argument.getSpreadElement() + if (spreadElement != null) + spreadElement.delete() + else + surroundWithArrayLiteral(argument) + } + + private fun surroundWithArrayLiteral(argument: KtValueArgument) { + val argumentExpression = argument.getArgumentExpression() ?: return + val factory = KtPsiFactory(argumentExpression) + val surrounded = factory.createExpressionByPattern("[$0]", argumentExpression) + + argumentExpression.replace(surrounded) + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val element = diagnostic.psiElement.safeAs() ?: return null + return ReplaceWithArrayCallInAnnotationFix(element) + } + } +} diff --git a/idea/testData/inspectionsLocal/replaceArrayOfWithLiteral/vararg.kt.after b/idea/testData/inspectionsLocal/replaceArrayOfWithLiteral/vararg.kt.after index 6b1e9b59e1b..294d3e0f3c9 100644 --- a/idea/testData/inspectionsLocal/replaceArrayOfWithLiteral/vararg.kt.after +++ b/idea/testData/inspectionsLocal/replaceArrayOfWithLiteral/vararg.kt.after @@ -2,5 +2,5 @@ annotation class Some(vararg val strings: String) -@Some(strings = *["alpha", "beta", "omega"]) +@Some(strings = ["alpha", "beta", "omega"]) class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt new file mode 100644 index 00000000000..728b5a3b81e --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val strings: String) + +@Some(strings = *[]) +class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt.after b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt.after new file mode 100644 index 00000000000..93273c46e9c --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt.after @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val strings: String) + +@Some(strings = []) +class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt new file mode 100644 index 00000000000..bcebbb9b558 --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val ints: Int) + +@Some(ints = *[1, 2, 3]) +class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt.after b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt.after new file mode 100644 index 00000000000..b440a6ab667 --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt.after @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val ints: Int) + +@Some(ints = [1, 2, 3]) +class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt new file mode 100644 index 00000000000..05b4db95035 --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val strings: String) + +@Some(strings = "value") +class My diff --git a/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt.after b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt.after new file mode 100644 index 00000000000..afc80464996 --- /dev/null +++ b/idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt.after @@ -0,0 +1,7 @@ +// "Replace with array call" "true" +// LANGUAGE_VERSION: 1.2 + +annotation class Some(vararg val strings: String) + +@Some(strings = ["value"]) +class My diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index af726b8a80b..7215eb40544 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -9279,6 +9279,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/replaceWithArrayCallInAnnotation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ReplaceWithArrayCallInAnnotation extends AbstractQuickFixTest { + public void testAllFilesPresentInReplaceWithArrayCallInAnnotation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/replaceWithArrayCallInAnnotation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("emptyLiteral.kt") + public void testEmptyLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/replaceWithArrayCallInAnnotation/emptyLiteral.kt"); + doTest(fileName); + } + + @TestMetadata("literalWithValues.kt") + public void testLiteralWithValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt"); + doTest(fileName); + } + + @TestMetadata("replaceSingleElementInNamedForm.kt") + public void testReplaceSingleElementInNamedForm() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/replaceWithDotCall") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)