diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index cb0bd1a5355..30aff953823 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -58,6 +58,7 @@ import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory import org.jetbrains.kotlin.idea.findUsages.handlers.KotlinFindClassUsagesHandler import org.jetbrains.kotlin.idea.highlighter.markers.hasActualsFor import org.jetbrains.kotlin.idea.imports.importableFqName +import org.jetbrains.kotlin.idea.quickfix.RemoveUnusedFunctionParameterFix import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors import org.jetbrains.kotlin.idea.search.usagesSearch.dataClassComponentFunction @@ -386,9 +387,14 @@ class SafeDeleteFix(declaration: KtDeclaration) : LocalQuickFix { override fun applyFix(project: Project, descriptor: ProblemDescriptor) { val declaration = descriptor.psiElement.getStrictParentOfType() ?: return if (!FileModificationService.getInstance().prepareFileForWrite(declaration.containingFile)) return - ApplicationManager.getApplication().invokeLater( - { SafeDeleteHandler.invoke(project, arrayOf(declaration), false) }, - ModalityState.NON_MODAL - ) + if (declaration is KtParameter && declaration.parent is KtParameterList && declaration.parent?.parent is KtFunction) { + RemoveUnusedFunctionParameterFix(declaration).invoke(project, declaration.findExistingEditor(), declaration.containingKtFile) + } + else { + ApplicationManager.getApplication().invokeLater( + { SafeDeleteHandler.invoke(project, arrayOf(declaration), false) }, + ModalityState.NON_MODAL + ) + } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt index 4aed1248ed7..21fc06b85ec 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUnusedFunctionParameterFix.kt @@ -18,14 +18,16 @@ 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.unsafeResolveToDescriptor -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.psi.KtFunctionLiteral -import org.jetbrains.kotlin.psi.KtNamedFunction -import org.jetbrains.kotlin.psi.KtParameter +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny +import org.jetbrains.kotlin.idea.util.application.runWriteAction +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.BindingContext class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixAction(parameter) { override fun getFamilyName() = ChangeFunctionSignatureFix.FAMILY_NAME @@ -36,8 +38,23 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA override fun invoke(project: Project, editor: Editor?, file: KtFile) { val element = element ?: return - val parameterDescriptor = element.unsafeResolveToDescriptor() as ValueParameterDescriptor - ChangeFunctionSignatureFix.runRemoveParameter(parameterDescriptor, element) + 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() + } + } + 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) + } + } } companion object : KotlinSingleIntentionActionFactory() { diff --git a/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt new file mode 100644 index 00000000000..22ccec67d4e --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt @@ -0,0 +1,3 @@ +// "Remove parameter 'a'" "true" + +class Foo(a: String) \ No newline at end of file diff --git a/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt.after b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt.after new file mode 100644 index 00000000000..f855b30d2d5 --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt.after @@ -0,0 +1,3 @@ +// "Remove parameter 'a'" "true" + +class Foo \ No newline at end of file diff --git a/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt new file mode 100644 index 00000000000..aa5a10c59dc --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt @@ -0,0 +1,3 @@ +// "Remove parameter 'a'" "true" + +class Foo(a: String, b: String) \ No newline at end of file diff --git a/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt.after b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt.after new file mode 100644 index 00000000000..e2442352883 --- /dev/null +++ b/idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt.after @@ -0,0 +1,3 @@ +// "Remove parameter 'a'" "true" + +class Foo(b: String) \ No newline at end of file diff --git a/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter.kt.after b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter.kt.after index 642ef76bdd0..9b98919ab29 100644 --- a/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter.kt.after +++ b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter.kt.after @@ -1,2 +1,2 @@ // "Safe delete 'property'" "true" -class UnusedPropertyAsConstructorParameter() +class UnusedPropertyAsConstructorParameter diff --git a/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt new file mode 100644 index 00000000000..1d9fa32865f --- /dev/null +++ b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt @@ -0,0 +1,2 @@ +// "Safe delete 'property'" "true" +class UnusedPropertyAsConstructorParameter(val property: String, val foo: String) diff --git a/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt.after b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt.after new file mode 100644 index 00000000000..ab8583dc7ee --- /dev/null +++ b/idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt.after @@ -0,0 +1,2 @@ +// "Safe delete 'property'" "true" +class UnusedPropertyAsConstructorParameter(val foo: String) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 893ad402df6..8430102142b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -1423,6 +1423,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedParameter.kt"); doTest(fileName); } + + @TestMetadata("removeUnusedPrimaryConstructorParameter.kt") + public void testRemoveUnusedPrimaryConstructorParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter.kt"); + doTest(fileName); + } + + @TestMetadata("removeUnusedPrimaryConstructorParameter2.kt") + public void testRemoveUnusedPrimaryConstructorParameter2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/changeSignature/removeUnusedPrimaryConstructorParameter2.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/quickfix/changeToLabeledReturn") @@ -9071,6 +9083,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("unusedPropertyAsConstructorParameter2.kt") + public void testUnusedPropertyAsConstructorParameter2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/unusedPropertyAsConstructorParameter2.kt"); + doTest(fileName); + } + @TestMetadata("unusedTypeParameter.kt") public void testUnusedTypeParameter() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/unusedTypeParameter.kt");