Add "Add constructor parameter" quick fix for NO_VALUE_FOR_PARAMETER

#KT-27353 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-06-18 23:33:09 +09:00
committed by Dmitry Gridin
parent aa73420312
commit fb94cb141f
18 changed files with 191 additions and 0 deletions
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class AddConstructorParameterFromSuperTypeCallFix(
constructor: KtValueArgumentList,
private val parameterName: String,
private val parameterType: FqName
) : KotlinQuickFixAction<KtValueArgumentList>(constructor) {
override fun getText() = "Add constructor parameter '$parameterName'"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val superTypeCallArgList = element ?: return
val constructorParamList = superTypeCallArgList.containingClass()?.createPrimaryConstructorIfAbsent()?.valueParameterList ?: return
val psiFactory = KtPsiFactory(superTypeCallArgList)
val constructorParam = constructorParamList.addParameter(psiFactory.createParameter("$parameterName: ${parameterType.render()}"))
val superTypeCallArg = superTypeCallArgList.addArgument(psiFactory.createArgument(parameterName))
ShortenReferences.DEFAULT.process(constructorParam)
editor?.caretModel?.moveToOffset(superTypeCallArg.endOffset)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtValueArgumentList>? {
val superTypeCallArgList = diagnostic.psiElement as? KtValueArgumentList ?: return null
val superTypeCall = superTypeCallArgList.parent as? KtSuperTypeCallEntry ?: return null
val containingClass = superTypeCall.containingClass() ?: return null
val parameter = DiagnosticFactory.cast(diagnostic, Errors.NO_VALUE_FOR_PARAMETER).a
if (parameter.index != superTypeCallArgList.arguments.size) return null
val parameterName = parameter.name.render()
val constructor = containingClass.resolveToDescriptorIfAny(BodyResolveMode.PARTIAL)?.constructors?.firstOrNull() ?: return null
if (constructor.valueParameters.any { it.name.asString() == parameterName }) return null
val parameterType = parameter.type.constructor.declarationDescriptor?.fqNameOrNull() ?: return null
return AddConstructorParameterFromSuperTypeCallFix(superTypeCallArgList, parameterName, parameterType)
}
}
}
@@ -609,5 +609,7 @@ class QuickFixRegistrar : QuickFixContributor {
RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION.registerFactory(RestrictedRetentionForExpressionAnnotationFactory)
RESTRICTED_RETENTION_FOR_EXPRESSION_ANNOTATION_WARNING.registerFactory(RestrictedRetentionForExpressionAnnotationFactory)
NO_VALUE_FOR_PARAMETER.registerFactory(AddConstructorParameterFromSuperTypeCallFix)
}
}
@@ -0,0 +1,5 @@
// "Add constructor parameter '`first param`'" "true"
// DISABLE-ERRORS
class `My class`
open class A(`first param`: `My class`)
class B : A(<caret>)
@@ -0,0 +1,5 @@
// "Add constructor parameter '`first param`'" "true"
// DISABLE-ERRORS
class `My class`
open class A(`first param`: `My class`)
class B(`first param`: `My class`) : A(`first param`)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'x'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B : A(<caret>)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'x'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int) : A(x)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'x'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B() : A(<caret>)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'x'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int) : A(x)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'y'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int) : A(x<caret>)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'y'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int, y: String) : A(x, y)
@@ -0,0 +1,3 @@
// "Add constructor parameter 'z'" "true"
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int, y: String) : A(x, y<caret>)
@@ -0,0 +1,3 @@
// "Add constructor parameter 'z'" "true"
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int, y: String, z: Long) : A(x, y, z)
@@ -0,0 +1,9 @@
// "Add constructor parameter 'z'" "false"
// DISABLE-ERRORS
// ACTION: Add 'x =' to argument
// ACTION: Add constructor parameter 'y'
// ACTION: Create secondary constructor
// ACTION: Remove parameter 'y'
// ACTION: Remove parameter 'z'
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int) : A(x<caret>)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'y'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(a: Int, b: String, c: Long) : A(a<caret>)
@@ -0,0 +1,4 @@
// "Add constructor parameter 'y'" "true"
// DISABLE-ERRORS
abstract class A(val x: Int, val y: String, val z: Long)
class B(a: Int, b: String, c: Long, y: String) : A(a, y)
@@ -0,0 +1,8 @@
// "Add constructor parameter 'y'" "false"
// DISABLE-ERRORS
// ACTION: Add 'x =' to argument
// ACTION: Create secondary constructor
// ACTION: Remove parameter 'y'
// ACTION: Remove parameter 'z'
abstract class A(val x: Int, val y: String, val z: Long)
class B(x: Int, y: String, z: Long) : A(x<caret>)
@@ -60,6 +60,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddConstructorParameterFromSuperTypeCall extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddConstructorParameterFromSuperTypeCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/addCrossinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -360,6 +360,59 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddConstructorParameterFromSuperTypeCall extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddConstructorParameterFromSuperTypeCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("backticks.kt")
public void testBackticks() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/backticks.kt");
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/basic.kt");
}
@TestMetadata("basic2.kt")
public void testBasic2() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/basic2.kt");
}
@TestMetadata("basic3.kt")
public void testBasic3() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/basic3.kt");
}
@TestMetadata("basic4.kt")
public void testBasic4() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/basic4.kt");
}
@TestMetadata("fewerArguments.kt")
public void testFewerArguments() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/fewerArguments.kt");
}
@TestMetadata("hasDifferentNameParameter.kt")
public void testHasDifferentNameParameter() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/hasDifferentNameParameter.kt");
}
@TestMetadata("hasSameNameParameter.kt")
public void testHasSameNameParameter() throws Exception {
runTest("idea/testData/quickfix/addConstructorParameterFromSuperTypeCall/hasSameNameParameter.kt");
}
}
@TestMetadata("idea/testData/quickfix/addCrossinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)