"Remove parameter" quick fix: remove also type constraint
#KT-26673 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
59009430e5
commit
455db32199
@@ -23,7 +23,9 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
|
|||||||
import org.jetbrains.kotlin.diagnostics.Errors
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToParameterDescriptorIfAny
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToParameterDescriptorIfAny
|
||||||
import org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention
|
import org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention
|
||||||
|
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
@@ -91,11 +93,22 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun runRemoveUnusedTypeParameters(typeParameters: List<KtTypeParameter>) {
|
fun runRemoveUnusedTypeParameters(typeParameters: List<KtTypeParameter>) {
|
||||||
val unusedTypeParams = typeParameters.filter { ReferencesSearch.search(it).findFirst() == null }
|
val unusedTypeParams = typeParameters.filter { typeParameter ->
|
||||||
|
ReferencesSearch.search(typeParameter).none { (it as? KtSimpleNameReference)?.expression?.parent !is KtTypeConstraint }
|
||||||
|
}
|
||||||
if (unusedTypeParams.isEmpty()) return
|
if (unusedTypeParams.isEmpty()) return
|
||||||
runWriteAction {
|
runWriteAction {
|
||||||
unusedTypeParams.forEach { typeParameter ->
|
unusedTypeParams.forEach { typeParameter ->
|
||||||
val typeParameterList = typeParameter.parent as? KtTypeParameterList ?: return@forEach
|
val typeParameterList = typeParameter.parent as? KtTypeParameterList ?: return@forEach
|
||||||
|
val typeConstraintList = typeParameterList.parent.getChildOfType<KtTypeConstraintList>()
|
||||||
|
if (typeConstraintList != null) {
|
||||||
|
val typeConstraint = typeConstraintList.constraints.find { it.subjectTypeParameterName?.text == typeParameter.text }
|
||||||
|
if (typeConstraint != null) EditCommaSeparatedListHelper.removeItem(typeConstraint)
|
||||||
|
if (typeConstraintList.constraints.size == 0) {
|
||||||
|
val prev = typeConstraintList.getPrevSiblingIgnoringWhitespaceAndComments()
|
||||||
|
if (prev?.node?.elementType == KtTokens.WHERE_KEYWORD) prev?.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
if (typeParameterList.parameters.size == 1)
|
if (typeParameterList.parameters.size == 1)
|
||||||
typeParameterList.delete()
|
typeParameterList.delete()
|
||||||
else
|
else
|
||||||
|
|||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
fun <X> foo(<caret>x: X) where X : Number {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo(1)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
fun foo() {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo()
|
||||||
|
}
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
fun <X, Y> bar(<caret>x: X, y: Y) where X : Number, Y : Number {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
bar(1, 2)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
fun <Y> bar(y: Y) where Y : Number {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
bar(2)
|
||||||
|
}
|
||||||
@@ -1866,6 +1866,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
runTest("idea/testData/quickfix/changeSignature/removeUnusedParameterWithTypeParameter7.kt");
|
runTest("idea/testData/quickfix/changeSignature/removeUnusedParameterWithTypeParameter7.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeUnusedParameterWithTypeParameterAndTypeConstraint.kt")
|
||||||
|
public void testRemoveUnusedParameterWithTypeParameterAndTypeConstraint() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/removeUnusedParameterWithTypeParameterAndTypeConstraint.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeUnusedParameterWithTypeParameterAndTypeConstraint2.kt")
|
||||||
|
public void testRemoveUnusedParameterWithTypeParameterAndTypeConstraint2() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/removeUnusedParameterWithTypeParameterAndTypeConstraint2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("removeUnusedPrimaryConstructorParameter.kt")
|
@TestMetadata("removeUnusedPrimaryConstructorParameter.kt")
|
||||||
public void testRemoveUnusedPrimaryConstructorParameter() throws Exception {
|
public void testRemoveUnusedPrimaryConstructorParameter() throws Exception {
|
||||||
runTest("idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt");
|
runTest("idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user