Add "Wrap with array literal" quick fix for annotation parameters

#KT-28969 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-01-05 13:53:13 +09:00
committed by Mikhail Glukhikh
parent 53b0aa5813
commit d02867a831
5 changed files with 47 additions and 0 deletions
@@ -22,6 +22,7 @@ import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
import org.jetbrains.kotlin.builtins.getFunctionalClassKind
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
@@ -29,6 +30,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.util.approximateWithResolvableType
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.name.FqName
@@ -239,6 +241,9 @@ class QuickFixFactoryForTypeMismatchError : KotlinIntentionActionsFactory() {
|| KotlinBuiltIns.isPrimitiveArray(expectedType)
) {
actions.add(AddArrayOfTypeFix(diagnosticElement, expectedType))
if (diagnosticElement.languageVersionSettings.supportsFeature(LanguageFeature.ArrayLiteralsInAnnotations)) {
actions.add(WrapWithArrayLiteralFix(diagnosticElement))
}
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
class WrapWithArrayLiteralFix(expression: KtExpression) : KotlinQuickFixAction<KtExpression>(expression) {
override fun getFamilyName() = "Wrap with array literal"
override fun getText() = "Wrap with []"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
element.replace(KtPsiFactory(project).createExpressionByPattern("[$0]", element))
}
}
@@ -0,0 +1,6 @@
// "Wrap with []" "true"
annotation class Foo(val value: Array<String>)
@Foo(value = "abc"<caret>)
class Bar
@@ -0,0 +1,6 @@
// "Wrap with []" "true"
annotation class Foo(val value: Array<String>)
@Foo(value = ["abc"])
class Bar
@@ -12394,6 +12394,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/typeMismatch/when3.kt");
}
@TestMetadata("wrapWithArrayLiteral.kt")
public void testWrapWithArrayLiteral() throws Exception {
runTest("idea/testData/quickfix/typeMismatch/wrapWithArrayLiteral.kt");
}
@TestMetadata("idea/testData/quickfix/typeMismatch/casts")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)