Provide quick fix for named arguments to varargs in annotations
See more in KT-20171
This commit is contained in:
@@ -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<KtValueArgument>(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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<KtExpression>(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<KtValueArgument>(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<KtExpression>? {
|
||||
val element = diagnostic.psiElement.safeAs<KtExpression>() ?: return null
|
||||
return ReplaceWithArrayCallInAnnotationFix(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = *["alpha", "beta", "omega"])
|
||||
@Some(strings = ["alpha", "beta", "omega"])
|
||||
class My
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Replace with array call" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = *<caret>[])
|
||||
class My
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Replace with array call" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = <caret>[])
|
||||
class My
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Replace with array call" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class Some(vararg val ints: Int)
|
||||
|
||||
@Some(ints = *[1, 2,<caret> 3])
|
||||
class My
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Replace with array call" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class Some(vararg val ints: Int)
|
||||
|
||||
@Some(ints = [1, 2,<caret> 3])
|
||||
class My
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Replace with array call" "true"
|
||||
// LANGUAGE_VERSION: 1.2
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = <caret>"value")
|
||||
class My
|
||||
Vendored
+7
@@ -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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user