Remove unused function parameter: delete empty constructor accurately
So #KT-22221 Fixed Fixes also some quick-fix tests
This commit is contained in:
@@ -18,14 +18,14 @@ package org.jetbrains.kotlin.idea.quickfix
|
|||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.MemberDescriptor
|
|
||||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
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.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
import org.jetbrains.kotlin.idea.intentions.RemoveEmptyPrimaryConstructorIntention
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
|
||||||
@@ -40,19 +40,20 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
|||||||
val element = element ?: return
|
val element = element ?: return
|
||||||
val primaryConstructor = element.parent?.parent as? KtPrimaryConstructor
|
val primaryConstructor = element.parent?.parent as? KtPrimaryConstructor
|
||||||
val parameterList = element.parent as? KtParameterList
|
val parameterList = element.parent as? KtParameterList
|
||||||
if (primaryConstructor != null && parameterList?.parameters?.size == 1 &&
|
val context = element.analyze()
|
||||||
(primaryConstructor.getContainingClassOrObject().resolveToDescriptorIfAny() as? MemberDescriptor)?.isExpect == false) {
|
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, element] as? ValueParameterDescriptor ?: return
|
||||||
runWriteAction {
|
ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element)
|
||||||
parameterList.delete()
|
val nextParameter = parameterList?.parameters?.getOrNull(parameterDescriptor.index)
|
||||||
}
|
if (nextParameter != null) {
|
||||||
|
editor?.caretModel?.moveToOffset(nextParameter.startOffset)
|
||||||
}
|
}
|
||||||
else {
|
if (primaryConstructor != null) {
|
||||||
val context = element.analyze()
|
val removeConstructorIntention = RemoveEmptyPrimaryConstructorIntention()
|
||||||
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, element] as? ValueParameterDescriptor ?: return
|
if (removeConstructorIntention.isApplicableTo(primaryConstructor)) {
|
||||||
ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element)
|
editor?.caretModel?.moveToOffset(primaryConstructor.endOffset)
|
||||||
val nextParameter = parameterList?.parameters?.getOrNull(parameterDescriptor.index)
|
runWriteAction {
|
||||||
if (editor != null && nextParameter != null) {
|
removeConstructorIntention.applyTo(primaryConstructor, editor = null)
|
||||||
editor.caretModel.moveToOffset(nextParameter.startOffset)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +63,7 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
|||||||
val parameter = Errors.UNUSED_PARAMETER.cast(diagnostic).psiElement
|
val parameter = Errors.UNUSED_PARAMETER.cast(diagnostic).psiElement
|
||||||
val parameterOwner = parameter.parent.parent
|
val parameterOwner = parameter.parent.parent
|
||||||
if (parameterOwner is KtFunctionLiteral ||
|
if (parameterOwner is KtFunctionLiteral ||
|
||||||
(parameterOwner is KtNamedFunction && parameterOwner.name == null)) return null
|
(parameterOwner is KtNamedFunction && parameterOwner.name == null)) return null
|
||||||
return RemoveUnusedFunctionParameterFix(parameter)
|
return RemoveUnusedFunctionParameterFix(parameter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
X("")
|
||||||
|
}
|
||||||
|
class X constructor(<caret>x: String)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
X()
|
||||||
|
}
|
||||||
|
class X
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
|
||||||
|
class X internal constructor(<caret>x: String)
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
// "Remove parameter 'x'" "true"
|
||||||
|
|
||||||
|
class X internal constructor()
|
||||||
@@ -1563,12 +1563,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeUnusedConstructorParameterWithUsage.kt")
|
||||||
|
public void testRemoveUnusedConstructorParameterWithUsage() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("removeUnusedExtensionParameter.kt")
|
@TestMetadata("removeUnusedExtensionParameter.kt")
|
||||||
public void testRemoveUnusedExtensionParameter() throws Exception {
|
public void testRemoveUnusedExtensionParameter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedExtensionParameter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedExtensionParameter.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("removeUnusedInternalConstructorParameter.kt")
|
||||||
|
public void testRemoveUnusedInternalConstructorParameter() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("removeUnusedParameter.kt")
|
@TestMetadata("removeUnusedParameter.kt")
|
||||||
public void testRemoveUnusedParameter() throws Exception {
|
public void testRemoveUnusedParameter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedParameter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedParameter.kt");
|
||||||
@@ -3467,15 +3479,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createFunction/fromJava")
|
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
|
||||||
public static class FromJava extends AbstractQuickFixTest {
|
|
||||||
public void testAllFilesPresentInFromJava() throws Exception {
|
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createFunction/fromJava"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors")
|
@TestMetadata("idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user