Add constructor parameters quickfix reuses existing parameters
This commit is contained in:
@@ -21,9 +21,11 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||||
import org.jetbrains.kotlin.idea.core.isVisible
|
import org.jetbrains.kotlin.idea.core.isVisible
|
||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||||
import org.jetbrains.kotlin.idea.util.ShortenReferences
|
import org.jetbrains.kotlin.idea.util.ShortenReferences
|
||||||
@@ -32,6 +34,7 @@ import org.jetbrains.kotlin.psi.psiUtil.replaced
|
|||||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
public object SuperClassNotInitialized : JetIntentionActionsFactory() {
|
public object SuperClassNotInitialized : JetIntentionActionsFactory() {
|
||||||
@@ -109,12 +112,26 @@ public object SuperClassNotInitialized : JetIntentionActionsFactory() {
|
|||||||
val typeRefsToShorten = ArrayList<JetTypeReference>()
|
val typeRefsToShorten = ArrayList<JetTypeReference>()
|
||||||
if (!superParameters.isEmpty()) {
|
if (!superParameters.isEmpty()) {
|
||||||
val parameterList = classDeclaration.getOrCreatePrimaryConstructorParameterList()
|
val parameterList = classDeclaration.getOrCreatePrimaryConstructorParameterList()
|
||||||
|
val oldParameters = parameterList.getParameters()
|
||||||
|
val parametersToAdd = ArrayList<JetParameter>()
|
||||||
for (parameter in superParameters) {
|
for (parameter in superParameters) {
|
||||||
val name = renderer.renderName(parameter.getName())
|
val name = renderer.renderName(parameter.getName())
|
||||||
val parameterText = name + ":" + renderer.renderType(parameter.getType())
|
|
||||||
val newParameter = parameterList.addParameter(factory.createParameter(parameterText))
|
|
||||||
typeRefsToShorten.add(newParameter.getTypeReference())
|
|
||||||
parameterNames.add(name)
|
parameterNames.add(name)
|
||||||
|
|
||||||
|
val nameString = parameter.getName().asString()
|
||||||
|
val existingParameter = oldParameters.firstOrNull { it.getName() == nameString }
|
||||||
|
if (existingParameter != null) {
|
||||||
|
val type = (existingParameter.resolveToDescriptor() as ValueParameterDescriptor).getType()
|
||||||
|
if (type.isSubtypeOf(parameter.getType())) continue // use existing parameter
|
||||||
|
}
|
||||||
|
|
||||||
|
val parameterText = name + ":" + renderer.renderType(parameter.getType())
|
||||||
|
parametersToAdd.add(factory.createParameter(parameterText))
|
||||||
|
}
|
||||||
|
|
||||||
|
for (parameter in parametersToAdd) {
|
||||||
|
val newParameter = parameterList.addParameter(parameter)
|
||||||
|
typeRefsToShorten.add(newParameter.getTypeReference())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// "Add constructor parameters from Base(p1: Int, p2: Int)" "true"
|
// "Add constructor parameters from Base(p1: Int, p2: Int)" "true"
|
||||||
open class Base(p1: Int, val p2: Int)
|
open class Base(p1: Int, private val p2: Int)
|
||||||
|
|
||||||
class C(p: Int) : Base<caret>
|
class C(p: Int) : Base<caret>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// "Add constructor parameters from Base(p1: Int, p2: Int)" "true"
|
// "Add constructor parameters from Base(p1: Int, p2: Int)" "true"
|
||||||
open class Base(p1: Int, val p2: Int)
|
open class Base(p1: Int, private val p2: Int)
|
||||||
|
|
||||||
class C(p: Int, p1: Int, p2: Int) : Base<caret>(p1, p2)
|
class C(p: Int, p1: Int, p2: Int) : Base<caret>(p1, p2)
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Add constructor parameters from Base(p1: Int, p2: Int, p3: Any, p4: String, p5: String)" "true"
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
open class Base<T>(p1: Int, private val p2: Int, p3: Any, p4: String, p5: T)
|
||||||
|
|
||||||
|
class C(p: Int, p2: Int, p3: String, p4: Any, p5: String) : Base<String><caret>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Add constructor parameters from Base(p1: Int, p2: Int, p3: Any, p4: String, p5: String)" "true"
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
// ERROR: Redeclaration: p4
|
||||||
|
open class Base<T>(p1: Int, private val p2: Int, p3: Any, p4: String, p5: T)
|
||||||
|
|
||||||
|
class C(p: Int, p2: Int, p3: String, p4: Any, p5: String, p1: Int, p4: String) : Base<String><caret>(p1, p2, p3, p4, p5)
|
||||||
@@ -4046,6 +4046,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/supertypeInitialization/primaryConstructorInaccessible.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/supertypeInitialization/primaryConstructorInaccessible.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("someParametersAlreadyExist.kt")
|
||||||
|
public void testSomeParametersAlreadyExist() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/supertypeInitialization/someParametersAlreadyExist.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/suppress")
|
@TestMetadata("idea/testData/quickfix/suppress")
|
||||||
|
|||||||
Reference in New Issue
Block a user