From ee3f89df873b953848b007504c520801c395feee Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 18 Jan 2018 17:10:15 +0300 Subject: [PATCH] Remove unused function parameter: delete empty constructor accurately So #KT-22221 Fixed Fixes also some quick-fix tests --- .../RemoveUnusedFunctionParameterFix.kt | 31 ++++++++++--------- ...moveUnusedConstructorParameterWithUsage.kt | 6 ++++ ...usedConstructorParameterWithUsage.kt.after | 6 ++++ ...emoveUnusedInternalConstructorParameter.kt | 3 ++ ...nusedInternalConstructorParameter.kt.after | 3 ++ .../idea/quickfix/QuickFixTestGenerated.java | 21 +++++++------ 6 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt create mode 100644 idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt.after create mode 100644 idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt create mode 100644 idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt index 21fc06b85ec..d6f4c4a12c3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt @@ -18,14 +18,14 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import org.jetbrains.kotlin.descriptors.MemberDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors 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.psi.* +import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext @@ -40,19 +40,20 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA val element = element ?: return val primaryConstructor = element.parent?.parent as? KtPrimaryConstructor val parameterList = element.parent as? KtParameterList - if (primaryConstructor != null && parameterList?.parameters?.size == 1 && - (primaryConstructor.getContainingClassOrObject().resolveToDescriptorIfAny() as? MemberDescriptor)?.isExpect == false) { - runWriteAction { - parameterList.delete() - } + val context = element.analyze() + val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, element] as? ValueParameterDescriptor ?: return + ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element) + val nextParameter = parameterList?.parameters?.getOrNull(parameterDescriptor.index) + if (nextParameter != null) { + editor?.caretModel?.moveToOffset(nextParameter.startOffset) } - else { - val context = element.analyze() - val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, element] as? ValueParameterDescriptor ?: return - ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element) - val nextParameter = parameterList?.parameters?.getOrNull(parameterDescriptor.index) - if (editor != null && nextParameter != null) { - editor.caretModel.moveToOffset(nextParameter.startOffset) + if (primaryConstructor != null) { + val removeConstructorIntention = RemoveEmptyPrimaryConstructorIntention() + if (removeConstructorIntention.isApplicableTo(primaryConstructor)) { + editor?.caretModel?.moveToOffset(primaryConstructor.endOffset) + runWriteAction { + removeConstructorIntention.applyTo(primaryConstructor, editor = null) + } } } } @@ -62,7 +63,7 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA val parameter = Errors.UNUSED_PARAMETER.cast(diagnostic).psiElement val parameterOwner = parameter.parent.parent if (parameterOwner is KtFunctionLiteral || - (parameterOwner is KtNamedFunction && parameterOwner.name == null)) return null + (parameterOwner is KtNamedFunction && parameterOwner.name == null)) return null return RemoveUnusedFunctionParameterFix(parameter) } } diff --git a/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt b/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt new file mode 100644 index 00000000000..e59459e76ed --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt @@ -0,0 +1,6 @@ +// "Remove parameter 'x'" "true" + +fun foo() { + X("") +} +class X constructor(x: String) diff --git a/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt.after b/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt.after new file mode 100644 index 00000000000..09e0c5322e9 --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedConstructorParameterWithUsage.kt.after @@ -0,0 +1,6 @@ +// "Remove parameter 'x'" "true" + +fun foo() { + X() +} +class X diff --git a/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt b/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt new file mode 100644 index 00000000000..45180c56d5e --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt @@ -0,0 +1,3 @@ +// "Remove parameter 'x'" "true" + +class X internal constructor(x: String) \ No newline at end of file diff --git a/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt.after b/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt.after new file mode 100644 index 00000000000..352daa46a12 --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedInternalConstructorParameter.kt.after @@ -0,0 +1,3 @@ +// "Remove parameter 'x'" "true" + +class X internal constructor() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index d9481682ec4..90bbb46bdfc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -1563,12 +1563,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { 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") public void testRemoveUnusedExtensionParameter() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedExtensionParameter.kt"); 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") public void testRemoveUnusedParameter() throws Exception { 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") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)