Quick fix for adding arrayOf wrapper for annotation parameters #KT-10063 Fixed
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.builtins.KotlinBuiltIns
|
||||||
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
|
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||||
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
|
|
||||||
|
class AddArrayOfTypeFix(expression: KtExpression, val expectedType: KotlinType) : KotlinQuickFixAction<KtExpression>(expression) {
|
||||||
|
|
||||||
|
val prefix = if (KotlinBuiltIns.isArray(expectedType)) {
|
||||||
|
"arrayOf"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val name = "$expectedType"
|
||||||
|
"${name.decapitalize()}Of"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getText() = "Add $prefix wrapper"
|
||||||
|
|
||||||
|
override fun getFamilyName() = "Add arrayOf wrapper"
|
||||||
|
|
||||||
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
|
val arrayOfExpression = KtPsiFactory(project).createExpressionByPattern("$0($1)", prefix, element)
|
||||||
|
element.replace(arrayOfExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
||||||
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters1;
|
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters1;
|
||||||
@@ -152,6 +153,16 @@ public class QuickFixFactoryForTypeMismatchError extends KotlinIntentionActionsF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KT-10063: arrayOf() bounding single array element
|
||||||
|
KtAnnotationEntry annotationEntry = PsiTreeUtil.getParentOfType(expression, KtAnnotationEntry.class);
|
||||||
|
if (annotationEntry != null) {
|
||||||
|
if (KotlinBuiltIns.isArray(expectedType) &&
|
||||||
|
TypeUtilsKt.isSubtypeOf(expressionType, expectedType.getArguments().get(0).getType()) ||
|
||||||
|
KotlinBuiltIns.isPrimitiveArray(expectedType)) {
|
||||||
|
actions.add(new AddArrayOfTypeFix(expression, expectedType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ResolvedCall<? extends CallableDescriptor> resolvedCall = CallUtilKt.getParentResolvedCall(expression, context, true);
|
ResolvedCall<? extends CallableDescriptor> resolvedCall = CallUtilKt.getParentResolvedCall(expression, context, true);
|
||||||
if (resolvedCall != null) {
|
if (resolvedCall != null) {
|
||||||
// to fix 'type mismatch' on 'if' branches
|
// to fix 'type mismatch' on 'if' branches
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add arrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val value: Array<String>)
|
||||||
|
|
||||||
|
@ArrAnn(<caret>"123") class My
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add arrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val value: Array<String>)
|
||||||
|
|
||||||
|
@ArrAnn(arrayOf("123")) class My
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add arrayOf wrapper" "false"
|
||||||
|
// ACTION: Add 'value =' to argument
|
||||||
|
// ACTION: Create test
|
||||||
|
// ACTION: Make internal
|
||||||
|
// ACTION: Make private
|
||||||
|
|
||||||
|
@ArrAnn(<caret>"123") class My
|
||||||
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
public @interface ArrAnn {
|
||||||
|
String[] value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add doubleArrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val name: DoubleArray)
|
||||||
|
|
||||||
|
@ArrAnn(name = <caret>3.14) class My
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add doubleArrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val name: DoubleArray)
|
||||||
|
|
||||||
|
@ArrAnn(name = doubleArrayOf(3.14)) class My
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add intArrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val value: IntArray)
|
||||||
|
|
||||||
|
@ArrAnn(<caret>42) class My
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add intArrayOf wrapper" "true"
|
||||||
|
|
||||||
|
annotation class ArrAnn(val value: IntArray)
|
||||||
|
|
||||||
|
@ArrAnn(intArrayOf(42)) class My
|
||||||
@@ -1540,6 +1540,12 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
|||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public static class TypeMismatch extends AbstractQuickFixMultiFileTest {
|
public static class TypeMismatch extends AbstractQuickFixMultiFileTest {
|
||||||
|
@TestMetadata("addArrayOfTypeForJavaAnnotation.before.Main.kt")
|
||||||
|
public void testAddArrayOfTypeForJavaAnnotation() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addArrayOfTypeForJavaAnnotation.before.Main.kt");
|
||||||
|
doTestWithExtraFile(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInTypeMismatch() throws Exception {
|
public void testAllFilesPresentInTypeMismatch() throws Exception {
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6891,6 +6891,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addArrayOfType.kt")
|
||||||
|
public void testAddArrayOfType() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addArrayOfType.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addArrayOfTypeForNamedParameter.kt")
|
||||||
|
public void testAddArrayOfTypeForNamedParameter() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addArrayOfTypeForNamedParameter.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("addExclExclToRemoveNullability.kt")
|
@TestMetadata("addExclExclToRemoveNullability.kt")
|
||||||
public void testAddExclExclToRemoveNullability() throws Exception {
|
public void testAddExclExclToRemoveNullability() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullability.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullability.kt");
|
||||||
@@ -6915,6 +6927,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addIntArrayOf.kt")
|
||||||
|
public void testAddIntArrayOf() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addIntArrayOf.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInTypeMismatch() throws Exception {
|
public void testAllFilesPresentInTypeMismatch() throws Exception {
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user