Add quickfix for FINAL_UPPER_BOUND: inline type parameter #KT-13773 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
28d187ca1a
commit
9d768e2375
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
class InlineTypeParameterFix(val typeReference: KtTypeReference) : KotlinQuickFixAction<KtTypeReference>(typeReference) {
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val parameter = typeReference.parent as? KtTypeParameter ?: return
|
||||
val bound = parameter.extendsBound ?: return
|
||||
val parameterList = parameter.parent as? KtTypeParameterList ?: return
|
||||
val parameterListOwner = typeReference.getStrictParentOfType<KtTypeParameterListOwner>() ?: return
|
||||
val context = parameterListOwner.analyzeFully()
|
||||
val parameterDescriptor = context[BindingContext.TYPE_PARAMETER, parameter] ?: return
|
||||
parameterListOwner.forEachDescendantOfType<KtTypeReference>() {
|
||||
val typeElement = it.typeElement
|
||||
val type = context[BindingContext.TYPE, it]
|
||||
if (typeElement != null && type != null && type.constructor.declarationDescriptor == parameterDescriptor) {
|
||||
typeElement.replace(bound)
|
||||
}
|
||||
}
|
||||
|
||||
if (parameterList.parameters.size == 1) {
|
||||
parameterList.delete()
|
||||
}
|
||||
else {
|
||||
EditCommaSeparatedListHelper.removeItem(parameter)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getText() = "Inline type parameter"
|
||||
|
||||
override fun getFamilyName() = text
|
||||
|
||||
companion object Factory : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic) = InlineTypeParameterFix(Errors.FINAL_UPPER_BOUND.cast(diagnostic).psiElement)
|
||||
}
|
||||
}
|
||||
@@ -416,5 +416,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION.registerFactory(AddTypeAnnotationToValueParameterFix)
|
||||
|
||||
UNRESOLVED_REFERENCE.registerFactory(CreateTypeParameterByRefActionFactory)
|
||||
|
||||
FINAL_UPPER_BOUND.registerFactory(InlineTypeParameterFix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC<T : Int<caret>>(val x: T, val y: String)
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC(val x: Int, val y: String)
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC<T : Int<caret>, S : String>(val x: T, val y: String) {
|
||||
var a: T = Int.MAX_VALUE
|
||||
|
||||
fun foo(b: T) {
|
||||
val c: T = Int.MIN_VALUE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC<S : String>(val x: Int, val y: String) {
|
||||
var a: Int = Int.MAX_VALUE
|
||||
|
||||
fun foo(b: Int) {
|
||||
val c: Int = Int.MIN_VALUE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC(val x: Int, val y: String) {
|
||||
fun <S : Int<caret>> foo() {
|
||||
val a: S = Int.MAX_VALUE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Inline type parameter" "true"
|
||||
|
||||
data class DC(val x: Int, val y: String) {
|
||||
fun foo() {
|
||||
val a: Int = Int.MAX_VALUE
|
||||
}
|
||||
}
|
||||
@@ -5464,6 +5464,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/inlineTypeParameterFix")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class InlineTypeParameterFix extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInInlineTypeParameterFix() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/inlineTypeParameterFix"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/inlineTypeParameterFix/basic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("basicMultipleDef.kt")
|
||||
public void testBasicMultipleDef() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/inlineTypeParameterFix/basicMultipleDef.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("function.kt")
|
||||
public void testFunction() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/inlineTypeParameterFix/function.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/insertDelegationCall")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user