Remove unused function parameter: delete empty constructor accurately

So #KT-22221 Fixed
Fixes also some quick-fix tests
This commit is contained in:
Mikhail Glukhikh
2018-01-18 17:10:15 +03:00
parent c4ef95dbf7
commit ee3f89df87
6 changed files with 46 additions and 24 deletions
@@ -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)
}
}
@@ -0,0 +1,6 @@
// "Remove parameter 'x'" "true"
fun foo() {
X("")
}
class X constructor(<caret>x: String)
@@ -0,0 +1,6 @@
// "Remove parameter 'x'" "true"
fun foo() {
X()
}
class X
@@ -0,0 +1,3 @@
// "Remove parameter 'x'" "true"
class X internal constructor(<caret>x: String)
@@ -0,0 +1,3 @@
// "Remove parameter 'x'" "true"
class X internal constructor()
@@ -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)