"Add constructor parameters" quick fix should add default parameters from super class (#2869)
#KT-33109 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
f0298aad77
commit
12f17a6724
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
|
|||||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||||
import org.jetbrains.kotlin.renderer.render
|
import org.jetbrains.kotlin.renderer.render
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
|
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
|
||||||
@@ -192,10 +193,17 @@ object SuperClassNotInitialized : KotlinIntentionActionsFactory() {
|
|||||||
if (type.isSubtypeOf(parameter.type)) continue // use existing parameter
|
if (type.isSubtypeOf(parameter.type)) continue // use existing parameter
|
||||||
}
|
}
|
||||||
|
|
||||||
val parameterText = if (varargElementType != null)
|
val defaultValue = if (parameter.declaresDefaultValue()) {
|
||||||
|
(DescriptorToSourceUtils.descriptorToDeclaration(parameter) as? KtParameter)
|
||||||
|
?.defaultValue?.text?.let { " = $it" } ?: ""
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
val parameterText = if (varargElementType != null) {
|
||||||
"vararg " + nameRendered + ":" + IdeDescriptorRenderers.SOURCE_CODE.renderType(varargElementType)
|
"vararg " + nameRendered + ":" + IdeDescriptorRenderers.SOURCE_CODE.renderType(varargElementType)
|
||||||
else
|
} else {
|
||||||
nameRendered + ":" + IdeDescriptorRenderers.SOURCE_CODE.renderType(parameter.type)
|
nameRendered + ":" + IdeDescriptorRenderers.SOURCE_CODE.renderType(parameter.type)
|
||||||
|
} + defaultValue
|
||||||
parametersToAdd.add(KtPsiFactory(element).createParameter(parameterText))
|
parametersToAdd.add(KtPsiFactory(element).createParameter(parameterText))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
import java.io.DataInputStream
|
import java.io.DataInputStream
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
class C(`in`: InputStream?) : DataInputStream(`in`)
|
class C(`in`: InputStream) : DataInputStream(`in`)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// "Add constructor parameters from Base(Int, Int)" "true"
|
// "Add constructor parameters from Base(Int, Int)" "true"
|
||||||
open class Base(p1: Int, private val p2: Int = 0)
|
open class Base(p1: Int, private val p2: Int = 0)
|
||||||
|
|
||||||
class C(p: Int, p1: Int, p2: Int) : Base<caret>(p1, p2)
|
class C(p: Int, p1: Int, p2: Int = 0) : Base<caret>(p1, p2)
|
||||||
|
|||||||
+1
-1
@@ -2,4 +2,4 @@
|
|||||||
// ACTION: Add constructor parameters from ArrayList(Int)
|
// ACTION: Add constructor parameters from ArrayList(Int)
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
class C(c: MutableCollection<out String>?) : ArrayList<String>(c)
|
class C(c: MutableCollection<out String>) : ArrayList<String>(c)
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Add constructor parameters from A(Array<out String>)" "true"
|
||||||
|
open class A(vararg strings: String = arrayOf("a", "b"))
|
||||||
|
|
||||||
|
class B : A<caret>
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// "Add constructor parameters from A(Array<out String>)" "true"
|
||||||
|
open class A(vararg strings: String = arrayOf("a", "b"))
|
||||||
|
|
||||||
|
class B(vararg strings: String = arrayOf("a", "b")) : A(*strings)
|
||||||
@@ -11804,6 +11804,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
public void testVararg3() throws Exception {
|
public void testVararg3() throws Exception {
|
||||||
runTest("idea/testData/quickfix/supertypeInitialization/vararg3.kt");
|
runTest("idea/testData/quickfix/supertypeInitialization/vararg3.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("vararg4.kt")
|
||||||
|
public void testVararg4() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/supertypeInitialization/vararg4.kt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/suppress")
|
@TestMetadata("idea/testData/quickfix/suppress")
|
||||||
|
|||||||
Reference in New Issue
Block a user